jpc_mqenc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 Encoder
  64. *
  65. * $Id: jpc_mqenc.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 "jasper/jas_stream.h"
  73. #include "jasper/jas_malloc.h"
  74. #include "jasper/jas_math.h"
  75. #include "jasper/jas_debug.h"
  76. #include "jpc_mqenc.h"
  77. /******************************************************************************\
  78. * Macros
  79. \******************************************************************************/
  80. #if defined(DEBUG)
  81. #define JPC_MQENC_CALL(n, x) \
  82. ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0))
  83. #else
  84. #define JPC_MQENC_CALL(n, x)
  85. #endif
  86. #define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \
  87. { \
  88. jpc_mqstate_t *state = *(curctx); \
  89. (areg) -= state->qeval; \
  90. if (!((areg) & 0x8000)) { \
  91. if ((areg) < state->qeval) { \
  92. (areg) = state->qeval; \
  93. } else { \
  94. (creg) += state->qeval; \
  95. } \
  96. *(curctx) = state->nmps; \
  97. jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \
  98. } else { \
  99. (creg) += state->qeval; \
  100. } \
  101. }
  102. #define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \
  103. { \
  104. jpc_mqstate_t *state = *(curctx); \
  105. (areg) -= state->qeval; \
  106. if ((areg) < state->qeval) { \
  107. (creg) += state->qeval; \
  108. } else { \
  109. (areg) = state->qeval; \
  110. } \
  111. *(curctx) = state->nlps; \
  112. jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \
  113. }
  114. #define jpc_mqenc_renorme(areg, creg, ctreg, enc) \
  115. { \
  116. do { \
  117. (areg) <<= 1; \
  118. (creg) <<= 1; \
  119. if (!--(ctreg)) { \
  120. jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \
  121. } \
  122. } while (!((areg) & 0x8000)); \
  123. }
  124. #define jpc_mqenc_byteout(areg, creg, ctreg, enc) \
  125. { \
  126. if ((enc)->outbuf != 0xff) { \
  127. if ((creg) & 0x8000000) { \
  128. if (++((enc)->outbuf) == 0xff) { \
  129. (creg) &= 0x7ffffff; \
  130. jpc_mqenc_byteout2(enc); \
  131. enc->outbuf = ((creg) >> 20) & 0xff; \
  132. (creg) &= 0xfffff; \
  133. (ctreg) = 7; \
  134. } else { \
  135. jpc_mqenc_byteout2(enc); \
  136. enc->outbuf = ((creg) >> 19) & 0xff; \
  137. (creg) &= 0x7ffff; \
  138. (ctreg) = 8; \
  139. } \
  140. } else { \
  141. jpc_mqenc_byteout2(enc); \
  142. (enc)->outbuf = ((creg) >> 19) & 0xff; \
  143. (creg) &= 0x7ffff; \
  144. (ctreg) = 8; \
  145. } \
  146. } else { \
  147. jpc_mqenc_byteout2(enc); \
  148. (enc)->outbuf = ((creg) >> 20) & 0xff; \
  149. (creg) &= 0xfffff; \
  150. (ctreg) = 7; \
  151. } \
  152. }
  153. #define jpc_mqenc_byteout2(enc) \
  154. { \
  155. if (enc->outbuf >= 0) { \
  156. if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \
  157. enc->err |= 1; \
  158. } \
  159. } \
  160. enc->lastbyte = enc->outbuf; \
  161. }
  162. /******************************************************************************\
  163. * Local function protoypes.
  164. \******************************************************************************/
  165. static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc);
  166. /******************************************************************************\
  167. * Code for creation and destruction of encoder.
  168. \******************************************************************************/
  169. /* Create a MQ encoder. */
  170. jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out)
  171. {
  172. jpc_mqenc_t *mqenc;
  173. /* Allocate memory for the MQ encoder. */
  174. if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) {
  175. goto error;
  176. }
  177. mqenc->out = out;
  178. mqenc->maxctxs = maxctxs;
  179. /* Allocate memory for the per-context state information. */
  180. if (!(mqenc->ctxs = jas_alloc2(mqenc->maxctxs, sizeof(jpc_mqstate_t *)))) {
  181. goto error;
  182. }
  183. /* Set the current context to the first one. */
  184. mqenc->curctx = mqenc->ctxs;
  185. jpc_mqenc_init(mqenc);
  186. /* Initialize the per-context state information to something sane. */
  187. jpc_mqenc_setctxs(mqenc, 0, 0);
  188. return mqenc;
  189. error:
  190. if (mqenc) {
  191. jpc_mqenc_destroy(mqenc);
  192. }
  193. return 0;
  194. }
  195. /* Destroy a MQ encoder. */
  196. void jpc_mqenc_destroy(jpc_mqenc_t *mqenc)
  197. {
  198. if (mqenc->ctxs) {
  199. jas_free(mqenc->ctxs);
  200. }
  201. jas_free(mqenc);
  202. }
  203. /******************************************************************************\
  204. * State initialization code.
  205. \******************************************************************************/
  206. /* Initialize the coding state of a MQ encoder. */
  207. void jpc_mqenc_init(jpc_mqenc_t *mqenc)
  208. {
  209. mqenc->areg = 0x8000;
  210. mqenc->outbuf = -1;
  211. mqenc->creg = 0;
  212. mqenc->ctreg = 12;
  213. mqenc->lastbyte = -1;
  214. mqenc->err = 0;
  215. }
  216. /* Initialize one or more contexts. */
  217. void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs)
  218. {
  219. jpc_mqstate_t **ctx;
  220. int n;
  221. ctx = mqenc->ctxs;
  222. n = JAS_MIN(mqenc->maxctxs, numctxs);
  223. while (--n >= 0) {
  224. *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps];
  225. ++ctx;
  226. ++ctxs;
  227. }
  228. n = mqenc->maxctxs - numctxs;
  229. while (--n >= 0) {
  230. *ctx = &jpc_mqstates[0];
  231. ++ctx;
  232. }
  233. }
  234. /* Get the coding state for a MQ encoder. */
  235. void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state)
  236. {
  237. state->areg = mqenc->areg;
  238. state->creg = mqenc->creg;
  239. state->ctreg = mqenc->ctreg;
  240. state->lastbyte = mqenc->lastbyte;
  241. }
  242. /******************************************************************************\
  243. * Code for coding symbols.
  244. \******************************************************************************/
  245. /* Encode a bit. */
  246. int jpc_mqenc_putbit_func(jpc_mqenc_t *mqenc, int bit)
  247. {
  248. const jpc_mqstate_t *state;
  249. JAS_DBGLOG(100, ("jpc_mqenc_putbit(%p, %d)\n", mqenc, bit));
  250. JPC_MQENC_CALL(100, jpc_mqenc_dump(mqenc, stderr));
  251. state = *(mqenc->curctx);
  252. if (state->mps == bit) {
  253. /* Apply the CODEMPS algorithm as defined in the standard. */
  254. mqenc->areg -= state->qeval;
  255. if (!(mqenc->areg & 0x8000)) {
  256. jpc_mqenc_codemps2(mqenc);
  257. } else {
  258. mqenc->creg += state->qeval;
  259. }
  260. } else {
  261. /* Apply the CODELPS algorithm as defined in the standard. */
  262. jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);
  263. }
  264. return jpc_mqenc_error(mqenc) ? (-1) : 0;
  265. }
  266. int jpc_mqenc_codemps2(jpc_mqenc_t *mqenc)
  267. {
  268. /* Note: This function only performs part of the work associated with
  269. the CODEMPS algorithm from the standard. Some of the work is also
  270. performed by the caller. */
  271. jpc_mqstate_t *state = *(mqenc->curctx);
  272. if (mqenc->areg < state->qeval) {
  273. mqenc->areg = state->qeval;
  274. } else {
  275. mqenc->creg += state->qeval;
  276. }
  277. *mqenc->curctx = state->nmps;
  278. jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
  279. return jpc_mqenc_error(mqenc) ? (-1) : 0;
  280. }
  281. int jpc_mqenc_codelps(jpc_mqenc_t *mqenc)
  282. {
  283. jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc);
  284. return jpc_mqenc_error(mqenc) ? (-1) : 0;
  285. }
  286. /******************************************************************************\
  287. * Miscellaneous code.
  288. \******************************************************************************/
  289. /* Terminate the code word. */
  290. int jpc_mqenc_flush(jpc_mqenc_t *mqenc, int termmode)
  291. {
  292. int_fast16_t k;
  293. switch (termmode) {
  294. case JPC_MQENC_PTERM:
  295. k = 11 - mqenc->ctreg + 1;
  296. while (k > 0) {
  297. mqenc->creg <<= mqenc->ctreg;
  298. mqenc->ctreg = 0;
  299. jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg,
  300. mqenc);
  301. k -= mqenc->ctreg;
  302. }
  303. if (mqenc->outbuf != 0xff) {
  304. jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
  305. }
  306. break;
  307. case JPC_MQENC_DEFTERM:
  308. jpc_mqenc_setbits(mqenc);
  309. mqenc->creg <<= mqenc->ctreg;
  310. jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
  311. mqenc->creg <<= mqenc->ctreg;
  312. jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
  313. if (mqenc->outbuf != 0xff) {
  314. jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc);
  315. }
  316. break;
  317. default:
  318. abort();
  319. break;
  320. }
  321. return 0;
  322. }
  323. static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc)
  324. {
  325. uint_fast32_t tmp = mqenc->creg + mqenc->areg;
  326. mqenc->creg |= 0xffff;
  327. if (mqenc->creg >= tmp) {
  328. mqenc->creg -= 0x8000;
  329. }
  330. }
  331. /* Dump a MQ encoder to a stream for debugging. */
  332. int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out)
  333. {
  334. fprintf(out, "AREG = %08x, CREG = %08x, CTREG = %d\n",
  335. (unsigned)mqenc->areg, (unsigned)mqenc->creg, (int)mqenc->ctreg);
  336. fprintf(out, "IND = %02d, MPS = %d, QEVAL = %04x\n",
  337. (int)(*mqenc->curctx - jpc_mqstates), (int)(*mqenc->curctx)->mps,
  338. (int)(*mqenc->curctx)->qeval);
  339. return 0;
  340. }