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