/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2002 Michael David Adams.4* All rights reserved.5*/67/* __START_OF_JASPER_LICENSE__8*9* JasPer License Version 2.010*11* Copyright (c) 2001-2006 Michael David Adams12* Copyright (c) 1999-2000 Image Power, Inc.13* Copyright (c) 1999-2000 The University of British Columbia14*15* All rights reserved.16*17* Permission is hereby granted, free of charge, to any person (the18* "User") obtaining a copy of this software and associated documentation19* files (the "Software"), to deal in the Software without restriction,20* including without limitation the rights to use, copy, modify, merge,21* publish, distribute, and/or sell copies of the Software, and to permit22* persons to whom the Software is furnished to do so, subject to the23* following conditions:24*25* 1. The above copyright notices and this permission notice (which26* includes the disclaimer below) shall be included in all copies or27* substantial portions of the Software.28*29* 2. The name of a copyright holder shall not be used to endorse or30* promote products derived from the Software without specific prior31* written permission.32*33* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS34* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER35* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS36* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING37* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A38* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO39* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL40* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING41* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,42* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION43* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE44* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE45* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.46* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS47* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL48* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS49* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE50* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE51* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL52* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,53* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL54* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH55* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,56* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH57* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY58* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.59*60* __END_OF_JASPER_LICENSE__61*/6263/*64* $Id: jpc_enc.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $65*/6667#ifndef JPC_ENC_H68#define JPC_ENC_H6970/******************************************************************************\71* Includes.72\******************************************************************************/7374#include "jasper/jas_seq.h"7576#include "jpc_t2cod.h"77#include "jpc_mqenc.h"78#include "jpc_cod.h"79#include "jpc_tagtree.h"80#include "jpc_cs.h"81#include "jpc_flt.h"82#include "jpc_tsfb.h"8384/******************************************************************************\85* Constants.86\******************************************************************************/8788/* The number of bits used in various lookup tables. */89#define JPC_NUMEXTRABITS JPC_NMSEDEC_FRACBITS9091/* An invalid R-D slope value. */92#define JPC_BADRDSLOPE (-1)9394/******************************************************************************\95* Coding parameters types.96\******************************************************************************/9798/* Per-component coding paramters. */99100typedef struct {101102/* The horizontal sampling period. */103uint_fast8_t sampgrdstepx;104105/* The vertical sampling period. */106uint_fast8_t sampgrdstepy;107108/* The sample alignment horizontal offset. */109uint_fast8_t sampgrdsubstepx;110111/* The sample alignment vertical offset. */112uint_fast8_t sampgrdsubstepy;113114/* The precision of the samples. */115uint_fast8_t prec;116117/* The signedness of the samples. */118bool sgnd;119120/* The number of step sizes. */121uint_fast16_t numstepsizes;122123/* The quantizer step sizes. */124uint_fast16_t stepsizes[JPC_MAXBANDS];125126} jpc_enc_ccp_t;127128/* Per-tile coding parameters. */129130typedef struct {131132/* The coding mode. */133bool intmode;134135/* The coding style (i.e., SOP, EPH). */136uint_fast8_t csty;137138/* The progression order. */139uint_fast8_t prg;140141/* The multicomponent transform. */142uint_fast8_t mctid;143144/* The number of layers. */145uint_fast16_t numlyrs;146147/* The normalized bit rates associated with the various148intermediate layers. */149jpc_fix_t *ilyrrates;150151} jpc_enc_tcp_t;152153/* Per tile-component coding parameters. */154155typedef struct {156157/* The coding style (i.e., explicit precinct sizes). */158uint_fast8_t csty;159160/* The maximum number of resolution levels allowed. */161uint_fast8_t maxrlvls;162163/* The exponent for the nominal code block width. */164uint_fast16_t cblkwidthexpn;165166/* The exponent for the nominal code block height. */167uint_fast16_t cblkheightexpn;168169/* The code block style parameters (e.g., lazy, terminate all,170segmentation symbols, causal, reset probability models). */171uint_fast8_t cblksty;172173/* The QMFB. */174uint_fast8_t qmfbid;175176/* The precinct width values. */177uint_fast16_t prcwidthexpns[JPC_MAXRLVLS];178179/* The precinct height values. */180uint_fast16_t prcheightexpns[JPC_MAXRLVLS];181182/* The number of guard bits. */183uint_fast8_t numgbits;184185} jpc_enc_tccp_t;186187/* Coding parameters. */188189typedef struct {190191/* The debug level. */192int debug;193194/* The horizontal offset from the origin of the reference grid to the195left edge of the image area. */196uint_fast32_t imgareatlx;197198/* The vertical offset from the origin of the reference grid to the199top edge of the image area. */200uint_fast32_t imgareatly;201202/* The horizontal offset from the origin of the reference grid to the203right edge of the image area (plus one). */204uint_fast32_t refgrdwidth;205206/* The vertical offset from the origin of the reference grid to the207bottom edge of the image area (plus one). */208uint_fast32_t refgrdheight;209210/* The horizontal offset from the origin of the tile grid to the211origin of the reference grid. */212uint_fast32_t tilegrdoffx;213214/* The vertical offset from the origin of the tile grid to the215origin of the reference grid. */216uint_fast32_t tilegrdoffy;217218/* The nominal tile width in units of the image reference grid. */219uint_fast32_t tilewidth;220221/* The nominal tile height in units of the image reference grid. */222uint_fast32_t tileheight;223224/* The number of tiles spanning the image area in the horizontal225direction. */226uint_fast32_t numhtiles;227228/* The number of tiles spanning the image area in the vertical229direction. */230uint_fast32_t numvtiles;231232/* The number of tiles. */233uint_fast32_t numtiles;234235/* The number of components. */236uint_fast16_t numcmpts;237238/* The per-component coding parameters. */239jpc_enc_ccp_t *ccps;240241/* The per-tile coding parameters. */242jpc_enc_tcp_t tcp;243244/* The per-tile-component coding parameters. */245jpc_enc_tccp_t tccp;246247/* The target code stream length in bytes. */248uint_fast32_t totalsize;249250/* The raw (i.e., uncompressed) size of the image in bytes. */251uint_fast32_t rawsize;252253} jpc_enc_cp_t;254255/******************************************************************************\256* Encoder class.257\******************************************************************************/258259/* Encoder per-coding-pass state information. */260261typedef struct {262263/* The starting offset for this pass. */264int start;265266/* The ending offset for this pass. */267int end;268269/* The type of data in this pass (i.e., MQ or raw). */270int type;271272/* Flag indicating that this pass is terminated. */273int term;274275/* The entropy coder state after coding this pass. */276jpc_mqencstate_t mqencstate;277278/* The layer to which this pass has been assigned. */279int lyrno;280281/* The R-D slope for this pass. */282jpc_flt_t rdslope;283284/* The weighted MSE reduction associated with this pass. */285jpc_flt_t wmsedec;286287/* The cumulative weighted MSE reduction. */288jpc_flt_t cumwmsedec;289290/* The normalized MSE reduction. */291long nmsedec;292293} jpc_enc_pass_t;294295/* Encoder per-code-block state information. */296297typedef struct {298299/* The number of passes. */300int numpasses;301302/* The per-pass information. */303jpc_enc_pass_t *passes;304305/* The number of passes encoded so far. */306int numencpasses;307308/* The number of insignificant MSBs. */309int numimsbs;310311/* The number of bits used to encode pass data lengths. */312int numlenbits;313314/* The byte stream for this code block. */315jas_stream_t *stream;316317/* The entropy encoder. */318jpc_mqenc_t *mqenc;319320/* The data for this code block. */321jas_matrix_t *data;322323/* The state for this code block. */324jas_matrix_t *flags;325326/* The number of bit planes required for this code block. */327int numbps;328329/* The next pass to be encoded. */330jpc_enc_pass_t *curpass;331332/* The per-code-block-group state information. */333struct jpc_enc_prc_s *prc;334335/* The saved current pass. */336/* This is used by the rate control code. */337jpc_enc_pass_t *savedcurpass;338339/* The saved length indicator size. */340/* This is used by the rate control code. */341int savednumlenbits;342343/* The saved number of encoded passes. */344/* This is used by the rate control code. */345int savednumencpasses;346347} jpc_enc_cblk_t;348349/* Encoder per-code-block-group state information. */350351typedef struct jpc_enc_prc_s {352353/* The x-coordinate of the top-left corner of the precinct. */354uint_fast32_t tlx;355356/* The y-coordinate of the top-left corner of the precinct. */357uint_fast32_t tly;358359/* The x-coordinate of the bottom-right corner of the precinct360(plus one). */361uint_fast32_t brx;362363/* The y-coordinate of the bottom-right corner of the precinct364(plus one). */365uint_fast32_t bry;366367/* The number of code blocks spanning the precinct in the horizontal368direction. */369int numhcblks;370371/* The number of code blocks spanning the precinct in the vertical372direction. */373int numvcblks;374375/* The total number of code blocks. */376int numcblks;377378/* The per-code-block information. */379jpc_enc_cblk_t *cblks;380381/* The inclusion tag tree. */382jpc_tagtree_t *incltree;383384/* The insignifcant MSBs tag tree. */385jpc_tagtree_t *nlibtree;386387/* The per-band information. */388struct jpc_enc_band_s *band;389390/* The saved inclusion tag tree. */391/* This is used by rate control. */392jpc_tagtree_t *savincltree;393394/* The saved leading-insignificant-bit-planes tag tree. */395/* This is used by rate control. */396jpc_tagtree_t *savnlibtree;397398} jpc_enc_prc_t;399400/* Encoder per-band state information. */401402typedef struct jpc_enc_band_s {403404/* The per precinct information. */405jpc_enc_prc_t *prcs;406407/* The coefficient data for this band. */408jas_matrix_t *data;409410/* The orientation of this band (i.e., LL, LH, HL, or HH). */411int orient;412413/* The number of bit planes associated with this band. */414int numbps;415416/* The quantizer step size. */417jpc_fix_t absstepsize;418419/* The encoded quantizer step size. */420int stepsize;421422/* The L2 norm of the synthesis basis functions associated with423this band. (The MCT is not considered in this value.) */424jpc_fix_t synweight;425426/* The analysis gain for this band. */427int analgain;428429/* The per-resolution-level information. */430struct jpc_enc_rlvl_s *rlvl;431432} jpc_enc_band_t;433434/* Encoder per-resolution-level state information. */435436typedef struct jpc_enc_rlvl_s {437438/* The x-coordinate of the top-left corner of the tile-component439at this resolution. */440uint_fast32_t tlx;441442/* The y-coordinate of the top-left corner of the tile-component443at this resolution. */444uint_fast32_t tly;445446/* The x-coordinate of the bottom-right corner of the tile-component447at this resolution (plus one). */448uint_fast32_t brx;449450/* The y-coordinate of the bottom-right corner of the tile-component451at this resolution (plus one). */452uint_fast32_t bry;453454/* The exponent value for the nominal precinct width measured455relative to the associated LL band. */456int prcwidthexpn;457458/* The exponent value for the nominal precinct height measured459relative to the associated LL band. */460int prcheightexpn;461462/* The number of precincts spanning the resolution level in the463horizontal direction. */464int numhprcs;465466/* The number of precincts spanning the resolution level in the467vertical direction. */468int numvprcs;469470/* The total number of precincts. */471int numprcs;472473/* The exponent value for the nominal code block group width.474This quantity is associated with the next lower resolution level475(assuming that there is one). */476int cbgwidthexpn;477478/* The exponent value for the nominal code block group height.479This quantity is associated with the next lower resolution level480(assuming that there is one). */481int cbgheightexpn;482483/* The exponent value for the code block width. */484uint_fast16_t cblkwidthexpn;485486/* The exponent value for the code block height. */487uint_fast16_t cblkheightexpn;488489/* The number of bands associated with this resolution level. */490int numbands;491492/* The per-band information. */493jpc_enc_band_t *bands;494495/* The parent tile-component. */496struct jpc_enc_tcmpt_s *tcmpt;497498} jpc_enc_rlvl_t;499500/* Encoder per-tile-component state information. */501502typedef struct jpc_enc_tcmpt_s {503504/* The number of resolution levels. */505int numrlvls;506507/* The per-resolution-level information. */508jpc_enc_rlvl_t *rlvls;509510/* The tile-component data. */511jas_matrix_t *data;512513/* The QMFB. */514int qmfbid;515516/* The number of bands. */517int numbands;518519/* The TSFB. */520jpc_tsfb_t *tsfb;521522/* The synthesis energy weight (for the MCT). */523jpc_fix_t synweight;524525/* The precinct width exponents. */526int prcwidthexpns[JPC_MAXRLVLS];527528/* The precinct height exponents. */529int prcheightexpns[JPC_MAXRLVLS];530531/* The code block width exponent. */532int cblkwidthexpn;533534/* The code block height exponent. */535int cblkheightexpn;536537/* Coding style (i.e., explicit precinct sizes). */538int csty;539540/* Code block style. */541int cblksty;542543/* The number of quantizer step sizes. */544int numstepsizes;545546/* The encoded quantizer step sizes. */547uint_fast16_t stepsizes[JPC_MAXBANDS];548549/* The parent tile. */550struct jpc_enc_tile_s *tile;551552} jpc_enc_tcmpt_t;553554/* Encoder per-tile state information. */555556typedef struct jpc_enc_tile_s {557558/* The tile number. */559uint_fast32_t tileno;560561/* The x-coordinate of the top-left corner of the tile measured with562respect to the reference grid. */563uint_fast32_t tlx;564565/* The y-coordinate of the top-left corner of the tile measured with566respect to the reference grid. */567uint_fast32_t tly;568569/* The x-coordinate of the bottom-right corner of the tile measured570with respect to the reference grid (plus one). */571uint_fast32_t brx;572573/* The y-coordinate of the bottom-right corner of the tile measured574with respect to the reference grid (plus one). */575uint_fast32_t bry;576577/* The coding style. */578uint_fast8_t csty;579580/* The progression order. */581uint_fast8_t prg;582583/* The number of layers. */584int numlyrs;585586/* The MCT to employ (if any). */587uint_fast8_t mctid;588589/* The packet iterator (used to determine the order of packet590generation). */591jpc_pi_t *pi;592593/* The coding mode (i.e., integer or real). */594bool intmode;595596/* The number of bytes to allocate to the various layers. */597uint_fast32_t *lyrsizes;598599/* The number of tile-components. */600int numtcmpts;601602/* The per tile-component information. */603jpc_enc_tcmpt_t *tcmpts;604605/* The raw (i.e., uncompressed) size of this tile. */606uint_fast32_t rawsize;607608} jpc_enc_tile_t;609610/* Encoder class. */611612typedef struct jpc_enc_s {613614/* The image being encoded. */615jas_image_t *image;616617/* The output stream. */618jas_stream_t *out;619620/* The coding parameters. */621jpc_enc_cp_t *cp;622623/* The tile currently being processed. */624jpc_enc_tile_t *curtile;625626/* The code stream state. */627jpc_cstate_t *cstate;628629/* The number of bytes output so far. */630uint_fast32_t len;631632/* The number of bytes available for the main body of the code stream. */633/* This is used for rate allocation purposes. */634uint_fast32_t mainbodysize;635636/* The marker segment currently being processed. */637/* This member is a convenience for making cleanup easier. */638jpc_ms_t *mrk;639640/* The stream used to temporarily hold tile-part data. */641jas_stream_t *tmpstream;642643} jpc_enc_t;644645#endif646647648