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 * $Id$
115 */
116
117 /******************************************************************************\
118 * Includes.
119 \******************************************************************************/
120
121 #include <stdio.h>
122 #include <stdlib.h>
123 #include <assert.h>
124 #include <math.h>
125
126 #include "jasper/jas_types.h"
127 #include "jasper/jas_math.h"
128
129 #include "jpc_bs.h"
130 #include "jpc_dec.h"
131 #include "jpc_cs.h"
132 #include "jpc_mqcod.h"
133 #include "jpc_t1cod.h"
134 #include "jpc_tsfb.h"
135
136 double jpc_pow2i(int n);
137
138 /******************************************************************************\
139 * Global data.
140 \******************************************************************************/
141
142 int jpc_zcctxnolut[4 * 256];
143 int jpc_spblut[256];
144 int jpc_scctxnolut[256];
145 int jpc_magctxnolut[4096];
146
147 jpc_fix_t jpc_signmsedec[1 << JPC_NMSEDEC_BITS];
148 jpc_fix_t jpc_refnmsedec[1 << JPC_NMSEDEC_BITS];
149 jpc_fix_t jpc_signmsedec0[1 << JPC_NMSEDEC_BITS];
150 jpc_fix_t jpc_refnmsedec0[1 << JPC_NMSEDEC_BITS];
151
152 jpc_mqctx_t jpc_mqctxs[JPC_NUMCTXS];
153
154 /******************************************************************************\
155 * Code.
156 \******************************************************************************/
157
JPC_PASSTYPE(int passno)158 int JPC_PASSTYPE(int passno)
159 {
160 int passtype;
161 switch (passno % 3) {
162 case 0:
163 passtype = JPC_CLNPASS;
164 break;
165 case 1:
166 passtype = JPC_SIGPASS;
167 break;
168 case 2:
169 passtype = JPC_REFPASS;
170 break;
171 default:
172 passtype = -1;
173 assert(0);
174 break;
175 }
176 return passtype;
177 }
178
JPC_NOMINALGAIN(int qmfbid,int numlvls,int lvlno,int orient)179 int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient)
180 {
181 if (qmfbid == JPC_COX_INS) {
182 return 0;
183 }
184 assert(qmfbid == JPC_COX_RFT);
185 if (lvlno == 0) {
186 assert(orient == JPC_TSFB_LL);
187 return 0;
188 } else {
189 switch (orient) {
190 case JPC_TSFB_LH:
191 case JPC_TSFB_HL:
192 return 1;
193 break;
194 case JPC_TSFB_HH:
195 return 2;
196 break;
197 }
198 }
199 abort();
200 }
201
202 /******************************************************************************\
203 * Coding pass related functions.
204 \******************************************************************************/
205
JPC_SEGTYPE(int passno,int firstpassno,int bypass)206 int JPC_SEGTYPE(int passno, int firstpassno, int bypass)
207 {
208 int passtype;
209 if (bypass) {
210 passtype = JPC_PASSTYPE(passno);
211 if (passtype == JPC_CLNPASS) {
212 return JPC_SEG_MQ;
213 }
214 return ((passno < firstpassno + 10) ? JPC_SEG_MQ : JPC_SEG_RAW);
215 } else {
216 return JPC_SEG_MQ;
217 }
218 }
219
JPC_SEGPASSCNT(int passno,int firstpassno,int numpasses,int bypass,int termall)220 int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass, int termall)
221 {
222 int ret;
223 int passtype;
224
225 if (termall) {
226 ret = 1;
227 } else if (bypass) {
228 if (passno < firstpassno + 10) {
229 ret = 10 - (passno - firstpassno);
230 } else {
231 passtype = JPC_PASSTYPE(passno);
232 switch (passtype) {
233 case JPC_SIGPASS:
234 ret = 2;
235 break;
236 case JPC_REFPASS:
237 ret = 1;
238 break;
239 case JPC_CLNPASS:
240 ret = 1;
241 break;
242 default:
243 ret = -1;
244 assert(0);
245 break;
246 }
247 }
248 } else {
249 ret = JPC_PREC * 3 - 2;
250 }
251 ret = JAS_MIN(ret, numpasses - passno);
252 return ret;
253 }
254
JPC_ISTERMINATED(int passno,int firstpassno,int numpasses,int termall,int lazy)255 int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall,
256 int lazy)
257 {
258 int ret;
259 int n;
260 if (passno - firstpassno == numpasses - 1) {
261 ret = 1;
262 } else {
263 n = JPC_SEGPASSCNT(passno, firstpassno, numpasses, lazy, termall);
264 ret = (n <= 1) ? 1 : 0;
265 }
266
267 return ret;
268 }
269
270 /******************************************************************************\
271 * Lookup table code.
272 \******************************************************************************/
273
jpc_initmqctxs(void)274 static void jpc_initmqctxs(void)
275 {
276 jpc_initctxs(jpc_mqctxs);
277 }
278
jpc_initluts()279 void jpc_initluts()
280 {
281 int i;
282 int orient;
283 int refine;
284 float u;
285 float v;
286 float t;
287
288 /* XXX - hack */
289 jpc_initmqctxs();
290
291 for (orient = 0; orient < 4; ++orient) {
292 for (i = 0; i < 256; ++i) {
293 jpc_zcctxnolut[(orient << 8) | i] = jpc_getzcctxno(i, orient);
294 }
295 }
296
297 for (i = 0; i < 256; ++i) {
298 jpc_spblut[i] = jpc_getspb(i << 4);
299 }
300
301 for (i = 0; i < 256; ++i) {
302 jpc_scctxnolut[i] = jpc_getscctxno(i << 4);
303 }
304
305 for (refine = 0; refine < 2; ++refine) {
306 for (i = 0; i < 2048; ++i) {
307 jpc_magctxnolut[(refine << 11) + i] = jpc_getmagctxno((refine ? JPC_REFINE : 0) | i);
308 }
309 }
310
311 for (i = 0; i < (1 << JPC_NMSEDEC_BITS); ++i) {
312 t = i * jpc_pow2i(-JPC_NMSEDEC_FRACBITS);
313 u = t;
314 v = t - 1.5;
315 jpc_signmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
316 /* XXX - this calc is not correct */
317 jpc_signmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
318 u = t - 1.0;
319 if (i & (1 << (JPC_NMSEDEC_BITS - 1))) {
320 v = t - 1.5;
321 } else {
322 v = t - 0.5;
323 }
324 jpc_refnmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
325 /* XXX - this calc is not correct */
326 jpc_refnmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS));
327 }
328 }
329
jpc_getsignmsedec_func(jpc_fix_t x,int bitpos)330 jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos)
331 {
332 jpc_fix_t y;
333 assert(!(x & (~JAS_ONES(bitpos + 1))));
334 y = jpc_getsignmsedec_macro(x, bitpos);
335 return y;
336 }
337
jpc_getzcctxno(int f,int orient)338 int jpc_getzcctxno(int f, int orient)
339 {
340 int h;
341 int v;
342 int d;
343 int n;
344 int t;
345 int hv;
346
347 /* Avoid compiler warning. */
348 n = 0;
349
350 h = ((f & JPC_WSIG) != 0) + ((f & JPC_ESIG) != 0);
351 v = ((f & JPC_NSIG) != 0) + ((f & JPC_SSIG) != 0);
352 d = ((f & JPC_NWSIG) != 0) + ((f & JPC_NESIG) != 0) + ((f & JPC_SESIG) != 0) + ((f & JPC_SWSIG) != 0);
353 switch (orient) {
354 case JPC_TSFB_HL:
355 t = h;
356 h = v;
357 v = t;
358 case JPC_TSFB_LL:
359 case JPC_TSFB_LH:
360 if (!h) {
361 if (!v) {
362 if (!d) {
363 n = 0;
364 } else if (d == 1) {
365 n = 1;
366 } else {
367 n = 2;
368 }
369 } else if (v == 1) {
370 n = 3;
371 } else {
372 n = 4;
373 }
374 } else if (h == 1) {
375 if (!v) {
376 if (!d) {
377 n = 5;
378 } else {
379 n = 6;
380 }
381 } else {
382 n = 7;
383 }
384 } else {
385 n = 8;
386 }
387 break;
388 case JPC_TSFB_HH:
389 hv = h + v;
390 if (!d) {
391 if (!hv) {
392 n = 0;
393 } else if (hv == 1) {
394 n = 1;
395 } else {
396 n = 2;
397 }
398 } else if (d == 1) {
399 if (!hv) {
400 n = 3;
401 } else if (hv == 1) {
402 n = 4;
403 } else {
404 n = 5;
405 }
406 } else if (d == 2) {
407 if (!hv) {
408 n = 6;
409 } else {
410 n = 7;
411 }
412 } else {
413 n = 8;
414 }
415 break;
416 }
417 assert(n < JPC_NUMZCCTXS);
418 return JPC_ZCCTXNO + n;
419 }
420
jpc_getspb(int f)421 int jpc_getspb(int f)
422 {
423 int hc;
424 int vc;
425 int n;
426
427 hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG), 1) -
428 JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) + ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1);
429 vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG), 1) -
430 JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) + ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1);
431 if (!hc && !vc) {
432 n = 0;
433 } else {
434 n = (!(hc > 0 || (!hc && vc > 0)));
435 }
436 return n;
437 }
438
jpc_getscctxno(int f)439 int jpc_getscctxno(int f)
440 {
441 int hc;
442 int vc;
443 int n;
444
445 /* Avoid compiler warning. */
446 n = 0;
447
448 hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG),
449 1) - JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) +
450 ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1);
451 vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG),
452 1) - JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) +
453 ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1);
454 assert(hc >= -1 && hc <= 1 && vc >= -1 && vc <= 1);
455 if (hc < 0) {
456 hc = -hc;
457 vc = -vc;
458 }
459 if (!hc) {
460 if (vc == -1) {
461 n = 1;
462 } else if (!vc) {
463 n = 0;
464 } else {
465 n = 1;
466 }
467 } else if (hc == 1) {
468 if (vc == -1) {
469 n = 2;
470 } else if (!vc) {
471 n = 3;
472 } else {
473 n = 4;
474 }
475 }
476 assert(n < JPC_NUMSCCTXS);
477 return JPC_SCCTXNO + n;
478 }
479
jpc_getmagctxno(int f)480 int jpc_getmagctxno(int f)
481 {
482 int n;
483
484 if (!(f & JPC_REFINE)) {
485 n = (f & (JPC_OTHSIGMSK)) ? 1 : 0;
486 } else {
487 n = 2;
488 }
489
490 assert(n < JPC_NUMMAGCTXS);
491 return JPC_MAGCTXNO + n;
492 }
493
jpc_initctxs(jpc_mqctx_t * ctxs)494 void jpc_initctxs(jpc_mqctx_t *ctxs)
495 {
496 jpc_mqctx_t *ctx;
497 int i;
498
499 ctx = ctxs;
500 for (i = 0; i < JPC_NUMCTXS; ++i) {
501 ctx->mps = 0;
502 switch (i) {
503 case JPC_UCTXNO:
504 ctx->ind = 46;
505 break;
506 case JPC_ZCCTXNO:
507 ctx->ind = 4;
508 break;
509 case JPC_AGGCTXNO:
510 ctx->ind = 3;
511 break;
512 default:
513 ctx->ind = 0;
514 break;
515 }
516 ++ctx;
517 }
518 }
519
520 /* Calculate the real quantity exp2(n), where x is an integer. */
jpc_pow2i(int n)521 double jpc_pow2i(int n)
522 {
523 double x;
524 double a;
525
526 x = 1.0;
527 if (n < 0) {
528 a = 0.5;
529 n = -n;
530 } else {
531 a = 2.0;
532 }
533 while (--n >= 0) {
534 x *= a;
535 }
536 return x;
537 }
538