JasPer 2.0.33
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#include "jasper/jas_image.h"
79
80#include "jpc_tsfb.h"
81#include "jpc_tagtree.h"
82#include "jpc_cs.h"
83#include "jpc_t1cod.h"
84#include "jpc_t2cod.h"
85
86/******************************************************************************\
87* Below are some ugly warts necessary to support packed packet headers.
88\******************************************************************************/
89
90/* PPM/PPT marker segment table entry. */
91
92typedef struct {
93
94 /* The index for this entry. */
95 uint_fast16_t ind;
96
97 /* The data length. */
98 uint_fast32_t len;
99
100 /* The data. */
101 jas_uchar *data;
102
103} jpc_ppxstabent_t;
104
105/* PPM/PPT marker segment table. */
106
107typedef struct {
108
109 /* The number of entries. */
110 unsigned numents;
111
112 /* The maximum number of entries (i.e., the allocated size of the array
113 below). */
114 unsigned maxents;
115
116 /* The table entries. */
117 jpc_ppxstabent_t **ents;
118
119} jpc_ppxstab_t;
120
121/* Stream list class. */
122
123typedef struct {
124
125 /* The number of streams in this list. */
126 unsigned numstreams;
127
128 /* The maximum number of streams that can be accomodated without
129 growing the streams array. */
130 unsigned maxstreams;
131
132 /* The streams. */
133 jas_stream_t **streams;
134
135} jpc_streamlist_t;
136
137/******************************************************************************\
138* Coding parameters class.
139\******************************************************************************/
140
141/* Per-component coding parameters. */
142
143typedef struct {
144
145 /* How were various coding parameters set? */
146 unsigned flags;
147
148 /* Per-component coding style parameters (e.g., explicit precinct sizes) */
149 uint_fast8_t csty;
150
151 /* The number of resolution levels. */
152 uint_fast8_t numrlvls;
153
154 /* The code block width exponent. */
155 uint_fast8_t cblkwidthexpn;
156
157 /* The code block height exponent. */
158 uint_fast8_t cblkheightexpn;
159
160 /* The QMFB ID. */
161 uint_fast8_t qmfbid;
162
163 /* The quantization style. */
164 uint_fast8_t qsty;
165
166 /* The number of quantizer step sizes. */
167 uint_fast16_t numstepsizes;
168
169 /* The step sizes. */
170 uint_fast16_t stepsizes[3 * JPC_MAXRLVLS + 1];
171
172 /* The number of guard bits. */
173 uint_fast8_t numguardbits;
174
175 /* The ROI shift value. */
176 uint_fast8_t roishift;
177
178 /* The code block parameters. */
179 uint_fast8_t cblkctx;
180
181 /* The precinct width exponents. */
182 uint_fast8_t prcwidthexpns[JPC_MAXRLVLS];
183
184 /* The precinct height exponents. */
185 uint_fast8_t prcheightexpns[JPC_MAXRLVLS];
186
187} jpc_dec_ccp_t;
188
189/* Coding paramters. */
190
191typedef struct {
192
193 /* How were these coding parameters set? */
194 unsigned flags;
195
196 /* Progression change list. */
197 jpc_pchglist_t *pchglist;
198
199 /* Progression order. */
200 uint_fast8_t prgord;
201
202 /* The number of layers. */
203 uint_fast16_t numlyrs;
204
205 /* The MCT ID. */
206 uint_fast8_t mctid;
207
208 /* The coding style parameters (e.g., SOP, EPH). */
209 uint_fast8_t csty;
210
211 /* The number of components. */
212 unsigned numcomps;
213
214 /* The per-component coding parameters. */
215 jpc_dec_ccp_t *ccps;
216
217} jpc_dec_cp_t;
218
219/******************************************************************************\
220* Decoder class.
221\******************************************************************************/
222
223/* Decoder per-segment state information. */
224
225typedef struct jpc_dec_seg_s {
226
227 /* The next segment in the list. */
228 struct jpc_dec_seg_s *next;
229
230 /* The previous segment in the list. */
231 struct jpc_dec_seg_s *prev;
232
233 /* The starting pass number for this segment. */
234 unsigned passno;
235
236 /* The number of passes in this segment. */
237 unsigned numpasses;
238
239 /* The maximum number of passes in this segment. */
240 unsigned maxpasses;
241
242 /* The type of data in this segment (i.e., MQ or raw). */
243 enum jpc_segtype type;
244
245 /* A stream containing the data for this segment. */
246 jas_stream_t *stream;
247
248 /* The number of bytes destined for this segment from the packet
249 currently being decoded. */
250 unsigned cnt;
251
252 /* A flag indicating if this segment has been terminated. */
253 int complete;
254
255 /* The layer number to which this segment belongs. */
256 /* If the segment spans multiple layers, then the largest layer number
257 spanned by the segment is used. */
258 unsigned lyrno;
259
260} jpc_dec_seg_t;
261
262/* Decoder segment list. */
263
264typedef struct {
265
266 /* The first entry in the list. */
267 jpc_dec_seg_t *head;
268
269 /* The last entry in the list. */
270 jpc_dec_seg_t *tail;
271
272} jpc_dec_seglist_t;
273
274/* Decoder per-code-block state information. */
275
276typedef struct {
277
278 /* The number of passes. */
279 unsigned numpasses;
280
281 /* A list of segments that still need to be decoded. */
282 jpc_dec_seglist_t segs;
283
284 /* The first incomplete/partial segment. */
285 jpc_dec_seg_t *curseg;
286
287 /* The number of leading insignificant bit planes for this code block. */
288 unsigned numimsbs;
289
290 /* The number of bits used to encode pass data lengths. */
291 unsigned numlenbits;
292
293 /* The first pass number containing data for this code block. */
294 unsigned firstpassno;
295
296 /* The sample data associated with this code block. */
297 jas_matrix_t *data;
298
299} jpc_dec_cblk_t;
300
301/* Decoder per-code-block-group state information. */
302
303typedef struct {
304
305 /* The x-coordinate of the top-left corner of the precinct. */
306 uint_fast32_t xstart;
307
308 /* The y-coordinate of the top-left corner of the precinct. */
309 uint_fast32_t ystart;
310
311 /* The x-coordinate of the bottom-right corner of the precinct
312 (plus one). */
313 uint_fast32_t xend;
314
315 /* The y-coordinate of the bottom-right corner of the precinct
316 (plus one). */
317 uint_fast32_t yend;
318
319 /* The number of code blocks spanning this precinct in the horizontal
320 direction. */
321 unsigned numhcblks;
322
323 /* The number of code blocks spanning this precinct in the vertical
324 direction. */
325 unsigned numvcblks;
326
327 /* The total number of code blocks in this precinct. */
328 unsigned numcblks;
329
330 /* The per code block information. */
331 jpc_dec_cblk_t *cblks;
332
333 /* The inclusion tag tree. */
334 jpc_tagtree_t *incltagtree;
335
336 /* The insignificant MSBs tag tree. */
337 jpc_tagtree_t *numimsbstagtree;
338
339} jpc_dec_prc_t;
340
341/* Decoder per-band state information. */
342
343typedef struct {
344
345 /* The per-code-block-group state information. */
346 jpc_dec_prc_t *prcs;
347
348 /* The sample data associated with this band. */
349 jas_matrix_t *data;
350
351 /* The orientation of this band (i.e., LL, LH, HL, or HH). */
352 enum jpc_tsfb_orient orient;
353
354 /* The encoded quantizer step size. */
355 unsigned stepsize;
356
357 /* The absolute quantizer step size. */
358 jpc_fix_t absstepsize;
359
360 /* The number of bit planes for this band. */
361 unsigned numbps;
362
363 /* The analysis gain associated with this band. */
364 int analgain;
365
366 /* The ROI shift value for this band. */
367 int roishift;
368
369} jpc_dec_band_t;
370
371/* Decoder per-resolution-level state information. */
372
373typedef struct {
374
375 /* The number of bands associated with this resolution level. */
376 unsigned numbands;
377
378 /* The per-band information. */
379 jpc_dec_band_t *bands;
380
381 /* The x-coordinate of the top-left corner of the tile-component
382 at this resolution. */
383 uint_fast32_t xstart;
384
385 /* The y-coordinate of the top-left corner of the tile-component
386 at this resolution. */
387 uint_fast32_t ystart;
388
389 /* The x-coordinate of the bottom-right corner of the tile-component
390 at this resolution (plus one). */
391 uint_fast32_t xend;
392
393 /* The y-coordinate of the bottom-right corner of the tile-component
394 at this resolution (plus one). */
395 uint_fast32_t yend;
396
397 /* The exponent value for the nominal precinct width measured
398 relative to the associated LL band. */
399 unsigned prcwidthexpn;
400
401 /* The exponent value for the nominal precinct height measured
402 relative to the associated LL band. */
403 unsigned prcheightexpn;
404
405 /* The number of precincts in the horizontal direction. */
406 unsigned numhprcs;
407
408 /* The number of precincts in the vertical direction. */
409 unsigned numvprcs;
410
411 /* The total number of precincts. */
412 unsigned numprcs;
413
414 /* The exponent value for the nominal code block group width.
415 This quantity is associated with the next lower resolution level
416 (assuming that there is one). */
417 unsigned cbgwidthexpn;
418
419 /* The exponent value for the nominal code block group height
420 This quantity is associated with the next lower resolution level
421 (assuming that there is one). */
422 unsigned cbgheightexpn;
423
424 /* The exponent value for the code block width. */
425 uint_fast16_t cblkwidthexpn;
426
427 /* The exponent value for the code block height. */
428 uint_fast16_t cblkheightexpn;
429
430} jpc_dec_rlvl_t;
431
432/* Decoder per-tile-component state information. */
433
434typedef struct {
435
436 /* The x-coordinate of the top-left corner of the tile-component
437 in the coordinate system of the tile-component. */
438 uint_fast32_t xstart;
439
440 /* The y-coordinate of the top-left corner of the tile-component
441 in the coordinate system of the tile-component. */
442 uint_fast32_t ystart;
443
444 /* The x-coordinate of the bottom-right corner of the tile-component
445 in the coordinate system of the tile-component (plus one). */
446 uint_fast32_t xend;
447
448 /* The y-coordinate of the bottom-right corner of the tile-component
449 in the coordinate system of the tile-component (plus one). */
450 uint_fast32_t yend;
451
452 /* The component data for the current tile. */
453 jas_matrix_t *data;
454
455 /* The number of resolution levels. */
456 unsigned numrlvls;
457
458 /* The per resolution level information. */
459 jpc_dec_rlvl_t *rlvls;
460
461 /* The TSFB. */
462 jpc_tsfb_t *tsfb;
463
464} jpc_dec_tcomp_t;
465
466/*
467 * Tile states.
468 */
469
470#define JPC_TILE_INIT 0
471#define JPC_TILE_ACTIVE 1
472#define JPC_TILE_ACTIVELAST 2
473#define JPC_TILE_DONE 3
474
475/* Decoder per-tile state information. */
476
477typedef struct {
478
479 /* The processing state for this tile. */
480 int state;
481
482 /* The x-coordinate of the top-left corner of the tile on the reference
483 grid. */
484 uint_fast32_t xstart;
485
486 /* The y-coordinate of the top-left corner of the tile on the reference
487 grid. */
488 uint_fast32_t ystart;
489
490 /* The x-coordinate of the bottom-right corner of the tile on the
491 reference grid (plus one). */
492 uint_fast32_t xend;
493
494 /* The y-coordinate of the bottom-right corner of the tile on the
495 reference grid (plus one). */
496 uint_fast32_t yend;
497
498 /* The packed packet header data for this tile. */
499 jpc_ppxstab_t *pptstab;
500
501 /* A stream containing the packed packet header data for this tile. */
502 jas_stream_t *pkthdrstream;
503
504 /* The coding parameters for this tile. */
505 jpc_dec_cp_t *cp;
506
507 /* The per tile-component information. */
508 jpc_dec_tcomp_t *tcomps;
509
510 /* The next expected tile-part number. */
511 unsigned partno;
512
513 /* The number of tile-parts. */
514 unsigned numparts;
515
516 /* The coding mode. */
517 int realmode;
518
519 /* The packet iterator for this tile. */
520 jpc_pi_t *pi;
521
522} jpc_dec_tile_t;
523
524/* Decoder per-component state information. */
525
526typedef struct {
527
528 /* The horizontal sampling period. */
529 uint_fast32_t hstep;
530
531 /* The vertical sampling period. */
532 uint_fast32_t vstep;
533
534 /* The number of samples in the horizontal direction. */
535 uint_fast32_t width;
536
537 /* The number of samples in the vertical direction. */
538 uint_fast32_t height;
539
540 /* The precision of the sample data. */
541 uint_fast16_t prec;
542
543 /* The signedness of the sample data. */
544 bool sgnd;
545
546 /* The sample alignment horizontal offset. */
547 uint_fast32_t hsubstep;
548
549 /* The sample alignment vertical offset. */
550 uint_fast32_t vsubstep;
551
552} jpc_dec_cmpt_t;
553
554/* Decoder state information. */
555
556typedef struct {
557
558 /* The decoded image. */
559 jas_image_t *image;
560
561 /* The x-coordinate of the top-left corner of the image area on
562 the reference grid. */
563 uint_fast32_t xstart;
564
565 /* The y-coordinate of the top-left corner of the image area on
566 the reference grid. */
567 uint_fast32_t ystart;
568
569 /* The x-coordinate of the bottom-right corner of the image area on
570 the reference grid (plus one). */
571 uint_fast32_t xend;
572
573 /* The y-coordinate of the bottom-right corner of the image area on
574 the reference grid (plus one). */
575 uint_fast32_t yend;
576
577 /* The nominal tile width in units of the image reference grid. */
578 uint_fast32_t tilewidth;
579
580 /* The nominal tile height in units of the image reference grid. */
581 uint_fast32_t tileheight;
582
583 /* The horizontal offset from the origin of the reference grid to the
584 left side of the first tile. */
585 uint_fast32_t tilexoff;
586
587 /* The vertical offset from the origin of the reference grid to the
588 top side of the first tile. */
589 uint_fast32_t tileyoff;
590
591 /* The number of tiles spanning the image area in the vertical
592 direction. */
593 unsigned numhtiles;
594
595 /* The number of tiles spanning the image area in the horizontal
596 direction. */
597 unsigned numvtiles;
598
599 /* The total number of tiles. */
600 unsigned numtiles;
601
602 /* The per-tile information. */
603 jpc_dec_tile_t *tiles;
604
605 /* The tile currently being processed. */
606 jpc_dec_tile_t *curtile;
607
608 /* The number of components. */
609 unsigned numcomps;
610
611 /* The stream containing the input JPEG-2000 code stream data. */
612 jas_stream_t *in;
613
614 /* The default coding parameters for all tiles. */
615 jpc_dec_cp_t *cp;
616
617 /* The maximum number of layers that may be decoded. */
618 unsigned maxlyrs;
619
620 /* The maximum number of packets that may be decoded. */
621 int maxpkts;
622
623 /* The number of packets decoded so far in the processing of the entire
624 code stream. */
625 unsigned numpkts;
626
627 /* The next expected PPM marker segment sequence number. */
628 unsigned ppmseqno;
629
630 /* The current state for code stream processing. */
631 int state;
632
633 /* The per-component information. */
634 jpc_dec_cmpt_t *cmpts;
635
636 /* The information from PPM marker segments. */
637 jpc_ppxstab_t *ppmstab;
638
639 /* A list of streams containing packet header data from PPM marker
640 segments. */
641 jpc_streamlist_t *pkthdrstreams;
642
643 /* The expected ending offset for a tile-part. */
644 long curtileendoff;
645
646 /* This is required by the tier-2 decoder. */
647 jpc_cstate_t *cstate;
648
649 size_t max_samples;
650
651} jpc_dec_t;
652
653/* Decoder options. */
654
655typedef struct {
656
657 /* The debug level for the decoder. */
658 int debug;
659
660 /* The maximum number of layers to decode. */
661 unsigned maxlyrs;
662
663 /* The maximum number of packets to decode. */
664 int maxpkts;
665
666 size_t max_samples;
667
668} jpc_dec_importopts_t;
669
670/******************************************************************************\
671* Functions.
672\******************************************************************************/
673
674/* Create a decoder segment object. */
675jpc_dec_seg_t *jpc_seg_alloc(void);
676
677/* Destroy a decoder segment object. */
678void jpc_seg_destroy(jpc_dec_seg_t *seg);
679
680/* Remove a segment from a segment list. */
681void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *node);
682
683/* Insert a segment into a segment list. */
684void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins,
685 jpc_dec_seg_t *node);
686
687#endif
JasPer Image Class.
I/O Stream Class.
Image class.
Definition: jas_image.h:202