Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_t1dec.c

raw
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 1 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 "netpbm/nstring.h"
128
129 #include "jasper/jas_stream.h"
130 #include "jasper/jas_math.h"
131
132 #include "jpc_bs.h"
133 #include "jpc_mqdec.h"
134 #include "jpc_t1dec.h"
135 #include "jpc_t1cod.h"
136 #include "jpc_dec.h"
137
138 /******************************************************************************\
139 *
140 \******************************************************************************/
141
142 #if defined(DEBUG)
143 static long t1dec_cnt = 0;
144 #endif
145
146 #if !defined(DEBUG)
147 #define JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) \
148 ((v) = jpc_mqdec_getbit(mqdec))
149 #else
150 #define JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) \
151 { \
152 (v) = jpc_mqdec_getbit(mqdec); \
153 if (jas_getdbglevel() >= 100) { \
154 fprintf(stderr, "index = %ld; passtype = %s; symtype = %s; sym = %d\n", t1dec_cnt, passtypename, symtypename, v); \
155 ++t1dec_cnt; \
156 } \
157 }
158 #endif
159 #define JPC_T1D_GETBITNOSKEW(mqdec, v, passtypename, symtypename) \
160 JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename)
161
162 #if !defined(DEBUG)
163 #define JPC_T1D_RAWGETBIT(bitstream, v, passtypename, symtypename) \
164 ((v) = jpc_bitstream_getbit(bitstream))
165 #else
166 #define JPC_T1D_RAWGETBIT(bitstream, v, passtypename, symtypename) \
167 { \
168 (v) = jpc_bitstream_getbit(bitstream); \
169 if (jas_getdbglevel() >= 100) { \
170 fprintf(stderr, "index = %ld; passtype = %s; symtype = %s; sym = %d\n", t1dec_cnt, passtypename, symtypename, v); \
171 ++t1dec_cnt; \
172 } \
173 }
174 #endif
175
176
177
178 /******************************************************************************\
179 * Code for significance pass.
180 \******************************************************************************/
181
182 #define jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, orient, mqdec, vcausalflag) \
183 { \
184 int f; \
185 int v; \
186 f = *(fp); \
187 if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
188 jpc_mqdec_setcurctx((mqdec), JPC_GETZCCTXNO(f, (orient))); \
189 JPC_T1D_GETBIT((mqdec), v, "SIG", "ZC"); \
190 if (v) { \
191 jpc_mqdec_setcurctx((mqdec), JPC_GETSCCTXNO(f)); \
192 JPC_T1D_GETBIT((mqdec), v, "SIG", "SC"); \
193 v ^= JPC_GETSPB(f); \
194 JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \
195 *(fp) |= JPC_SIG; \
196 *(dp) = (v) ? (-(oneplushalf)) : (oneplushalf); \
197 } \
198 *(fp) |= JPC_VISIT; \
199 } \
200 }
201
202 #define jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, vcausalflag) \
203 { \
204 jpc_fix_t f = *(fp); \
205 jpc_fix_t v; \
206 if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \
207 JPC_T1D_RAWGETBIT(in, v, "SIG", "ZC"); \
208 if (v < 0) { \
209 return -1; \
210 } \
211 if (v) { \
212 JPC_T1D_RAWGETBIT(in, v, "SIG", "SC"); \
213 if (v < 0) { \
214 return -1; \
215 } \
216 JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \
217 *(fp) |= JPC_SIG; \
218 *(dp) = v ? (-oneplushalf) : (oneplushalf); \
219 } \
220 *(fp) |= JPC_VISIT; \
221 } \
222 }
223
224
225
226 static int
227 dec_sigpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos,
228 int orient,
229 int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data)
230 {
231 int i;
232 int j;
233 int one;
234 int half;
235 int oneplushalf;
236 int vscanlen;
237 int width;
238 int height;
239 jpc_fix_t *fp;
240 int frowstep;
241 int fstripestep;
242 jpc_fix_t *fstripestart;
243 jpc_fix_t *fvscanstart;
244 jpc_fix_t *dp;
245 int drowstep;
246 int dstripestep;
247 jpc_fix_t *dstripestart;
248 jpc_fix_t *dvscanstart;
249 int k;
250
251 width = jas_matrix_numcols(data);
252 height = jas_matrix_numrows(data);
253 frowstep = jas_matrix_rowstep(flags);
254 drowstep = jas_matrix_rowstep(data);
255 fstripestep = frowstep << 2;
256 dstripestep = drowstep << 2;
257
258 one = 1 << bitpos;
259 half = one >> 1;
260 oneplushalf = one | half;
261
262 fstripestart = jas_matrix_getref(flags, 1, 1);
263 dstripestart = jas_matrix_getref(data, 0, 0);
264 for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
265 dstripestart += dstripestep) {
266 fvscanstart = fstripestart;
267 dvscanstart = dstripestart;
268 vscanlen = JAS_MIN(i, 4);
269 for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
270 fp = fvscanstart;
271 dp = dvscanstart;
272 k = vscanlen;
273
274 /* Process first sample in vertical scan. */
275 jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
276 orient, mqdec, vcausalflag);
277 if (--k <= 0) {
278 continue;
279 }
280 fp += frowstep;
281 dp += drowstep;
282
283 /* Process second sample in vertical scan. */
284 jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
285 orient, mqdec, 0);
286 if (--k <= 0) {
287 continue;
288 }
289 fp += frowstep;
290 dp += drowstep;
291
292 /* Process third sample in vertical scan. */
293 jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
294 orient, mqdec, 0);
295 if (--k <= 0) {
296 continue;
297 }
298 fp += frowstep;
299 dp += drowstep;
300
301 /* Process fourth sample in vertical scan. */
302 jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf,
303 orient, mqdec, 0);
304 }
305 }
306 return 0;
307 }
308
309
310
311 static int
312 dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos,
313 int vcausalflag,
314 jas_matrix_t *flags, jas_matrix_t *data)
315 {
316 int i;
317 int j;
318 int k;
319 int one;
320 int half;
321 int oneplushalf;
322 int vscanlen;
323 int width;
324 int height;
325 jpc_fix_t *fp;
326 int frowstep;
327 int fstripestep;
328 jpc_fix_t *fstripestart;
329 jpc_fix_t *fvscanstart;
330 jpc_fix_t *dp;
331 int drowstep;
332 int dstripestep;
333 jpc_fix_t *dstripestart;
334 jpc_fix_t *dvscanstart;
335
336 width = jas_matrix_numcols(data);
337 height = jas_matrix_numrows(data);
338 frowstep = jas_matrix_rowstep(flags);
339 drowstep = jas_matrix_rowstep(data);
340 fstripestep = frowstep << 2;
341 dstripestep = drowstep << 2;
342
343 one = 1 << bitpos;
344 half = one >> 1;
345 oneplushalf = one | half;
346
347 fstripestart = jas_matrix_getref(flags, 1, 1);
348 dstripestart = jas_matrix_getref(data, 0, 0);
349 for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
350 dstripestart += dstripestep) {
351 fvscanstart = fstripestart;
352 dvscanstart = dstripestart;
353 vscanlen = JAS_MIN(i, 4);
354 for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
355 fp = fvscanstart;
356 dp = dvscanstart;
357 k = vscanlen;
358
359 /* Process first sample in vertical scan. */
360 jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
361 in, vcausalflag);
362 if (--k <= 0) {
363 continue;
364 }
365 fp += frowstep;
366 dp += drowstep;
367
368 /* Process second sample in vertical scan. */
369 jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
370 in, 0);
371 if (--k <= 0) {
372 continue;
373 }
374 fp += frowstep;
375 dp += drowstep;
376
377 /* Process third sample in vertical scan. */
378 jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
379 in, 0);
380 if (--k <= 0) {
381 continue;
382 }
383 fp += frowstep;
384 dp += drowstep;
385
386 /* Process fourth sample in vertical scan. */
387 jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf,
388 in, 0);
389
390 }
391 }
392 return 0;
393 }
394
395
396
397 /******************************************************************************\
398 * Code for refinement pass.
399 \******************************************************************************/
400
401 #define jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, vcausalflag) \
402 { \
403 int v; \
404 int t; \
405 if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
406 jpc_mqdec_setcurctx((mqdec), JPC_GETMAGCTXNO(*(fp))); \
407 JPC_T1D_GETBITNOSKEW((mqdec), v, "REF", "MR"); \
408 t = (v ? (poshalf) : (neghalf)); \
409 *(dp) += (*(dp) < 0) ? (-t) : t; \
410 *(fp) |= JPC_REFINE; \
411 } \
412 }
413
414
415
416 static int
417 dec_refpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos,
418 int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data)
419 {
420 int i;
421 int j;
422 int vscanlen;
423 int width;
424 int height;
425 int one;
426 int poshalf;
427 int neghalf;
428 jpc_fix_t *fp;
429 int frowstep;
430 int fstripestep;
431 jpc_fix_t *fstripestart;
432 jpc_fix_t *fvscanstart;
433 jpc_fix_t *dp;
434 int drowstep;
435 int dstripestep;
436 jpc_fix_t *dstripestart;
437 jpc_fix_t *dvscanstart;
438 int k;
439
440 width = jas_matrix_numcols(data);
441 height = jas_matrix_numrows(data);
442 frowstep = jas_matrix_rowstep(flags);
443 drowstep = jas_matrix_rowstep(data);
444 fstripestep = frowstep << 2;
445 dstripestep = drowstep << 2;
446
447 one = 1 << bitpos;
448 poshalf = one >> 1;
449 neghalf = (bitpos > 0) ? (-poshalf) : (-1);
450
451 fstripestart = jas_matrix_getref(flags, 1, 1);
452 dstripestart = jas_matrix_getref(data, 0, 0);
453 for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
454 dstripestart += dstripestep) {
455 fvscanstart = fstripestart;
456 dvscanstart = dstripestart;
457 vscanlen = JAS_MIN(i, 4);
458 for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
459 fp = fvscanstart;
460 dp = dvscanstart;
461 k = vscanlen;
462
463 /* Process first sample in vertical scan. */
464 jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec,
465 vcausalflag);
466 if (--k <= 0) {
467 continue;
468 }
469 fp += frowstep;
470 dp += drowstep;
471
472 /* Process second sample in vertical scan. */
473 jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0);
474 if (--k <= 0) {
475 continue;
476 }
477 fp += frowstep;
478 dp += drowstep;
479
480 /* Process third sample in vertical scan. */
481 jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0);
482 if (--k <= 0) {
483 continue;
484 }
485 fp += frowstep;
486 dp += drowstep;
487
488 /* Process fourth sample in vertical scan. */
489 jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0);
490 }
491 }
492
493 return 0;
494 }
495
496 #define jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, vcausalflag) \
497 { \
498 jpc_fix_t v; \
499 jpc_fix_t t; \
500 if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \
501 JPC_T1D_RAWGETBIT(in, v, "REF", "MAGREF"); \
502 if (v < 0) { \
503 return -1; \
504 } \
505 t = (v ? poshalf : neghalf); \
506 *(dp) += (*(dp) < 0) ? (-t) : t; \
507 *(fp) |= JPC_REFINE; \
508 } \
509 }
510
511
512
513 static int
514 dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos,
515 int vcausalflag,
516 jas_matrix_t *flags, jas_matrix_t *data)
517 {
518 int i;
519 int j;
520 int k;
521 int vscanlen;
522 int width;
523 int height;
524 int one;
525 int poshalf;
526 int neghalf;
527 jpc_fix_t *fp;
528 int frowstep;
529 int fstripestep;
530 jpc_fix_t *fstripestart;
531 jpc_fix_t *fvscanstart;
532 jpc_fix_t *dp;
533 int drowstep;
534 int dstripestep;
535 jpc_fix_t *dstripestart;
536 jpc_fix_t *dvscanstart;
537
538 width = jas_matrix_numcols(data);
539 height = jas_matrix_numrows(data);
540 frowstep = jas_matrix_rowstep(flags);
541 drowstep = jas_matrix_rowstep(data);
542 fstripestep = frowstep << 2;
543 dstripestep = drowstep << 2;
544
545 one = 1 << bitpos;
546 poshalf = one >> 1;
547 neghalf = (bitpos > 0) ? (-poshalf) : (-1);
548
549 fstripestart = jas_matrix_getref(flags, 1, 1);
550 dstripestart = jas_matrix_getref(data, 0, 0);
551 for (i = height; i > 0; i -= 4, fstripestart += fstripestep,
552 dstripestart += dstripestep) {
553 fvscanstart = fstripestart;
554 dvscanstart = dstripestart;
555 vscanlen = JAS_MIN(i, 4);
556 for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
557 fp = fvscanstart;
558 dp = dvscanstart;
559 k = vscanlen;
560
561 /* Process first sample in vertical scan. */
562 jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in,
563 vcausalflag);
564 if (--k <= 0) {
565 continue;
566 }
567 fp += frowstep;
568 dp += drowstep;
569
570 /* Process second sample in vertical scan. */
571 jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0);
572 if (--k <= 0) {
573 continue;
574 }
575 fp += frowstep;
576 dp += drowstep;
577
578 /* Process third sample in vertical scan. */
579 jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0);
580 if (--k <= 0) {
581 continue;
582 }
583 fp += frowstep;
584 dp += drowstep;
585
586 /* Process fourth sample in vertical scan. */
587 jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0);
588 }
589 }
590 return 0;
591 }
592
593
594
595 /******************************************************************************\
596 * Code for cleanup pass.
597 \******************************************************************************/
598
599 #define jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, flabel, plabel, vcausalflag) \
600 { \
601 int v; \
602 flabel \
603 if (!((f) & (JPC_SIG | JPC_VISIT))) { \
604 jpc_mqdec_setcurctx((mqdec), JPC_GETZCCTXNO((f), (orient))); \
605 JPC_T1D_GETBIT((mqdec), v, "CLN", "ZC"); \
606 if (v) { \
607 plabel \
608 /* Coefficient is significant. */ \
609 jpc_mqdec_setcurctx((mqdec), JPC_GETSCCTXNO(f)); \
610 JPC_T1D_GETBIT((mqdec), v, "CLN", "SC"); \
611 v ^= JPC_GETSPB(f); \
612 *(dp) = (v) ? (-(oneplushalf)) : (oneplushalf); \
613 JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \
614 *(fp) |= JPC_SIG; \
615 } \
616 } \
617 /* XXX - Is this correct? Can aggregation cause some VISIT bits not to be reset? Check. */ \
618 *(fp) &= ~JPC_VISIT; \
619 }
620
621 static int
622 dec_clnpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos,
623 int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags,
624 jas_matrix_t *data)
625 {
626 int i;
627 int j;
628 int k;
629 int vscanlen;
630 int v;
631 int half;
632 int runlen;
633 int f;
634 int width;
635 int height;
636 int one;
637 int oneplushalf;
638
639 jpc_fix_t *fp;
640 int frowstep;
641 int fstripestep;
642 jpc_fix_t *fstripestart;
643 jpc_fix_t *fvscanstart;
644
645 jpc_fix_t *dp;
646 int drowstep;
647 int dstripestep;
648 jpc_fix_t *dstripestart;
649 jpc_fix_t *dvscanstart;
650
651 one = 1 << bitpos;
652 half = one >> 1;
653 oneplushalf = one | half;
654
655 width = jas_matrix_numcols(data);
656 height = jas_matrix_numrows(data);
657
658 frowstep = jas_matrix_rowstep(flags);
659 drowstep = jas_matrix_rowstep(data);
660 fstripestep = frowstep << 2;
661 dstripestep = drowstep << 2;
662
663 fstripestart = jas_matrix_getref(flags, 1, 1);
664 dstripestart = jas_matrix_getref(data, 0, 0);
665 for (i = 0; i < height; i += 4, fstripestart += fstripestep,
666 dstripestart += dstripestep) {
667 fvscanstart = fstripestart;
668 dvscanstart = dstripestart;
669 vscanlen = JAS_MIN(4, height - i);
670 for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) {
671 fp = fvscanstart;
672 if (vscanlen >= 4 && (!((*fp) & (JPC_SIG | JPC_VISIT |
673 JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG |
674 JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) &
675 (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep,
676 !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) {
677
678 jpc_mqdec_setcurctx(mqdec, JPC_AGGCTXNO);
679 JPC_T1D_GETBIT(mqdec, v, "CLN", "AGG");
680 if (!v) {
681 continue;
682 }
683 jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO);
684 JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL");
685 runlen = v;
686 JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL");
687 runlen = (runlen << 1) | v;
688 f = *(fp = fvscanstart + frowstep * runlen);
689 dp = dvscanstart + drowstep * runlen;
690 k = vscanlen - runlen;
691 switch (runlen) {
692 case 0:
693 goto clnpass_partial0;
694 break;
695 case 1:
696 goto clnpass_partial1;
697 break;
698 case 2:
699 goto clnpass_partial2;
700 break;
701 case 3:
702 goto clnpass_partial3;
703 break;
704 }
705 } else {
706 f = *(fp = fvscanstart);
707 dp = dvscanstart;
708 k = vscanlen;
709 goto clnpass_full0;
710 }
711
712 /* Process first sample in vertical scan. */
713 jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
714 mqdec, clnpass_full0:, clnpass_partial0:,
715 vcausalflag);
716 if (--k <= 0) {
717 continue;
718 }
719 fp += frowstep;
720 dp += drowstep;
721
722 /* Process second sample in vertical scan. */
723 f = *fp;
724 jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
725 mqdec, ;, clnpass_partial1:, 0);
726 if (--k <= 0) {
727 continue;
728 }
729 fp += frowstep;
730 dp += drowstep;
731
732 /* Process third sample in vertical scan. */
733 f = *fp;
734 jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
735 mqdec, ;, clnpass_partial2:, 0);
736 if (--k <= 0) {
737 continue;
738 }
739 fp += frowstep;
740 dp += drowstep;
741
742 /* Process fourth sample in vertical scan. */
743 f = *fp;
744 jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient,
745 mqdec, ;, clnpass_partial3:, 0);
746 }
747 }
748
749 if (segsymflag) {
750 int segsymval;
751 segsymval = 0;
752 jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO);
753 JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
754 segsymval = (segsymval << 1) | (v & 1);
755 JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
756 segsymval = (segsymval << 1) | (v & 1);
757 JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
758 segsymval = (segsymval << 1) | (v & 1);
759 JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM");
760 segsymval = (segsymval << 1) | (v & 1);
761 if (segsymval != 0xa) {
762 fprintf(stderr, "warning: bad segmentation symbol\n");
763 }
764 }
765
766 return 0;
767 }
768
769
770
771 static void
772 jpc_dec_decodecblk(jpc_dec_t * const dec,
773 jpc_dec_tile_t * const tile,
774 jpc_dec_tcomp_t * const tcomp,
775 jpc_dec_band_t * const band,
776 jpc_dec_cblk_t * const cblk,
777 int const dopartial,
778 int const maxlyrs,
779 const char ** const errorP) {
780
781 jpc_dec_seg_t *seg;
782 int i;
783 int bpno;
784 int passtype;
785 int ret;
786 int compno;
787 int filldata;
788 int fillmask;
789 jpc_dec_ccp_t *ccp;
790
791 compno = tcomp - tile->tcomps;
792
793 if (!cblk->flags) {
794 /* Note: matrix is assumed to be zeroed */
795 unsigned int const nrow = jas_matrix_numrows(cblk->data) + 2;
796 unsigned int const ncol = jas_matrix_numcols(cblk->data) + 2;
797 cblk->flags = jas_matrix_create(nrow, ncol);
798 if (!cblk->flags) {
799 pm_asprintf(errorP, "Out of memory allocating a flags matrix of "
800 "%u rows by %u columns", nrow, ncol);
801 return;
802 }
803 }
804
805 seg = cblk->segs.head;
806 while (seg && (seg != cblk->curseg || dopartial) && (maxlyrs < 0 ||
807 seg->lyrno < maxlyrs)) {
808 assert(seg->numpasses >= seg->maxpasses || dopartial);
809 assert(seg->stream);
810 jas_stream_rewind(seg->stream);
811 jas_stream_setrwcount(seg->stream, 0);
812 if (seg->type == JPC_SEG_MQ) {
813 if (!cblk->mqdec) {
814 if (!(cblk->mqdec = jpc_mqdec_create(JPC_NUMCTXS, 0))) {
815 pm_asprintf(errorP, "jpc_mqdec_create failed");
816 return;
817 }
818 jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs);
819 }
820 jpc_mqdec_setinput(cblk->mqdec, seg->stream);
821 jpc_mqdec_init(cblk->mqdec);
822 } else {
823 assert(seg->type == JPC_SEG_RAW);
824 if (!cblk->nulldec) {
825 if (!(cblk->nulldec = jpc_bitstream_sopen(seg->stream, "r"))) {
826 assert(0);
827 }
828 }
829 }
830
831
832 for (i = 0; i < seg->numpasses; ++i) {
833 if (cblk->numimsbs > band->numbps) {
834 ccp = &tile->cp->ccps[compno];
835 if (ccp->roishift <= 0) {
836 fprintf(stderr, "warning: corrupt code stream\n");
837 } else {
838 if (cblk->numimsbs < ccp->roishift - band->numbps) {
839 fprintf(stderr, "warning: corrupt code stream\n");
840 }
841 }
842 }
843 bpno = band->roishift + band->numbps - 1 - (cblk->numimsbs +
844 (seg->passno + i - cblk->firstpassno + 2) / 3);
845 if (bpno < 0) {
846 goto exit;
847 }
848 #if 1
849 passtype = (seg->passno + i + 2) % 3;
850 #else
851 passtype = JPC_PASSTYPE(seg->passno + i + 2);
852 #endif
853 assert(bpno >= 0 && bpno < 31);
854 switch (passtype) {
855 case JPC_SIGPASS:
856 ret = (seg->type == JPC_SEG_MQ) ? dec_sigpass(dec,
857 cblk->mqdec, bpno, band->orient,
858 (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
859 cblk->flags, cblk->data) :
860 dec_rawsigpass(dec, cblk->nulldec, bpno,
861 (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
862 cblk->flags, cblk->data);
863 break;
864 case JPC_REFPASS:
865 ret = (seg->type == JPC_SEG_MQ) ?
866 dec_refpass(dec, cblk->mqdec, bpno,
867 (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
868 cblk->flags, cblk->data) :
869 dec_rawrefpass(dec, cblk->nulldec, bpno,
870 (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0,
871 cblk->flags, cblk->data);
872 break;
873 case JPC_CLNPASS:
874 assert(seg->type == JPC_SEG_MQ);
875 ret = dec_clnpass(dec, cblk->mqdec, bpno,
876 band->orient, (tile->cp->ccps[compno].cblkctx &
877 JPC_COX_VSC) != 0, (tile->cp->ccps[compno].cblkctx &
878 JPC_COX_SEGSYM) != 0, cblk->flags,
879 cblk->data);
880 break;
881 default:
882 ret = -1;
883 break;
884 }
885 /* Do we need to reset after each coding pass? */
886 if (tile->cp->ccps[compno].cblkctx & JPC_COX_RESET) {
887 jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs);
888 }
889
890 if (ret) {
891 pm_asprintf(errorP,
892 "coding pass failed passtype=%d segtype=%d",
893 passtype, seg->type);
894 return;
895 }
896
897 }
898
899 if (seg->type == JPC_SEG_MQ) {
900 /* Note: dont destroy mq decoder because context info will be
901 lost
902 */
903 } else {
904 assert(seg->type == JPC_SEG_RAW);
905 if (tile->cp->ccps[compno].cblkctx & JPC_COX_PTERM) {
906 fillmask = 0x7f;
907 filldata = 0x2a;
908 } else {
909 fillmask = 0;
910 filldata = 0;
911 }
912 if ((ret = jpc_bitstream_inalign(cblk->nulldec, fillmask,
913 filldata)) < 0) {
914 pm_asprintf(errorP, "jpc_bitstream_inalign failed");
915 return;
916 } else if (ret > 0) {
917 pm_message("warning: bad termination pattern detected");
918 }
919 jpc_bitstream_close(cblk->nulldec);
920 cblk->nulldec = 0;
921 }
922
923 cblk->curseg = seg->next;
924 jpc_seglist_remove(&cblk->segs, seg);
925 jpc_seg_destroy(seg);
926 seg = cblk->curseg;
927 }
928
929 assert(dopartial ? (!cblk->curseg) : 1);
930
931 exit:
932 *errorP = NULL;
933 }
934
935
936
937 void
938 jpc_dec_decodecblks(jpc_dec_t * const decP,
939 jpc_dec_tile_t * const tileP,
940 const char ** const errorP) {
941 /*----------------------------------------------------------------------------
942 Decode all of the code blocks for a particular tile
943 -----------------------------------------------------------------------------*/
944 unsigned int compcnt;
945 jpc_dec_tcomp_t * tcompP;
946
947 for (compcnt = 0, tcompP = tileP->tcomps;
948 compcnt < decP->numcomps;
949 ++compcnt, ++tcompP) {
950
951 unsigned int rlvlcnt;
952 jpc_dec_rlvl_t * rlvlP;
953
954 for (rlvlcnt = 0, rlvlP = tcompP->rlvls;
955 rlvlcnt < tcompP->numrlvls;
956 ++rlvlcnt, ++rlvlP) {
957
958 if (rlvlP->bands) {
959 unsigned int bandcnt;
960 jpc_dec_band_t * bandP;
961
962 for (bandcnt = 0, bandP = rlvlP->bands;
963 bandcnt < rlvlP->numbands;
964 ++bandcnt, ++bandP) {
965
966 if (bandP->data) {
967 unsigned int prccnt;
968 jpc_dec_prc_t * prcP;
969
970 for (prccnt = 0, prcP = bandP->prcs;
971 prccnt < rlvlP->numprcs;
972 ++prccnt, ++prcP) {
973
974 if (prcP->cblks) {
975 unsigned int cblkcnt;
976 jpc_dec_cblk_t * cblkP;
977
978 for (cblkcnt = 0, cblkP = prcP->cblks;
979 cblkcnt < prcP->numcblks;
980 ++cblkcnt, ++cblkP) {
981
982 const char * error;
983
984 jpc_dec_decodecblk(decP, tileP, tcompP,
985 bandP, cblkP, 1,
986 JPC_MAXLYRS, &error);
987 if (error) {
988 pm_asprintf(errorP,
989 "jpc_dec_decodecblk "
990 "failed on component %u, "
991 "resolution level %u, "
992 "band %u, prc %u, "
993 "code block %u. %s",
994 compcnt, rlvlcnt, bandcnt,
995 prccnt, cblkcnt, error);
996 pm_strfree(error);
997 return;
998 }
999 }
1000 }
1001 }
1002 }
1003 }
1004 }
1005 }
1006 }
1007 *errorP = NULL;
1008 }
1009