2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
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
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
65 * MQ Arithmetic Decoder
67 * $Id: jpc_mqdec.c,v 1.1 2005/05/22 18:33:04 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
78 #include "jasper/jas_types.h"
79 #include "jasper/jas_malloc.h"
80 #include "jasper/jas_math.h"
81 #include "jasper/jas_debug.h"
83 #include "jpc_mqdec.h"
85 /******************************************************************************\
87 \******************************************************************************/
90 #define MQDEC_CALL(n, x) \
91 ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))
93 #define MQDEC_CALL(n, x)
96 /******************************************************************************\
97 * Local function prototypes.
98 \******************************************************************************/
100 static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec);
102 /******************************************************************************\
103 * Code for creation and destruction of a MQ decoder.
104 \******************************************************************************/
106 /* Create a MQ decoder. */
107 jpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in)
111 /* There must be at least one context. */
114 /* Allocate memory for the MQ decoder. */
115 if (!(mqdec = jas_malloc(sizeof(jpc_mqdec_t)))) {
119 mqdec->maxctxs = maxctxs;
120 /* Allocate memory for the per-context state information. */
121 if (!(mqdec->ctxs = jas_malloc(mqdec->maxctxs * sizeof(jpc_mqstate_t *)))) {
124 /* Set the current context to the first context. */
125 mqdec->curctx = mqdec->ctxs;
127 /* If an input stream has been associated with the MQ decoder,
128 initialize the decoder state from the stream. */
130 jpc_mqdec_init(mqdec);
132 /* Initialize the per-context state information. */
133 jpc_mqdec_setctxs(mqdec, 0, 0);
138 /* Oops... Something has gone wrong. */
140 jpc_mqdec_destroy(mqdec);
145 /* Destroy a MQ decoder. */
146 void jpc_mqdec_destroy(jpc_mqdec_t *mqdec)
149 jas_free(mqdec->ctxs);
154 /******************************************************************************\
155 * Code for initialization of a MQ decoder.
156 \******************************************************************************/
158 /* Initialize the state of a MQ decoder. */
160 void jpc_mqdec_init(jpc_mqdec_t *mqdec)
166 /* Get the next byte from the input stream. */
167 if ((c = jas_stream_getc(mqdec->in)) == EOF) {
168 /* We have encountered an I/O error or EOF. */
173 mqdec->creg += mqdec->inbuffer << 16;
174 jpc_mqdec_bytein(mqdec);
177 mqdec->areg = 0x8000;
180 /* Set the input stream for a MQ decoder. */
182 void jpc_mqdec_setinput(jpc_mqdec_t *mqdec, jas_stream_t *in)
187 /* Initialize one or more contexts. */
189 void jpc_mqdec_setctxs(jpc_mqdec_t *mqdec, int numctxs, jpc_mqctx_t *ctxs)
195 n = JAS_MIN(mqdec->maxctxs, numctxs);
197 *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];
201 n = mqdec->maxctxs - numctxs;
203 *ctx = &jpc_mqstates[0];
208 /* Initialize a context. */
210 void jpc_mqdec_setctx(jpc_mqdec_t *mqdec, int ctxno, jpc_mqctx_t *ctx)
212 jpc_mqstate_t **ctxi;
213 ctxi = &mqdec->ctxs[ctxno];
214 *ctxi = &jpc_mqstates[2 * ctx->ind + ctx->mps];
217 /******************************************************************************\
218 * Code for decoding a bit.
219 \******************************************************************************/
223 int jpc_mqdec_getbit_func(register jpc_mqdec_t *mqdec)
226 JAS_DBGLOG(100, ("jpc_mqdec_getbit_func(%p)\n", mqdec));
227 MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr));
228 bit = jpc_mqdec_getbit_macro(mqdec);
229 MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr));
230 JAS_DBGLOG(100, ("ctx = %d, decoded %d\n", mqdec->curctx -
235 /* Apply MPS_EXCHANGE algorithm (with RENORMD). */
236 int jpc_mqdec_mpsexchrenormd(register jpc_mqdec_t *mqdec)
239 register jpc_mqstate_t *state = *mqdec->curctx;
240 jpc_mqdec_mpsexchange(mqdec->areg, state->qeval, mqdec->curctx, ret);
241 jpc_mqdec_renormd(mqdec->areg, mqdec->creg, mqdec->ctreg, mqdec->in,
242 mqdec->eof, mqdec->inbuffer);
246 /* Apply LPS_EXCHANGE algorithm (with RENORMD). */
247 int jpc_mqdec_lpsexchrenormd(register jpc_mqdec_t *mqdec)
250 register jpc_mqstate_t *state = *mqdec->curctx;
251 jpc_mqdec_lpsexchange(mqdec->areg, state->qeval, mqdec->curctx, ret);
252 jpc_mqdec_renormd(mqdec->areg, mqdec->creg, mqdec->ctreg, mqdec->in,
253 mqdec->eof, mqdec->inbuffer);
257 /******************************************************************************\
259 \******************************************************************************/
261 /* Apply the BYTEIN algorithm. */
262 static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec)
265 unsigned char prevbuf;
268 if ((c = jas_stream_getc(mqdec->in)) == EOF) {
272 prevbuf = mqdec->inbuffer;
274 if (prevbuf == 0xff) {
276 mqdec->creg += 0xff00;
279 mqdec->creg += c << 9;
283 mqdec->creg += c << 8;
287 mqdec->creg += 0xff00;
292 /******************************************************************************\
293 * Code for debugging.
294 \******************************************************************************/
296 /* Dump a MQ decoder to a stream for debugging. */
298 void jpc_mqdec_dump(jpc_mqdec_t *mqdec, FILE *out)
300 fprintf(out, "MQDEC A = %08lx, C = %08lx, CT=%08lx, ",
301 (unsigned long) mqdec->areg, (unsigned long) mqdec->creg,
302 (unsigned long) mqdec->ctreg);
303 fprintf(out, "CTX = %d, ", mqdec->curctx - mqdec->ctxs);
304 fprintf(out, "IND %d, MPS %d, QEVAL %x\n", *mqdec->curctx -
305 jpc_mqstates, (*mqdec->curctx)->mps, (*mqdec->curctx)->qeval);