jpc_mqdec.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. #ifndef JPC_MQDEC_H
  68. #define JPC_MQDEC_H
  69. /******************************************************************************\
  70. * Includes.
  71. \******************************************************************************/
  72. #include "jasper/jas_types.h"
  73. #include "jasper/jas_stream.h"
  74. #include "jpc_mqcod.h"
  75. /******************************************************************************\
  76. * Types.
  77. \******************************************************************************/
  78. /* MQ arithmetic decoder. */
  79. typedef struct {
  80. /* The C register. */
  81. uint_fast32_t creg;
  82. /* The A register. */
  83. uint_fast32_t areg;
  84. /* The CT register. */
  85. uint_fast32_t ctreg;
  86. /* The current context. */
  87. jpc_mqstate_t **curctx;
  88. /* The per-context information. */
  89. jpc_mqstate_t **ctxs;
  90. /* The maximum number of contexts. */
  91. int maxctxs;
  92. /* The stream from which to read data. */
  93. jas_stream_t *in;
  94. /* The last character read. */
  95. uchar inbuffer;
  96. /* The EOF indicator. */
  97. int eof;
  98. } jpc_mqdec_t;
  99. /******************************************************************************\
  100. * Functions/macros for construction and destruction.
  101. \******************************************************************************/
  102. /* Create a MQ decoder. */
  103. jpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in);
  104. /* Destroy a MQ decoder. */
  105. void jpc_mqdec_destroy(jpc_mqdec_t *dec);
  106. /******************************************************************************\
  107. * Functions/macros for initialization.
  108. \******************************************************************************/
  109. /* Set the input stream associated with a MQ decoder. */
  110. void jpc_mqdec_setinput(jpc_mqdec_t *dec, jas_stream_t *in);
  111. /* Initialize a MQ decoder. */
  112. void jpc_mqdec_init(jpc_mqdec_t *dec);
  113. /******************************************************************************\
  114. * Functions/macros for manipulating contexts.
  115. \******************************************************************************/
  116. /* Set the current context for a MQ decoder. */
  117. #define jpc_mqdec_setcurctx(dec, ctxno) \
  118. ((mqdec)->curctx = &(mqdec)->ctxs[ctxno]);
  119. /* Set the state information for a particular context of a MQ decoder. */
  120. void jpc_mqdec_setctx(jpc_mqdec_t *dec, int ctxno, jpc_mqctx_t *ctx);
  121. /* Set the state information for all contexts of a MQ decoder. */
  122. void jpc_mqdec_setctxs(jpc_mqdec_t *dec, int numctxs, jpc_mqctx_t *ctxs);
  123. /******************************************************************************\
  124. * Functions/macros for decoding bits.
  125. \******************************************************************************/
  126. /* Decode a symbol. */
  127. #if !defined(DEBUG)
  128. #define jpc_mqdec_getbit(dec) \
  129. jpc_mqdec_getbit_macro(dec)
  130. #else
  131. #define jpc_mqdec_getbit(dec) \
  132. jpc_mqdec_getbit_func(dec)
  133. #endif
  134. /* Decode a symbol (assuming an unskewed probability distribution). */
  135. #if !defined(DEBUG)
  136. #define jpc_mqdec_getbitnoskew(dec) \
  137. jpc_mqdec_getbit_macro(dec)
  138. #else
  139. #define jpc_mqdec_getbitnoskew(dec) \
  140. jpc_mqdec_getbit_func(dec)
  141. #endif
  142. /******************************************************************************\
  143. * Functions/macros for debugging.
  144. \******************************************************************************/
  145. /* Dump the MQ decoder state for debugging. */
  146. void jpc_mqdec_dump(jpc_mqdec_t *dec, FILE *out);
  147. /******************************************************************************\
  148. * EVERYTHING BELOW THIS POINT IS IMPLEMENTATION SPECIFIC AND NOT PART OF THE
  149. * APPLICATION INTERFACE. DO NOT RELY ON ANY OF THE INTERNAL FUNCTIONS/MACROS
  150. * GIVEN BELOW.
  151. \******************************************************************************/
  152. #define jpc_mqdec_getbit_macro(dec) \
  153. ((((dec)->areg -= (*(dec)->curctx)->qeval), \
  154. (dec)->creg >> 16 >= (*(dec)->curctx)->qeval) ? \
  155. ((((dec)->creg -= (*(dec)->curctx)->qeval << 16), \
  156. (dec)->areg & 0x8000) ? (*(dec)->curctx)->mps : \
  157. jpc_mqdec_mpsexchrenormd(dec)) : \
  158. jpc_mqdec_lpsexchrenormd(dec))
  159. #define jpc_mqdec_mpsexchange(areg, delta, curctx, bit) \
  160. { \
  161. if ((areg) < (delta)) { \
  162. register jpc_mqstate_t *state = *(curctx); \
  163. /* LPS decoded. */ \
  164. (bit) = state->mps ^ 1; \
  165. *(curctx) = state->nlps; \
  166. } else { \
  167. register jpc_mqstate_t *state = *(curctx); \
  168. /* MPS decoded. */ \
  169. (bit) = state->mps; \
  170. *(curctx) = state->nmps; \
  171. } \
  172. }
  173. #define jpc_mqdec_lpsexchange(areg, delta, curctx, bit) \
  174. { \
  175. if ((areg) >= (delta)) { \
  176. register jpc_mqstate_t *state = *(curctx); \
  177. (areg) = (delta); \
  178. (bit) = state->mps ^ 1; \
  179. *(curctx) = state->nlps; \
  180. } else { \
  181. register jpc_mqstate_t *state = *(curctx); \
  182. (areg) = (delta); \
  183. (bit) = state->mps; \
  184. *(curctx) = state->nmps; \
  185. } \
  186. }
  187. #define jpc_mqdec_renormd(areg, creg, ctreg, in, eof, inbuf) \
  188. { \
  189. do { \
  190. if (!(ctreg)) { \
  191. jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf); \
  192. } \
  193. (areg) <<= 1; \
  194. (creg) <<= 1; \
  195. --(ctreg); \
  196. } while (!((areg) & 0x8000)); \
  197. }
  198. #define jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf) \
  199. { \
  200. int c; \
  201. unsigned char prevbuf; \
  202. if (!(eof)) { \
  203. if ((c = jas_stream_getc(in)) == EOF) { \
  204. (eof) = 1; \
  205. c = 0xff; \
  206. } \
  207. prevbuf = (inbuf); \
  208. (inbuf) = c; \
  209. if (prevbuf == 0xff) { \
  210. if (c > 0x8f) { \
  211. (creg) += 0xff00; \
  212. (ctreg) = 8; \
  213. } else { \
  214. (creg) += c << 9; \
  215. (ctreg) = 7; \
  216. } \
  217. } else { \
  218. (creg) += c << 8; \
  219. (ctreg) = 8; \
  220. } \
  221. } else { \
  222. (creg) += 0xff00; \
  223. (ctreg) = 8; \
  224. } \
  225. }
  226. int jpc_mqdec_getbit_func(jpc_mqdec_t *dec);
  227. int jpc_mqdec_mpsexchrenormd(jpc_mqdec_t *dec);
  228. int jpc_mqdec_lpsexchrenormd(jpc_mqdec_t *dec);
  229. #endif