Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 244 | chris | 1 | /* |
| 2 | * Copyright (c) 1999-2000 Image Power, Inc. and the University of |
||
| 3 | * British Columbia. |
||
| 4 | * Copyright (c) 2001-2003 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: jpc_t2dec.c,v 1.1 2003/05/15 01:30:32 ace Exp $ |
||
| 117 | */ |
||
| 118 | |||
| 119 | /******************************************************************************\ |
||
| 120 | * Includes. |
||
| 121 | \******************************************************************************/ |
||
| 122 | |||
| 123 | #include <stdio.h> |
||
| 124 | #include <stdlib.h> |
||
| 125 | #include <assert.h> |
||
| 126 | |||
| 127 | #include "jas_types.h" |
||
| 128 | #include "jas_fix.h" |
||
| 129 | #include "jas_malloc.h" |
||
| 130 | #include "jas_math.h" |
||
| 131 | #include "jas_stream.h" |
||
| 132 | #include "jas_debug.h" |
||
| 133 | |||
| 134 | #include "jpc_bs.h" |
||
| 135 | #include "jpc_dec.h" |
||
| 136 | #include "jpc_cs.h" |
||
| 137 | #include "jpc_mqdec.h" |
||
| 138 | #include "jpc_t2dec.h" |
||
| 139 | #include "jpc_t1cod.h" |
||
| 140 | #include "jpc_math.h" |
||
| 141 | |||
| 142 | /******************************************************************************\ |
||
| 143 | * |
||
| 144 | \******************************************************************************/ |
||
| 145 | |||
| 146 | long jpc_dec_lookahead(jas_stream_t *in); |
||
| 147 | static int jpc_getcommacode(jpc_bitstream_t *in); |
||
| 148 | static int jpc_getnumnewpasses(jpc_bitstream_t *in); |
||
| 149 | static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int lvlno, |
||
| 150 | int prcno, int lyrno); |
||
| 151 | |||
| 152 | /******************************************************************************\ |
||
| 153 | * Code. |
||
| 154 | \******************************************************************************/ |
||
| 155 | |||
| 156 | static int jpc_getcommacode(jpc_bitstream_t *in) |
||
| 157 | { |
||
| 158 | int n; |
||
| 159 | int v; |
||
| 160 | |||
| 161 | n = 0; |
||
| 162 | for (;;) { |
||
| 163 | if ((v = jpc_bitstream_getbit(in)) < 0) { |
||
| 164 | return -1; |
||
| 165 | } |
||
| 166 | if (jpc_bitstream_eof(in)) { |
||
| 167 | return -1; |
||
| 168 | } |
||
| 169 | if (!v) { |
||
| 170 | break; |
||
| 171 | } |
||
| 172 | ++n; |
||
| 173 | } |
||
| 174 | |||
| 175 | return n; |
||
| 176 | } |
||
| 177 | |||
| 178 | static int jpc_getnumnewpasses(jpc_bitstream_t *in) |
||
| 179 | { |
||
| 180 | int n; |
||
| 181 | |||
| 182 | if ((n = jpc_bitstream_getbit(in)) > 0) { |
||
| 183 | if ((n = jpc_bitstream_getbit(in)) > 0) { |
||
| 184 | if ((n = jpc_bitstream_getbits(in, 2)) == 3) { |
||
| 185 | if ((n = jpc_bitstream_getbits(in, 5)) == 31) { |
||
| 186 | if ((n = jpc_bitstream_getbits(in, 7)) >= 0) { |
||
| 187 | n += 36 + 1; |
||
| 188 | } |
||
| 189 | } else if (n >= 0) { |
||
| 190 | n += 5 + 1; |
||
| 191 | } |
||
| 192 | } else if (n >= 0) { |
||
| 193 | n += 2 + 1; |
||
| 194 | } |
||
| 195 | } else if (!n) { |
||
| 196 | n += 2; |
||
| 197 | } |
||
| 198 | } else if (!n) { |
||
| 199 | ++n; |
||
| 200 | } |
||
| 201 | |||
| 202 | return n; |
||
| 203 | } |
||
| 204 | |||
| 205 | static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int rlvlno, |
||
| 206 | int prcno, int lyrno) |
||
| 207 | { |
||
| 208 | jpc_bitstream_t *inb; |
||
| 209 | jpc_dec_tcomp_t *tcomp; |
||
| 210 | jpc_dec_rlvl_t *rlvl; |
||
| 211 | jpc_dec_band_t *band; |
||
| 212 | jpc_dec_cblk_t *cblk; |
||
| 213 | int n; |
||
| 214 | int m; |
||
| 215 | int i; |
||
| 216 | jpc_tagtreenode_t *leaf; |
||
| 217 | int included; |
||
| 218 | int ret; |
||
| 219 | int numnewpasses; |
||
| 220 | jpc_dec_seg_t *seg; |
||
| 221 | int len; |
||
| 222 | int present; |
||
| 223 | int savenumnewpasses; |
||
| 224 | int mycounter; |
||
| 225 | jpc_ms_t *ms; |
||
| 226 | jpc_dec_tile_t *tile; |
||
| 227 | jpc_dec_ccp_t *ccp; |
||
| 228 | jpc_dec_cp_t *cp; |
||
| 229 | int bandno; |
||
| 230 | jpc_dec_prc_t *prc; |
||
| 231 | int usedcblkcnt; |
||
| 232 | int cblkno; |
||
| 233 | uint_fast32_t bodylen; |
||
| 234 | bool discard; |
||
| 235 | int passno; |
||
| 236 | int maxpasses; |
||
| 237 | int hdrlen; |
||
| 238 | int hdroffstart; |
||
| 239 | int hdroffend; |
||
| 240 | |||
| 241 | /* Avoid compiler warning about possible use of uninitialized |
||
| 242 | variable. */ |
||
| 243 | bodylen = 0; |
||
| 244 | |||
| 245 | discard = (lyrno >= dec->maxlyrs); |
||
| 246 | |||
| 247 | tile = dec->curtile; |
||
| 248 | cp = tile->cp; |
||
| 249 | ccp = &cp->ccps[compno]; |
||
| 250 | |||
| 251 | /* |
||
| 252 | * Decode the packet header. |
||
| 253 | */ |
||
| 254 | |||
| 255 | /* Decode the SOP marker segment if present. */ |
||
| 256 | if (cp->csty & JPC_COD_SOP) { |
||
| 257 | if (jpc_dec_lookahead(in) == JPC_MS_SOP) { |
||
| 258 | if (!(ms = jpc_getms(in, dec->cstate))) { |
||
| 259 | return -1; |
||
| 260 | } |
||
| 261 | if (jpc_ms_gettype(ms) != JPC_MS_SOP) { |
||
| 262 | jpc_ms_destroy(ms); |
||
| 263 | fprintf(stderr, "missing SOP marker segment\n"); |
||
| 264 | return -1; |
||
| 265 | } |
||
| 266 | jpc_ms_destroy(ms); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | hdroffstart = jas_stream_getrwcount(pkthdrstream); |
||
| 271 | |||
| 272 | if (!(inb = jpc_bitstream_sopen(pkthdrstream, "r"))) { |
||
| 273 | return -1; |
||
| 274 | } |
||
| 275 | |||
| 276 | if ((present = jpc_bitstream_getbit(inb)) < 0) { |
||
| 277 | return 1; |
||
| 278 | } |
||
| 279 | JAS_DBGLOG(10, ("\n", present)); |
||
| 280 | JAS_DBGLOG(10, ("present=%d ", present)); |
||
| 281 | |||
| 282 | /* Is the packet non-empty? */ |
||
| 283 | if (present) { |
||
| 284 | /* The packet is non-empty. */ |
||
| 285 | tcomp = &tile->tcomps[compno]; |
||
| 286 | rlvl = &tcomp->rlvls[rlvlno]; |
||
| 287 | bodylen = 0; |
||
| 288 | for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
||
| 289 | ++bandno, ++band) { |
||
| 290 | if (!band->data) { |
||
| 291 | continue; |
||
| 292 | } |
||
| 293 | prc = &band->prcs[prcno]; |
||
| 294 | if (!prc->cblks) { |
||
| 295 | continue; |
||
| 296 | } |
||
| 297 | usedcblkcnt = 0; |
||
| 298 | for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; |
||
| 299 | ++cblkno, ++cblk) { |
||
| 300 | ++usedcblkcnt; |
||
| 301 | if (!cblk->numpasses) { |
||
| 302 | leaf = jpc_tagtree_getleaf(prc->incltagtree, usedcblkcnt - 1); |
||
| 303 | if ((included = jpc_tagtree_decode(prc->incltagtree, leaf, lyrno + 1, inb)) < 0) { |
||
| 304 | return -1; |
||
| 305 | } |
||
| 306 | } else { |
||
| 307 | if ((included = jpc_bitstream_getbit(inb)) < 0) { |
||
| 308 | return -1; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | JAS_DBGLOG(10, ("\n")); |
||
| 312 | JAS_DBGLOG(10, ("included=%d ", included)); |
||
| 313 | if (!included) { |
||
| 314 | continue; |
||
| 315 | } |
||
| 316 | if (!cblk->numpasses) { |
||
| 317 | i = 1; |
||
| 318 | leaf = jpc_tagtree_getleaf(prc->numimsbstagtree, usedcblkcnt - 1); |
||
| 319 | for (;;) { |
||
| 320 | if ((ret = jpc_tagtree_decode(prc->numimsbstagtree, leaf, i, inb)) < 0) { |
||
| 321 | return -1; |
||
| 322 | } |
||
| 323 | if (ret) { |
||
| 324 | break; |
||
| 325 | } |
||
| 326 | ++i; |
||
| 327 | } |
||
| 328 | cblk->numimsbs = i - 1; |
||
| 329 | cblk->firstpassno = cblk->numimsbs * 3; |
||
| 330 | } |
||
| 331 | if ((numnewpasses = jpc_getnumnewpasses(inb)) < 0) { |
||
| 332 | return -1; |
||
| 333 | } |
||
| 334 | JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses)); |
||
| 335 | seg = cblk->curseg; |
||
| 336 | savenumnewpasses = numnewpasses; |
||
| 337 | mycounter = 0; |
||
| 338 | if (numnewpasses > 0) { |
||
| 339 | if ((m = jpc_getcommacode(inb)) < 0) { |
||
| 340 | return -1; |
||
| 341 | } |
||
| 342 | cblk->numlenbits += m; |
||
| 343 | JAS_DBGLOG(10, ("increment=%d ", m)); |
||
| 344 | while (numnewpasses > 0) { |
||
| 345 | passno = cblk->firstpassno + cblk->numpasses + mycounter; |
||
| 346 | /* XXX - the maxpasses is not set precisely but this doesn't matter... */ |
||
| 347 | maxpasses = JPC_SEGPASSCNT(passno, cblk->firstpassno, 10000, (ccp->cblkctx & JPC_COX_LAZY) != 0, (ccp->cblkctx & JPC_COX_TERMALL) != 0); |
||
| 348 | if (!discard && !seg) { |
||
| 349 | if (!(seg = jpc_seg_alloc())) { |
||
| 350 | return -1; |
||
| 351 | } |
||
| 352 | jpc_seglist_insert(&cblk->segs, cblk->segs.tail, seg); |
||
| 353 | if (!cblk->curseg) { |
||
| 354 | cblk->curseg = seg; |
||
| 355 | } |
||
| 356 | seg->passno = passno; |
||
| 357 | seg->type = JPC_SEGTYPE(seg->passno, cblk->firstpassno, (ccp->cblkctx & JPC_COX_LAZY) != 0); |
||
| 358 | seg->maxpasses = maxpasses; |
||
| 359 | } |
||
| 360 | n = JAS_MIN(numnewpasses, maxpasses); |
||
| 361 | mycounter += n; |
||
| 362 | numnewpasses -= n; |
||
| 363 | if ((len = jpc_bitstream_getbits(inb, cblk->numlenbits + jpc_floorlog2(n))) < 0) { |
||
| 364 | return -1; |
||
| 365 | } |
||
| 366 | JAS_DBGLOG(10, ("len=%d ", len)); |
||
| 367 | if (!discard) { |
||
| 368 | seg->lyrno = lyrno; |
||
| 369 | seg->numpasses += n; |
||
| 370 | seg->cnt = len; |
||
| 371 | seg = seg->next; |
||
| 372 | } |
||
| 373 | bodylen += len; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | cblk->numpasses += savenumnewpasses; |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | jpc_bitstream_inalign(inb, 0, 0); |
||
| 381 | |||
| 382 | } else { |
||
| 383 | if (jpc_bitstream_inalign(inb, 0x7f, 0)) { |
||
| 384 | fprintf(stderr, "alignment failed\n"); |
||
| 385 | return -1; |
||
| 386 | } |
||
| 387 | } |
||
| 388 | jpc_bitstream_close(inb); |
||
| 389 | |||
| 390 | hdroffend = jas_stream_getrwcount(pkthdrstream); |
||
| 391 | hdrlen = hdroffend - hdroffstart; |
||
| 392 | if (jas_getdbglevel() >= 5) { |
||
| 393 | fprintf(stderr, "hdrlen=%lu bodylen=%lu \n", (unsigned long) hdrlen, |
||
| 394 | (unsigned long) bodylen); |
||
| 395 | } |
||
| 396 | |||
| 397 | if (cp->csty & JPC_COD_EPH) { |
||
| 398 | if (jpc_dec_lookahead(pkthdrstream) == JPC_MS_EPH) { |
||
| 399 | if (!(ms = jpc_getms(pkthdrstream, dec->cstate))) { |
||
| 400 | fprintf(stderr, "cannot get (EPH) marker segment\n"); |
||
| 401 | return -1; |
||
| 402 | } |
||
| 403 | if (jpc_ms_gettype(ms) != JPC_MS_EPH) { |
||
| 404 | jpc_ms_destroy(ms); |
||
| 405 | fprintf(stderr, "missing EPH marker segment\n"); |
||
| 406 | return -1; |
||
| 407 | } |
||
| 408 | jpc_ms_destroy(ms); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /* decode the packet body. */ |
||
| 413 | |||
| 414 | if (jas_getdbglevel() >= 1) { |
||
| 415 | fprintf(stderr, "packet body offset=%06ld\n", (long) jas_stream_getrwcount(in)); |
||
| 416 | } |
||
| 417 | |||
| 418 | if (!discard) { |
||
| 419 | tcomp = &tile->tcomps[compno]; |
||
| 420 | rlvl = &tcomp->rlvls[rlvlno]; |
||
| 421 | for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
||
| 422 | ++bandno, ++band) { |
||
| 423 | if (!band->data) { |
||
| 424 | continue; |
||
| 425 | } |
||
| 426 | prc = &band->prcs[prcno]; |
||
| 427 | if (!prc->cblks) { |
||
| 428 | continue; |
||
| 429 | } |
||
| 430 | for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; |
||
| 431 | ++cblkno, ++cblk) { |
||
| 432 | seg = cblk->curseg; |
||
| 433 | while (seg) { |
||
| 434 | if (!seg->stream) { |
||
| 435 | if (!(seg->stream = jas_stream_memopen(0, 0))) { |
||
| 436 | return -1; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | #if 0 |
||
| 440 | 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); |
||
| 441 | #endif |
||
| 442 | if (seg->cnt > 0) { |
||
| 443 | if (jpc_getdata(in, seg->stream, seg->cnt) < 0) { |
||
| 444 | return -1; |
||
| 445 | } |
||
| 446 | seg->cnt = 0; |
||
| 447 | } |
||
| 448 | if (seg->numpasses >= seg->maxpasses) { |
||
| 449 | cblk->curseg = seg->next; |
||
| 450 | } |
||
| 451 | seg = seg->next; |
||
| 452 | } |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } else { |
||
| 456 | if (jas_stream_gobble(in, bodylen) != JAS_CAST(int, bodylen)) { |
||
| 457 | return -1; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | return 0; |
||
| 461 | } |
||
| 462 | |||
| 463 | /********************************************************************************************/ |
||
| 464 | /********************************************************************************************/ |
||
| 465 | |||
| 466 | int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in) |
||
| 467 | { |
||
| 468 | jpc_dec_tile_t *tile; |
||
| 469 | jpc_pi_t *pi; |
||
| 470 | int ret; |
||
| 471 | |||
| 472 | tile = dec->curtile; |
||
| 473 | pi = tile->pi; |
||
| 474 | for (;;) { |
||
| 475 | if (!tile->pkthdrstream || jas_stream_peekc(tile->pkthdrstream) == EOF) { |
||
| 476 | switch (jpc_dec_lookahead(in)) { |
||
| 477 | case JPC_MS_EOC: |
||
| 478 | case JPC_MS_SOT: |
||
| 479 | return 0; |
||
| 480 | break; |
||
| 481 | case JPC_MS_SOP: |
||
| 482 | case JPC_MS_EPH: |
||
| 483 | case 0: |
||
| 484 | break; |
||
| 485 | default: |
||
| 486 | return -1; |
||
| 487 | break; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | if ((ret = jpc_pi_next(pi))) { |
||
| 491 | return ret; |
||
| 492 | } |
||
| 493 | if (dec->maxpkts >= 0 && dec->numpkts >= dec->maxpkts) { |
||
| 494 | fprintf(stderr, "warning: stopping decode prematurely as requested\n"); |
||
| 495 | return 0; |
||
| 496 | } |
||
| 497 | if (jas_getdbglevel() >= 1) { |
||
| 498 | fprintf(stderr, "packet offset=%08ld prg=%d cmptno=%02d " |
||
| 499 | "rlvlno=%02d prcno=%03d lyrno=%02d\n", (long) |
||
| 500 | jas_stream_getrwcount(in), jpc_pi_prg(pi), jpc_pi_cmptno(pi), |
||
| 501 | jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi)); |
||
| 502 | } |
||
| 503 | if (jpc_dec_decodepkt(dec, pkthdrstream, in, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), |
||
| 504 | jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { |
||
| 505 | return -1; |
||
| 506 | } |
||
| 507 | ++dec->numpkts; |
||
| 508 | } |
||
| 509 | |||
| 510 | return 0; |
||
| 511 | } |
||
| 512 | |||
| 513 | jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile) |
||
| 514 | { |
||
| 515 | jpc_pi_t *pi; |
||
| 516 | int compno; |
||
| 517 | jpc_picomp_t *picomp; |
||
| 518 | jpc_pirlvl_t *pirlvl; |
||
| 519 | jpc_dec_tcomp_t *tcomp; |
||
| 520 | int rlvlno; |
||
| 521 | jpc_dec_rlvl_t *rlvl; |
||
| 522 | int prcno; |
||
| 523 | int *prclyrno; |
||
| 524 | jpc_dec_cmpt_t *cmpt; |
||
| 525 | |||
| 526 | if (!(pi = jpc_pi_create0())) { |
||
| 527 | return 0; |
||
| 528 | } |
||
| 529 | pi->numcomps = dec->numcomps; |
||
| 530 | if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) { |
||
| 531 | jpc_pi_destroy(pi); |
||
| 532 | return 0; |
||
| 533 | } |
||
| 534 | for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, |
||
| 535 | ++picomp) { |
||
| 536 | picomp->pirlvls = 0; |
||
| 537 | } |
||
| 538 | |||
| 539 | for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps; |
||
| 540 | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
||
| 541 | picomp->numrlvls = tcomp->numrlvls; |
||
| 542 | if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls * |
||
| 543 | sizeof(jpc_pirlvl_t)))) { |
||
| 544 | jpc_pi_destroy(pi); |
||
| 545 | return 0; |
||
| 546 | } |
||
| 547 | for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
||
| 548 | picomp->numrlvls; ++rlvlno, ++pirlvl) { |
||
| 549 | pirlvl->prclyrnos = 0; |
||
| 550 | } |
||
| 551 | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
||
| 552 | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
||
| 553 | /* XXX sizeof(long) should be sizeof different type */ |
||
| 554 | pirlvl->numprcs = rlvl->numprcs; |
||
| 555 | if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs * |
||
| 556 | sizeof(long)))) { |
||
| 557 | jpc_pi_destroy(pi); |
||
| 558 | return 0; |
||
| 559 | } |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | pi->maxrlvls = 0; |
||
| 564 | for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps, cmpt = |
||
| 565 | dec->cmpts; compno < pi->numcomps; ++compno, ++tcomp, ++picomp, |
||
| 566 | ++cmpt) { |
||
| 567 | picomp->hsamp = cmpt->hstep; |
||
| 568 | picomp->vsamp = cmpt->vstep; |
||
| 569 | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
||
| 570 | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
||
| 571 | pirlvl->prcwidthexpn = rlvl->prcwidthexpn; |
||
| 572 | pirlvl->prcheightexpn = rlvl->prcheightexpn; |
||
| 573 | for (prcno = 0, prclyrno = pirlvl->prclyrnos; |
||
| 574 | prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { |
||
| 575 | *prclyrno = 0; |
||
| 576 | } |
||
| 577 | pirlvl->numhprcs = rlvl->numhprcs; |
||
| 578 | } |
||
| 579 | if (pi->maxrlvls < tcomp->numrlvls) { |
||
| 580 | pi->maxrlvls = tcomp->numrlvls; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | pi->numlyrs = tile->cp->numlyrs; |
||
| 585 | pi->xstart = tile->xstart; |
||
| 586 | pi->ystart = tile->ystart; |
||
| 587 | pi->xend = tile->xend; |
||
| 588 | pi->yend = tile->yend; |
||
| 589 | |||
| 590 | pi->picomp = 0; |
||
| 591 | pi->pirlvl = 0; |
||
| 592 | pi->x = 0; |
||
| 593 | pi->y = 0; |
||
| 594 | pi->compno = 0; |
||
| 595 | pi->rlvlno = 0; |
||
| 596 | pi->prcno = 0; |
||
| 597 | pi->lyrno = 0; |
||
| 598 | pi->xstep = 0; |
||
| 599 | pi->ystep = 0; |
||
| 600 | |||
| 601 | pi->pchgno = -1; |
||
| 602 | |||
| 603 | pi->defaultpchg.prgord = tile->cp->prgord; |
||
| 604 | pi->defaultpchg.compnostart = 0; |
||
| 605 | pi->defaultpchg.compnoend = pi->numcomps; |
||
| 606 | pi->defaultpchg.rlvlnostart = 0; |
||
| 607 | pi->defaultpchg.rlvlnoend = pi->maxrlvls; |
||
| 608 | pi->defaultpchg.lyrnoend = pi->numlyrs; |
||
| 609 | pi->pchg = 0; |
||
| 610 | |||
| 611 | pi->valid = 0; |
||
| 612 | |||
| 613 | return pi; |
||
| 614 | } |
||
| 615 | |||
| 616 | long jpc_dec_lookahead(jas_stream_t *in) |
||
| 617 | { |
||
| 618 | uint_fast16_t x; |
||
| 619 | if (jpc_getuint16(in, &x)) { |
||
| 620 | return -1; |
||
| 621 | } |
||
| 622 | if (jas_stream_ungetc(in, x & 0xff) == EOF || |
||
| 623 | jas_stream_ungetc(in, x >> 8) == EOF) { |
||
| 624 | return -1; |
||
| 625 | } |
||
| 626 | if (x >= JPC_MS_INMIN && x <= JPC_MS_INMAX) { |
||
| 627 | return x; |
||
| 628 | } |
||
| 629 | return 0; |
||
| 630 | } |