Path: blob/master/thirdparty/libjpeg-turbo/src/jcphuff.c
9904 views
/*1* jcphuff.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1995-1997, Thomas G. Lane.5* Lossless JPEG Modifications:6* Copyright (C) 1999, Ken Murchison.7* libjpeg-turbo Modifications:8* Copyright (C) 2011, 2015, 2018, 2021-2022, 2024, D. R. Commander.9* Copyright (C) 2016, 2018, 2022, Matthieu Darbois.10* Copyright (C) 2020, Arm Limited.11* Copyright (C) 2021, Alex Richardson.12* For conditions of distribution and use, see the accompanying README.ijg13* file.14*15* This file contains Huffman entropy encoding routines for progressive JPEG.16*17* We do not support output suspension in this module, since the library18* currently does not allow multiple-scan files to be written with output19* suspension.20*/2122#define JPEG_INTERNALS23#include "jinclude.h"24#include "jpeglib.h"25#ifdef WITH_SIMD26#include "jsimd.h"27#else28#include "jchuff.h" /* Declarations shared with jc*huff.c */29#endif30#include <limits.h>3132#ifdef HAVE_INTRIN_H33#include <intrin.h>34#ifdef _MSC_VER35#ifdef HAVE_BITSCANFORWARD6436#pragma intrinsic(_BitScanForward64)37#endif38#ifdef HAVE_BITSCANFORWARD39#pragma intrinsic(_BitScanForward)40#endif41#endif42#endif4344#ifdef C_PROGRESSIVE_SUPPORTED4546#include "jpeg_nbits.h"474849/* Expanded entropy encoder object for progressive Huffman encoding. */5051typedef struct {52struct jpeg_entropy_encoder pub; /* public fields */5354/* Pointer to routine to prepare data for encode_mcu_AC_first() */55void (*AC_first_prepare) (const JCOEF *block,56const int *jpeg_natural_order_start, int Sl,57int Al, UJCOEF *values, size_t *zerobits);58/* Pointer to routine to prepare data for encode_mcu_AC_refine() */59int (*AC_refine_prepare) (const JCOEF *block,60const int *jpeg_natural_order_start, int Sl,61int Al, UJCOEF *absvalues, size_t *bits);6263/* Mode flag: TRUE for optimization, FALSE for actual data output */64boolean gather_statistics;6566/* Bit-level coding status.67* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.68*/69JOCTET *next_output_byte; /* => next byte to write in buffer */70size_t free_in_buffer; /* # of byte spaces remaining in buffer */71size_t put_buffer; /* current bit-accumulation buffer */72int put_bits; /* # of bits now in it */73j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */7475/* Coding status for DC components */76int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */7778/* Coding status for AC components */79int ac_tbl_no; /* the table number of the single component */80unsigned int EOBRUN; /* run length of EOBs */81unsigned int BE; /* # of buffered correction bits before MCU */82char *bit_buffer; /* buffer for correction bits (1 per char) */83/* packing correction bits tightly would save some space but cost time... */8485unsigned int restarts_to_go; /* MCUs left in this restart interval */86int next_restart_num; /* next restart number to write (0-7) */8788/* Pointers to derived tables (these workspaces have image lifespan).89* Since any one scan codes only DC or only AC, we only need one set90* of tables, not one for DC and one for AC.91*/92c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];9394/* Statistics tables for optimization; again, one set is enough */95long *count_ptrs[NUM_HUFF_TBLS];96} phuff_entropy_encoder;9798typedef phuff_entropy_encoder *phuff_entropy_ptr;99100/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit101* buffer can hold. Larger sizes may slightly improve compression, but102* 1000 is already well into the realm of overkill.103* The minimum safe size is 64 bits.104*/105106#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */107108/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.109* We assume that int right shift is unsigned if JLONG right shift is,110* which should be safe.111*/112113#ifdef RIGHT_SHIFT_IS_UNSIGNED114#define ISHIFT_TEMPS int ishift_temp;115#define IRIGHT_SHIFT(x, shft) \116((ishift_temp = (x)) < 0 ? \117(ishift_temp >> (shft)) | ((~0) << (16 - (shft))) : \118(ishift_temp >> (shft)))119#else120#define ISHIFT_TEMPS121#define IRIGHT_SHIFT(x, shft) ((x) >> (shft))122#endif123124#define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))125126/* Forward declarations */127METHODDEF(boolean) encode_mcu_DC_first(j_compress_ptr cinfo,128JBLOCKROW *MCU_data);129METHODDEF(void) encode_mcu_AC_first_prepare130(const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,131UJCOEF *values, size_t *zerobits);132METHODDEF(boolean) encode_mcu_AC_first(j_compress_ptr cinfo,133JBLOCKROW *MCU_data);134METHODDEF(boolean) encode_mcu_DC_refine(j_compress_ptr cinfo,135JBLOCKROW *MCU_data);136METHODDEF(int) encode_mcu_AC_refine_prepare137(const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,138UJCOEF *absvalues, size_t *bits);139METHODDEF(boolean) encode_mcu_AC_refine(j_compress_ptr cinfo,140JBLOCKROW *MCU_data);141METHODDEF(void) finish_pass_phuff(j_compress_ptr cinfo);142METHODDEF(void) finish_pass_gather_phuff(j_compress_ptr cinfo);143144145/* Count bit loop zeroes */146INLINE147METHODDEF(int)148count_zeroes(size_t *x)149{150#if defined(HAVE_BUILTIN_CTZL)151int result;152result = __builtin_ctzl(*x);153*x >>= result;154#elif defined(HAVE_BITSCANFORWARD64)155unsigned long result;156_BitScanForward64(&result, *x);157*x >>= result;158#elif defined(HAVE_BITSCANFORWARD)159unsigned long result;160_BitScanForward(&result, *x);161*x >>= result;162#else163int result = 0;164while ((*x & 1) == 0) {165++result;166*x >>= 1;167}168#endif169return (int)result;170}171172173/*174* Initialize for a Huffman-compressed scan using progressive JPEG.175*/176177METHODDEF(void)178start_pass_phuff(j_compress_ptr cinfo, boolean gather_statistics)179{180phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;181boolean is_DC_band;182int ci, tbl;183jpeg_component_info *compptr;184185entropy->cinfo = cinfo;186entropy->gather_statistics = gather_statistics;187188is_DC_band = (cinfo->Ss == 0);189190/* We assume jcmaster.c already validated the scan parameters. */191192/* Select execution routines */193if (cinfo->Ah == 0) {194if (is_DC_band)195entropy->pub.encode_mcu = encode_mcu_DC_first;196else197entropy->pub.encode_mcu = encode_mcu_AC_first;198#ifdef WITH_SIMD199if (jsimd_can_encode_mcu_AC_first_prepare())200entropy->AC_first_prepare = jsimd_encode_mcu_AC_first_prepare;201else202#endif203entropy->AC_first_prepare = encode_mcu_AC_first_prepare;204} else {205if (is_DC_band)206entropy->pub.encode_mcu = encode_mcu_DC_refine;207else {208entropy->pub.encode_mcu = encode_mcu_AC_refine;209#ifdef WITH_SIMD210if (jsimd_can_encode_mcu_AC_refine_prepare())211entropy->AC_refine_prepare = jsimd_encode_mcu_AC_refine_prepare;212else213#endif214entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare;215/* AC refinement needs a correction bit buffer */216if (entropy->bit_buffer == NULL)217entropy->bit_buffer = (char *)218(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,219MAX_CORR_BITS * sizeof(char));220}221}222if (gather_statistics)223entropy->pub.finish_pass = finish_pass_gather_phuff;224else225entropy->pub.finish_pass = finish_pass_phuff;226227/* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1228* for AC coefficients.229*/230for (ci = 0; ci < cinfo->comps_in_scan; ci++) {231compptr = cinfo->cur_comp_info[ci];232/* Initialize DC predictions to 0 */233entropy->last_dc_val[ci] = 0;234/* Get table index */235if (is_DC_band) {236if (cinfo->Ah != 0) /* DC refinement needs no table */237continue;238tbl = compptr->dc_tbl_no;239} else {240entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;241}242if (gather_statistics) {243/* Check for invalid table index */244/* (make_c_derived_tbl does this in the other path) */245if (tbl < 0 || tbl >= NUM_HUFF_TBLS)246ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);247/* Allocate and zero the statistics tables */248/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */249if (entropy->count_ptrs[tbl] == NULL)250entropy->count_ptrs[tbl] = (long *)251(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,252257 * sizeof(long));253memset(entropy->count_ptrs[tbl], 0, 257 * sizeof(long));254} else {255/* Compute derived values for Huffman table */256/* We may do this more than once for a table, but it's not expensive */257jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,258&entropy->derived_tbls[tbl]);259}260}261262/* Initialize AC stuff */263entropy->EOBRUN = 0;264entropy->BE = 0;265266/* Initialize bit buffer to empty */267entropy->put_buffer = 0;268entropy->put_bits = 0;269270/* Initialize restart stuff */271entropy->restarts_to_go = cinfo->restart_interval;272entropy->next_restart_num = 0;273}274275276/* Outputting bytes to the file.277* NB: these must be called only when actually outputting,278* that is, entropy->gather_statistics == FALSE.279*/280281/* Emit a byte */282#define emit_byte(entropy, val) { \283*(entropy)->next_output_byte++ = (JOCTET)(val); \284if (--(entropy)->free_in_buffer == 0) \285dump_buffer(entropy); \286}287288289LOCAL(void)290dump_buffer(phuff_entropy_ptr entropy)291/* Empty the output buffer; we do not support suspension in this module. */292{293struct jpeg_destination_mgr *dest = entropy->cinfo->dest;294295if (!(*dest->empty_output_buffer) (entropy->cinfo))296ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);297/* After a successful buffer dump, must reset buffer pointers */298entropy->next_output_byte = dest->next_output_byte;299entropy->free_in_buffer = dest->free_in_buffer;300}301302303/* Outputting bits to the file */304305/* Only the right 24 bits of put_buffer are used; the valid bits are306* left-justified in this part. At most 16 bits can be passed to emit_bits307* in one call, and we never retain more than 7 bits in put_buffer308* between calls, so 24 bits are sufficient.309*/310311LOCAL(void)312emit_bits(phuff_entropy_ptr entropy, unsigned int code, int size)313/* Emit some bits, unless we are in gather mode */314{315/* This routine is heavily used, so it's worth coding tightly. */316register size_t put_buffer = (size_t)code;317register int put_bits = entropy->put_bits;318319/* if size is 0, caller used an invalid Huffman table entry */320if (size == 0)321ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);322323if (entropy->gather_statistics)324return; /* do nothing if we're only getting stats */325326put_buffer &= (((size_t)1) << size) - 1; /* mask off any extra bits in code */327328put_bits += size; /* new number of bits in buffer */329330put_buffer <<= 24 - put_bits; /* align incoming bits */331332put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */333334while (put_bits >= 8) {335int c = (int)((put_buffer >> 16) & 0xFF);336337emit_byte(entropy, c);338if (c == 0xFF) { /* need to stuff a zero byte? */339emit_byte(entropy, 0);340}341put_buffer <<= 8;342put_bits -= 8;343}344345entropy->put_buffer = put_buffer; /* update variables */346entropy->put_bits = put_bits;347}348349350LOCAL(void)351flush_bits(phuff_entropy_ptr entropy)352{353emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */354entropy->put_buffer = 0; /* and reset bit-buffer to empty */355entropy->put_bits = 0;356}357358359/*360* Emit (or just count) a Huffman symbol.361*/362363LOCAL(void)364emit_symbol(phuff_entropy_ptr entropy, int tbl_no, int symbol)365{366if (entropy->gather_statistics)367entropy->count_ptrs[tbl_no][symbol]++;368else {369c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];370emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);371}372}373374375/*376* Emit bits from a correction bit buffer.377*/378379LOCAL(void)380emit_buffered_bits(phuff_entropy_ptr entropy, char *bufstart,381unsigned int nbits)382{383if (entropy->gather_statistics)384return; /* no real work */385386while (nbits > 0) {387emit_bits(entropy, (unsigned int)(*bufstart), 1);388bufstart++;389nbits--;390}391}392393394/*395* Emit any pending EOBRUN symbol.396*/397398LOCAL(void)399emit_eobrun(phuff_entropy_ptr entropy)400{401register int temp, nbits;402403if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */404temp = entropy->EOBRUN;405nbits = JPEG_NBITS_NONZERO(temp) - 1;406/* safety check: shouldn't happen given limited correction-bit buffer */407if (nbits > 14)408ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);409410emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);411if (nbits)412emit_bits(entropy, entropy->EOBRUN, nbits);413414entropy->EOBRUN = 0;415416/* Emit any buffered correction bits */417emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);418entropy->BE = 0;419}420}421422423/*424* Emit a restart marker & resynchronize predictions.425*/426427LOCAL(void)428emit_restart(phuff_entropy_ptr entropy, int restart_num)429{430int ci;431432emit_eobrun(entropy);433434if (!entropy->gather_statistics) {435flush_bits(entropy);436emit_byte(entropy, 0xFF);437emit_byte(entropy, JPEG_RST0 + restart_num);438}439440if (entropy->cinfo->Ss == 0) {441/* Re-initialize DC predictions to 0 */442for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)443entropy->last_dc_val[ci] = 0;444} else {445/* Re-initialize all AC-related fields to 0 */446entropy->EOBRUN = 0;447entropy->BE = 0;448}449}450451452/*453* MCU encoding for DC initial scan (either spectral selection,454* or first pass of successive approximation).455*/456457METHODDEF(boolean)458encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)459{460phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;461register int temp, temp2, temp3;462register int nbits;463int blkn, ci;464int Al = cinfo->Al;465JBLOCKROW block;466jpeg_component_info *compptr;467ISHIFT_TEMPS468int max_coef_bits = cinfo->data_precision + 2;469470entropy->next_output_byte = cinfo->dest->next_output_byte;471entropy->free_in_buffer = cinfo->dest->free_in_buffer;472473/* Emit restart marker if needed */474if (cinfo->restart_interval)475if (entropy->restarts_to_go == 0)476emit_restart(entropy, entropy->next_restart_num);477478/* Encode the MCU data blocks */479for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {480block = MCU_data[blkn];481ci = cinfo->MCU_membership[blkn];482compptr = cinfo->cur_comp_info[ci];483484/* Compute the DC value after the required point transform by Al.485* This is simply an arithmetic right shift.486*/487temp2 = IRIGHT_SHIFT((int)((*block)[0]), Al);488489/* DC differences are figured on the point-transformed values. */490temp = temp2 - entropy->last_dc_val[ci];491entropy->last_dc_val[ci] = temp2;492493/* Encode the DC coefficient difference per section G.1.2.1 */494495/* This is a well-known technique for obtaining the absolute value without496* a branch. It is derived from an assembly language technique presented497* in "How to Optimize for the Pentium Processors", Copyright (c) 1996,498* 1997 by Agner Fog.499*/500temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);501temp ^= temp3;502temp -= temp3; /* temp is abs value of input */503/* For a negative input, want temp2 = bitwise complement of abs(input) */504temp2 = temp ^ temp3;505506/* Find the number of bits needed for the magnitude of the coefficient */507nbits = JPEG_NBITS(temp);508/* Check for out-of-range coefficient values.509* Since we're encoding a difference, the range limit is twice as much.510*/511if (nbits > max_coef_bits + 1)512ERREXIT(cinfo, JERR_BAD_DCT_COEF);513514/* Count/emit the Huffman-coded symbol for the number of bits */515emit_symbol(entropy, compptr->dc_tbl_no, nbits);516517/* Emit that number of bits of the value, if positive, */518/* or the complement of its magnitude, if negative. */519if (nbits) /* emit_bits rejects calls with size 0 */520emit_bits(entropy, (unsigned int)temp2, nbits);521}522523cinfo->dest->next_output_byte = entropy->next_output_byte;524cinfo->dest->free_in_buffer = entropy->free_in_buffer;525526/* Update restart-interval state too */527if (cinfo->restart_interval) {528if (entropy->restarts_to_go == 0) {529entropy->restarts_to_go = cinfo->restart_interval;530entropy->next_restart_num++;531entropy->next_restart_num &= 7;532}533entropy->restarts_to_go--;534}535536return TRUE;537}538539540/*541* Data preparation for encode_mcu_AC_first().542*/543544#define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \545for (k = 0; k < Sl; k++) { \546temp = block[jpeg_natural_order_start[k]]; \547if (temp == 0) \548continue; \549/* We must apply the point transform by Al. For AC coefficients this \550* is an integer division with rounding towards 0. To do this portably \551* in C, we shift after obtaining the absolute value; so the code is \552* interwoven with finding the abs value (temp) and output bits (temp2). \553*/ \554temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \555temp ^= temp2; \556temp -= temp2; /* temp is abs value of input */ \557temp >>= Al; /* apply the point transform */ \558/* Watch out for case that nonzero coef is zero after point transform */ \559if (temp == 0) \560continue; \561/* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \562temp2 ^= temp; \563values[k] = (UJCOEF)temp; \564values[k + DCTSIZE2] = (UJCOEF)temp2; \565zerobits |= ((size_t)1U) << k; \566} \567}568569METHODDEF(void)570encode_mcu_AC_first_prepare(const JCOEF *block,571const int *jpeg_natural_order_start, int Sl,572int Al, UJCOEF *values, size_t *bits)573{574register int k, temp, temp2;575size_t zerobits = 0U;576int Sl0 = Sl;577578#if SIZEOF_SIZE_T == 4579if (Sl0 > 32)580Sl0 = 32;581#endif582583COMPUTE_ABSVALUES_AC_FIRST(Sl0);584585bits[0] = zerobits;586#if SIZEOF_SIZE_T == 4587zerobits = 0U;588589if (Sl > 32) {590Sl -= 32;591jpeg_natural_order_start += 32;592values += 32;593594COMPUTE_ABSVALUES_AC_FIRST(Sl);595}596bits[1] = zerobits;597#endif598}599600/*601* MCU encoding for AC initial scan (either spectral selection,602* or first pass of successive approximation).603*/604605#define ENCODE_COEFS_AC_FIRST(label) { \606while (zerobits) { \607r = count_zeroes(&zerobits); \608cvalue += r; \609label \610temp = cvalue[0]; \611temp2 = cvalue[DCTSIZE2]; \612\613/* if run length > 15, must emit special run-length-16 codes (0xF0) */ \614while (r > 15) { \615emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \616r -= 16; \617} \618\619/* Find the number of bits needed for the magnitude of the coefficient */ \620nbits = JPEG_NBITS_NONZERO(temp); /* there must be at least one 1 bit */ \621/* Check for out-of-range coefficient values */ \622if (nbits > max_coef_bits) \623ERREXIT(cinfo, JERR_BAD_DCT_COEF); \624\625/* Count/emit Huffman symbol for run length / number of bits */ \626emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \627\628/* Emit that number of bits of the value, if positive, */ \629/* or the complement of its magnitude, if negative. */ \630emit_bits(entropy, (unsigned int)temp2, nbits); \631\632cvalue++; \633zerobits >>= 1; \634} \635}636637METHODDEF(boolean)638encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)639{640phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;641register int temp, temp2;642register int nbits, r;643int Sl = cinfo->Se - cinfo->Ss + 1;644int Al = cinfo->Al;645UJCOEF values_unaligned[2 * DCTSIZE2 + 15];646UJCOEF *values;647const UJCOEF *cvalue;648size_t zerobits;649size_t bits[8 / SIZEOF_SIZE_T];650int max_coef_bits = cinfo->data_precision + 2;651652#ifdef ZERO_BUFFERS653memset(values_unaligned, 0, sizeof(values_unaligned));654memset(bits, 0, sizeof(bits));655#endif656657entropy->next_output_byte = cinfo->dest->next_output_byte;658entropy->free_in_buffer = cinfo->dest->free_in_buffer;659660/* Emit restart marker if needed */661if (cinfo->restart_interval)662if (entropy->restarts_to_go == 0)663emit_restart(entropy, entropy->next_restart_num);664665#ifdef WITH_SIMD666cvalue = values = (UJCOEF *)PAD((JUINTPTR)values_unaligned, 16);667#else668/* Not using SIMD, so alignment is not needed */669cvalue = values = values_unaligned;670#endif671672/* Prepare data */673entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,674Sl, Al, values, bits);675676zerobits = bits[0];677#if SIZEOF_SIZE_T == 4678zerobits |= bits[1];679#endif680681/* Emit any pending EOBRUN */682if (zerobits && (entropy->EOBRUN > 0))683emit_eobrun(entropy);684685#if SIZEOF_SIZE_T == 4686zerobits = bits[0];687#endif688689/* Encode the AC coefficients per section G.1.2.2, fig. G.3 */690691ENCODE_COEFS_AC_FIRST((void)0;);692693#if SIZEOF_SIZE_T == 4694zerobits = bits[1];695if (zerobits) {696int diff = ((values + DCTSIZE2 / 2) - cvalue);697r = count_zeroes(&zerobits);698r += diff;699cvalue += r;700goto first_iter_ac_first;701}702703ENCODE_COEFS_AC_FIRST(first_iter_ac_first:);704#endif705706if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */707entropy->EOBRUN++; /* count an EOB */708if (entropy->EOBRUN == 0x7FFF)709emit_eobrun(entropy); /* force it out to avoid overflow */710}711712cinfo->dest->next_output_byte = entropy->next_output_byte;713cinfo->dest->free_in_buffer = entropy->free_in_buffer;714715/* Update restart-interval state too */716if (cinfo->restart_interval) {717if (entropy->restarts_to_go == 0) {718entropy->restarts_to_go = cinfo->restart_interval;719entropy->next_restart_num++;720entropy->next_restart_num &= 7;721}722entropy->restarts_to_go--;723}724725return TRUE;726}727728729/*730* MCU encoding for DC successive approximation refinement scan.731* Note: we assume such scans can be multi-component, although the spec732* is not very clear on the point.733*/734735METHODDEF(boolean)736encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)737{738phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;739register int temp;740int blkn;741int Al = cinfo->Al;742JBLOCKROW block;743744entropy->next_output_byte = cinfo->dest->next_output_byte;745entropy->free_in_buffer = cinfo->dest->free_in_buffer;746747/* Emit restart marker if needed */748if (cinfo->restart_interval)749if (entropy->restarts_to_go == 0)750emit_restart(entropy, entropy->next_restart_num);751752/* Encode the MCU data blocks */753for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {754block = MCU_data[blkn];755756/* We simply emit the Al'th bit of the DC coefficient value. */757temp = (*block)[0];758emit_bits(entropy, (unsigned int)(temp >> Al), 1);759}760761cinfo->dest->next_output_byte = entropy->next_output_byte;762cinfo->dest->free_in_buffer = entropy->free_in_buffer;763764/* Update restart-interval state too */765if (cinfo->restart_interval) {766if (entropy->restarts_to_go == 0) {767entropy->restarts_to_go = cinfo->restart_interval;768entropy->next_restart_num++;769entropy->next_restart_num &= 7;770}771entropy->restarts_to_go--;772}773774return TRUE;775}776777778/*779* Data preparation for encode_mcu_AC_refine().780*/781782#define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \783/* It is convenient to make a pre-pass to determine the transformed \784* coefficients' absolute values and the EOB position. \785*/ \786for (k = 0; k < Sl; k++) { \787temp = block[jpeg_natural_order_start[k]]; \788/* We must apply the point transform by Al. For AC coefficients this \789* is an integer division with rounding towards 0. To do this portably \790* in C, we shift after obtaining the absolute value. \791*/ \792temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \793temp ^= temp2; \794temp -= temp2; /* temp is abs value of input */ \795temp >>= Al; /* apply the point transform */ \796if (temp != 0) { \797zerobits |= ((size_t)1U) << k; \798signbits |= ((size_t)(temp2 + 1)) << k; \799} \800absvalues[k] = (UJCOEF)temp; /* save abs value for main pass */ \801if (temp == 1) \802EOB = k + koffset; /* EOB = index of last newly-nonzero coef */ \803} \804}805806METHODDEF(int)807encode_mcu_AC_refine_prepare(const JCOEF *block,808const int *jpeg_natural_order_start, int Sl,809int Al, UJCOEF *absvalues, size_t *bits)810{811register int k, temp, temp2;812int EOB = 0;813size_t zerobits = 0U, signbits = 0U;814int Sl0 = Sl;815816#if SIZEOF_SIZE_T == 4817if (Sl0 > 32)818Sl0 = 32;819#endif820821COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0);822823bits[0] = zerobits;824#if SIZEOF_SIZE_T == 8825bits[1] = signbits;826#else827bits[2] = signbits;828829zerobits = 0U;830signbits = 0U;831832if (Sl > 32) {833Sl -= 32;834jpeg_natural_order_start += 32;835absvalues += 32;836837COMPUTE_ABSVALUES_AC_REFINE(Sl, 32);838}839840bits[1] = zerobits;841bits[3] = signbits;842#endif843844return EOB;845}846847848/*849* MCU encoding for AC successive approximation refinement scan.850*/851852#define ENCODE_COEFS_AC_REFINE(label) { \853while (zerobits) { \854idx = count_zeroes(&zerobits); \855r += idx; \856cabsvalue += idx; \857signbits >>= idx; \858label \859/* Emit any required ZRLs, but not if they can be folded into EOB */ \860while (r > 15 && (cabsvalue <= EOBPTR)) { \861/* emit any pending EOBRUN and the BE correction bits */ \862emit_eobrun(entropy); \863/* Emit ZRL */ \864emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \865r -= 16; \866/* Emit buffered correction bits that must be associated with ZRL */ \867emit_buffered_bits(entropy, BR_buffer, BR); \868BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \869BR = 0; \870} \871\872temp = *cabsvalue++; \873\874/* If the coef was previously nonzero, it only needs a correction bit. \875* NOTE: a straight translation of the spec's figure G.7 would suggest \876* that we also need to test r > 15. But if r > 15, we can only get here \877* if k > EOB, which implies that this coefficient is not 1. \878*/ \879if (temp > 1) { \880/* The correction bit is the next bit of the absolute value. */ \881BR_buffer[BR++] = (char)(temp & 1); \882signbits >>= 1; \883zerobits >>= 1; \884continue; \885} \886\887/* Emit any pending EOBRUN and the BE correction bits */ \888emit_eobrun(entropy); \889\890/* Count/emit Huffman symbol for run length / number of bits */ \891emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \892\893/* Emit output bit for newly-nonzero coef */ \894temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \895emit_bits(entropy, (unsigned int)temp, 1); \896\897/* Emit buffered correction bits that must be associated with this code */ \898emit_buffered_bits(entropy, BR_buffer, BR); \899BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \900BR = 0; \901r = 0; /* reset zero run length */ \902signbits >>= 1; \903zerobits >>= 1; \904} \905}906907METHODDEF(boolean)908encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)909{910phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;911register int temp, r, idx;912char *BR_buffer;913unsigned int BR;914int Sl = cinfo->Se - cinfo->Ss + 1;915int Al = cinfo->Al;916UJCOEF absvalues_unaligned[DCTSIZE2 + 15];917UJCOEF *absvalues;918const UJCOEF *cabsvalue, *EOBPTR;919size_t zerobits, signbits;920size_t bits[16 / SIZEOF_SIZE_T];921922#ifdef ZERO_BUFFERS923memset(absvalues_unaligned, 0, sizeof(absvalues_unaligned));924memset(bits, 0, sizeof(bits));925#endif926927entropy->next_output_byte = cinfo->dest->next_output_byte;928entropy->free_in_buffer = cinfo->dest->free_in_buffer;929930/* Emit restart marker if needed */931if (cinfo->restart_interval)932if (entropy->restarts_to_go == 0)933emit_restart(entropy, entropy->next_restart_num);934935#ifdef WITH_SIMD936cabsvalue = absvalues = (UJCOEF *)PAD((JUINTPTR)absvalues_unaligned, 16);937#else938/* Not using SIMD, so alignment is not needed */939cabsvalue = absvalues = absvalues_unaligned;940#endif941942/* Prepare data */943EOBPTR = absvalues +944entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,945Sl, Al, absvalues, bits);946947/* Encode the AC coefficients per section G.1.2.3, fig. G.7 */948949r = 0; /* r = run length of zeros */950BR = 0; /* BR = count of buffered bits added now */951BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */952953zerobits = bits[0];954#if SIZEOF_SIZE_T == 8955signbits = bits[1];956#else957signbits = bits[2];958#endif959ENCODE_COEFS_AC_REFINE((void)0;);960961#if SIZEOF_SIZE_T == 4962zerobits = bits[1];963signbits = bits[3];964965if (zerobits) {966int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue);967idx = count_zeroes(&zerobits);968signbits >>= idx;969idx += diff;970r += idx;971cabsvalue += idx;972goto first_iter_ac_refine;973}974975ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:);976#endif977978r |= (int)((absvalues + Sl) - cabsvalue);979980if (r > 0 || BR > 0) { /* If there are trailing zeroes, */981entropy->EOBRUN++; /* count an EOB */982entropy->BE += BR; /* concat my correction bits to older ones */983/* We force out the EOB if we risk either:984* 1. overflow of the EOB counter;985* 2. overflow of the correction bit buffer during the next MCU.986*/987if (entropy->EOBRUN == 0x7FFF ||988entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))989emit_eobrun(entropy);990}991992cinfo->dest->next_output_byte = entropy->next_output_byte;993cinfo->dest->free_in_buffer = entropy->free_in_buffer;994995/* Update restart-interval state too */996if (cinfo->restart_interval) {997if (entropy->restarts_to_go == 0) {998entropy->restarts_to_go = cinfo->restart_interval;999entropy->next_restart_num++;1000entropy->next_restart_num &= 7;1001}1002entropy->restarts_to_go--;1003}10041005return TRUE;1006}100710081009/*1010* Finish up at the end of a Huffman-compressed progressive scan.1011*/10121013METHODDEF(void)1014finish_pass_phuff(j_compress_ptr cinfo)1015{1016phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;10171018entropy->next_output_byte = cinfo->dest->next_output_byte;1019entropy->free_in_buffer = cinfo->dest->free_in_buffer;10201021/* Flush out any buffered data */1022emit_eobrun(entropy);1023flush_bits(entropy);10241025cinfo->dest->next_output_byte = entropy->next_output_byte;1026cinfo->dest->free_in_buffer = entropy->free_in_buffer;1027}102810291030/*1031* Finish up a statistics-gathering pass and create the new Huffman tables.1032*/10331034METHODDEF(void)1035finish_pass_gather_phuff(j_compress_ptr cinfo)1036{1037phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy;1038boolean is_DC_band;1039int ci, tbl;1040jpeg_component_info *compptr;1041JHUFF_TBL **htblptr;1042boolean did[NUM_HUFF_TBLS];10431044/* Flush out buffered data (all we care about is counting the EOB symbol) */1045emit_eobrun(entropy);10461047is_DC_band = (cinfo->Ss == 0);10481049/* It's important not to apply jpeg_gen_optimal_table more than once1050* per table, because it clobbers the input frequency counts!1051*/1052memset(did, 0, sizeof(did));10531054for (ci = 0; ci < cinfo->comps_in_scan; ci++) {1055compptr = cinfo->cur_comp_info[ci];1056if (is_DC_band) {1057if (cinfo->Ah != 0) /* DC refinement needs no table */1058continue;1059tbl = compptr->dc_tbl_no;1060} else {1061tbl = compptr->ac_tbl_no;1062}1063if (!did[tbl]) {1064if (is_DC_band)1065htblptr = &cinfo->dc_huff_tbl_ptrs[tbl];1066else1067htblptr = &cinfo->ac_huff_tbl_ptrs[tbl];1068if (*htblptr == NULL)1069*htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);1070jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);1071did[tbl] = TRUE;1072}1073}1074}107510761077/*1078* Module initialization routine for progressive Huffman entropy encoding.1079*/10801081GLOBAL(void)1082jinit_phuff_encoder(j_compress_ptr cinfo)1083{1084phuff_entropy_ptr entropy;1085int i;10861087entropy = (phuff_entropy_ptr)1088(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,1089sizeof(phuff_entropy_encoder));1090cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;1091entropy->pub.start_pass = start_pass_phuff;10921093/* Mark tables unallocated */1094for (i = 0; i < NUM_HUFF_TBLS; i++) {1095entropy->derived_tbls[i] = NULL;1096entropy->count_ptrs[i] = NULL;1097}1098entropy->bit_buffer = NULL; /* needed only in AC refinement scan */1099}11001101#endif /* C_PROGRESSIVE_SUPPORTED */110211031104