Move the sources to trunk
[opencv] / otherlibs / _graphics / src / libjasper / jpc_t1cod.h
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 License Version 2.0
11  * 
12  * Copyright (c) 1999-2000 Image Power, Inc.
13  * Copyright (c) 1999-2000 The University of British Columbia
14  * Copyright (c) 2001-2003 Michael David Adams
15  * 
16  * All rights reserved.
17  * 
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  * 
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  * 
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  * 
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * $Id: jpc_t1cod.h,v 1.1 2007/01/15 16:09:28 vp153 Exp $
66  */
67
68 #ifndef JPC_T1COD_H
69 #define JPC_T1COD_H
70
71 /******************************************************************************\
72 * Includes.
73 \******************************************************************************/
74
75 #include "jasper/jas_fix.h"
76 #include "jasper/jas_math.h"
77
78 #include "jpc_mqcod.h"
79 #include "jpc_tsfb.h"
80
81 /******************************************************************************\
82 * Constants.
83 \******************************************************************************/
84
85 /* The number of bits used to index into various lookup tables. */
86 #define JPC_NMSEDEC_BITS        7
87 #define JPC_NMSEDEC_FRACBITS    (JPC_NMSEDEC_BITS - 1)
88
89 /*
90  * Segment types.
91  */
92
93 /* Invalid. */
94 #define JPC_SEG_INVALID 0
95 /* MQ. */
96 #define JPC_SEG_MQ              1
97 /* Raw. */
98 #define JPC_SEG_RAW             2
99
100 /* The nominal word size. */
101 #define JPC_PREC        32
102
103 /* Tier-1 coding pass types. */
104 #define JPC_SIGPASS     0       /* significance */
105 #define JPC_REFPASS     1       /* refinement */
106 #define JPC_CLNPASS     2       /* cleanup */
107
108 /*
109  * Per-sample state information for tier-1 coding.
110  */
111
112 /* The northeast neighbour has been found to be significant. */
113 #define JPC_NESIG       0x0001
114 /* The southeast neighbour has been found to be significant. */
115 #define JPC_SESIG       0x0002
116 /* The southwest neighbour has been found to be significant. */
117 #define JPC_SWSIG       0x0004
118 /* The northwest neighbour has been found to be significant. */
119 #define JPC_NWSIG       0x0008
120 /* The north neighbour has been found to be significant. */
121 #define JPC_NSIG        0x0010
122 /* The east neighbour has been found to be significant. */
123 #define JPC_ESIG        0x0020
124 /* The south neighbour has been found to be significant. */
125 #define JPC_SSIG        0x0040
126 /* The west neighbour has been found to be significant. */
127 #define JPC_WSIG        0x0080
128 /* The significance mask for 8-connected neighbours. */
129 #define JPC_OTHSIGMSK \
130         (JPC_NSIG | JPC_NESIG | JPC_ESIG | JPC_SESIG | JPC_SSIG | JPC_SWSIG | JPC_WSIG | JPC_NWSIG)
131 /* The significance mask for 4-connected neighbours. */
132 #define JPC_PRIMSIGMSK  (JPC_NSIG | JPC_ESIG | JPC_SSIG | JPC_WSIG)
133
134 /* The north neighbour is negative in value. */
135 #define JPC_NSGN        0x0100
136 /* The east neighbour is negative in value. */
137 #define JPC_ESGN        0x0200
138 /* The south neighbour is negative in value. */
139 #define JPC_SSGN        0x0400
140 /* The west neighbour is negative in value. */
141 #define JPC_WSGN        0x0800
142 /* The sign mask for 4-connected neighbours. */
143 #define JPC_SGNMSK      (JPC_NSGN | JPC_ESGN | JPC_SSGN | JPC_WSGN)
144
145 /* This sample has been found to be significant. */
146 #define JPC_SIG         0x1000
147 /* The sample has been refined. */
148 #define JPC_REFINE      0x2000
149 /* This sample has been processed during the significance pass. */
150 #define JPC_VISIT       0x4000
151
152 /* The number of aggregation contexts. */
153 #define JPC_NUMAGGCTXS  1
154 /* The number of zero coding contexts. */
155 #define JPC_NUMZCCTXS   9
156 /* The number of magnitude contexts. */
157 #define JPC_NUMMAGCTXS  3
158 /* The number of sign coding contexts. */
159 #define JPC_NUMSCCTXS   5
160 /* The number of uniform contexts. */
161 #define JPC_NUMUCTXS    1
162
163 /* The context ID for the first aggregation context. */
164 #define JPC_AGGCTXNO    0
165 /* The context ID for the first zero coding context. */
166 #define JPC_ZCCTXNO             (JPC_AGGCTXNO + JPC_NUMAGGCTXS)
167 /* The context ID for the first magnitude context. */
168 #define JPC_MAGCTXNO    (JPC_ZCCTXNO + JPC_NUMZCCTXS)
169 /* The context ID for the first sign coding context. */
170 #define JPC_SCCTXNO             (JPC_MAGCTXNO + JPC_NUMMAGCTXS)
171 /* The context ID for the first uniform context. */
172 #define JPC_UCTXNO              (JPC_SCCTXNO + JPC_NUMSCCTXS)
173 /* The total number of contexts. */
174 #define JPC_NUMCTXS             (JPC_UCTXNO + JPC_NUMUCTXS)
175
176 /******************************************************************************\
177 * External data.
178 \******************************************************************************/
179
180 /* These lookup tables are used by various macros/functions. */
181 /* Do not access these lookup tables directly. */
182 extern int jpc_zcctxnolut[];
183 extern int jpc_spblut[];
184 extern int jpc_scctxnolut[];
185 extern int jpc_magctxnolut[];
186 extern jpc_fix_t jpc_refnmsedec[];
187 extern jpc_fix_t jpc_signmsedec[];
188 extern jpc_fix_t jpc_refnmsedec0[];
189 extern jpc_fix_t jpc_signmsedec0[];
190
191 /* The initial settings for the MQ contexts. */
192 extern jpc_mqctx_t jpc_mqctxs[];
193
194 /******************************************************************************\
195 * Functions and macros.
196 \******************************************************************************/
197
198 /* Initialize the MQ contexts. */
199 void jpc_initctxs(jpc_mqctx_t *ctxs);
200
201 /* Get the zero coding context. */
202 int jpc_getzcctxno(int f, int orient);
203 #define JPC_GETZCCTXNO(f, orient) \
204         (jpc_zcctxnolut[((orient) << 8) | ((f) & JPC_OTHSIGMSK)])
205
206 /* Get the sign prediction bit. */
207 int jpc_getspb(int f);
208 #define JPC_GETSPB(f) \
209         (jpc_spblut[((f) & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4])
210
211 /* Get the sign coding context. */
212 int jpc_getscctxno(int f);
213 #define JPC_GETSCCTXNO(f) \
214         (jpc_scctxnolut[((f) & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4])
215
216 /* Get the magnitude context. */
217 int jpc_getmagctxno(int f);
218 #define JPC_GETMAGCTXNO(f) \
219         (jpc_magctxnolut[((f) & JPC_OTHSIGMSK) | ((((f) & JPC_REFINE) != 0) << 11)])
220
221 /* Get the normalized MSE reduction for significance passes. */
222 #define JPC_GETSIGNMSEDEC(x, bitpos)    jpc_getsignmsedec_macro(x, bitpos)
223 jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos);
224 #define jpc_getsignmsedec_macro(x, bitpos) \
225         ((bitpos > JPC_NMSEDEC_FRACBITS) ? jpc_signmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)] : \
226           (jpc_signmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)]))
227
228 /* Get the normalized MSE reduction for refinement passes. */
229 #define JPC_GETREFNMSEDEC(x, bitpos)    jpc_getrefnmsedec_macro(x, bitpos)
230 jpc_fix_t jpc_refsignmsedec_func(jpc_fix_t x, int bitpos);
231 #define jpc_getrefnmsedec_macro(x, bitpos) \
232         ((bitpos > JPC_NMSEDEC_FRACBITS) ? jpc_refnmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)] : \
233           (jpc_refnmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)]))
234
235 /* Arithmetic shift right (with ability to shift left also). */
236 #define JPC_ASR(x, n) \
237         (((n) >= 0) ? ((x) >> (n)) : ((x) << (-(n))))
238
239 /* Update the per-sample state information. */
240 #define JPC_UPDATEFLAGS4(fp, rowstep, s, vcausalflag) \
241 { \
242         register jpc_fix_t *np = (fp) - (rowstep); \
243         register jpc_fix_t *sp = (fp) + (rowstep); \
244         if ((vcausalflag)) { \
245                 sp[-1] |= JPC_NESIG; \
246                 sp[1] |= JPC_NWSIG; \
247                 if (s) { \
248                         *sp |= JPC_NSIG | JPC_NSGN; \
249                         (fp)[-1] |= JPC_ESIG | JPC_ESGN; \
250                         (fp)[1] |= JPC_WSIG | JPC_WSGN; \
251                 } else { \
252                         *sp |= JPC_NSIG; \
253                         (fp)[-1] |= JPC_ESIG; \
254                         (fp)[1] |= JPC_WSIG; \
255                 } \
256         } else { \
257                 np[-1] |= JPC_SESIG; \
258                 np[1] |= JPC_SWSIG; \
259                 sp[-1] |= JPC_NESIG; \
260                 sp[1] |= JPC_NWSIG; \
261                 if (s) { \
262                         *np |= JPC_SSIG | JPC_SSGN; \
263                         *sp |= JPC_NSIG | JPC_NSGN; \
264                         (fp)[-1] |= JPC_ESIG | JPC_ESGN; \
265                         (fp)[1] |= JPC_WSIG | JPC_WSGN; \
266                 } else { \
267                         *np |= JPC_SSIG; \
268                         *sp |= JPC_NSIG; \
269                         (fp)[-1] |= JPC_ESIG; \
270                         (fp)[1] |= JPC_WSIG; \
271                 } \
272         } \
273 }
274
275 /* Initialize the lookup tables used by the codec. */
276 void jpc_initluts(void);
277
278 /* Get the nominal gain associated with a particular band. */
279 int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient);
280
281 /* Get the coding pass type. */
282 int JPC_PASSTYPE(int passno);
283
284 /* Get the segment type. */
285 int JPC_SEGTYPE(int passno, int firstpassno, int bypass);
286
287 /* Get the number of coding passess in the segment. */
288 int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass,
289   int termall);
290
291 /* Is the coding pass terminated? */
292 int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall,
293   int lazy);
294
295 #endif