mqc_inl.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_INL_H
  39. #define OPJ_MQC_INL_H
  40. /* For internal use of opj_mqc_decode_macro() */
  41. #define opj_mqc_mpsexchange_macro(d, curctx, a) \
  42. { \
  43. if (a < (*curctx)->qeval) { \
  44. d = !((*curctx)->mps); \
  45. *curctx = (*curctx)->nlps; \
  46. } else { \
  47. d = (*curctx)->mps; \
  48. *curctx = (*curctx)->nmps; \
  49. } \
  50. }
  51. /* For internal use of opj_mqc_decode_macro() */
  52. #define opj_mqc_lpsexchange_macro(d, curctx, a) \
  53. { \
  54. if (a < (*curctx)->qeval) { \
  55. a = (*curctx)->qeval; \
  56. d = (*curctx)->mps; \
  57. *curctx = (*curctx)->nmps; \
  58. } else { \
  59. a = (*curctx)->qeval; \
  60. d = !((*curctx)->mps); \
  61. *curctx = (*curctx)->nlps; \
  62. } \
  63. }
  64. /**
  65. Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
  66. @param mqc MQC handle
  67. @return Returns the decoded symbol (0 or 1)
  68. */
  69. static INLINE OPJ_UINT32 opj_mqc_raw_decode(opj_mqc_t *mqc)
  70. {
  71. OPJ_UINT32 d;
  72. if (mqc->ct == 0) {
  73. /* Given opj_mqc_raw_init_dec() we know that at some point we will */
  74. /* have a 0xFF 0xFF artificial marker */
  75. if (mqc->c == 0xff) {
  76. if (*mqc->bp > 0x8f) {
  77. mqc->c = 0xff;
  78. mqc->ct = 8;
  79. } else {
  80. mqc->c = *mqc->bp;
  81. mqc->bp ++;
  82. mqc->ct = 7;
  83. }
  84. } else {
  85. mqc->c = *mqc->bp;
  86. mqc->bp ++;
  87. mqc->ct = 8;
  88. }
  89. }
  90. mqc->ct--;
  91. d = ((OPJ_UINT32)mqc->c >> mqc->ct) & 0x01U;
  92. return d;
  93. }
  94. #define opj_mqc_bytein_macro(mqc, c, ct) \
  95. { \
  96. OPJ_UINT32 l_c; \
  97. /* Given opj_mqc_init_dec() we know that at some point we will */ \
  98. /* have a 0xFF 0xFF artificial marker */ \
  99. l_c = *(mqc->bp + 1); \
  100. if (*mqc->bp == 0xff) { \
  101. if (l_c > 0x8f) { \
  102. c += 0xff00; \
  103. ct = 8; \
  104. mqc->end_of_byte_stream_counter ++; \
  105. } else { \
  106. mqc->bp++; \
  107. c += l_c << 9; \
  108. ct = 7; \
  109. } \
  110. } else { \
  111. mqc->bp++; \
  112. c += l_c << 8; \
  113. ct = 8; \
  114. } \
  115. }
  116. /* For internal use of opj_mqc_decode_macro() */
  117. #define opj_mqc_renormd_macro(mqc, a, c, ct) \
  118. { \
  119. do { \
  120. if (ct == 0) { \
  121. opj_mqc_bytein_macro(mqc, c, ct); \
  122. } \
  123. a <<= 1; \
  124. c <<= 1; \
  125. ct--; \
  126. } while (a < 0x8000); \
  127. }
  128. #define opj_mqc_decode_macro(d, mqc, curctx, a, c, ct) \
  129. { \
  130. /* Implements ISO 15444-1 C.3.2 Decoding a decision (DECODE) */ \
  131. /* Note: alternate "J.2 - Decoding an MPS or an LPS in the */ \
  132. /* software-conventions decoder" has been tried, but does not bring any */ \
  133. /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */ \
  134. a -= (*curctx)->qeval; \
  135. if ((c >> 16) < (*curctx)->qeval) { \
  136. opj_mqc_lpsexchange_macro(d, curctx, a); \
  137. opj_mqc_renormd_macro(mqc, a, c, ct); \
  138. } else { \
  139. c -= (*curctx)->qeval << 16; \
  140. if ((a & 0x8000) == 0) { \
  141. opj_mqc_mpsexchange_macro(d, curctx, a); \
  142. opj_mqc_renormd_macro(mqc, a, c, ct); \
  143. } else { \
  144. d = (*curctx)->mps; \
  145. } \
  146. } \
  147. }
  148. #define DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \
  149. register const opj_mqc_state_t **curctx = mqc->curctx; \
  150. register OPJ_UINT32 c = mqc->c; \
  151. register OPJ_UINT32 a = mqc->a; \
  152. register OPJ_UINT32 ct = mqc->ct
  153. #define UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \
  154. mqc->curctx = curctx; \
  155. mqc->c = c; \
  156. mqc->a = a; \
  157. mqc->ct = ct;
  158. /**
  159. Input a byte
  160. @param mqc MQC handle
  161. */
  162. static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
  163. {
  164. opj_mqc_bytein_macro(mqc, mqc->c, mqc->ct);
  165. }
  166. /**
  167. Renormalize mqc->a and mqc->c while decoding
  168. @param mqc MQC handle
  169. */
  170. #define opj_mqc_renormd(mqc) \
  171. opj_mqc_renormd_macro(mqc, mqc->a, mqc->c, mqc->ct)
  172. /**
  173. Decode a symbol
  174. @param d OPJ_UINT32 value where to store the decoded symbol
  175. @param mqc MQC handle
  176. @return Returns the decoded symbol (0 or 1) in d
  177. */
  178. #define opj_mqc_decode(d, mqc) \
  179. opj_mqc_decode_macro(d, mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct)
  180. /**
  181. Output a byte, doing bit-stuffing if necessary.
  182. After a 0xff byte, the next byte must be smaller than 0x90.
  183. @param mqc MQC handle
  184. */
  185. void opj_mqc_byteout(opj_mqc_t *mqc);
  186. /**
  187. Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
  188. @param mqc MQC handle
  189. @param a_ value of mqc->a
  190. @param c_ value of mqc->c_
  191. @param ct_ value of mqc->ct_
  192. */
  193. #define opj_mqc_renorme_macro(mqc, a_, c_, ct_) \
  194. { \
  195. do { \
  196. a_ <<= 1; \
  197. c_ <<= 1; \
  198. ct_--; \
  199. if (ct_ == 0) { \
  200. mqc->c = c_; \
  201. opj_mqc_byteout(mqc); \
  202. c_ = mqc->c; \
  203. ct_ = mqc->ct; \
  204. } \
  205. } while( (a_ & 0x8000) == 0); \
  206. }
  207. #define opj_mqc_codemps_macro(mqc, curctx, a, c, ct) \
  208. { \
  209. a -= (*curctx)->qeval; \
  210. if ((a & 0x8000) == 0) { \
  211. if (a < (*curctx)->qeval) { \
  212. a = (*curctx)->qeval; \
  213. } else { \
  214. c += (*curctx)->qeval; \
  215. } \
  216. *curctx = (*curctx)->nmps; \
  217. opj_mqc_renorme_macro(mqc, a, c, ct); \
  218. } else { \
  219. c += (*curctx)->qeval; \
  220. } \
  221. }
  222. #define opj_mqc_codelps_macro(mqc, curctx, a, c, ct) \
  223. { \
  224. a -= (*curctx)->qeval; \
  225. if (a < (*curctx)->qeval) { \
  226. c += (*curctx)->qeval; \
  227. } else { \
  228. a = (*curctx)->qeval; \
  229. } \
  230. *curctx = (*curctx)->nlps; \
  231. opj_mqc_renorme_macro(mqc, a, c, ct); \
  232. }
  233. #define opj_mqc_encode_macro(mqc, curctx, a, c, ct, d) \
  234. { \
  235. if ((*curctx)->mps == (d)) { \
  236. opj_mqc_codemps_macro(mqc, curctx, a, c, ct); \
  237. } else { \
  238. opj_mqc_codelps_macro(mqc, curctx, a, c, ct); \
  239. } \
  240. }
  241. #define opj_mqc_bypass_enc_macro(mqc, c, ct, d) \
  242. {\
  243. if (ct == BYPASS_CT_INIT) {\
  244. ct = 8;\
  245. }\
  246. ct--;\
  247. c = c + ((d) << ct);\
  248. if (ct == 0) {\
  249. *mqc->bp = (OPJ_BYTE)c;\
  250. ct = 8;\
  251. /* If the previous byte was 0xff, make sure that the next msb is 0 */ \
  252. if (*mqc->bp == 0xff) {\
  253. ct = 7;\
  254. }\
  255. mqc->bp++;\
  256. c = 0;\
  257. }\
  258. }
  259. #endif /* OPJ_MQC_INL_H */