mqc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #ifndef OPJ_MQC_H
  39. #define OPJ_MQC_H
  40. #include "opj_common.h"
  41. /**
  42. @file mqc.h
  43. @brief Implementation of an MQ-Coder (MQC)
  44. The functions in MQC.C have for goal to realize the MQ-coder operations. The functions
  45. in MQC.C are used by some function in T1.C.
  46. */
  47. /** @defgroup MQC MQC - Implementation of an MQ-Coder */
  48. /*@{*/
  49. /**
  50. This struct defines the state of a context.
  51. */
  52. typedef struct opj_mqc_state {
  53. /** the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
  54. OPJ_UINT32 qeval;
  55. /** the Most Probable Symbol (0 or 1) */
  56. OPJ_UINT32 mps;
  57. /** next state if the next encoded symbol is the MPS */
  58. const struct opj_mqc_state *nmps;
  59. /** next state if the next encoded symbol is the LPS */
  60. const struct opj_mqc_state *nlps;
  61. } opj_mqc_state_t;
  62. #define MQC_NUMCTXS 19
  63. /**
  64. MQ coder
  65. */
  66. typedef struct opj_mqc {
  67. /** temporary buffer where bits are coded or decoded */
  68. OPJ_UINT32 c;
  69. /** only used by MQ decoder */
  70. OPJ_UINT32 a;
  71. /** number of bits already read or free to write */
  72. OPJ_UINT32 ct;
  73. /* only used by decoder, to count the number of times a terminating 0xFF >0x8F marker is read */
  74. OPJ_UINT32 end_of_byte_stream_counter;
  75. /** pointer to the current position in the buffer */
  76. OPJ_BYTE *bp;
  77. /** pointer to the start of the buffer */
  78. OPJ_BYTE *start;
  79. /** pointer to the end of the buffer */
  80. OPJ_BYTE *end;
  81. /** Array of contexts */
  82. const opj_mqc_state_t *ctxs[MQC_NUMCTXS];
  83. /** Active context */
  84. const opj_mqc_state_t **curctx;
  85. /* lut_ctxno_zc shifted by (1 << 9) * bandno */
  86. const OPJ_BYTE* lut_ctxno_zc_orient;
  87. /** Original value of the 2 bytes at end[0] and end[1] */
  88. OPJ_BYTE backup[OPJ_COMMON_CBLK_DATA_EXTRA];
  89. } opj_mqc_t;
  90. #define BYPASS_CT_INIT 0xDEADBEEF
  91. #include "mqc_inl.h"
  92. /** @name Exported functions */
  93. /*@{*/
  94. /* ----------------------------------------------------------------------- */
  95. /**
  96. Return the number of bytes written/read since initialisation
  97. @param mqc MQC handle
  98. @return Returns the number of bytes already encoded
  99. */
  100. OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc);
  101. /**
  102. Reset the states of all the context of the coder/decoder
  103. (each context is set to a state where 0 and 1 are more or less equiprobable)
  104. @param mqc MQC handle
  105. */
  106. void opj_mqc_resetstates(opj_mqc_t *mqc);
  107. /**
  108. Set the state of a particular context
  109. @param mqc MQC handle
  110. @param ctxno Number that identifies the context
  111. @param msb The MSB of the new state of the context
  112. @param prob Number that identifies the probability of the symbols for the new state of the context
  113. */
  114. void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb,
  115. OPJ_INT32 prob);
  116. /**
  117. Initialize the encoder
  118. @param mqc MQC handle
  119. @param bp Pointer to the start of the buffer where the bytes will be written
  120. */
  121. void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp);
  122. /**
  123. Set the current context used for coding/decoding
  124. @param mqc MQC handle
  125. @param ctxno Number that identifies the context
  126. */
  127. #define opj_mqc_setcurctx(mqc, ctxno) (mqc)->curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
  128. /**
  129. Flush the encoder, so that all remaining data is written
  130. @param mqc MQC handle
  131. */
  132. void opj_mqc_flush(opj_mqc_t *mqc);
  133. /**
  134. BYPASS mode switch, initialization operation.
  135. JPEG 2000 p 505.
  136. @param mqc MQC handle
  137. */
  138. void opj_mqc_bypass_init_enc(opj_mqc_t *mqc);
  139. /** Return number of extra bytes to add to opj_mqc_numbytes() for the²
  140. size of a non-terminating BYPASS pass
  141. @param mqc MQC handle
  142. @param erterm 1 if ERTERM is enabled, 0 otherwise
  143. */
  144. OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm);
  145. /**
  146. BYPASS mode switch, coding operation.
  147. JPEG 2000 p 505.
  148. @param mqc MQC handle
  149. @param d The symbol to be encoded (0 or 1)
  150. */
  151. void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d);
  152. /**
  153. BYPASS mode switch, flush operation
  154. @param mqc MQC handle
  155. @param erterm 1 if ERTERM is enabled, 0 otherwise
  156. */
  157. void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm);
  158. /**
  159. RESET mode switch
  160. @param mqc MQC handle
  161. */
  162. void opj_mqc_reset_enc(opj_mqc_t *mqc);
  163. #ifdef notdef
  164. /**
  165. RESTART mode switch (TERMALL)
  166. @param mqc MQC handle
  167. @return Returns 1 (always)
  168. */
  169. OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc);
  170. #endif
  171. /**
  172. RESTART mode switch (TERMALL) reinitialisation
  173. @param mqc MQC handle
  174. */
  175. void opj_mqc_restart_init_enc(opj_mqc_t *mqc);
  176. /**
  177. ERTERM mode switch (PTERM)
  178. @param mqc MQC handle
  179. */
  180. void opj_mqc_erterm_enc(opj_mqc_t *mqc);
  181. /**
  182. SEGMARK mode switch (SEGSYM)
  183. @param mqc MQC handle
  184. */
  185. void opj_mqc_segmark_enc(opj_mqc_t *mqc);
  186. /**
  187. Initialize the decoder for MQ decoding.
  188. opj_mqc_finish_dec() must be absolutely called after finishing the decoding
  189. passes, so as to restore the bytes temporarily overwritten.
  190. @param mqc MQC handle
  191. @param bp Pointer to the start of the buffer from which the bytes will be read
  192. Note that OPJ_COMMON_CBLK_DATA_EXTRA bytes at the end of the buffer
  193. will be temporarily overwritten with an artificial 0xFF 0xFF marker.
  194. (they will be backuped in the mqc structure to be restored later)
  195. So bp must be at least len + OPJ_COMMON_CBLK_DATA_EXTRA large, and
  196. writable.
  197. @param len Length of the input buffer
  198. @param extra_writable_bytes Indicate how many bytes after len are writable.
  199. This is to indicate your consent that bp must be
  200. large enough.
  201. */
  202. void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
  203. OPJ_UINT32 extra_writable_bytes);
  204. /**
  205. Initialize the decoder for RAW decoding.
  206. opj_mqc_finish_dec() must be absolutely called after finishing the decoding
  207. passes, so as to restore the bytes temporarily overwritten.
  208. @param mqc MQC handle
  209. @param bp Pointer to the start of the buffer from which the bytes will be read
  210. Note that OPJ_COMMON_CBLK_DATA_EXTRA bytes at the end of the buffer
  211. will be temporarily overwritten with an artificial 0xFF 0xFF marker.
  212. (they will be backuped in the mqc structure to be restored later)
  213. So bp must be at least len + OPJ_COMMON_CBLK_DATA_EXTRA large, and
  214. writable.
  215. @param len Length of the input buffer
  216. @param extra_writable_bytes Indicate how many bytes after len are writable.
  217. This is to indicate your consent that bp must be
  218. large enough.
  219. */
  220. void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
  221. OPJ_UINT32 extra_writable_bytes);
  222. /**
  223. Terminate RAW/MQC decoding
  224. This restores the bytes temporarily overwritten by opj_mqc_init_dec()/
  225. opj_mqc_raw_init_dec()
  226. @param mqc MQC handle
  227. */
  228. void opq_mqc_finish_dec(opj_mqc_t *mqc);
  229. /**
  230. Decode a symbol
  231. @param mqc MQC handle
  232. @return Returns the decoded symbol (0 or 1)
  233. */
  234. /*static INLINE OPJ_UINT32 opj_mqc_decode(opj_mqc_t * const mqc);*/
  235. /* ----------------------------------------------------------------------- */
  236. /*@}*/
  237. /*@}*/
  238. #endif /* OPJ_MQC_H */