Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / libjasper / jpc_mqenc.c
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 Encoder
66  *
67  * $Id: jpc_mqenc.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
68  */
69
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73
74 #include <assert.h>
75 #include <stdlib.h>
76
77 #include "jasper/jas_stream.h"
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_math.h"
80 #include "jasper/jas_debug.h"
81
82 #include "jpc_mqenc.h"
83
84 /******************************************************************************\
85 * Macros
86 \******************************************************************************/
87
88 #if defined(DEBUG)
89 #define JPC_MQENC_CALL(n, x) \
90         ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))
91 #else
92 #define JPC_MQENC_CALL(n, x)
93 #endif
94
95 #define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \
96 { \
97         jpc_mqstate_t *state = *(curctx); \
98         (areg) -= state->qeval; \
99         if (!((areg) & 0x8000)) { \
100                 if ((areg) < state->qeval) { \
101                         (areg) = state->qeval; \
102                 } else { \
103                         (creg) += state->qeval; \
104                 } \
105                 *(curctx) = state->nmps; \
106                 jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \
107         } else { \
108                 (creg) += state->qeval; \
109         } \
110 }
111
112 #define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \
113 { \
114         jpc_mqstate_t *state = *(curctx); \
115         (areg) -= state->qeval; \
116         if ((areg) < state->qeval) { \
117                 (creg) += state->qeval; \
118         } else { \
119                 (areg) = state->qeval; \
120         } \
121         *(curctx) = state->nlps; \
122         jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \
123 }
124
125 #define jpc_mqenc_renorme(areg, creg, ctreg, enc) \
126 { \
127         do { \
128                 (areg) <<= 1; \
129                 (creg) <<= 1; \
130                 if (!--(ctreg)) { \
131                         jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \
132                 } \
133         } while (!((areg) & 0x8000)); \
134 }
135
136 #define jpc_mqenc_byteout(areg, creg, ctreg, enc) \
137 { \
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; \
144                                 (creg) &= 0xfffff; \
145                                 (ctreg) = 7; \
146                         } else { \
147                                 jpc_mqenc_byteout2(enc); \
148                                 enc->outbuf = ((creg) >> 19) & 0xff; \
149                                 (creg) &= 0x7ffff; \
150                                 (ctreg) = 8; \
151                         } \
152                 } else { \
153                         jpc_mqenc_byteout2(enc); \
154                         (enc)->outbuf = ((creg) >> 19) & 0xff; \
155                         (creg) &= 0x7ffff; \
156                         (ctreg) = 8; \
157                 } \
158         } else { \
159                 jpc_mqenc_byteout2(enc); \
160                 (enc)->outbuf = ((creg) >> 20) & 0xff; \
161                 (creg) &= 0xfffff; \
162                 (ctreg) = 7; \
163         } \
164 }
165
166 #define jpc_mqenc_byteout2(enc) \
167 { \
168         if (enc->outbuf >= 0) { \
169                 if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \
170                         enc->err |= 1; \
171                 } \
172         } \
173         enc->lastbyte = enc->outbuf; \
174 }
175
176 /******************************************************************************\
177 * Local function protoypes.
178 \******************************************************************************/
179
180 static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc);
181
182 /******************************************************************************\
183 * Code for creation and destruction of encoder.
184 \******************************************************************************/
185
186 /* Create a MQ encoder. */
187
188 jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out)
189 {
190         jpc_mqenc_t *mqenc;
191
192         /* Allocate memory for the MQ encoder. */
193         if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) {
194                 goto error;
195         }
196         mqenc->out = out;
197         mqenc->maxctxs = maxctxs;
198
199         /* Allocate memory for the per-context state information. */
200         if (!(mqenc->ctxs = jas_malloc(mqenc->maxctxs * sizeof(jpc_mqstate_t *)))) {
201                 goto error;
202         }
203
204         /* Set the current context to the first one. */
205         mqenc->curctx = mqenc->ctxs;
206
207         jpc_mqenc_init(mqenc);
208
209         /* Initialize the per-context state information to something sane. */
210         jpc_mqenc_setctxs(mqenc, 0, 0);
211
212         return mqenc;
213
214 error:
215         if (mqenc) {
216                 jpc_mqenc_destroy(mqenc);
217         }
218         return 0;
219 }
220
221 /* Destroy a MQ encoder. */
222
223 void jpc_mqenc_destroy(jpc_mqenc_t *mqenc)
224 {
225         if (mqenc->ctxs) {
226                 jas_free(mqenc->ctxs);
227         }
228         jas_free(mqenc);
229 }
230
231 /******************************************************************************\
232 * State initialization code.
233 \******************************************************************************/
234
235 /* Initialize the coding state of a MQ encoder. */
236
237 void jpc_mqenc_init(jpc_mqenc_t *mqenc)
238 {
239         mqenc->areg = 0x8000;
240         mqenc->outbuf = -1;
241         mqenc->creg = 0;
242         mqenc->ctreg = 12;
243         mqenc->lastbyte = -1;
244         mqenc->err = 0;
245 }
246
247 /* Initialize one or more contexts. */
248
249 void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs)
250 {
251         jpc_mqstate_t **ctx;
252         int n;
253
254         ctx = mqenc->ctxs;
255         n = JAS_MIN(mqenc->maxctxs, numctxs);
256         while (--n >= 0) {
257                 *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];
258                 ++ctx;
259                 ++ctxs;
260         }
261         n = mqenc->maxctxs - numctxs;
262         while (--n >= 0) {
263                 *ctx = &jpc_mqstates[0];
264                 ++ctx;
265         }
266
267 }
268
269 /* Get the coding state for a MQ encoder. */
270
271 void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state)
272 {
273         state->areg = mqenc->areg;
274         state->creg = mqenc->creg;
275         state->ctreg = mqenc->ctreg;
276         state->lastbyte = mqenc->lastbyte;
277 }
278
279 /******************************************************************************\
280 * Code for coding symbols.
281 \******************************************************************************/
282
283 /* Encode a bit. */
284
285 int jpc_mqenc_putbit_func(jpc_mqenc_t *mqenc, int bit)
286 {
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));
290
291         state = *(mqenc->curctx);
292
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);
298                 } else {
299                         mqenc->creg += state->qeval;
300                 }
301         } else {
302                 /* Apply the CODELPS algorithm as defined in the standard. */
303                 jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);
304         }
305
306         return jpc_mqenc_error(mqenc) ? (-1) : 0;
307 }
308
309 int jpc_mqenc_codemps2(jpc_mqenc_t *mqenc)
310 {
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. */
314
315         jpc_mqstate_t *state = *(mqenc->curctx);
316         if (mqenc->areg < state->qeval) {
317                 mqenc->areg = state->qeval;
318         } else {
319                 mqenc->creg += state->qeval;
320         }
321         *mqenc->curctx = state->nmps;
322         jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
323         return jpc_mqenc_error(mqenc) ? (-1) : 0;
324 }
325
326 int jpc_mqenc_codelps(jpc_mqenc_t *mqenc)
327 {
328         jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);
329         return jpc_mqenc_error(mqenc) ? (-1) : 0;
330 }
331
332 /******************************************************************************\
333 * Miscellaneous code.
334 \******************************************************************************/
335
336 /* Terminate the code word. */
337
338 int jpc_mqenc_flush(jpc_mqenc_t *mqenc, int termmode)
339 {
340         int_fast16_t k;
341
342         switch (termmode) {
343         case JPC_MQENC_PTERM:
344                 k = 11 - mqenc->ctreg + 1;
345                 while (k > 0) {
346                         mqenc->creg <<= mqenc->ctreg;
347                         mqenc->ctreg = 0;
348                         jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg,
349                           mqenc);
350                         k -= mqenc->ctreg;
351                 }
352                 if (mqenc->outbuf != 0xff) {
353                         jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
354                 }
355                 break;
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);
364                 }
365                 break;
366         default:
367                 abort();
368                 break;
369         }
370         return 0;
371 }
372
373 static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc)
374 {
375         uint_fast32_t tmp = mqenc->creg + mqenc->areg;
376         mqenc->creg |= 0xffff;
377         if (mqenc->creg >= tmp) {
378                 mqenc->creg -= 0x8000;
379         }
380 }
381
382 /* Dump a MQ encoder to a stream for debugging. */
383
384 int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out)
385 {
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);
391         return 0;
392 }