1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2003 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  * MQ Arithmetic Decoder
66  *
67  * $Id$
68  */
69 
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73 
74 #include "jpc_mqdec.h"
75 
76 #include "jasper/jas_types.h"
77 #include "jasper/jas_malloc.h"
78 #include "jasper/jas_math.h"
79 #include "jasper/jas_debug.h"
80 
81 #include <assert.h>
82 
83 /******************************************************************************\
84 * Local macros.
85 \******************************************************************************/
86 
87 #ifndef NDEBUG
88 #define	MQDEC_CALL(n, x) \
89 	((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))
90 #else
91 #define	MQDEC_CALL(n, x)
92 #endif
93 
94 /******************************************************************************\
95 * Local function prototypes.
96 \******************************************************************************/
97 
98 static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec);
99 
100 /******************************************************************************\
101 * Code for creation and destruction of a MQ decoder.
102 \******************************************************************************/
103 
104 /* Create a MQ decoder. */
jpc_mqdec_create(unsigned maxctxs,jas_stream_t * in)105 jpc_mqdec_t *jpc_mqdec_create(unsigned maxctxs, jas_stream_t *in)
106 {
107 	jpc_mqdec_t *mqdec;
108 
109 	/* There must be at least one context. */
110 	assert(maxctxs > 0);
111 
112 	/* Allocate memory for the MQ decoder. */
113 	if (!(mqdec = jas_malloc(sizeof(jpc_mqdec_t)))) {
114 		goto error;
115 	}
116 	mqdec->in = in;
117 	mqdec->maxctxs = maxctxs;
118 	/* Allocate memory for the per-context state information. */
119 	if (!(mqdec->ctxs = jas_alloc2(mqdec->maxctxs, sizeof(jpc_mqstate_t *)))) {
120 		goto error;
121 	}
122 	/* Set the current context to the first context. */
123 	mqdec->curctx = mqdec->ctxs;
124 
125 	/* If an input stream has been associated with the MQ decoder,
126 	  initialize the decoder state from the stream. */
127 	if (mqdec->in) {
128 		jpc_mqdec_init(mqdec);
129 	}
130 	/* Initialize the per-context state information. */
131 	jpc_mqdec_setctxs(mqdec, 0, 0);
132 
133 	return mqdec;
134 
135 error:
136 	/* Oops...  Something has gone wrong. */
137 	if (mqdec) {
138 		jpc_mqdec_destroy(mqdec);
139 	}
140 	return 0;
141 }
142 
143 /* Destroy a MQ decoder. */
jpc_mqdec_destroy(jpc_mqdec_t * mqdec)144 void jpc_mqdec_destroy(jpc_mqdec_t *mqdec)
145 {
146 	jas_free(mqdec->ctxs);
147 	jas_free(mqdec);
148 }
149 
150 /******************************************************************************\
151 * Code for initialization of a MQ decoder.
152 \******************************************************************************/
153 
154 /* Initialize the state of a MQ decoder. */
155 
jpc_mqdec_init(jpc_mqdec_t * mqdec)156 void jpc_mqdec_init(jpc_mqdec_t *mqdec)
157 {
158 	int c;
159 
160 	mqdec->eof = false;
161 	mqdec->creg = 0;
162 	/* Get the next byte from the input stream. */
163 	if ((c = jas_stream_getc(mqdec->in)) == EOF) {
164 		/* We have encountered an I/O error or EOF. */
165 		c = 0xff;
166 		mqdec->eof = true;
167 	}
168 	mqdec->inbuffer = c;
169 	mqdec->creg += mqdec->inbuffer << 16;
170 	jpc_mqdec_bytein(mqdec);
171 	mqdec->creg <<= 7;
172 	mqdec->ctreg -= 7;
173 	mqdec->areg = 0x8000;
174 }
175 
176 /* Set the input stream for a MQ decoder. */
177 
jpc_mqdec_setinput(jpc_mqdec_t * mqdec,jas_stream_t * in)178 void jpc_mqdec_setinput(jpc_mqdec_t *mqdec, jas_stream_t *in)
179 {
180 	mqdec->in = in;
181 }
182 
183 /* Initialize one or more contexts. */
184 
jpc_mqdec_setctxs(const jpc_mqdec_t * mqdec,unsigned numctxs,const jpc_mqctx_t * ctxs)185 void jpc_mqdec_setctxs(const jpc_mqdec_t *mqdec, unsigned numctxs, const jpc_mqctx_t *ctxs)
186 {
187 	const jpc_mqstate_t **ctx;
188 
189 	ctx = mqdec->ctxs;
190 	unsigned n = JAS_MIN(mqdec->maxctxs, numctxs);
191 	while (n-- > 0) {
192 		*ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];
193 		++ctx;
194 		++ctxs;
195 	}
196 	n = mqdec->maxctxs - numctxs;
197 	while (n-- > 0) {
198 		*ctx = &jpc_mqstates[0];
199 		++ctx;
200 	}
201 }
202 
203 /******************************************************************************\
204 * Code for decoding a bit.
205 \******************************************************************************/
206 
207 /* Decode a bit. */
208 
jpc_mqdec_getbit_func(register jpc_mqdec_t * mqdec)209 bool jpc_mqdec_getbit_func(register jpc_mqdec_t *mqdec)
210 {
211 	bool bit;
212 	JAS_DBGLOG(100, ("jpc_mqdec_getbit_func(%p)\n", mqdec));
213 	MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr));
214 	bit = jpc_mqdec_getbit_macro(mqdec);
215 	MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr));
216 	JAS_DBGLOG(100, ("ctx = %d, decoded %d\n", mqdec->curctx -
217 	  mqdec->ctxs, bit));
218 	return bit;
219 }
220 
jpc_mqdec_renormd(jpc_mqdec_t * mqdec)221 static void jpc_mqdec_renormd(jpc_mqdec_t *mqdec)
222 {
223 	do {
224 		if (!mqdec->ctreg)
225 			jpc_mqdec_bytein(mqdec);
226 
227 		mqdec->areg <<= 1;
228 		mqdec->creg <<= 1;
229 		--mqdec->ctreg;
230 	} while (!(mqdec->areg & 0x8000));
231 }
232 
233 /* Apply MPS_EXCHANGE algorithm (with RENORMD). */
jpc_mqdec_mpsexchrenormd(register jpc_mqdec_t * mqdec)234 bool jpc_mqdec_mpsexchrenormd(register jpc_mqdec_t *mqdec)
235 {
236 	register const jpc_mqstate_t *state = *mqdec->curctx;
237 	bool ret = jpc_mqdec_mpsexchange(mqdec->areg, state->qeval, mqdec->curctx);
238 	jpc_mqdec_renormd(mqdec);
239 	return ret;
240 }
241 
242 /* Apply LPS_EXCHANGE algorithm (with RENORMD). */
jpc_mqdec_lpsexchrenormd(register jpc_mqdec_t * mqdec)243 bool jpc_mqdec_lpsexchrenormd(register jpc_mqdec_t *mqdec)
244 {
245 	register const jpc_mqstate_t *state = *mqdec->curctx;
246 	bool ret = jpc_mqdec_lpsexchange(&mqdec->areg, state->qeval, mqdec->curctx);
247 	jpc_mqdec_renormd(mqdec);
248 	return ret;
249 }
250 
251 /******************************************************************************\
252 * Support code.
253 \******************************************************************************/
254 
255 /* Apply the BYTEIN algorithm. */
jpc_mqdec_bytein(jpc_mqdec_t * mqdec)256 static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec)
257 {
258 	int c;
259 	unsigned char prevbuf;
260 
261 	if (!mqdec->eof) {
262 		if ((c = jas_stream_getc(mqdec->in)) == EOF) {
263 			mqdec->eof = true;
264 			c = 0xff;
265 		}
266 		prevbuf = mqdec->inbuffer;
267 		mqdec->inbuffer = c;
268 		if (prevbuf == 0xff) {
269 			if (c > 0x8f) {
270 				mqdec->creg += 0xff00;
271 				mqdec->ctreg = 8;
272 			} else {
273 				mqdec->creg += c << 9;
274 				mqdec->ctreg = 7;
275 			}
276 		} else {
277 			mqdec->creg += c << 8;
278 			mqdec->ctreg = 8;
279 		}
280 	} else {
281 		mqdec->creg += 0xff00;
282 		mqdec->ctreg = 8;
283 	}
284 }
285 
286 /******************************************************************************\
287 * Code for debugging.
288 \******************************************************************************/
289 
290 /* Dump a MQ decoder to a stream for debugging. */
291 
jpc_mqdec_dump(const jpc_mqdec_t * mqdec,FILE * out)292 void jpc_mqdec_dump(const jpc_mqdec_t *mqdec, FILE *out)
293 {
294 	fprintf(out, "MQDEC A = %08lx, C = %08lx, CT=%08lx, ",
295 	  (unsigned long) mqdec->areg, (unsigned long) mqdec->creg,
296 	  (unsigned long) mqdec->ctreg);
297 	fprintf(out, "CTX = %" PRIdPTR ", ", mqdec->curctx - mqdec->ctxs);
298 	fprintf(out, "IND %" PRIdPTR ", MPS %d, QEVAL %"PRIxLEAST16"\n", *mqdec->curctx -
299 	  jpc_mqstates, (*mqdec->curctx)->mps, (*mqdec->curctx)->qeval);
300 }
301