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