2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
67 * $Id: jpc_t2dec.c,v 1.1 2005/05/22 18:33:05 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
78 #include "jasper/jas_types.h"
79 #include "jasper/jas_fix.h"
80 #include "jasper/jas_malloc.h"
81 #include "jasper/jas_math.h"
82 #include "jasper/jas_stream.h"
83 #include "jasper/jas_debug.h"
88 #include "jpc_mqdec.h"
89 #include "jpc_t2dec.h"
90 #include "jpc_t1cod.h"
93 /******************************************************************************\
95 \******************************************************************************/
97 long jpc_dec_lookahead(jas_stream_t *in);
98 static int jpc_getcommacode(jpc_bitstream_t *in);
99 static int jpc_getnumnewpasses(jpc_bitstream_t *in);
100 static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int lvlno,
101 int prcno, int lyrno);
103 /******************************************************************************\
105 \******************************************************************************/
107 static int jpc_getcommacode(jpc_bitstream_t *in)
114 if ((v = jpc_bitstream_getbit(in)) < 0) {
117 if (jpc_bitstream_eof(in)) {
129 static int jpc_getnumnewpasses(jpc_bitstream_t *in)
133 if ((n = jpc_bitstream_getbit(in)) > 0) {
134 if ((n = jpc_bitstream_getbit(in)) > 0) {
135 if ((n = jpc_bitstream_getbits(in, 2)) == 3) {
136 if ((n = jpc_bitstream_getbits(in, 5)) == 31) {
137 if ((n = jpc_bitstream_getbits(in, 7)) >= 0) {
156 static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int rlvlno,
157 int prcno, int lyrno)
159 jpc_bitstream_t *inb;
160 jpc_dec_tcomp_t *tcomp;
161 jpc_dec_rlvl_t *rlvl;
162 jpc_dec_band_t *band;
163 jpc_dec_cblk_t *cblk;
167 jpc_tagtreenode_t *leaf;
174 int savenumnewpasses;
177 jpc_dec_tile_t *tile;
184 uint_fast32_t bodylen;
192 /* Avoid compiler warning about possible use of uninitialized
196 discard = (lyrno >= dec->maxlyrs);
200 ccp = &cp->ccps[compno];
203 * Decode the packet header.
206 /* Decode the SOP marker segment if present. */
207 if (cp->csty & JPC_COD_SOP) {
208 if (jpc_dec_lookahead(in) == JPC_MS_SOP) {
209 if (!(ms = jpc_getms(in, dec->cstate))) {
212 if (jpc_ms_gettype(ms) != JPC_MS_SOP) {
214 fprintf(stderr, "missing SOP marker segment\n");
221 hdroffstart = jas_stream_getrwcount(pkthdrstream);
223 if (!(inb = jpc_bitstream_sopen(pkthdrstream, "r"))) {
227 if ((present = jpc_bitstream_getbit(inb)) < 0) {
230 JAS_DBGLOG(10, ("\n", present));
231 JAS_DBGLOG(10, ("present=%d ", present));
233 /* Is the packet non-empty? */
235 /* The packet is non-empty. */
236 tcomp = &tile->tcomps[compno];
237 rlvl = &tcomp->rlvls[rlvlno];
239 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
244 prc = &band->prcs[prcno];
249 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
252 if (!cblk->numpasses) {
253 leaf = jpc_tagtree_getleaf(prc->incltagtree, usedcblkcnt - 1);
254 if ((included = jpc_tagtree_decode(prc->incltagtree, leaf, lyrno + 1, inb)) < 0) {
258 if ((included = jpc_bitstream_getbit(inb)) < 0) {
262 JAS_DBGLOG(10, ("\n"));
263 JAS_DBGLOG(10, ("included=%d ", included));
267 if (!cblk->numpasses) {
269 leaf = jpc_tagtree_getleaf(prc->numimsbstagtree, usedcblkcnt - 1);
271 if ((ret = jpc_tagtree_decode(prc->numimsbstagtree, leaf, i, inb)) < 0) {
279 cblk->numimsbs = i - 1;
280 cblk->firstpassno = cblk->numimsbs * 3;
282 if ((numnewpasses = jpc_getnumnewpasses(inb)) < 0) {
285 JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses));
287 savenumnewpasses = numnewpasses;
289 if (numnewpasses > 0) {
290 if ((m = jpc_getcommacode(inb)) < 0) {
293 cblk->numlenbits += m;
294 JAS_DBGLOG(10, ("increment=%d ", m));
295 while (numnewpasses > 0) {
296 passno = cblk->firstpassno + cblk->numpasses + mycounter;
297 /* XXX - the maxpasses is not set precisely but this doesn't matter... */
298 maxpasses = JPC_SEGPASSCNT(passno, cblk->firstpassno, 10000, (ccp->cblkctx & JPC_COX_LAZY) != 0, (ccp->cblkctx & JPC_COX_TERMALL) != 0);
299 if (!discard && !seg) {
300 if (!(seg = jpc_seg_alloc())) {
303 jpc_seglist_insert(&cblk->segs, cblk->segs.tail, seg);
307 seg->passno = passno;
308 seg->type = JPC_SEGTYPE(seg->passno, cblk->firstpassno, (ccp->cblkctx & JPC_COX_LAZY) != 0);
309 seg->maxpasses = maxpasses;
311 n = JAS_MIN(numnewpasses, maxpasses);
314 if ((len = jpc_bitstream_getbits(inb, cblk->numlenbits + jpc_floorlog2(n))) < 0) {
317 JAS_DBGLOG(10, ("len=%d ", len));
327 cblk->numpasses += savenumnewpasses;
331 jpc_bitstream_inalign(inb, 0, 0);
334 if (jpc_bitstream_inalign(inb, 0x7f, 0)) {
335 fprintf(stderr, "alignment failed\n");
339 jpc_bitstream_close(inb);
341 hdroffend = jas_stream_getrwcount(pkthdrstream);
342 hdrlen = hdroffend - hdroffstart;
343 if (jas_getdbglevel() >= 5) {
344 fprintf(stderr, "hdrlen=%lu bodylen=%lu \n", (unsigned long) hdrlen,
345 (unsigned long) bodylen);
348 if (cp->csty & JPC_COD_EPH) {
349 if (jpc_dec_lookahead(pkthdrstream) == JPC_MS_EPH) {
350 if (!(ms = jpc_getms(pkthdrstream, dec->cstate))) {
351 fprintf(stderr, "cannot get (EPH) marker segment\n");
354 if (jpc_ms_gettype(ms) != JPC_MS_EPH) {
356 fprintf(stderr, "missing EPH marker segment\n");
363 /* decode the packet body. */
365 if (jas_getdbglevel() >= 1) {
366 fprintf(stderr, "packet body offset=%06ld\n", (long) jas_stream_getrwcount(in));
370 tcomp = &tile->tcomps[compno];
371 rlvl = &tcomp->rlvls[rlvlno];
372 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
377 prc = &band->prcs[prcno];
381 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
386 if (!(seg->stream = jas_stream_memopen(0, 0))) {
391 fprintf(stderr, "lyrno=%02d, compno=%02d, lvlno=%02d, prcno=%02d, bandno=%02d, cblkno=%02d, passno=%02d numpasses=%02d cnt=%d numbps=%d, numimsbs=%d\n", lyrno, compno, rlvlno, prcno, band - rlvl->bands, cblk - prc->cblks, seg->passno, seg->numpasses, seg->cnt, band->numbps, cblk->numimsbs);
394 if (jpc_getdata(in, seg->stream, seg->cnt) < 0) {
399 if (seg->numpasses >= seg->maxpasses) {
400 cblk->curseg = seg->next;
407 if (jas_stream_gobble(in, bodylen) != JAS_CAST(int, bodylen)) {
414 /********************************************************************************************/
415 /********************************************************************************************/
417 int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in)
419 jpc_dec_tile_t *tile;
426 if (!tile->pkthdrstream || jas_stream_peekc(tile->pkthdrstream) == EOF) {
427 switch (jpc_dec_lookahead(in)) {
441 if ((ret = jpc_pi_next(pi))) {
444 if (dec->maxpkts >= 0 && dec->numpkts >= dec->maxpkts) {
445 fprintf(stderr, "warning: stopping decode prematurely as requested\n");
448 if (jas_getdbglevel() >= 1) {
449 fprintf(stderr, "packet offset=%08ld prg=%d cmptno=%02d "
450 "rlvlno=%02d prcno=%03d lyrno=%02d\n", (long)
451 jas_stream_getrwcount(in), jpc_pi_prg(pi), jpc_pi_cmptno(pi),
452 jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi));
454 if (jpc_dec_decodepkt(dec, pkthdrstream, in, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi),
455 jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) {
464 jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile)
468 jpc_picomp_t *picomp;
469 jpc_pirlvl_t *pirlvl;
470 jpc_dec_tcomp_t *tcomp;
472 jpc_dec_rlvl_t *rlvl;
475 jpc_dec_cmpt_t *cmpt;
477 if (!(pi = jpc_pi_create0())) {
480 pi->numcomps = dec->numcomps;
481 if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) {
485 for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno,
490 for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps;
491 compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
492 picomp->numrlvls = tcomp->numrlvls;
493 if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls *
494 sizeof(jpc_pirlvl_t)))) {
498 for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
499 picomp->numrlvls; ++rlvlno, ++pirlvl) {
500 pirlvl->prclyrnos = 0;
502 for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
503 rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
504 /* XXX sizeof(long) should be sizeof different type */
505 pirlvl->numprcs = rlvl->numprcs;
506 if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs *
515 for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps, cmpt =
516 dec->cmpts; compno < pi->numcomps; ++compno, ++tcomp, ++picomp,
518 picomp->hsamp = cmpt->hstep;
519 picomp->vsamp = cmpt->vstep;
520 for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
521 rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
522 pirlvl->prcwidthexpn = rlvl->prcwidthexpn;
523 pirlvl->prcheightexpn = rlvl->prcheightexpn;
524 for (prcno = 0, prclyrno = pirlvl->prclyrnos;
525 prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
528 pirlvl->numhprcs = rlvl->numhprcs;
530 if (pi->maxrlvls < tcomp->numrlvls) {
531 pi->maxrlvls = tcomp->numrlvls;
535 pi->numlyrs = tile->cp->numlyrs;
536 pi->xstart = tile->xstart;
537 pi->ystart = tile->ystart;
538 pi->xend = tile->xend;
539 pi->yend = tile->yend;
554 pi->defaultpchg.prgord = tile->cp->prgord;
555 pi->defaultpchg.compnostart = 0;
556 pi->defaultpchg.compnoend = pi->numcomps;
557 pi->defaultpchg.rlvlnostart = 0;
558 pi->defaultpchg.rlvlnoend = pi->maxrlvls;
559 pi->defaultpchg.lyrnoend = pi->numlyrs;
567 long jpc_dec_lookahead(jas_stream_t *in)
570 if (jpc_getuint16(in, &x)) {
573 if (jas_stream_ungetc(in, x & 0xff) == EOF ||
574 jas_stream_ungetc(in, x >> 8) == EOF) {
577 if (x >= JPC_MS_INMIN && x <= JPC_MS_INMAX) {