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 Encoder
67 * $Id: jpc_mqenc.c,v 1.1 2005/05/22 18:33:05 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
77 #include "jasper/jas_stream.h"
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_math.h"
80 #include "jasper/jas_debug.h"
82 #include "jpc_mqenc.h"
84 /******************************************************************************\
86 \******************************************************************************/
89 #define JPC_MQENC_CALL(n, x) \
90 ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))
92 #define JPC_MQENC_CALL(n, x)
95 #define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \
97 jpc_mqstate_t *state = *(curctx); \
98 (areg) -= state->qeval; \
99 if (!((areg) & 0x8000)) { \
100 if ((areg) < state->qeval) { \
101 (areg) = state->qeval; \
103 (creg) += state->qeval; \
105 *(curctx) = state->nmps; \
106 jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \
108 (creg) += state->qeval; \
112 #define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \
114 jpc_mqstate_t *state = *(curctx); \
115 (areg) -= state->qeval; \
116 if ((areg) < state->qeval) { \
117 (creg) += state->qeval; \
119 (areg) = state->qeval; \
121 *(curctx) = state->nlps; \
122 jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \
125 #define jpc_mqenc_renorme(areg, creg, ctreg, enc) \
131 jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \
133 } while (!((areg) & 0x8000)); \
136 #define jpc_mqenc_byteout(areg, creg, ctreg, enc) \
138 if ((enc)->outbuf != 0xff) { \
139 if ((creg) & 0x8000000) { \
140 if (++((enc)->outbuf) == 0xff) { \
141 (creg) &= 0x7ffffff; \
142 jpc_mqenc_byteout2(enc); \
143 enc->outbuf = ((creg) >> 20) & 0xff; \
147 jpc_mqenc_byteout2(enc); \
148 enc->outbuf = ((creg) >> 19) & 0xff; \
153 jpc_mqenc_byteout2(enc); \
154 (enc)->outbuf = ((creg) >> 19) & 0xff; \
159 jpc_mqenc_byteout2(enc); \
160 (enc)->outbuf = ((creg) >> 20) & 0xff; \
166 #define jpc_mqenc_byteout2(enc) \
168 if (enc->outbuf >= 0) { \
169 if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \
173 enc->lastbyte = enc->outbuf; \
176 /******************************************************************************\
177 * Local function protoypes.
178 \******************************************************************************/
180 static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc);
182 /******************************************************************************\
183 * Code for creation and destruction of encoder.
184 \******************************************************************************/
186 /* Create a MQ encoder. */
188 jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out)
192 /* Allocate memory for the MQ encoder. */
193 if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) {
197 mqenc->maxctxs = maxctxs;
199 /* Allocate memory for the per-context state information. */
200 if (!(mqenc->ctxs = jas_malloc(mqenc->maxctxs * sizeof(jpc_mqstate_t *)))) {
204 /* Set the current context to the first one. */
205 mqenc->curctx = mqenc->ctxs;
207 jpc_mqenc_init(mqenc);
209 /* Initialize the per-context state information to something sane. */
210 jpc_mqenc_setctxs(mqenc, 0, 0);
216 jpc_mqenc_destroy(mqenc);
221 /* Destroy a MQ encoder. */
223 void jpc_mqenc_destroy(jpc_mqenc_t *mqenc)
226 jas_free(mqenc->ctxs);
231 /******************************************************************************\
232 * State initialization code.
233 \******************************************************************************/
235 /* Initialize the coding state of a MQ encoder. */
237 void jpc_mqenc_init(jpc_mqenc_t *mqenc)
239 mqenc->areg = 0x8000;
243 mqenc->lastbyte = -1;
247 /* Initialize one or more contexts. */
249 void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs)
255 n = JAS_MIN(mqenc->maxctxs, numctxs);
257 *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];
261 n = mqenc->maxctxs - numctxs;
263 *ctx = &jpc_mqstates[0];
269 /* Get the coding state for a MQ encoder. */
271 void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state)
273 state->areg = mqenc->areg;
274 state->creg = mqenc->creg;
275 state->ctreg = mqenc->ctreg;
276 state->lastbyte = mqenc->lastbyte;
279 /******************************************************************************\
280 * Code for coding symbols.
281 \******************************************************************************/
285 int jpc_mqenc_putbit_func(jpc_mqenc_t *mqenc, int bit)
287 const jpc_mqstate_t *state;
288 JAS_DBGLOG(100, ("jpc_mqenc_putbit(%p, %d)\n", mqenc, bit));
289 JPC_MQENC_CALL(100, jpc_mqenc_dump(mqenc, stderr));
291 state = *(mqenc->curctx);
293 if (state->mps == bit) {
294 /* Apply the CODEMPS algorithm as defined in the standard. */
295 mqenc->areg -= state->qeval;
296 if (!(mqenc->areg & 0x8000)) {
297 jpc_mqenc_codemps2(mqenc);
299 mqenc->creg += state->qeval;
302 /* Apply the CODELPS algorithm as defined in the standard. */
303 jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);
306 return jpc_mqenc_error(mqenc) ? (-1) : 0;
309 int jpc_mqenc_codemps2(jpc_mqenc_t *mqenc)
311 /* Note: This function only performs part of the work associated with
312 the CODEMPS algorithm from the standard. Some of the work is also
313 performed by the caller. */
315 jpc_mqstate_t *state = *(mqenc->curctx);
316 if (mqenc->areg < state->qeval) {
317 mqenc->areg = state->qeval;
319 mqenc->creg += state->qeval;
321 *mqenc->curctx = state->nmps;
322 jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
323 return jpc_mqenc_error(mqenc) ? (-1) : 0;
326 int jpc_mqenc_codelps(jpc_mqenc_t *mqenc)
328 jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);
329 return jpc_mqenc_error(mqenc) ? (-1) : 0;
332 /******************************************************************************\
333 * Miscellaneous code.
334 \******************************************************************************/
336 /* Terminate the code word. */
338 int jpc_mqenc_flush(jpc_mqenc_t *mqenc, int termmode)
343 case JPC_MQENC_PTERM:
344 k = 11 - mqenc->ctreg + 1;
346 mqenc->creg <<= mqenc->ctreg;
348 jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg,
352 if (mqenc->outbuf != 0xff) {
353 jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
356 case JPC_MQENC_DEFTERM:
357 jpc_mqenc_setbits(mqenc);
358 mqenc->creg <<= mqenc->ctreg;
359 jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
360 mqenc->creg <<= mqenc->ctreg;
361 jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
362 if (mqenc->outbuf != 0xff) {
363 jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
373 static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc)
375 uint_fast32_t tmp = mqenc->creg + mqenc->areg;
376 mqenc->creg |= 0xffff;
377 if (mqenc->creg >= tmp) {
378 mqenc->creg -= 0x8000;
382 /* Dump a MQ encoder to a stream for debugging. */
384 int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out)
386 fprintf(out, "AREG = %08x, CREG = %08x, CTREG = %d\n",
387 mqenc->areg, mqenc->creg, mqenc->ctreg);
388 fprintf(out, "IND = %02d, MPS = %d, QEVAL = %04x\n",
389 *mqenc->curctx - jpc_mqstates, (*mqenc->curctx)->mps,
390 (*mqenc->curctx)->qeval);