| 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 Software License |
| 11 |
* |
| 12 |
* IMAGE POWER JPEG-2000 PUBLIC LICENSE |
| 13 |
* ************************************ |
| 14 |
* |
| 15 |
* GRANT: |
| 16 |
* |
| 17 |
* Permission is hereby granted, free of charge, to any person (the "User") |
| 18 |
* obtaining a copy of this software and associated documentation, to deal |
| 19 |
* in the JasPer Software without restriction, including without limitation |
| 20 |
* the right to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 |
* and/or sell copies of the JasPer Software (in source and binary forms), |
| 22 |
* and to permit persons to whom the JasPer Software is furnished to do so, |
| 23 |
* provided further that the License Conditions below are met. |
| 24 |
* |
| 25 |
* License Conditions |
| 26 |
* ****************** |
| 27 |
* |
| 28 |
* A. Redistributions of source code must retain the above copyright notice, |
| 29 |
* and this list of conditions, and the following disclaimer. |
| 30 |
* |
| 31 |
* B. Redistributions in binary form must reproduce the above copyright |
| 32 |
* notice, and this list of conditions, and the following disclaimer in |
| 33 |
* the documentation and/or other materials provided with the distribution. |
| 34 |
* |
| 35 |
* C. Neither the name of Image Power, Inc. nor any other contributor |
| 36 |
* (including, but not limited to, the University of British Columbia and |
| 37 |
* Michael David Adams) may be used to endorse or promote products derived |
| 38 |
* from this software without specific prior written permission. |
| 39 |
* |
| 40 |
* D. User agrees that it shall not commence any action against Image Power, |
| 41 |
* Inc., the University of British Columbia, Michael David Adams, or any |
| 42 |
* other contributors (collectively "Licensors") for infringement of any |
| 43 |
* intellectual property rights ("IPR") held by the User in respect of any |
| 44 |
* technology that User owns or has a right to license or sublicense and |
| 45 |
* which is an element required in order to claim compliance with ISO/IEC |
| 46 |
* 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property |
| 47 |
* rights worldwide arising under statutory or common law, and whether |
| 48 |
* or not perfected, including, without limitation, all (i) patents and |
| 49 |
* patent applications owned or licensable by User; (ii) rights associated |
| 50 |
* with works of authorship including copyrights, copyright applications, |
| 51 |
* copyright registrations, mask work rights, mask work applications, |
| 52 |
* mask work registrations; (iii) rights relating to the protection of |
| 53 |
* trade secrets and confidential information; (iv) any right analogous |
| 54 |
* to those set forth in subsections (i), (ii), or (iii) and any other |
| 55 |
* proprietary rights relating to intangible property (other than trademark, |
| 56 |
* trade dress, or service mark rights); and (v) divisions, continuations, |
| 57 |
* renewals, reissues and extensions of the foregoing (as and to the extent |
| 58 |
* applicable) now existing, hereafter filed, issued or acquired. |
| 59 |
* |
| 60 |
* E. If User commences an infringement action against any Licensor(s) then |
| 61 |
* such Licensor(s) shall have the right to terminate User's license and |
| 62 |
* all sublicenses that have been granted hereunder by User to other parties. |
| 63 |
* |
| 64 |
* F. This software is for use only in hardware or software products that |
| 65 |
* are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license |
| 66 |
* or right to this Software is granted for products that do not comply |
| 67 |
* with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased |
| 68 |
* from the ISO. |
| 69 |
* |
| 70 |
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. |
| 71 |
* NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
| 72 |
* THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND |
| 73 |
* CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY |
| 74 |
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, |
| 75 |
* WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE, |
| 76 |
* IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING |
| 77 |
* TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE |
| 78 |
* OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING |
| 79 |
* PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS. |
| 80 |
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE |
| 81 |
* IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE |
| 82 |
* IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY |
| 83 |
* OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY |
| 84 |
* OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING, |
| 85 |
* REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, |
| 86 |
* WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE |
| 87 |
* INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC., |
| 88 |
* MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE |
| 89 |
* JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO |
| 90 |
* THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR |
| 91 |
* CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, |
| 92 |
* DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR |
| 93 |
* MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF |
| 94 |
* SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY |
| 95 |
* OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT |
| 96 |
* FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR |
| 97 |
* RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING |
| 98 |
* FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, |
| 99 |
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT |
| 100 |
* LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE |
| 101 |
* JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY |
| 102 |
* TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE |
| 103 |
* ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS |
| 104 |
* OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT |
| 105 |
* KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING |
| 106 |
* TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS |
| 107 |
* CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE |
| 108 |
* NOTICE SPECIFIED IN THIS SECTION. |
| 109 |
* |
| 110 |
* __END_OF_JASPER_LICENSE__ |
| 111 |
*/ |
| 112 |
|
| 113 |
/* |
| 114 |
* Tier 2 Decoder |
| 115 |
* |
| 116 |
* $Id$ |
| 117 |
*/ |
| 118 |
|
| 119 |
/******************************************************************************\ |
| 120 |
* Includes. |
| 121 |
\******************************************************************************/ |
| 122 |
|
| 123 |
#include <stdio.h> |
| 124 |
#include <stdlib.h> |
| 125 |
#include <assert.h> |
| 126 |
|
| 127 |
#include "jasper/jas_types.h" |
| 128 |
#include "jasper/jas_malloc.h" |
| 129 |
#include "jasper/jas_math.h" |
| 130 |
#include "jasper/jas_stream.h" |
| 131 |
#include "jasper/jas_debug.h" |
| 132 |
|
| 133 |
#include "jpc_bs.h" |
| 134 |
#include "jpc_dec.h" |
| 135 |
#include "jpc_cs.h" |
| 136 |
#include "jpc_mqdec.h" |
| 137 |
#include "jpc_t2dec.h" |
| 138 |
#include "jpc_t1cod.h" |
| 139 |
#include "jpc_math.h" |
| 140 |
|
| 141 |
/******************************************************************************\ |
| 142 |
* |
| 143 |
\******************************************************************************/ |
| 144 |
|
| 145 |
long jpc_dec_lookahead(jas_stream_t *in); |
| 146 |
static int jpc_getcommacode(jpc_bitstream_t *in); |
| 147 |
static int jpc_getnumnewpasses(jpc_bitstream_t *in); |
| 148 |
static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int lvlno, |
| 149 |
int prcno, int lyrno); |
| 150 |
|
| 151 |
/******************************************************************************\ |
| 152 |
* Code. |
| 153 |
\******************************************************************************/ |
| 154 |
|
| 155 |
static int jpc_getcommacode(jpc_bitstream_t *in) |
| 156 |
{ |
| 157 |
int n; |
| 158 |
int v; |
| 159 |
|
| 160 |
n = 0; |
| 161 |
for (;;) { |
| 162 |
if ((v = jpc_bitstream_getbit(in)) < 0) { |
| 163 |
return -1; |
| 164 |
} |
| 165 |
if (jpc_bitstream_eof(in)) { |
| 166 |
return -1; |
| 167 |
} |
| 168 |
if (!v) { |
| 169 |
break; |
| 170 |
} |
| 171 |
++n; |
| 172 |
} |
| 173 |
|
| 174 |
return n; |
| 175 |
} |
| 176 |
|
| 177 |
static int jpc_getnumnewpasses(jpc_bitstream_t *in) |
| 178 |
{ |
| 179 |
int n; |
| 180 |
|
| 181 |
if ((n = jpc_bitstream_getbit(in)) > 0) { |
| 182 |
if ((n = jpc_bitstream_getbit(in)) > 0) { |
| 183 |
if ((n = jpc_bitstream_getbits(in, 2)) == 3) { |
| 184 |
if ((n = jpc_bitstream_getbits(in, 5)) == 31) { |
| 185 |
if ((n = jpc_bitstream_getbits(in, 7)) >= 0) { |
| 186 |
n += 36 + 1; |
| 187 |
} |
| 188 |
} else if (n >= 0) { |
| 189 |
n += 5 + 1; |
| 190 |
} |
| 191 |
} else if (n >= 0) { |
| 192 |
n += 2 + 1; |
| 193 |
} |
| 194 |
} else if (!n) { |
| 195 |
n += 2; |
| 196 |
} |
| 197 |
} else if (!n) { |
| 198 |
++n; |
| 199 |
} |
| 200 |
|
| 201 |
return n; |
| 202 |
} |
| 203 |
|
| 204 |
static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int rlvlno, |
| 205 |
int prcno, int lyrno) |
| 206 |
{ |
| 207 |
jpc_bitstream_t *inb; |
| 208 |
jpc_dec_tcomp_t *tcomp; |
| 209 |
jpc_dec_rlvl_t *rlvl; |
| 210 |
jpc_dec_band_t *band; |
| 211 |
jpc_dec_cblk_t *cblk; |
| 212 |
int n; |
| 213 |
int m; |
| 214 |
int i; |
| 215 |
jpc_tagtreenode_t *leaf; |
| 216 |
int included; |
| 217 |
int ret; |
| 218 |
int numnewpasses; |
| 219 |
jpc_dec_seg_t *seg; |
| 220 |
int len; |
| 221 |
int present; |
| 222 |
int savenumnewpasses; |
| 223 |
int mycounter; |
| 224 |
jpc_ms_t *ms; |
| 225 |
jpc_dec_tile_t *tile; |
| 226 |
jpc_dec_ccp_t *ccp; |
| 227 |
jpc_dec_cp_t *cp; |
| 228 |
int bandno; |
| 229 |
jpc_dec_prc_t *prc; |
| 230 |
int usedcblkcnt; |
| 231 |
int cblkno; |
| 232 |
uint_fast32_t bodylen; |
| 233 |
bool discard; |
| 234 |
int passno; |
| 235 |
int maxpasses; |
| 236 |
int hdrlen; |
| 237 |
int hdroffstart; |
| 238 |
int hdroffend; |
| 239 |
|
| 240 |
discard = (lyrno >= dec->maxlyrs); |
| 241 |
|
| 242 |
tile = dec->curtile; |
| 243 |
cp = tile->cp; |
| 244 |
ccp = &cp->ccps[compno]; |
| 245 |
|
| 246 |
/* |
| 247 |
* Decode the packet header. |
| 248 |
*/ |
| 249 |
|
| 250 |
/* Decode the SOP marker segment if present. */ |
| 251 |
if (cp->csty & JPC_COD_SOP) { |
| 252 |
if (jpc_dec_lookahead(in) == JPC_MS_SOP) { |
| 253 |
if (!(ms = jpc_getms(in, dec->cstate))) { |
| 254 |
return -1; |
| 255 |
} |
| 256 |
if (jpc_ms_gettype(ms) != JPC_MS_SOP) { |
| 257 |
jpc_ms_destroy(ms); |
| 258 |
fprintf(stderr, "missing SOP marker segment\n"); |
| 259 |
return -1; |
| 260 |
} |
| 261 |
jpc_ms_destroy(ms); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
hdroffstart = jas_stream_getrwcount(pkthdrstream); |
| 266 |
|
| 267 |
if (!(inb = jpc_bitstream_sopen(pkthdrstream, "r"))) { |
| 268 |
return -1; |
| 269 |
} |
| 270 |
|
| 271 |
if ((present = jpc_bitstream_getbit(inb)) < 0) { |
| 272 |
return 1; |
| 273 |
} |
| 274 |
JAS_DBGLOG(10, ("\n", present)); |
| 275 |
JAS_DBGLOG(10, ("present=%d ", present)); |
| 276 |
|
| 277 |
/* Is the packet non-empty? */ |
| 278 |
if (present) { |
| 279 |
/* The packet is non-empty. */ |
| 280 |
tcomp = &tile->tcomps[compno]; |
| 281 |
rlvl = &tcomp->rlvls[rlvlno]; |
| 282 |
bodylen = 0; |
| 283 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 284 |
++bandno, ++band) { |
| 285 |
if (!band->data) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
prc = &band->prcs[prcno]; |
| 289 |
if (!prc->cblks) { |
| 290 |
continue; |
| 291 |
} |
| 292 |
usedcblkcnt = 0; |
| 293 |
for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; |
| 294 |
++cblkno, ++cblk) { |
| 295 |
++usedcblkcnt; |
| 296 |
if (!cblk->numpasses) { |
| 297 |
leaf = jpc_tagtree_getleaf(prc->incltagtree, usedcblkcnt - 1); |
| 298 |
if ((included = jpc_tagtree_decode(prc->incltagtree, leaf, lyrno + 1, inb)) < 0) { |
| 299 |
return -1; |
| 300 |
} |
| 301 |
} else { |
| 302 |
if ((included = jpc_bitstream_getbit(inb)) < 0) { |
| 303 |
return -1; |
| 304 |
} |
| 305 |
} |
| 306 |
JAS_DBGLOG(10, ("\n")); |
| 307 |
JAS_DBGLOG(10, ("included=%d ", included)); |
| 308 |
if (!included) { |
| 309 |
continue; |
| 310 |
} |
| 311 |
if (!cblk->numpasses) { |
| 312 |
i = 1; |
| 313 |
leaf = jpc_tagtree_getleaf(prc->numimsbstagtree, usedcblkcnt - 1); |
| 314 |
for (;;) { |
| 315 |
if ((ret = jpc_tagtree_decode(prc->numimsbstagtree, leaf, i, inb)) < 0) { |
| 316 |
return -1; |
| 317 |
} |
| 318 |
if (ret) { |
| 319 |
break; |
| 320 |
} |
| 321 |
++i; |
| 322 |
} |
| 323 |
cblk->numimsbs = i - 1; |
| 324 |
cblk->firstpassno = cblk->numimsbs * 3; |
| 325 |
} |
| 326 |
if ((numnewpasses = jpc_getnumnewpasses(inb)) < 0) { |
| 327 |
return -1; |
| 328 |
} |
| 329 |
JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses)); |
| 330 |
seg = cblk->curseg; |
| 331 |
savenumnewpasses = numnewpasses; |
| 332 |
mycounter = 0; |
| 333 |
if (numnewpasses > 0) { |
| 334 |
if ((m = jpc_getcommacode(inb)) < 0) { |
| 335 |
return -1; |
| 336 |
} |
| 337 |
cblk->numlenbits += m; |
| 338 |
JAS_DBGLOG(10, ("increment=%d ", m)); |
| 339 |
while (numnewpasses > 0) { |
| 340 |
passno = cblk->firstpassno + cblk->numpasses + mycounter; |
| 341 |
/* XXX - the maxpasses is not set precisely but this doesn't matter... */ |
| 342 |
maxpasses = JPC_SEGPASSCNT(passno, cblk->firstpassno, 10000, (ccp->cblkctx & JPC_COX_LAZY) != 0, (ccp->cblkctx & JPC_COX_TERMALL) != 0); |
| 343 |
if (!discard && !seg) { |
| 344 |
if (!(seg = jpc_seg_alloc())) { |
| 345 |
return -1; |
| 346 |
} |
| 347 |
jpc_seglist_insert(&cblk->segs, cblk->segs.tail, seg); |
| 348 |
if (!cblk->curseg) { |
| 349 |
cblk->curseg = seg; |
| 350 |
} |
| 351 |
seg->passno = passno; |
| 352 |
seg->type = JPC_SEGTYPE(seg->passno, cblk->firstpassno, (ccp->cblkctx & JPC_COX_LAZY) != 0); |
| 353 |
seg->maxpasses = maxpasses; |
| 354 |
} |
| 355 |
n = JAS_MIN(numnewpasses, maxpasses); |
| 356 |
mycounter += n; |
| 357 |
numnewpasses -= n; |
| 358 |
if ((len = jpc_bitstream_getbits(inb, cblk->numlenbits + jpc_floorlog2(n))) < 0) { |
| 359 |
return -1; |
| 360 |
} |
| 361 |
JAS_DBGLOG(10, ("len=%d ", len)); |
| 362 |
if (!discard) { |
| 363 |
seg->lyrno = lyrno; |
| 364 |
seg->numpasses += n; |
| 365 |
seg->cnt = len; |
| 366 |
seg = seg->next; |
| 367 |
} |
| 368 |
bodylen += len; |
| 369 |
} |
| 370 |
} |
| 371 |
cblk->numpasses += savenumnewpasses; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
jpc_bitstream_inalign(inb, 0, 0); |
| 376 |
|
| 377 |
} else { |
| 378 |
if (jpc_bitstream_inalign(inb, 0x7f, 0)) { |
| 379 |
fprintf(stderr, "alignment failed\n"); |
| 380 |
return -1; |
| 381 |
} |
| 382 |
} |
| 383 |
jpc_bitstream_close(inb); |
| 384 |
|
| 385 |
hdroffend = jas_stream_getrwcount(pkthdrstream); |
| 386 |
hdrlen = hdroffend - hdroffstart; |
| 387 |
if (jas_getdbglevel() >= 5) { |
| 388 |
fprintf(stderr, "hdrlen=%lu bodylen=%lu \n", (unsigned long) hdrlen, |
| 389 |
(unsigned long) bodylen); |
| 390 |
} |
| 391 |
|
| 392 |
if (cp->csty & JPC_COD_EPH) { |
| 393 |
if (jpc_dec_lookahead(pkthdrstream) == JPC_MS_EPH) { |
| 394 |
if (!(ms = jpc_getms(pkthdrstream, dec->cstate))) { |
| 395 |
fprintf(stderr, "cannot get (EPH) marker segment\n"); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
if (jpc_ms_gettype(ms) != JPC_MS_EPH) { |
| 399 |
jpc_ms_destroy(ms); |
| 400 |
fprintf(stderr, "missing EPH marker segment\n"); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
jpc_ms_destroy(ms); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/* decode the packet body. */ |
| 408 |
|
| 409 |
if (jas_getdbglevel() >= 1) { |
| 410 |
fprintf(stderr, "packet body offset=%06ld\n", (long) jas_stream_getrwcount(in)); |
| 411 |
} |
| 412 |
|
| 413 |
if (!discard) { |
| 414 |
tcomp = &tile->tcomps[compno]; |
| 415 |
rlvl = &tcomp->rlvls[rlvlno]; |
| 416 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 417 |
++bandno, ++band) { |
| 418 |
if (!band->data) { |
| 419 |
continue; |
| 420 |
} |
| 421 |
prc = &band->prcs[prcno]; |
| 422 |
if (!prc->cblks) { |
| 423 |
continue; |
| 424 |
} |
| 425 |
for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; |
| 426 |
++cblkno, ++cblk) { |
| 427 |
seg = cblk->curseg; |
| 428 |
while (seg) { |
| 429 |
if (!seg->stream) { |
| 430 |
if (!(seg->stream = jas_stream_memopen(0, 0))) { |
| 431 |
return -1; |
| 432 |
} |
| 433 |
} |
| 434 |
#if 0 |
| 435 |
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); |
| 436 |
#endif |
| 437 |
if (seg->cnt > 0) { |
| 438 |
if (jpc_getdata(in, seg->stream, seg->cnt) < 0) { |
| 439 |
return -1; |
| 440 |
} |
| 441 |
seg->cnt = 0; |
| 442 |
} |
| 443 |
if (seg->numpasses >= seg->maxpasses) { |
| 444 |
cblk->curseg = seg->next; |
| 445 |
} |
| 446 |
seg = seg->next; |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
} else { |
| 451 |
if (jas_stream_gobble(in, bodylen) != bodylen) { |
| 452 |
return -1; |
| 453 |
} |
| 454 |
} |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
/********************************************************************************************/ |
| 459 |
/********************************************************************************************/ |
| 460 |
|
| 461 |
int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in) |
| 462 |
{ |
| 463 |
jpc_dec_tile_t *tile; |
| 464 |
jpc_pi_t *pi; |
| 465 |
int ret; |
| 466 |
|
| 467 |
tile = dec->curtile; |
| 468 |
pi = tile->pi; |
| 469 |
for (;;) { |
| 470 |
if (!tile->pkthdrstream || jas_stream_peekc(tile->pkthdrstream) == EOF) { |
| 471 |
switch (jpc_dec_lookahead(in)) { |
| 472 |
case JPC_MS_EOC: |
| 473 |
case JPC_MS_SOT: |
| 474 |
return 0; |
| 475 |
break; |
| 476 |
case JPC_MS_SOP: |
| 477 |
case JPC_MS_EPH: |
| 478 |
case 0: |
| 479 |
break; |
| 480 |
default: |
| 481 |
return -1; |
| 482 |
break; |
| 483 |
} |
| 484 |
} |
| 485 |
if ((ret = jpc_pi_next(pi))) { |
| 486 |
return ret; |
| 487 |
} |
| 488 |
if (dec->maxpkts >= 0 && dec->numpkts >= dec->maxpkts) { |
| 489 |
fprintf(stderr, "warning: stopping decode prematurely as requested\n"); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
if (jas_getdbglevel() >= 1) { |
| 493 |
fprintf(stderr, "packet offset=%08ld prg=%d cmptno=%02d " |
| 494 |
"rlvlno=%02d prcno=%03d lyrno=%02d\n", (long) |
| 495 |
jas_stream_getrwcount(in), jpc_pi_prg(pi), jpc_pi_cmptno(pi), |
| 496 |
jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi)); |
| 497 |
} |
| 498 |
if (jpc_dec_decodepkt(dec, pkthdrstream, in, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), |
| 499 |
jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { |
| 500 |
return -1; |
| 501 |
} |
| 502 |
++dec->numpkts; |
| 503 |
} |
| 504 |
|
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile) |
| 509 |
{ |
| 510 |
jpc_pi_t *pi; |
| 511 |
int compno; |
| 512 |
jpc_picomp_t *picomp; |
| 513 |
jpc_pirlvl_t *pirlvl; |
| 514 |
jpc_dec_tcomp_t *tcomp; |
| 515 |
int rlvlno; |
| 516 |
jpc_dec_rlvl_t *rlvl; |
| 517 |
int prcno; |
| 518 |
int *prclyrno; |
| 519 |
jpc_dec_cmpt_t *cmpt; |
| 520 |
|
| 521 |
if (!(pi = jpc_pi_create0())) { |
| 522 |
return 0; |
| 523 |
} |
| 524 |
pi->numcomps = dec->numcomps; |
| 525 |
if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) { |
| 526 |
jpc_pi_destroy(pi); |
| 527 |
return 0; |
| 528 |
} |
| 529 |
for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, |
| 530 |
++picomp) { |
| 531 |
picomp->pirlvls = 0; |
| 532 |
} |
| 533 |
|
| 534 |
for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps; |
| 535 |
compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
| 536 |
picomp->numrlvls = tcomp->numrlvls; |
| 537 |
if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls * |
| 538 |
sizeof(jpc_pirlvl_t)))) { |
| 539 |
jpc_pi_destroy(pi); |
| 540 |
return 0; |
| 541 |
} |
| 542 |
for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
| 543 |
picomp->numrlvls; ++rlvlno, ++pirlvl) { |
| 544 |
pirlvl->prclyrnos = 0; |
| 545 |
} |
| 546 |
for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
| 547 |
rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
| 548 |
/* XXX sizeof(long) should be sizeof different type */ |
| 549 |
pirlvl->numprcs = rlvl->numprcs; |
| 550 |
if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs * |
| 551 |
sizeof(long)))) { |
| 552 |
jpc_pi_destroy(pi); |
| 553 |
return 0; |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
pi->maxrlvls = 0; |
| 559 |
for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps, cmpt = |
| 560 |
dec->cmpts; compno < pi->numcomps; ++compno, ++tcomp, ++picomp, |
| 561 |
++cmpt) { |
| 562 |
picomp->hsamp = cmpt->hstep; |
| 563 |
picomp->vsamp = cmpt->vstep; |
| 564 |
for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
| 565 |
rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
| 566 |
pirlvl->prcwidthexpn = rlvl->prcwidthexpn; |
| 567 |
pirlvl->prcheightexpn = rlvl->prcheightexpn; |
| 568 |
for (prcno = 0, prclyrno = pirlvl->prclyrnos; |
| 569 |
prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { |
| 570 |
*prclyrno = 0; |
| 571 |
} |
| 572 |
pirlvl->numhprcs = rlvl->numhprcs; |
| 573 |
} |
| 574 |
if (pi->maxrlvls < tcomp->numrlvls) { |
| 575 |
pi->maxrlvls = tcomp->numrlvls; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
pi->numlyrs = tile->cp->numlyrs; |
| 580 |
pi->xstart = tile->xstart; |
| 581 |
pi->ystart = tile->ystart; |
| 582 |
pi->xend = tile->xend; |
| 583 |
pi->yend = tile->yend; |
| 584 |
|
| 585 |
pi->picomp = 0; |
| 586 |
pi->pirlvl = 0; |
| 587 |
pi->x = 0; |
| 588 |
pi->y = 0; |
| 589 |
pi->compno = 0; |
| 590 |
pi->rlvlno = 0; |
| 591 |
pi->prcno = 0; |
| 592 |
pi->lyrno = 0; |
| 593 |
pi->xstep = 0; |
| 594 |
pi->ystep = 0; |
| 595 |
|
| 596 |
pi->pchgno = -1; |
| 597 |
|
| 598 |
pi->defaultpchg.prgord = tile->cp->prgord; |
| 599 |
pi->defaultpchg.compnostart = 0; |
| 600 |
pi->defaultpchg.compnoend = pi->numcomps; |
| 601 |
pi->defaultpchg.rlvlnostart = 0; |
| 602 |
pi->defaultpchg.rlvlnoend = pi->maxrlvls; |
| 603 |
pi->defaultpchg.lyrnoend = pi->numlyrs; |
| 604 |
pi->pchg = 0; |
| 605 |
|
| 606 |
pi->valid = 0; |
| 607 |
|
| 608 |
return pi; |
| 609 |
} |
| 610 |
|
| 611 |
long jpc_dec_lookahead(jas_stream_t *in) |
| 612 |
{ |
| 613 |
uint_fast16_t x; |
| 614 |
if (jpc_getuint16(in, &x)) { |
| 615 |
return -1; |
| 616 |
} |
| 617 |
if (jas_stream_ungetc(in, x & 0xff) == EOF || |
| 618 |
jas_stream_ungetc(in, x >> 8) == EOF) { |
| 619 |
return -1; |
| 620 |
} |
| 621 |
if (x >= JPC_MS_INMIN && x <= JPC_MS_INMAX) { |
| 622 |
return x; |
| 623 |
} |
| 624 |
return 0; |
| 625 |
} |