jpc_mqdec.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * MQ Arithmetic Decoder
  64. *
  65. * $Id: jpc_mqdec.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <assert.h>
  71. #include <stdlib.h>
  72. #include <stdarg.h>
  73. #include "jasper/jas_types.h"
  74. #include "jasper/jas_malloc.h"
  75. #include "jasper/jas_math.h"
  76. #include "jasper/jas_debug.h"
  77. #include "jpc_mqdec.h"
  78. /******************************************************************************\
  79. * Local macros.
  80. \******************************************************************************/
  81. #if defined(DEBUG)
  82. #define MQDEC_CALL(n, x) \
  83. ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))
  84. #else
  85. #define MQDEC_CALL(n, x)
  86. #endif
  87. /******************************************************************************\
  88. * Local function prototypes.
  89. \******************************************************************************/
  90. static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec);
  91. /******************************************************************************\
  92. * Code for creation and destruction of a MQ decoder.
  93. \******************************************************************************/
  94. /* Create a MQ decoder. */
  95. jpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in)
  96. {
  97. jpc_mqdec_t *mqdec;
  98. /* There must be at least one context. */
  99. assert(maxctxs > 0);
  100. /* Allocate memory for the MQ decoder. */
  101. if (!(mqdec = jas_malloc(sizeof(jpc_mqdec_t)))) {
  102. goto error;
  103. }
  104. mqdec->in = in;
  105. mqdec->maxctxs = maxctxs;
  106. /* Allocate memory for the per-context state information. */
  107. if (!(mqdec->ctxs = jas_alloc2(mqdec->maxctxs, sizeof(jpc_mqstate_t *)))) {
  108. goto error;
  109. }
  110. /* Set the current context to the first context. */
  111. mqdec->curctx = mqdec->ctxs;
  112. /* If an input stream has been associated with the MQ decoder,
  113. initialize the decoder state from the stream. */
  114. if (mqdec->in) {
  115. jpc_mqdec_init(mqdec);
  116. }
  117. /* Initialize the per-context state information. */
  118. jpc_mqdec_setctxs(mqdec, 0, 0);
  119. return mqdec;
  120. error:
  121. /* Oops... Something has gone wrong. */
  122. if (mqdec) {
  123. jpc_mqdec_destroy(mqdec);
  124. }
  125. return 0;
  126. }
  127. /* Destroy a MQ decoder. */
  128. void jpc_mqdec_destroy(jpc_mqdec_t *mqdec)
  129. {
  130. if (mqdec->ctxs) {
  131. jas_free(mqdec->ctxs);
  132. }
  133. jas_free(mqdec);
  134. }
  135. /******************************************************************************\
  136. * Code for initialization of a MQ decoder.
  137. \******************************************************************************/
  138. /* Initialize the state of a MQ decoder. */
  139. void jpc_mqdec_init(jpc_mqdec_t *mqdec)
  140. {
  141. int c;
  142. mqdec->eof = 0;
  143. mqdec->creg = 0;
  144. /* Get the next byte from the input stream. */
  145. if ((c = jas_stream_getc(mqdec->in)) == EOF) {
  146. /* We have encountered an I/O error or EOF. */
  147. c = 0xff;
  148. mqdec->eof = 1;
  149. }
  150. mqdec->inbuffer = c;
  151. mqdec->creg += mqdec->inbuffer << 16;
  152. jpc_mqdec_bytein(mqdec);
  153. mqdec->creg <<= 7;
  154. mqdec->ctreg -= 7;
  155. mqdec->areg = 0x8000;
  156. }
  157. /* Set the input stream for a MQ decoder. */
  158. void jpc_mqdec_setinput(jpc_mqdec_t *mqdec, jas_stream_t *in)
  159. {
  160. mqdec->in = in;
  161. }
  162. /* Initialize one or more contexts. */
  163. void jpc_mqdec_setctxs(jpc_mqdec_t *mqdec, int numctxs, jpc_mqctx_t *ctxs)
  164. {
  165. jpc_mqstate_t **ctx;
  166. int n;
  167. ctx = mqdec->ctxs;
  168. n = JAS_MIN(mqdec->maxctxs, numctxs);
  169. while (--n >= 0) {
  170. *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];
  171. ++ctx;
  172. ++ctxs;
  173. }
  174. n = mqdec->maxctxs - numctxs;
  175. while (--n >= 0) {
  176. *ctx = &jpc_mqstates[0];
  177. ++ctx;
  178. }
  179. }
  180. /* Initialize a context. */
  181. void jpc_mqdec_setctx(jpc_mqdec_t *mqdec, int ctxno, jpc_mqctx_t *ctx)
  182. {
  183. jpc_mqstate_t **ctxi;
  184. ctxi = &mqdec->ctxs[ctxno];
  185. *ctxi = &jpc_mqstates[2 * ctx->ind + ctx->mps];
  186. }
  187. /******************************************************************************\
  188. * Code for decoding a bit.
  189. \******************************************************************************/
  190. /* Decode a bit. */
  191. int jpc_mqdec_getbit_func(register jpc_mqdec_t *mqdec)
  192. {
  193. int bit;
  194. JAS_DBGLOG(100, ("jpc_mqdec_getbit_func(%p)\n", mqdec));
  195. MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr));
  196. bit = jpc_mqdec_getbit_macro(mqdec);
  197. MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr));
  198. JAS_DBGLOG(100, ("ctx = %d, decoded %d\n", mqdec->curctx -
  199. mqdec->ctxs, bit));
  200. return bit;
  201. }
  202. /* Apply MPS_EXCHANGE algorithm (with RENORMD). */
  203. int jpc_mqdec_mpsexchrenormd(register jpc_mqdec_t *mqdec)
  204. {
  205. int ret;
  206. register jpc_mqstate_t *state = *mqdec->curctx;
  207. jpc_mqdec_mpsexchange(mqdec->areg, state->qeval, mqdec->curctx, ret);
  208. jpc_mqdec_renormd(mqdec->areg, mqdec->creg, mqdec->ctreg, mqdec->in,
  209. mqdec->eof, mqdec->inbuffer);
  210. return ret;
  211. }
  212. /* Apply LPS_EXCHANGE algorithm (with RENORMD). */
  213. int jpc_mqdec_lpsexchrenormd(register jpc_mqdec_t *mqdec)
  214. {
  215. int ret;
  216. register jpc_mqstate_t *state = *mqdec->curctx;
  217. jpc_mqdec_lpsexchange(mqdec->areg, state->qeval, mqdec->curctx, ret);
  218. jpc_mqdec_renormd(mqdec->areg, mqdec->creg, mqdec->ctreg, mqdec->in,
  219. mqdec->eof, mqdec->inbuffer);
  220. return ret;
  221. }
  222. /******************************************************************************\
  223. * Support code.
  224. \******************************************************************************/
  225. /* Apply the BYTEIN algorithm. */
  226. static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec)
  227. {
  228. int c;
  229. unsigned char prevbuf;
  230. if (!mqdec->eof) {
  231. if ((c = jas_stream_getc(mqdec->in)) == EOF) {
  232. mqdec->eof = 1;
  233. c = 0xff;
  234. }
  235. prevbuf = mqdec->inbuffer;
  236. mqdec->inbuffer = c;
  237. if (prevbuf == 0xff) {
  238. if (c > 0x8f) {
  239. mqdec->creg += 0xff00;
  240. mqdec->ctreg = 8;
  241. } else {
  242. mqdec->creg += c << 9;
  243. mqdec->ctreg = 7;
  244. }
  245. } else {
  246. mqdec->creg += c << 8;
  247. mqdec->ctreg = 8;
  248. }
  249. } else {
  250. mqdec->creg += 0xff00;
  251. mqdec->ctreg = 8;
  252. }
  253. }
  254. /******************************************************************************\
  255. * Code for debugging.
  256. \******************************************************************************/
  257. /* Dump a MQ decoder to a stream for debugging. */
  258. void jpc_mqdec_dump(jpc_mqdec_t *mqdec, FILE *out)
  259. {
  260. fprintf(out, "MQDEC A = %08lx, C = %08lx, CT=%08lx, ",
  261. (unsigned long) mqdec->areg, (unsigned long) mqdec->creg,
  262. (unsigned long) mqdec->ctreg);
  263. fprintf(out, "CTX = %d, ", (int)(mqdec->curctx - mqdec->ctxs));
  264. fprintf(out, "IND %d, MPS %d, QEVAL %x\n", (int)(*mqdec->curctx -
  265. jpc_mqstates), (int)(*mqdec->curctx)->mps, (int)(*mqdec->curctx)->qeval);
  266. }