/*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* JPEG-2000 Decoder65*66* $Id: jpc_dec.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $67*/6869#ifndef JPC_DEC_H70#define JPC_DEC_H7172/******************************************************************************\73* Includes.74\******************************************************************************/7576#include "jasper/jas_stream.h"7778#include "jpc_tsfb.h"79#include "jpc_bs.h"80#include "jpc_tagtree.h"81#include "jpc_cs.h"82#include "jpc_cod.h"83#include "jpc_mqdec.h"84#include "jpc_t2cod.h"8586/******************************************************************************\87* Below are some ugly warts necessary to support packed packet headers.88\******************************************************************************/8990/* PPM/PPT marker segment table entry. */9192typedef struct {9394/* The index for this entry. */95uint_fast16_t ind;9697/* The data length. */98uint_fast32_t len;99100/* The data. */101uchar *data;102103} jpc_ppxstabent_t;104105/* PPM/PPT marker segment table. */106107typedef struct {108109/* The number of entries. */110int numents;111112/* The maximum number of entries (i.e., the allocated size of the array113below). */114int maxents;115116/* The table entries. */117jpc_ppxstabent_t **ents;118119} jpc_ppxstab_t;120121/* Stream list class. */122123typedef struct {124125/* The number of streams in this list. */126int numstreams;127128/* The maximum number of streams that can be accomodated without129growing the streams array. */130int maxstreams;131132/* The streams. */133jas_stream_t **streams;134135} jpc_streamlist_t;136137/******************************************************************************\138* Coding parameters class.139\******************************************************************************/140141/* Per-component coding parameters. */142143typedef struct {144145/* How were various coding parameters set? */146int flags;147148/* Per-component coding style parameters (e.g., explicit precinct sizes) */149uint_fast8_t csty;150151/* The number of resolution levels. */152uint_fast8_t numrlvls;153154/* The code block width exponent. */155uint_fast8_t cblkwidthexpn;156157/* The code block height exponent. */158uint_fast8_t cblkheightexpn;159160/* The QMFB ID. */161uint_fast8_t qmfbid;162163/* The quantization style. */164uint_fast8_t qsty;165166/* The number of quantizer step sizes. */167uint_fast16_t numstepsizes;168169/* The step sizes. */170uint_fast16_t stepsizes[3 * JPC_MAXRLVLS + 1];171172/* The number of guard bits. */173uint_fast8_t numguardbits;174175/* The ROI shift value. */176uint_fast8_t roishift;177178/* The code block parameters. */179uint_fast8_t cblkctx;180181/* The precinct width exponents. */182uint_fast8_t prcwidthexpns[JPC_MAXRLVLS];183184/* The precinct height exponents. */185uint_fast8_t prcheightexpns[JPC_MAXRLVLS];186187} jpc_dec_ccp_t;188189/* Coding paramters. */190191typedef struct {192193/* How were these coding parameters set? */194int flags;195196/* Progression change list. */197jpc_pchglist_t *pchglist;198199/* Progression order. */200uint_fast8_t prgord;201202/* The number of layers. */203uint_fast16_t numlyrs;204205/* The MCT ID. */206uint_fast8_t mctid;207208/* The coding style parameters (e.g., SOP, EPH). */209uint_fast8_t csty;210211/* The number of components. */212int numcomps;213214/* The per-component coding parameters. */215jpc_dec_ccp_t *ccps;216217} jpc_dec_cp_t;218219/******************************************************************************\220* Decoder class.221\******************************************************************************/222223/* Decoder per-segment state information. */224225typedef struct jpc_dec_seg_s {226227/* The next segment in the list. */228struct jpc_dec_seg_s *next;229230/* The previous segment in the list. */231struct jpc_dec_seg_s *prev;232233/* The starting pass number for this segment. */234int passno;235236/* The number of passes in this segment. */237int numpasses;238239/* The maximum number of passes in this segment. */240int maxpasses;241242/* The type of data in this segment (i.e., MQ or raw). */243int type;244245/* A stream containing the data for this segment. */246jas_stream_t *stream;247248/* The number of bytes destined for this segment from the packet249currently being decoded. */250int cnt;251252/* A flag indicating if this segment has been terminated. */253int complete;254255/* The layer number to which this segment belongs. */256/* If the segment spans multiple layers, then the largest layer number257spanned by the segment is used. */258int lyrno;259260} jpc_dec_seg_t;261262/* Decoder segment list. */263264typedef struct {265266/* The first entry in the list. */267jpc_dec_seg_t *head;268269/* The last entry in the list. */270jpc_dec_seg_t *tail;271272} jpc_dec_seglist_t;273274/* Decoder per-code-block state information. */275276typedef struct {277278/* The number of passes. */279int numpasses;280281/* A list of segments that still need to be decoded. */282jpc_dec_seglist_t segs;283284/* The first incomplete/partial segment. */285jpc_dec_seg_t *curseg;286287/* The number of leading insignificant bit planes for this code block. */288int numimsbs;289290/* The number of bits used to encode pass data lengths. */291int numlenbits;292293/* The first pass number containing data for this code block. */294int firstpassno;295296/* The MQ decoder. */297jpc_mqdec_t *mqdec;298299/* The raw bit stream decoder. */300jpc_bitstream_t *nulldec;301302/* The per-sample state information for this code block. */303jas_matrix_t *flags;304305/* The sample data associated with this code block. */306jas_matrix_t *data;307308} jpc_dec_cblk_t;309310/* Decoder per-code-block-group state information. */311312typedef struct {313314/* The x-coordinate of the top-left corner of the precinct. */315uint_fast32_t xstart;316317/* The y-coordinate of the top-left corner of the precinct. */318uint_fast32_t ystart;319320/* The x-coordinate of the bottom-right corner of the precinct321(plus one). */322uint_fast32_t xend;323324/* The y-coordinate of the bottom-right corner of the precinct325(plus one). */326uint_fast32_t yend;327328/* The number of code blocks spanning this precinct in the horizontal329direction. */330int numhcblks;331332/* The number of code blocks spanning this precinct in the vertical333direction. */334int numvcblks;335336/* The total number of code blocks in this precinct. */337int numcblks;338339/* The per code block information. */340jpc_dec_cblk_t *cblks;341342/* The inclusion tag tree. */343jpc_tagtree_t *incltagtree;344345/* The insignificant MSBs tag tree. */346jpc_tagtree_t *numimsbstagtree;347348} jpc_dec_prc_t;349350/* Decoder per-band state information. */351352typedef struct {353354/* The per-code-block-group state information. */355jpc_dec_prc_t *prcs;356357/* The sample data associated with this band. */358jas_matrix_t *data;359360/* The orientation of this band (i.e., LL, LH, HL, or HH). */361int orient;362363/* The encoded quantizer step size. */364int stepsize;365366/* The absolute quantizer step size. */367jpc_fix_t absstepsize;368369/* The number of bit planes for this band. */370int numbps;371372/* The analysis gain associated with this band. */373int analgain;374375/* The ROI shift value for this band. */376int roishift;377378} jpc_dec_band_t;379380/* Decoder per-resolution-level state information. */381382typedef struct {383384/* The number of bands associated with this resolution level. */385int numbands;386387/* The per-band information. */388jpc_dec_band_t *bands;389390/* The x-coordinate of the top-left corner of the tile-component391at this resolution. */392uint_fast32_t xstart;393394/* The y-coordinate of the top-left corner of the tile-component395at this resolution. */396uint_fast32_t ystart;397398/* The x-coordinate of the bottom-right corner of the tile-component399at this resolution (plus one). */400uint_fast32_t xend;401402/* The y-coordinate of the bottom-right corner of the tile-component403at this resolution (plus one). */404uint_fast32_t yend;405406/* The exponent value for the nominal precinct width measured407relative to the associated LL band. */408int prcwidthexpn;409410/* The exponent value for the nominal precinct height measured411relative to the associated LL band. */412int prcheightexpn;413414/* The number of precincts in the horizontal direction. */415int numhprcs;416417/* The number of precincts in the vertical direction. */418int numvprcs;419420/* The total number of precincts. */421int numprcs;422423/* The exponent value for the nominal code block group width.424This quantity is associated with the next lower resolution level425(assuming that there is one). */426int cbgwidthexpn;427428/* The exponent value for the nominal code block group height429This quantity is associated with the next lower resolution level430(assuming that there is one). */431int cbgheightexpn;432433/* The exponent value for the code block width. */434uint_fast16_t cblkwidthexpn;435436/* The exponent value for the code block height. */437uint_fast16_t cblkheightexpn;438439} jpc_dec_rlvl_t;440441/* Decoder per-tile-component state information. */442443typedef struct {444445/* The x-coordinate of the top-left corner of the tile-component446in the coordinate system of the tile-component. */447uint_fast32_t xstart;448449/* The y-coordinate of the top-left corner of the tile-component450in the coordinate system of the tile-component. */451uint_fast32_t ystart;452453/* The x-coordinate of the bottom-right corner of the tile-component454in the coordinate system of the tile-component (plus one). */455uint_fast32_t xend;456457/* The y-coordinate of the bottom-right corner of the tile-component458in the coordinate system of the tile-component (plus one). */459uint_fast32_t yend;460461/* The component data for the current tile. */462jas_matrix_t *data;463464/* The number of resolution levels. */465int numrlvls;466467/* The per resolution level information. */468jpc_dec_rlvl_t *rlvls;469470/* The TSFB. */471jpc_tsfb_t *tsfb;472473} jpc_dec_tcomp_t;474475/*476* Tile states.477*/478479#define JPC_TILE_INIT 0480#define JPC_TILE_ACTIVE 1481#define JPC_TILE_ACTIVELAST 2482#define JPC_TILE_DONE 3483484/* Decoder per-tile state information. */485486typedef struct {487488/* The processing state for this tile. */489int state;490491/* The x-coordinate of the top-left corner of the tile on the reference492grid. */493uint_fast32_t xstart;494495/* The y-coordinate of the top-left corner of the tile on the reference496grid. */497uint_fast32_t ystart;498499/* The x-coordinate of the bottom-right corner of the tile on the500reference grid (plus one). */501uint_fast32_t xend;502503/* The y-coordinate of the bottom-right corner of the tile on the504reference grid (plus one). */505uint_fast32_t yend;506507/* The packed packet header data for this tile. */508jpc_ppxstab_t *pptstab;509510/* A stream containing the packed packet header data for this tile. */511jas_stream_t *pkthdrstream;512513/* The current position within the packed packet header stream. */514long pkthdrstreampos;515516/* The coding parameters for this tile. */517jpc_dec_cp_t *cp;518519/* The per tile-component information. */520jpc_dec_tcomp_t *tcomps;521522/* The next expected tile-part number. */523int partno;524525/* The number of tile-parts. */526int numparts;527528/* The coding mode. */529int realmode;530531/* The packet iterator for this tile. */532jpc_pi_t *pi;533534} jpc_dec_tile_t;535536/* Decoder per-component state information. */537538typedef struct {539540/* The horizontal sampling period. */541uint_fast32_t hstep;542543/* The vertical sampling period. */544uint_fast32_t vstep;545546/* The number of samples in the horizontal direction. */547uint_fast32_t width;548549/* The number of samples in the vertical direction. */550uint_fast32_t height;551552/* The precision of the sample data. */553uint_fast16_t prec;554555/* The signedness of the sample data. */556bool sgnd;557558/* The sample alignment horizontal offset. */559uint_fast32_t hsubstep;560561/* The sample alignment vertical offset. */562uint_fast32_t vsubstep;563564} jpc_dec_cmpt_t;565566/* Decoder state information. */567568typedef struct {569570/* The decoded image. */571jas_image_t *image;572573/* The x-coordinate of the top-left corner of the image area on574the reference grid. */575uint_fast32_t xstart;576577/* The y-coordinate of the top-left corner of the image area on578the reference grid. */579uint_fast32_t ystart;580581/* The x-coordinate of the bottom-right corner of the image area on582the reference grid (plus one). */583uint_fast32_t xend;584585/* The y-coordinate of the bottom-right corner of the image area on586the reference grid (plus one). */587uint_fast32_t yend;588589/* The nominal tile width in units of the image reference grid. */590uint_fast32_t tilewidth;591592/* The nominal tile height in units of the image reference grid. */593uint_fast32_t tileheight;594595/* The horizontal offset from the origin of the reference grid to the596left side of the first tile. */597uint_fast32_t tilexoff;598599/* The vertical offset from the origin of the reference grid to the600top side of the first tile. */601uint_fast32_t tileyoff;602603/* The number of tiles spanning the image area in the vertical604direction. */605int numhtiles;606607/* The number of tiles spanning the image area in the horizontal608direction. */609int numvtiles;610611/* The total number of tiles. */612int numtiles;613614/* The per-tile information. */615jpc_dec_tile_t *tiles;616617/* The tile currently being processed. */618jpc_dec_tile_t *curtile;619620/* The number of components. */621int numcomps;622623/* The stream containing the input JPEG-2000 code stream data. */624jas_stream_t *in;625626/* The default coding parameters for all tiles. */627jpc_dec_cp_t *cp;628629/* The maximum number of layers that may be decoded. */630int maxlyrs;631632/* The maximum number of packets that may be decoded. */633int maxpkts;634635/* The number of packets decoded so far in the processing of the entire636code stream. */637int numpkts;638639/* The next expected PPM marker segment sequence number. */640int ppmseqno;641642/* The current state for code stream processing. */643int state;644645/* The per-component information. */646jpc_dec_cmpt_t *cmpts;647648/* The information from PPM marker segments. */649jpc_ppxstab_t *ppmstab;650651/* A list of streams containing packet header data from PPM marker652segments. */653jpc_streamlist_t *pkthdrstreams;654655/* The expected ending offset for a tile-part. */656long curtileendoff;657658/* This is required by the tier-2 decoder. */659jpc_cstate_t *cstate;660661} jpc_dec_t;662663/* Decoder options. */664665typedef struct {666667/* The debug level for the decoder. */668int debug;669670/* The maximum number of layers to decode. */671int maxlyrs;672673/* The maximum number of packets to decode. */674int maxpkts;675676} jpc_dec_importopts_t;677678/******************************************************************************\679* Functions.680\******************************************************************************/681682/* Create a decoder segment object. */683jpc_dec_seg_t *jpc_seg_alloc(void);684685/* Destroy a decoder segment object. */686void jpc_seg_destroy(jpc_dec_seg_t *seg);687688/* Remove a segment from a segment list. */689void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *node);690691/* Insert a segment into a segment list. */692void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins,693jpc_dec_seg_t *node);694695#endif696697698