mqc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. #include "opj_includes.h"
  39. #include <assert.h>
  40. /** @defgroup MQC MQC - Implementation of an MQ-Coder */
  41. /*@{*/
  42. /** @name Local static functions */
  43. /*@{*/
  44. /**
  45. Fill mqc->c with 1's for flushing
  46. @param mqc MQC handle
  47. */
  48. static void opj_mqc_setbits(opj_mqc_t *mqc);
  49. /*@}*/
  50. /*@}*/
  51. /* <summary> */
  52. /* This array defines all the possible states for a context. */
  53. /* </summary> */
  54. static const opj_mqc_state_t mqc_states[47 * 2] = {
  55. {0x5601, 0, &mqc_states[2], &mqc_states[3]},
  56. {0x5601, 1, &mqc_states[3], &mqc_states[2]},
  57. {0x3401, 0, &mqc_states[4], &mqc_states[12]},
  58. {0x3401, 1, &mqc_states[5], &mqc_states[13]},
  59. {0x1801, 0, &mqc_states[6], &mqc_states[18]},
  60. {0x1801, 1, &mqc_states[7], &mqc_states[19]},
  61. {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
  62. {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
  63. {0x0521, 0, &mqc_states[10], &mqc_states[58]},
  64. {0x0521, 1, &mqc_states[11], &mqc_states[59]},
  65. {0x0221, 0, &mqc_states[76], &mqc_states[66]},
  66. {0x0221, 1, &mqc_states[77], &mqc_states[67]},
  67. {0x5601, 0, &mqc_states[14], &mqc_states[13]},
  68. {0x5601, 1, &mqc_states[15], &mqc_states[12]},
  69. {0x5401, 0, &mqc_states[16], &mqc_states[28]},
  70. {0x5401, 1, &mqc_states[17], &mqc_states[29]},
  71. {0x4801, 0, &mqc_states[18], &mqc_states[28]},
  72. {0x4801, 1, &mqc_states[19], &mqc_states[29]},
  73. {0x3801, 0, &mqc_states[20], &mqc_states[28]},
  74. {0x3801, 1, &mqc_states[21], &mqc_states[29]},
  75. {0x3001, 0, &mqc_states[22], &mqc_states[34]},
  76. {0x3001, 1, &mqc_states[23], &mqc_states[35]},
  77. {0x2401, 0, &mqc_states[24], &mqc_states[36]},
  78. {0x2401, 1, &mqc_states[25], &mqc_states[37]},
  79. {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
  80. {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
  81. {0x1601, 0, &mqc_states[58], &mqc_states[42]},
  82. {0x1601, 1, &mqc_states[59], &mqc_states[43]},
  83. {0x5601, 0, &mqc_states[30], &mqc_states[29]},
  84. {0x5601, 1, &mqc_states[31], &mqc_states[28]},
  85. {0x5401, 0, &mqc_states[32], &mqc_states[28]},
  86. {0x5401, 1, &mqc_states[33], &mqc_states[29]},
  87. {0x5101, 0, &mqc_states[34], &mqc_states[30]},
  88. {0x5101, 1, &mqc_states[35], &mqc_states[31]},
  89. {0x4801, 0, &mqc_states[36], &mqc_states[32]},
  90. {0x4801, 1, &mqc_states[37], &mqc_states[33]},
  91. {0x3801, 0, &mqc_states[38], &mqc_states[34]},
  92. {0x3801, 1, &mqc_states[39], &mqc_states[35]},
  93. {0x3401, 0, &mqc_states[40], &mqc_states[36]},
  94. {0x3401, 1, &mqc_states[41], &mqc_states[37]},
  95. {0x3001, 0, &mqc_states[42], &mqc_states[38]},
  96. {0x3001, 1, &mqc_states[43], &mqc_states[39]},
  97. {0x2801, 0, &mqc_states[44], &mqc_states[38]},
  98. {0x2801, 1, &mqc_states[45], &mqc_states[39]},
  99. {0x2401, 0, &mqc_states[46], &mqc_states[40]},
  100. {0x2401, 1, &mqc_states[47], &mqc_states[41]},
  101. {0x2201, 0, &mqc_states[48], &mqc_states[42]},
  102. {0x2201, 1, &mqc_states[49], &mqc_states[43]},
  103. {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
  104. {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
  105. {0x1801, 0, &mqc_states[52], &mqc_states[46]},
  106. {0x1801, 1, &mqc_states[53], &mqc_states[47]},
  107. {0x1601, 0, &mqc_states[54], &mqc_states[48]},
  108. {0x1601, 1, &mqc_states[55], &mqc_states[49]},
  109. {0x1401, 0, &mqc_states[56], &mqc_states[50]},
  110. {0x1401, 1, &mqc_states[57], &mqc_states[51]},
  111. {0x1201, 0, &mqc_states[58], &mqc_states[52]},
  112. {0x1201, 1, &mqc_states[59], &mqc_states[53]},
  113. {0x1101, 0, &mqc_states[60], &mqc_states[54]},
  114. {0x1101, 1, &mqc_states[61], &mqc_states[55]},
  115. {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
  116. {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
  117. {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
  118. {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
  119. {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
  120. {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
  121. {0x0521, 0, &mqc_states[68], &mqc_states[62]},
  122. {0x0521, 1, &mqc_states[69], &mqc_states[63]},
  123. {0x0441, 0, &mqc_states[70], &mqc_states[64]},
  124. {0x0441, 1, &mqc_states[71], &mqc_states[65]},
  125. {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
  126. {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
  127. {0x0221, 0, &mqc_states[74], &mqc_states[68]},
  128. {0x0221, 1, &mqc_states[75], &mqc_states[69]},
  129. {0x0141, 0, &mqc_states[76], &mqc_states[70]},
  130. {0x0141, 1, &mqc_states[77], &mqc_states[71]},
  131. {0x0111, 0, &mqc_states[78], &mqc_states[72]},
  132. {0x0111, 1, &mqc_states[79], &mqc_states[73]},
  133. {0x0085, 0, &mqc_states[80], &mqc_states[74]},
  134. {0x0085, 1, &mqc_states[81], &mqc_states[75]},
  135. {0x0049, 0, &mqc_states[82], &mqc_states[76]},
  136. {0x0049, 1, &mqc_states[83], &mqc_states[77]},
  137. {0x0025, 0, &mqc_states[84], &mqc_states[78]},
  138. {0x0025, 1, &mqc_states[85], &mqc_states[79]},
  139. {0x0015, 0, &mqc_states[86], &mqc_states[80]},
  140. {0x0015, 1, &mqc_states[87], &mqc_states[81]},
  141. {0x0009, 0, &mqc_states[88], &mqc_states[82]},
  142. {0x0009, 1, &mqc_states[89], &mqc_states[83]},
  143. {0x0005, 0, &mqc_states[90], &mqc_states[84]},
  144. {0x0005, 1, &mqc_states[91], &mqc_states[85]},
  145. {0x0001, 0, &mqc_states[90], &mqc_states[86]},
  146. {0x0001, 1, &mqc_states[91], &mqc_states[87]},
  147. {0x5601, 0, &mqc_states[92], &mqc_states[92]},
  148. {0x5601, 1, &mqc_states[93], &mqc_states[93]},
  149. };
  150. /*
  151. ==========================================================
  152. local functions
  153. ==========================================================
  154. */
  155. static void opj_mqc_setbits(opj_mqc_t *mqc)
  156. {
  157. OPJ_UINT32 tempc = mqc->c + mqc->a;
  158. mqc->c |= 0xffff;
  159. if (mqc->c >= tempc) {
  160. mqc->c -= 0x8000;
  161. }
  162. }
  163. /*
  164. ==========================================================
  165. MQ-Coder interface
  166. ==========================================================
  167. */
  168. OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc)
  169. {
  170. const ptrdiff_t diff = mqc->bp - mqc->start;
  171. #if 0
  172. assert(diff <= 0xffffffff && diff >= 0); /* UINT32_MAX */
  173. #endif
  174. return (OPJ_UINT32)diff;
  175. }
  176. void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp)
  177. {
  178. /* To avoid the curctx pointer to be dangling, but not strictly */
  179. /* required as the current context is always set before encoding */
  180. opj_mqc_setcurctx(mqc, 0);
  181. /* As specified in Figure C.10 - Initialization of the encoder */
  182. /* (C.2.8 Initialization of the encoder (INITENC)) */
  183. mqc->a = 0x8000;
  184. mqc->c = 0;
  185. /* Yes, we point before the start of the buffer, but this is safe */
  186. /* given opj_tcd_code_block_enc_allocate_data() */
  187. mqc->bp = bp - 1;
  188. mqc->ct = 12;
  189. /* At this point we should test *(mqc->bp) against 0xFF, but this is not */
  190. /* necessary, as this is only used at the beginning of the code block */
  191. /* and our initial fake byte is set at 0 */
  192. assert(*(mqc->bp) != 0xff);
  193. mqc->start = bp;
  194. mqc->end_of_byte_stream_counter = 0;
  195. }
  196. void opj_mqc_flush(opj_mqc_t *mqc)
  197. {
  198. /* C.2.9 Termination of coding (FLUSH) */
  199. /* Figure C.11 – FLUSH procedure */
  200. opj_mqc_setbits(mqc);
  201. mqc->c <<= mqc->ct;
  202. opj_mqc_byteout(mqc);
  203. mqc->c <<= mqc->ct;
  204. opj_mqc_byteout(mqc);
  205. /* It is forbidden that a coding pass ends with 0xff */
  206. if (*mqc->bp != 0xff) {
  207. /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
  208. mqc->bp++;
  209. }
  210. }
  211. void opj_mqc_bypass_init_enc(opj_mqc_t *mqc)
  212. {
  213. /* This function is normally called after at least one opj_mqc_flush() */
  214. /* which will have advance mqc->bp by at least 2 bytes beyond its */
  215. /* initial position */
  216. assert(mqc->bp >= mqc->start);
  217. mqc->c = 0;
  218. /* in theory we should initialize to 8, but use this special value */
  219. /* as a hint that opj_mqc_bypass_enc() has never been called, so */
  220. /* as to avoid the 0xff 0x7f elimination trick in opj_mqc_bypass_flush_enc() */
  221. /* to trigger when we don't have output any bit during this bypass sequence */
  222. /* Any value > 8 will do */
  223. mqc->ct = BYPASS_CT_INIT;
  224. /* Given that we are called after opj_mqc_flush(), the previous byte */
  225. /* cannot be 0xff. */
  226. assert(mqc->bp[-1] != 0xff);
  227. }
  228. void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d)
  229. {
  230. if (mqc->ct == BYPASS_CT_INIT) {
  231. mqc->ct = 8;
  232. }
  233. mqc->ct--;
  234. mqc->c = mqc->c + (d << mqc->ct);
  235. if (mqc->ct == 0) {
  236. *mqc->bp = (OPJ_BYTE)mqc->c;
  237. mqc->ct = 8;
  238. /* If the previous byte was 0xff, make sure that the next msb is 0 */
  239. if (*mqc->bp == 0xff) {
  240. mqc->ct = 7;
  241. }
  242. mqc->bp++;
  243. mqc->c = 0;
  244. }
  245. }
  246. OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm)
  247. {
  248. return (mqc->ct < 7 ||
  249. (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0;
  250. }
  251. void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm)
  252. {
  253. /* Is there any bit remaining to be flushed ? */
  254. /* If the last output byte is 0xff, we can discard it, unless */
  255. /* erterm is required (I'm not completely sure why in erterm */
  256. /* we must output 0xff 0x2a if the last byte was 0xff instead of */
  257. /* discarding it, but Kakadu requires it when decoding */
  258. /* in -fussy mode) */
  259. if (mqc->ct < 7 || (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) {
  260. OPJ_BYTE bit_value = 0;
  261. /* If so, fill the remaining lsbs with an alternating sequence of */
  262. /* 0,1,... */
  263. /* Note: it seems the standard only requires that for a ERTERM flush */
  264. /* and doesn't specify what to do for a regular BYPASS flush */
  265. while (mqc->ct > 0) {
  266. mqc->ct--;
  267. mqc->c += (OPJ_UINT32)(bit_value << mqc->ct);
  268. bit_value = (OPJ_BYTE)(1U - bit_value);
  269. }
  270. *mqc->bp = (OPJ_BYTE)mqc->c;
  271. /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
  272. mqc->bp++;
  273. } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) {
  274. /* Discard last 0xff */
  275. assert(!erterm);
  276. mqc->bp --;
  277. } else if (mqc->ct == 8 && !erterm &&
  278. mqc->bp[-1] == 0x7f && mqc->bp[-2] == 0xff) {
  279. /* Tiny optimization: discard terminating 0xff 0x7f since it is */
  280. /* interpreted as 0xff 0x7f [0xff 0xff] by the decoder, and given */
  281. /* the bit stuffing, in fact as 0xff 0xff [0xff ..] */
  282. /* Happens once on opj_compress -i ../MAPA.tif -o MAPA.j2k -M 1 */
  283. mqc->bp -= 2;
  284. }
  285. assert(mqc->bp[-1] != 0xff);
  286. }
  287. void opj_mqc_reset_enc(opj_mqc_t *mqc)
  288. {
  289. opj_mqc_resetstates(mqc);
  290. opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  291. opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  292. opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  293. }
  294. #ifdef notdef
  295. OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc)
  296. {
  297. OPJ_UINT32 correction = 1;
  298. /* <flush part> */
  299. OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
  300. mqc->c <<= mqc->ct;
  301. while (n > 0) {
  302. opj_mqc_byteout(mqc);
  303. n -= (OPJ_INT32)mqc->ct;
  304. mqc->c <<= mqc->ct;
  305. }
  306. opj_mqc_byteout(mqc);
  307. return correction;
  308. }
  309. #endif
  310. void opj_mqc_restart_init_enc(opj_mqc_t *mqc)
  311. {
  312. /* <Re-init part> */
  313. /* As specified in Figure C.10 - Initialization of the encoder */
  314. /* (C.2.8 Initialization of the encoder (INITENC)) */
  315. mqc->a = 0x8000;
  316. mqc->c = 0;
  317. mqc->ct = 12;
  318. /* This function is normally called after at least one opj_mqc_flush() */
  319. /* which will have advance mqc->bp by at least 2 bytes beyond its */
  320. /* initial position */
  321. mqc->bp --;
  322. assert(mqc->bp >= mqc->start - 1);
  323. assert(*mqc->bp != 0xff);
  324. if (*mqc->bp == 0xff) {
  325. mqc->ct = 13;
  326. }
  327. }
  328. void opj_mqc_erterm_enc(opj_mqc_t *mqc)
  329. {
  330. OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
  331. while (k > 0) {
  332. mqc->c <<= mqc->ct;
  333. mqc->ct = 0;
  334. opj_mqc_byteout(mqc);
  335. k -= (OPJ_INT32)mqc->ct;
  336. }
  337. if (*mqc->bp != 0xff) {
  338. opj_mqc_byteout(mqc);
  339. }
  340. }
  341. static INLINE void opj_mqc_renorme(opj_mqc_t *mqc)
  342. {
  343. opj_mqc_renorme_macro(mqc, mqc->a, mqc->c, mqc->ct);
  344. }
  345. /**
  346. Encode the most probable symbol
  347. @param mqc MQC handle
  348. */
  349. static INLINE void opj_mqc_codemps(opj_mqc_t *mqc)
  350. {
  351. opj_mqc_codemps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
  352. }
  353. /**
  354. Encode the most least symbol
  355. @param mqc MQC handle
  356. */
  357. static INLINE void opj_mqc_codelps(opj_mqc_t *mqc)
  358. {
  359. opj_mqc_codelps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
  360. }
  361. /**
  362. Encode a symbol using the MQ-coder
  363. @param mqc MQC handle
  364. @param d The symbol to be encoded (0 or 1)
  365. */
  366. static INLINE void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d)
  367. {
  368. if ((*mqc->curctx)->mps == d) {
  369. opj_mqc_codemps(mqc);
  370. } else {
  371. opj_mqc_codelps(mqc);
  372. }
  373. }
  374. void opj_mqc_segmark_enc(opj_mqc_t *mqc)
  375. {
  376. OPJ_UINT32 i;
  377. opj_mqc_setcurctx(mqc, 18);
  378. for (i = 1; i < 5; i++) {
  379. opj_mqc_encode(mqc, i % 2);
  380. }
  381. }
  382. static void opj_mqc_init_dec_common(opj_mqc_t *mqc,
  383. OPJ_BYTE *bp,
  384. OPJ_UINT32 len,
  385. OPJ_UINT32 extra_writable_bytes)
  386. {
  387. (void)extra_writable_bytes;
  388. assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA);
  389. mqc->start = bp;
  390. mqc->end = bp + len;
  391. /* Insert an artificial 0xFF 0xFF marker at end of the code block */
  392. /* data so that the bytein routines stop on it. This saves us comparing */
  393. /* the bp and end pointers */
  394. /* But before inserting it, backup th bytes we will overwrite */
  395. memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA);
  396. mqc->end[0] = 0xFF;
  397. mqc->end[1] = 0xFF;
  398. mqc->bp = bp;
  399. }
  400. void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
  401. OPJ_UINT32 extra_writable_bytes)
  402. {
  403. /* Implements ISO 15444-1 C.3.5 Initialization of the decoder (INITDEC) */
  404. /* Note: alternate "J.1 - Initialization of the software-conventions */
  405. /* decoder" has been tried, but does */
  406. /* not bring any improvement. */
  407. /* See https://github.com/uclouvain/openjpeg/issues/921 */
  408. opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
  409. opj_mqc_setcurctx(mqc, 0);
  410. mqc->end_of_byte_stream_counter = 0;
  411. if (len == 0) {
  412. mqc->c = 0xff << 16;
  413. } else {
  414. mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
  415. }
  416. opj_mqc_bytein(mqc);
  417. mqc->c <<= 7;
  418. mqc->ct -= 7;
  419. mqc->a = 0x8000;
  420. }
  421. void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
  422. OPJ_UINT32 extra_writable_bytes)
  423. {
  424. opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
  425. mqc->c = 0;
  426. mqc->ct = 0;
  427. }
  428. void opq_mqc_finish_dec(opj_mqc_t *mqc)
  429. {
  430. /* Restore the bytes overwritten by opj_mqc_init_dec_common() */
  431. memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA);
  432. }
  433. void opj_mqc_resetstates(opj_mqc_t *mqc)
  434. {
  435. OPJ_UINT32 i;
  436. for (i = 0; i < MQC_NUMCTXS; i++) {
  437. mqc->ctxs[i] = mqc_states;
  438. }
  439. }
  440. void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb,
  441. OPJ_INT32 prob)
  442. {
  443. mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
  444. }
  445. void opj_mqc_byteout(opj_mqc_t *mqc)
  446. {
  447. /* bp is initialized to start - 1 in opj_mqc_init_enc() */
  448. /* but this is safe, see opj_tcd_code_block_enc_allocate_data() */
  449. assert(mqc->bp >= mqc->start - 1);
  450. if (*mqc->bp == 0xff) {
  451. mqc->bp++;
  452. *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
  453. mqc->c &= 0xfffff;
  454. mqc->ct = 7;
  455. } else {
  456. if ((mqc->c & 0x8000000) == 0) {
  457. mqc->bp++;
  458. *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
  459. mqc->c &= 0x7ffff;
  460. mqc->ct = 8;
  461. } else {
  462. (*mqc->bp)++;
  463. if (*mqc->bp == 0xff) {
  464. mqc->c &= 0x7ffffff;
  465. mqc->bp++;
  466. *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
  467. mqc->c &= 0xfffff;
  468. mqc->ct = 7;
  469. } else {
  470. mqc->bp++;
  471. *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
  472. mqc->c &= 0x7ffff;
  473. mqc->ct = 8;
  474. }
  475. }
  476. }
  477. }