My Project  2.0.17
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) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
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$
66  */
67 
68 #ifndef JPC_T1COD_H
69 #define JPC_T1COD_H
70 
71 /******************************************************************************\
72 * Includes.
73 \******************************************************************************/
74 
75 #include "jpc_fix.h"
76 #include "jpc_mqcod.h"
77 
78 /******************************************************************************\
79 * Constants.
80 \******************************************************************************/
81 
82 /* The number of bits used to index into various lookup tables. */
83 #define JPC_NMSEDEC_BITS 7
84 #define JPC_NMSEDEC_FRACBITS (JPC_NMSEDEC_BITS - 1)
85 
86 /*
87  * Segment types.
88  */
89 
90 /* Invalid. */
91 #define JPC_SEG_INVALID 0
92 /* MQ. */
93 #define JPC_SEG_MQ 1
94 /* Raw. */
95 #define JPC_SEG_RAW 2
96 
97 /* The nominal word size. */
98 #define JPC_PREC 32
99 
100 /* Tier-1 coding pass types. */
101 #define JPC_SIGPASS 0 /* significance */
102 #define JPC_REFPASS 1 /* refinement */
103 #define JPC_CLNPASS 2 /* cleanup */
104 
105 /*
106  * Per-sample state information for tier-1 coding.
107  */
108 
109 /* The northeast neighbour has been found to be significant. */
110 #define JPC_NESIG 0x0001
111 /* The southeast neighbour has been found to be significant. */
112 #define JPC_SESIG 0x0002
113 /* The southwest neighbour has been found to be significant. */
114 #define JPC_SWSIG 0x0004
115 /* The northwest neighbour has been found to be significant. */
116 #define JPC_NWSIG 0x0008
117 /* The north neighbour has been found to be significant. */
118 #define JPC_NSIG 0x0010
119 /* The east neighbour has been found to be significant. */
120 #define JPC_ESIG 0x0020
121 /* The south neighbour has been found to be significant. */
122 #define JPC_SSIG 0x0040
123 /* The west neighbour has been found to be significant. */
124 #define JPC_WSIG 0x0080
125 /* The significance mask for 8-connected neighbours. */
126 #define JPC_OTHSIGMSK \
127  (JPC_NSIG | JPC_NESIG | JPC_ESIG | JPC_SESIG | JPC_SSIG | JPC_SWSIG | JPC_WSIG | JPC_NWSIG)
128 /* The significance mask for 4-connected neighbours. */
129 #define JPC_PRIMSIGMSK (JPC_NSIG | JPC_ESIG | JPC_SSIG | JPC_WSIG)
130 
131 /* The north neighbour is negative in value. */
132 #define JPC_NSGN 0x0100
133 /* The east neighbour is negative in value. */
134 #define JPC_ESGN 0x0200
135 /* The south neighbour is negative in value. */
136 #define JPC_SSGN 0x0400
137 /* The west neighbour is negative in value. */
138 #define JPC_WSGN 0x0800
139 /* The sign mask for 4-connected neighbours. */
140 #define JPC_SGNMSK (JPC_NSGN | JPC_ESGN | JPC_SSGN | JPC_WSGN)
141 
142 /* This sample has been found to be significant. */
143 #define JPC_SIG 0x1000
144 /* The sample has been refined. */
145 #define JPC_REFINE 0x2000
146 /* This sample has been processed during the significance pass. */
147 #define JPC_VISIT 0x4000
148 
149 /* The number of aggregation contexts. */
150 #define JPC_NUMAGGCTXS 1
151 /* The number of zero coding contexts. */
152 #define JPC_NUMZCCTXS 9
153 /* The number of magnitude contexts. */
154 #define JPC_NUMMAGCTXS 3
155 /* The number of sign coding contexts. */
156 #define JPC_NUMSCCTXS 5
157 /* The number of uniform contexts. */
158 #define JPC_NUMUCTXS 1
159 
160 /* The context ID for the first aggregation context. */
161 #define JPC_AGGCTXNO 0
162 /* The context ID for the first zero coding context. */
163 #define JPC_ZCCTXNO (JPC_AGGCTXNO + JPC_NUMAGGCTXS)
164 /* The context ID for the first magnitude context. */
165 #define JPC_MAGCTXNO (JPC_ZCCTXNO + JPC_NUMZCCTXS)
166 /* The context ID for the first sign coding context. */
167 #define JPC_SCCTXNO (JPC_MAGCTXNO + JPC_NUMMAGCTXS)
168 /* The context ID for the first uniform context. */
169 #define JPC_UCTXNO (JPC_SCCTXNO + JPC_NUMSCCTXS)
170 /* The total number of contexts. */
171 #define JPC_NUMCTXS (JPC_UCTXNO + JPC_NUMUCTXS)
172 
173 /******************************************************************************\
174 * External data.
175 \******************************************************************************/
176 
177 /* These lookup tables are used by various macros/functions. */
178 /* Do not access these lookup tables directly. */
179 extern uint_least8_t jpc_zcctxnolut[];
180 extern bool jpc_spblut[];
181 extern uint_least8_t jpc_scctxnolut[];
182 extern uint_least8_t jpc_magctxnolut[];
183 extern jpc_fix_t jpc_refnmsedec[];
184 extern jpc_fix_t jpc_signmsedec[];
185 extern jpc_fix_t jpc_refnmsedec0[];
186 extern jpc_fix_t jpc_signmsedec0[];
187 
188 /* The initial settings for the MQ contexts. */
189 extern jpc_mqctx_t jpc_mqctxs[];
190 
191 /******************************************************************************\
192 * Functions and macros.
193 \******************************************************************************/
194 
195 /* Arithmetic shift right (with ability to shift left also). */
196 JAS_ATTRIBUTE_CONST
197 static inline jpc_fix_t JPC_ASR(jpc_fix_t x, int n)
198 {
199  return n >= 0
200  ? x >> n
201  : x << -n;
202 }
203 
204 /* Get the zero coding context. */
205 JAS_ATTRIBUTE_CONST
206 static inline uint_least8_t JPC_GETZCCTXNO(unsigned f, unsigned orient)
207 {
208  return jpc_zcctxnolut[(orient << 8) | (f & JPC_OTHSIGMSK)];
209 }
210 
211 /* Get the sign prediction bit. */
212 JAS_ATTRIBUTE_CONST
213 static inline bool JPC_GETSPB(unsigned f)
214 {
215  return jpc_spblut[(f & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4];
216 }
217 
218 /* Get the sign coding context. */
219 JAS_ATTRIBUTE_CONST
220 static inline uint_least8_t JPC_GETSCCTXNO(unsigned f)
221 {
222  return jpc_scctxnolut[(f & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4];
223 }
224 
225 /* Get the magnitude context. */
226 JAS_ATTRIBUTE_CONST
227 static inline uint_least8_t JPC_GETMAGCTXNO(unsigned f)
228 {
229  return jpc_magctxnolut[(f & JPC_OTHSIGMSK) | (((f & JPC_REFINE) != 0) << 11)];
230 }
231 
232 /* Get the normalized MSE reduction for significance passes. */
233 JAS_ATTRIBUTE_CONST
234 static inline jpc_fix_t JPC_GETSIGNMSEDEC(jpc_fix_t x, int bitpos)
235 {
236  return bitpos > JPC_NMSEDEC_FRACBITS
237  ? jpc_signmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)]
238  : jpc_signmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)];
239 }
240 
241 /* Get the normalized MSE reduction for refinement passes. */
242 JAS_ATTRIBUTE_CONST
243 static inline jpc_fix_t JPC_GETREFNMSEDEC(jpc_fix_t x, int bitpos)
244 {
245  return bitpos > JPC_NMSEDEC_FRACBITS
246  ? jpc_refnmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)]
247  : jpc_refnmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)];
248 }
249 
250 /* Update the per-sample state information. */
251 static inline void JPC_UPDATEFLAGS4(jpc_fix_t *fp, unsigned rowstep, bool s, bool vcausalflag)
252 {
253  jpc_fix_t *np = fp - rowstep;
254  jpc_fix_t *sp = fp + rowstep;
255  if (vcausalflag) {
256  sp[-1] |= JPC_NESIG;
257  sp[1] |= JPC_NWSIG;
258  if (s) {
259  *sp |= JPC_NSIG | JPC_NSGN;
260  fp[-1] |= JPC_ESIG | JPC_ESGN;
261  fp[1] |= JPC_WSIG | JPC_WSGN;
262  } else {
263  *sp |= JPC_NSIG;
264  fp[-1] |= JPC_ESIG;
265  fp[1] |= JPC_WSIG;
266  }
267  } else {
268  np[-1] |= JPC_SESIG;
269  np[1] |= JPC_SWSIG;
270  sp[-1] |= JPC_NESIG;
271  sp[1] |= JPC_NWSIG;
272  if (s) {
273  *np |= JPC_SSIG | JPC_SSGN;
274  *sp |= JPC_NSIG | JPC_NSGN;
275  fp[-1] |= JPC_ESIG | JPC_ESGN;
276  fp[1] |= JPC_WSIG | JPC_WSGN;
277  } else {
278  *np |= JPC_SSIG;
279  *sp |= JPC_NSIG;
280  fp[-1] |= JPC_ESIG;
281  fp[1] |= JPC_WSIG;
282  }
283  }
284 }
285 
286 /* Initialize the lookup tables used by the codec. */
287 void jpc_initluts(void);
288 
289 /* Get the nominal gain associated with a particular band. */
290 JAS_ATTRIBUTE_CONST
291 int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient);
292 
293 /* Get the coding pass type. */
294 JAS_ATTRIBUTE_CONST
295 int JPC_PASSTYPE(int passno);
296 
297 /* Get the segment type. */
298 JAS_ATTRIBUTE_CONST
299 int JPC_SEGTYPE(int passno, int firstpassno, int bypass);
300 
301 /* Get the number of coding passess in the segment. */
302 JAS_ATTRIBUTE_CONST
303 int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass,
304  int termall);
305 
306 /* Is the coding pass terminated? */
307 JAS_ATTRIBUTE_CONST
308 int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall,
309  int lazy);
310 
311 #endif