jpc_dec.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * 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 permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * JPEG-2000 Decoder
  64. *
  65. * $Id: jpc_dec.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. #ifndef JPC_DEC_H
  68. #define JPC_DEC_H
  69. /******************************************************************************\
  70. * Includes.
  71. \******************************************************************************/
  72. #include "jasper/jas_stream.h"
  73. #include "jpc_tsfb.h"
  74. #include "jpc_bs.h"
  75. #include "jpc_tagtree.h"
  76. #include "jpc_cs.h"
  77. #include "jpc_cod.h"
  78. #include "jpc_mqdec.h"
  79. #include "jpc_t2cod.h"
  80. /******************************************************************************\
  81. * Below are some ugly warts necessary to support packed packet headers.
  82. \******************************************************************************/
  83. /* PPM/PPT marker segment table entry. */
  84. typedef struct {
  85. /* The index for this entry. */
  86. uint_fast16_t ind;
  87. /* The data length. */
  88. uint_fast32_t len;
  89. /* The data. */
  90. uchar *data;
  91. } jpc_ppxstabent_t;
  92. /* PPM/PPT marker segment table. */
  93. typedef struct {
  94. /* The number of entries. */
  95. int numents;
  96. /* The maximum number of entries (i.e., the allocated size of the array
  97. below). */
  98. int maxents;
  99. /* The table entries. */
  100. jpc_ppxstabent_t **ents;
  101. } jpc_ppxstab_t;
  102. /* Stream list class. */
  103. typedef struct {
  104. /* The number of streams in this list. */
  105. int numstreams;
  106. /* The maximum number of streams that can be accomodated without
  107. growing the streams array. */
  108. int maxstreams;
  109. /* The streams. */
  110. jas_stream_t **streams;
  111. } jpc_streamlist_t;
  112. /******************************************************************************\
  113. * Coding parameters class.
  114. \******************************************************************************/
  115. /* Per-component coding parameters. */
  116. typedef struct {
  117. /* How were various coding parameters set? */
  118. int flags;
  119. /* Per-component coding style parameters (e.g., explicit precinct sizes) */
  120. uint_fast8_t csty;
  121. /* The number of resolution levels. */
  122. uint_fast8_t numrlvls;
  123. /* The code block width exponent. */
  124. uint_fast8_t cblkwidthexpn;
  125. /* The code block height exponent. */
  126. uint_fast8_t cblkheightexpn;
  127. /* The QMFB ID. */
  128. uint_fast8_t qmfbid;
  129. /* The quantization style. */
  130. uint_fast8_t qsty;
  131. /* The number of quantizer step sizes. */
  132. uint_fast16_t numstepsizes;
  133. /* The step sizes. */
  134. uint_fast16_t stepsizes[3 * JPC_MAXRLVLS + 1];
  135. /* The number of guard bits. */
  136. uint_fast8_t numguardbits;
  137. /* The ROI shift value. */
  138. uint_fast8_t roishift;
  139. /* The code block parameters. */
  140. uint_fast8_t cblkctx;
  141. /* The precinct width exponents. */
  142. uint_fast8_t prcwidthexpns[JPC_MAXRLVLS];
  143. /* The precinct height exponents. */
  144. uint_fast8_t prcheightexpns[JPC_MAXRLVLS];
  145. } jpc_dec_ccp_t;
  146. /* Coding paramters. */
  147. typedef struct {
  148. /* How were these coding parameters set? */
  149. int flags;
  150. /* Progression change list. */
  151. jpc_pchglist_t *pchglist;
  152. /* Progression order. */
  153. uint_fast8_t prgord;
  154. /* The number of layers. */
  155. uint_fast16_t numlyrs;
  156. /* The MCT ID. */
  157. uint_fast8_t mctid;
  158. /* The coding style parameters (e.g., SOP, EPH). */
  159. uint_fast8_t csty;
  160. /* The number of components. */
  161. int numcomps;
  162. /* The per-component coding parameters. */
  163. jpc_dec_ccp_t *ccps;
  164. } jpc_dec_cp_t;
  165. /******************************************************************************\
  166. * Decoder class.
  167. \******************************************************************************/
  168. /* Decoder per-segment state information. */
  169. typedef struct jpc_dec_seg_s {
  170. /* The next segment in the list. */
  171. struct jpc_dec_seg_s *next;
  172. /* The previous segment in the list. */
  173. struct jpc_dec_seg_s *prev;
  174. /* The starting pass number for this segment. */
  175. int passno;
  176. /* The number of passes in this segment. */
  177. int numpasses;
  178. /* The maximum number of passes in this segment. */
  179. int maxpasses;
  180. /* The type of data in this segment (i.e., MQ or raw). */
  181. int type;
  182. /* A stream containing the data for this segment. */
  183. jas_stream_t *stream;
  184. /* The number of bytes destined for this segment from the packet
  185. currently being decoded. */
  186. int cnt;
  187. /* A flag indicating if this segment has been terminated. */
  188. int complete;
  189. /* The layer number to which this segment belongs. */
  190. /* If the segment spans multiple layers, then the largest layer number
  191. spanned by the segment is used. */
  192. int lyrno;
  193. } jpc_dec_seg_t;
  194. /* Decoder segment list. */
  195. typedef struct {
  196. /* The first entry in the list. */
  197. jpc_dec_seg_t *head;
  198. /* The last entry in the list. */
  199. jpc_dec_seg_t *tail;
  200. } jpc_dec_seglist_t;
  201. /* Decoder per-code-block state information. */
  202. typedef struct {
  203. /* The number of passes. */
  204. int numpasses;
  205. /* A list of segments that still need to be decoded. */
  206. jpc_dec_seglist_t segs;
  207. /* The first incomplete/partial segment. */
  208. jpc_dec_seg_t *curseg;
  209. /* The number of leading insignificant bit planes for this code block. */
  210. int numimsbs;
  211. /* The number of bits used to encode pass data lengths. */
  212. int numlenbits;
  213. /* The first pass number containing data for this code block. */
  214. int firstpassno;
  215. /* The MQ decoder. */
  216. jpc_mqdec_t *mqdec;
  217. /* The raw bit stream decoder. */
  218. jpc_bitstream_t *nulldec;
  219. /* The per-sample state information for this code block. */
  220. jas_matrix_t *flags;
  221. /* The sample data associated with this code block. */
  222. jas_matrix_t *data;
  223. } jpc_dec_cblk_t;
  224. /* Decoder per-code-block-group state information. */
  225. typedef struct {
  226. /* The x-coordinate of the top-left corner of the precinct. */
  227. uint_fast32_t xstart;
  228. /* The y-coordinate of the top-left corner of the precinct. */
  229. uint_fast32_t ystart;
  230. /* The x-coordinate of the bottom-right corner of the precinct
  231. (plus one). */
  232. uint_fast32_t xend;
  233. /* The y-coordinate of the bottom-right corner of the precinct
  234. (plus one). */
  235. uint_fast32_t yend;
  236. /* The number of code blocks spanning this precinct in the horizontal
  237. direction. */
  238. int numhcblks;
  239. /* The number of code blocks spanning this precinct in the vertical
  240. direction. */
  241. int numvcblks;
  242. /* The total number of code blocks in this precinct. */
  243. int numcblks;
  244. /* The per code block information. */
  245. jpc_dec_cblk_t *cblks;
  246. /* The inclusion tag tree. */
  247. jpc_tagtree_t *incltagtree;
  248. /* The insignificant MSBs tag tree. */
  249. jpc_tagtree_t *numimsbstagtree;
  250. } jpc_dec_prc_t;
  251. /* Decoder per-band state information. */
  252. typedef struct {
  253. /* The per-code-block-group state information. */
  254. jpc_dec_prc_t *prcs;
  255. /* The sample data associated with this band. */
  256. jas_matrix_t *data;
  257. /* The orientation of this band (i.e., LL, LH, HL, or HH). */
  258. int orient;
  259. /* The encoded quantizer step size. */
  260. int stepsize;
  261. /* The absolute quantizer step size. */
  262. jpc_fix_t absstepsize;
  263. /* The number of bit planes for this band. */
  264. int numbps;
  265. /* The analysis gain associated with this band. */
  266. int analgain;
  267. /* The ROI shift value for this band. */
  268. int roishift;
  269. } jpc_dec_band_t;
  270. /* Decoder per-resolution-level state information. */
  271. typedef struct {
  272. /* The number of bands associated with this resolution level. */
  273. int numbands;
  274. /* The per-band information. */
  275. jpc_dec_band_t *bands;
  276. /* The x-coordinate of the top-left corner of the tile-component
  277. at this resolution. */
  278. uint_fast32_t xstart;
  279. /* The y-coordinate of the top-left corner of the tile-component
  280. at this resolution. */
  281. uint_fast32_t ystart;
  282. /* The x-coordinate of the bottom-right corner of the tile-component
  283. at this resolution (plus one). */
  284. uint_fast32_t xend;
  285. /* The y-coordinate of the bottom-right corner of the tile-component
  286. at this resolution (plus one). */
  287. uint_fast32_t yend;
  288. /* The exponent value for the nominal precinct width measured
  289. relative to the associated LL band. */
  290. int prcwidthexpn;
  291. /* The exponent value for the nominal precinct height measured
  292. relative to the associated LL band. */
  293. int prcheightexpn;
  294. /* The number of precincts in the horizontal direction. */
  295. int numhprcs;
  296. /* The number of precincts in the vertical direction. */
  297. int numvprcs;
  298. /* The total number of precincts. */
  299. int numprcs;
  300. /* The exponent value for the nominal code block group width.
  301. This quantity is associated with the next lower resolution level
  302. (assuming that there is one). */
  303. int cbgwidthexpn;
  304. /* The exponent value for the nominal code block group height
  305. This quantity is associated with the next lower resolution level
  306. (assuming that there is one). */
  307. int cbgheightexpn;
  308. /* The exponent value for the code block width. */
  309. uint_fast16_t cblkwidthexpn;
  310. /* The exponent value for the code block height. */
  311. uint_fast16_t cblkheightexpn;
  312. } jpc_dec_rlvl_t;
  313. /* Decoder per-tile-component state information. */
  314. typedef struct {
  315. /* The x-coordinate of the top-left corner of the tile-component
  316. in the coordinate system of the tile-component. */
  317. uint_fast32_t xstart;
  318. /* The y-coordinate of the top-left corner of the tile-component
  319. in the coordinate system of the tile-component. */
  320. uint_fast32_t ystart;
  321. /* The x-coordinate of the bottom-right corner of the tile-component
  322. in the coordinate system of the tile-component (plus one). */
  323. uint_fast32_t xend;
  324. /* The y-coordinate of the bottom-right corner of the tile-component
  325. in the coordinate system of the tile-component (plus one). */
  326. uint_fast32_t yend;
  327. /* The component data for the current tile. */
  328. jas_matrix_t *data;
  329. /* The number of resolution levels. */
  330. int numrlvls;
  331. /* The per resolution level information. */
  332. jpc_dec_rlvl_t *rlvls;
  333. /* The TSFB. */
  334. jpc_tsfb_t *tsfb;
  335. } jpc_dec_tcomp_t;
  336. /*
  337. * Tile states.
  338. */
  339. #define JPC_TILE_INIT 0
  340. #define JPC_TILE_ACTIVE 1
  341. #define JPC_TILE_ACTIVELAST 2
  342. #define JPC_TILE_DONE 3
  343. /* Decoder per-tile state information. */
  344. typedef struct {
  345. /* The processing state for this tile. */
  346. int state;
  347. /* The x-coordinate of the top-left corner of the tile on the reference
  348. grid. */
  349. uint_fast32_t xstart;
  350. /* The y-coordinate of the top-left corner of the tile on the reference
  351. grid. */
  352. uint_fast32_t ystart;
  353. /* The x-coordinate of the bottom-right corner of the tile on the
  354. reference grid (plus one). */
  355. uint_fast32_t xend;
  356. /* The y-coordinate of the bottom-right corner of the tile on the
  357. reference grid (plus one). */
  358. uint_fast32_t yend;
  359. /* The packed packet header data for this tile. */
  360. jpc_ppxstab_t *pptstab;
  361. /* A stream containing the packed packet header data for this tile. */
  362. jas_stream_t *pkthdrstream;
  363. /* The current position within the packed packet header stream. */
  364. long pkthdrstreampos;
  365. /* The coding parameters for this tile. */
  366. jpc_dec_cp_t *cp;
  367. /* The per tile-component information. */
  368. jpc_dec_tcomp_t *tcomps;
  369. /* The next expected tile-part number. */
  370. int partno;
  371. /* The number of tile-parts. */
  372. int numparts;
  373. /* The coding mode. */
  374. int realmode;
  375. /* The packet iterator for this tile. */
  376. jpc_pi_t *pi;
  377. } jpc_dec_tile_t;
  378. /* Decoder per-component state information. */
  379. typedef struct {
  380. /* The horizontal sampling period. */
  381. uint_fast32_t hstep;
  382. /* The vertical sampling period. */
  383. uint_fast32_t vstep;
  384. /* The number of samples in the horizontal direction. */
  385. uint_fast32_t width;
  386. /* The number of samples in the vertical direction. */
  387. uint_fast32_t height;
  388. /* The precision of the sample data. */
  389. uint_fast16_t prec;
  390. /* The signedness of the sample data. */
  391. bool sgnd;
  392. /* The sample alignment horizontal offset. */
  393. uint_fast32_t hsubstep;
  394. /* The sample alignment vertical offset. */
  395. uint_fast32_t vsubstep;
  396. } jpc_dec_cmpt_t;
  397. /* Decoder state information. */
  398. typedef struct {
  399. /* The decoded image. */
  400. jas_image_t *image;
  401. /* The x-coordinate of the top-left corner of the image area on
  402. the reference grid. */
  403. uint_fast32_t xstart;
  404. /* The y-coordinate of the top-left corner of the image area on
  405. the reference grid. */
  406. uint_fast32_t ystart;
  407. /* The x-coordinate of the bottom-right corner of the image area on
  408. the reference grid (plus one). */
  409. uint_fast32_t xend;
  410. /* The y-coordinate of the bottom-right corner of the image area on
  411. the reference grid (plus one). */
  412. uint_fast32_t yend;
  413. /* The nominal tile width in units of the image reference grid. */
  414. uint_fast32_t tilewidth;
  415. /* The nominal tile height in units of the image reference grid. */
  416. uint_fast32_t tileheight;
  417. /* The horizontal offset from the origin of the reference grid to the
  418. left side of the first tile. */
  419. uint_fast32_t tilexoff;
  420. /* The vertical offset from the origin of the reference grid to the
  421. top side of the first tile. */
  422. uint_fast32_t tileyoff;
  423. /* The number of tiles spanning the image area in the vertical
  424. direction. */
  425. int numhtiles;
  426. /* The number of tiles spanning the image area in the horizontal
  427. direction. */
  428. int numvtiles;
  429. /* The total number of tiles. */
  430. int numtiles;
  431. /* The per-tile information. */
  432. jpc_dec_tile_t *tiles;
  433. /* The tile currently being processed. */
  434. jpc_dec_tile_t *curtile;
  435. /* The number of components. */
  436. int numcomps;
  437. /* The stream containing the input JPEG-2000 code stream data. */
  438. jas_stream_t *in;
  439. /* The default coding parameters for all tiles. */
  440. jpc_dec_cp_t *cp;
  441. /* The maximum number of layers that may be decoded. */
  442. int maxlyrs;
  443. /* The maximum number of packets that may be decoded. */
  444. int maxpkts;
  445. /* The number of packets decoded so far in the processing of the entire
  446. code stream. */
  447. int numpkts;
  448. /* The next expected PPM marker segment sequence number. */
  449. int ppmseqno;
  450. /* The current state for code stream processing. */
  451. int state;
  452. /* The per-component information. */
  453. jpc_dec_cmpt_t *cmpts;
  454. /* The information from PPM marker segments. */
  455. jpc_ppxstab_t *ppmstab;
  456. /* A list of streams containing packet header data from PPM marker
  457. segments. */
  458. jpc_streamlist_t *pkthdrstreams;
  459. /* The expected ending offset for a tile-part. */
  460. long curtileendoff;
  461. /* This is required by the tier-2 decoder. */
  462. jpc_cstate_t *cstate;
  463. } jpc_dec_t;
  464. /* Decoder options. */
  465. typedef struct {
  466. /* The debug level for the decoder. */
  467. int debug;
  468. /* The maximum number of layers to decode. */
  469. int maxlyrs;
  470. /* The maximum number of packets to decode. */
  471. int maxpkts;
  472. } jpc_dec_importopts_t;
  473. /******************************************************************************\
  474. * Functions.
  475. \******************************************************************************/
  476. /* Create a decoder segment object. */
  477. jpc_dec_seg_t *jpc_seg_alloc(void);
  478. /* Destroy a decoder segment object. */
  479. void jpc_seg_destroy(jpc_dec_seg_t *seg);
  480. /* Remove a segment from a segment list. */
  481. void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *node);
  482. /* Insert a segment into a segment list. */
  483. void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins,
  484. jpc_dec_seg_t *node);
  485. #endif