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