mqc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. /** @defgroup MQC MQC - Implementation of an MQ-Coder */
  33. /*@{*/
  34. /** @name Local static functions */
  35. /*@{*/
  36. /**
  37. Output a byte, doing bit-stuffing if necessary.
  38. After a 0xff byte, the next byte must be smaller than 0x90.
  39. @param mqc MQC handle
  40. */
  41. static void mqc_byteout(opj_mqc_t *mqc);
  42. /**
  43. Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
  44. @param mqc MQC handle
  45. */
  46. static void mqc_renorme(opj_mqc_t *mqc);
  47. /**
  48. Encode the most probable symbol
  49. @param mqc MQC handle
  50. */
  51. static void mqc_codemps(opj_mqc_t *mqc);
  52. /**
  53. Encode the most least symbol
  54. @param mqc MQC handle
  55. */
  56. static void mqc_codelps(opj_mqc_t *mqc);
  57. /**
  58. Fill mqc->c with 1's for flushing
  59. @param mqc MQC handle
  60. */
  61. static void mqc_setbits(opj_mqc_t *mqc);
  62. /**
  63. FIXME: documentation ???
  64. @param mqc MQC handle
  65. @return
  66. */
  67. static INLINE int mqc_mpsexchange(opj_mqc_t *const mqc);
  68. /**
  69. FIXME: documentation ???
  70. @param mqc MQC handle
  71. @return
  72. */
  73. static INLINE int mqc_lpsexchange(opj_mqc_t *const mqc);
  74. /**
  75. Input a byte
  76. @param mqc MQC handle
  77. */
  78. static INLINE void mqc_bytein(opj_mqc_t *const mqc);
  79. /**
  80. Renormalize mqc->a and mqc->c while decoding
  81. @param mqc MQC handle
  82. */
  83. static INLINE void mqc_renormd(opj_mqc_t *const mqc);
  84. /*@}*/
  85. /*@}*/
  86. /* <summary> */
  87. /* This array defines all the possible states for a context. */
  88. /* </summary> */
  89. static opj_mqc_state_t mqc_states[47 * 2] = {
  90. {0x5601, 0, &mqc_states[2], &mqc_states[3]},
  91. {0x5601, 1, &mqc_states[3], &mqc_states[2]},
  92. {0x3401, 0, &mqc_states[4], &mqc_states[12]},
  93. {0x3401, 1, &mqc_states[5], &mqc_states[13]},
  94. {0x1801, 0, &mqc_states[6], &mqc_states[18]},
  95. {0x1801, 1, &mqc_states[7], &mqc_states[19]},
  96. {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
  97. {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
  98. {0x0521, 0, &mqc_states[10], &mqc_states[58]},
  99. {0x0521, 1, &mqc_states[11], &mqc_states[59]},
  100. {0x0221, 0, &mqc_states[76], &mqc_states[66]},
  101. {0x0221, 1, &mqc_states[77], &mqc_states[67]},
  102. {0x5601, 0, &mqc_states[14], &mqc_states[13]},
  103. {0x5601, 1, &mqc_states[15], &mqc_states[12]},
  104. {0x5401, 0, &mqc_states[16], &mqc_states[28]},
  105. {0x5401, 1, &mqc_states[17], &mqc_states[29]},
  106. {0x4801, 0, &mqc_states[18], &mqc_states[28]},
  107. {0x4801, 1, &mqc_states[19], &mqc_states[29]},
  108. {0x3801, 0, &mqc_states[20], &mqc_states[28]},
  109. {0x3801, 1, &mqc_states[21], &mqc_states[29]},
  110. {0x3001, 0, &mqc_states[22], &mqc_states[34]},
  111. {0x3001, 1, &mqc_states[23], &mqc_states[35]},
  112. {0x2401, 0, &mqc_states[24], &mqc_states[36]},
  113. {0x2401, 1, &mqc_states[25], &mqc_states[37]},
  114. {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
  115. {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
  116. {0x1601, 0, &mqc_states[58], &mqc_states[42]},
  117. {0x1601, 1, &mqc_states[59], &mqc_states[43]},
  118. {0x5601, 0, &mqc_states[30], &mqc_states[29]},
  119. {0x5601, 1, &mqc_states[31], &mqc_states[28]},
  120. {0x5401, 0, &mqc_states[32], &mqc_states[28]},
  121. {0x5401, 1, &mqc_states[33], &mqc_states[29]},
  122. {0x5101, 0, &mqc_states[34], &mqc_states[30]},
  123. {0x5101, 1, &mqc_states[35], &mqc_states[31]},
  124. {0x4801, 0, &mqc_states[36], &mqc_states[32]},
  125. {0x4801, 1, &mqc_states[37], &mqc_states[33]},
  126. {0x3801, 0, &mqc_states[38], &mqc_states[34]},
  127. {0x3801, 1, &mqc_states[39], &mqc_states[35]},
  128. {0x3401, 0, &mqc_states[40], &mqc_states[36]},
  129. {0x3401, 1, &mqc_states[41], &mqc_states[37]},
  130. {0x3001, 0, &mqc_states[42], &mqc_states[38]},
  131. {0x3001, 1, &mqc_states[43], &mqc_states[39]},
  132. {0x2801, 0, &mqc_states[44], &mqc_states[38]},
  133. {0x2801, 1, &mqc_states[45], &mqc_states[39]},
  134. {0x2401, 0, &mqc_states[46], &mqc_states[40]},
  135. {0x2401, 1, &mqc_states[47], &mqc_states[41]},
  136. {0x2201, 0, &mqc_states[48], &mqc_states[42]},
  137. {0x2201, 1, &mqc_states[49], &mqc_states[43]},
  138. {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
  139. {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
  140. {0x1801, 0, &mqc_states[52], &mqc_states[46]},
  141. {0x1801, 1, &mqc_states[53], &mqc_states[47]},
  142. {0x1601, 0, &mqc_states[54], &mqc_states[48]},
  143. {0x1601, 1, &mqc_states[55], &mqc_states[49]},
  144. {0x1401, 0, &mqc_states[56], &mqc_states[50]},
  145. {0x1401, 1, &mqc_states[57], &mqc_states[51]},
  146. {0x1201, 0, &mqc_states[58], &mqc_states[52]},
  147. {0x1201, 1, &mqc_states[59], &mqc_states[53]},
  148. {0x1101, 0, &mqc_states[60], &mqc_states[54]},
  149. {0x1101, 1, &mqc_states[61], &mqc_states[55]},
  150. {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
  151. {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
  152. {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
  153. {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
  154. {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
  155. {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
  156. {0x0521, 0, &mqc_states[68], &mqc_states[62]},
  157. {0x0521, 1, &mqc_states[69], &mqc_states[63]},
  158. {0x0441, 0, &mqc_states[70], &mqc_states[64]},
  159. {0x0441, 1, &mqc_states[71], &mqc_states[65]},
  160. {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
  161. {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
  162. {0x0221, 0, &mqc_states[74], &mqc_states[68]},
  163. {0x0221, 1, &mqc_states[75], &mqc_states[69]},
  164. {0x0141, 0, &mqc_states[76], &mqc_states[70]},
  165. {0x0141, 1, &mqc_states[77], &mqc_states[71]},
  166. {0x0111, 0, &mqc_states[78], &mqc_states[72]},
  167. {0x0111, 1, &mqc_states[79], &mqc_states[73]},
  168. {0x0085, 0, &mqc_states[80], &mqc_states[74]},
  169. {0x0085, 1, &mqc_states[81], &mqc_states[75]},
  170. {0x0049, 0, &mqc_states[82], &mqc_states[76]},
  171. {0x0049, 1, &mqc_states[83], &mqc_states[77]},
  172. {0x0025, 0, &mqc_states[84], &mqc_states[78]},
  173. {0x0025, 1, &mqc_states[85], &mqc_states[79]},
  174. {0x0015, 0, &mqc_states[86], &mqc_states[80]},
  175. {0x0015, 1, &mqc_states[87], &mqc_states[81]},
  176. {0x0009, 0, &mqc_states[88], &mqc_states[82]},
  177. {0x0009, 1, &mqc_states[89], &mqc_states[83]},
  178. {0x0005, 0, &mqc_states[90], &mqc_states[84]},
  179. {0x0005, 1, &mqc_states[91], &mqc_states[85]},
  180. {0x0001, 0, &mqc_states[90], &mqc_states[86]},
  181. {0x0001, 1, &mqc_states[91], &mqc_states[87]},
  182. {0x5601, 0, &mqc_states[92], &mqc_states[92]},
  183. {0x5601, 1, &mqc_states[93], &mqc_states[93]},
  184. };
  185. /*
  186. ==========================================================
  187. local functions
  188. ==========================================================
  189. */
  190. static void mqc_byteout(opj_mqc_t *mqc) {
  191. if (*mqc->bp == 0xff) {
  192. mqc->bp++;
  193. *mqc->bp = mqc->c >> 20;
  194. mqc->c &= 0xfffff;
  195. mqc->ct = 7;
  196. } else {
  197. if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */
  198. mqc->bp++;
  199. *mqc->bp = mqc->c >> 19;
  200. mqc->c &= 0x7ffff;
  201. mqc->ct = 8;
  202. } else {
  203. (*mqc->bp)++;
  204. if (*mqc->bp == 0xff) {
  205. mqc->c &= 0x7ffffff;
  206. mqc->bp++;
  207. *mqc->bp = mqc->c >> 20;
  208. mqc->c &= 0xfffff;
  209. mqc->ct = 7;
  210. } else {
  211. mqc->bp++;
  212. *mqc->bp = mqc->c >> 19;
  213. mqc->c &= 0x7ffff;
  214. mqc->ct = 8;
  215. }
  216. }
  217. }
  218. }
  219. static void mqc_renorme(opj_mqc_t *mqc) {
  220. do {
  221. mqc->a <<= 1;
  222. mqc->c <<= 1;
  223. mqc->ct--;
  224. if (mqc->ct == 0) {
  225. mqc_byteout(mqc);
  226. }
  227. } while ((mqc->a & 0x8000) == 0);
  228. }
  229. static void mqc_codemps(opj_mqc_t *mqc) {
  230. mqc->a -= (*mqc->curctx)->qeval;
  231. if ((mqc->a & 0x8000) == 0) {
  232. if (mqc->a < (*mqc->curctx)->qeval) {
  233. mqc->a = (*mqc->curctx)->qeval;
  234. } else {
  235. mqc->c += (*mqc->curctx)->qeval;
  236. }
  237. *mqc->curctx = (*mqc->curctx)->nmps;
  238. mqc_renorme(mqc);
  239. } else {
  240. mqc->c += (*mqc->curctx)->qeval;
  241. }
  242. }
  243. static void mqc_codelps(opj_mqc_t *mqc) {
  244. mqc->a -= (*mqc->curctx)->qeval;
  245. if (mqc->a < (*mqc->curctx)->qeval) {
  246. mqc->c += (*mqc->curctx)->qeval;
  247. } else {
  248. mqc->a = (*mqc->curctx)->qeval;
  249. }
  250. *mqc->curctx = (*mqc->curctx)->nlps;
  251. mqc_renorme(mqc);
  252. }
  253. static void mqc_setbits(opj_mqc_t *mqc) {
  254. unsigned int tempc = mqc->c + mqc->a;
  255. mqc->c |= 0xffff;
  256. if (mqc->c >= tempc) {
  257. mqc->c -= 0x8000;
  258. }
  259. }
  260. static INLINE int mqc_mpsexchange(opj_mqc_t *const mqc) {
  261. int d;
  262. if (mqc->a < (*mqc->curctx)->qeval) {
  263. d = 1 - (*mqc->curctx)->mps;
  264. *mqc->curctx = (*mqc->curctx)->nlps;
  265. } else {
  266. d = (*mqc->curctx)->mps;
  267. *mqc->curctx = (*mqc->curctx)->nmps;
  268. }
  269. return d;
  270. }
  271. static INLINE int mqc_lpsexchange(opj_mqc_t *const mqc) {
  272. int d;
  273. if (mqc->a < (*mqc->curctx)->qeval) {
  274. mqc->a = (*mqc->curctx)->qeval;
  275. d = (*mqc->curctx)->mps;
  276. *mqc->curctx = (*mqc->curctx)->nmps;
  277. } else {
  278. mqc->a = (*mqc->curctx)->qeval;
  279. d = 1 - (*mqc->curctx)->mps;
  280. *mqc->curctx = (*mqc->curctx)->nlps;
  281. }
  282. return d;
  283. }
  284. #ifdef MQC_PERF_OPT
  285. static INLINE void mqc_bytein(opj_mqc_t *const mqc) {
  286. unsigned int i = *((unsigned int *) mqc->bp);
  287. mqc->c += i & 0xffff00;
  288. mqc->ct = i & 0x0f;
  289. mqc->bp += (i >> 2) & 0x04;
  290. }
  291. #else
  292. static void mqc_bytein(opj_mqc_t *const mqc) {
  293. if (mqc->bp != mqc->end) {
  294. unsigned int c;
  295. if (mqc->bp + 1 != mqc->end) {
  296. c = *(mqc->bp + 1);
  297. } else {
  298. c = 0xff;
  299. }
  300. if (*mqc->bp == 0xff) {
  301. if (c > 0x8f) {
  302. mqc->c += 0xff00;
  303. mqc->ct = 8;
  304. } else {
  305. mqc->bp++;
  306. mqc->c += c << 9;
  307. mqc->ct = 7;
  308. }
  309. } else {
  310. mqc->bp++;
  311. mqc->c += c << 8;
  312. mqc->ct = 8;
  313. }
  314. } else {
  315. mqc->c += 0xff00;
  316. mqc->ct = 8;
  317. }
  318. }
  319. #endif
  320. static INLINE void mqc_renormd(opj_mqc_t *const mqc) {
  321. do {
  322. if (mqc->ct == 0) {
  323. mqc_bytein(mqc);
  324. }
  325. mqc->a <<= 1;
  326. mqc->c <<= 1;
  327. mqc->ct--;
  328. } while (mqc->a < 0x8000);
  329. }
  330. /*
  331. ==========================================================
  332. MQ-Coder interface
  333. ==========================================================
  334. */
  335. opj_mqc_t* mqc_create(void) {
  336. opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
  337. #ifdef MQC_PERF_OPT
  338. mqc->buffer = NULL;
  339. #endif
  340. return mqc;
  341. }
  342. void mqc_destroy(opj_mqc_t *mqc) {
  343. if(mqc) {
  344. #ifdef MQC_PERF_OPT
  345. if (mqc->buffer) {
  346. opj_free(mqc->buffer);
  347. }
  348. #endif
  349. opj_free(mqc);
  350. }
  351. }
  352. int mqc_numbytes(opj_mqc_t *mqc) {
  353. return mqc->bp - mqc->start;
  354. }
  355. void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
  356. mqc_setcurctx(mqc, 0);
  357. mqc->a = 0x8000;
  358. mqc->c = 0;
  359. mqc->bp = bp - 1;
  360. mqc->ct = 12;
  361. if (*mqc->bp == 0xff) {
  362. mqc->ct = 13;
  363. }
  364. mqc->start = bp;
  365. }
  366. void mqc_encode(opj_mqc_t *mqc, int d) {
  367. if ((*mqc->curctx)->mps == d) {
  368. mqc_codemps(mqc);
  369. } else {
  370. mqc_codelps(mqc);
  371. }
  372. }
  373. void mqc_flush(opj_mqc_t *mqc) {
  374. mqc_setbits(mqc);
  375. mqc->c <<= mqc->ct;
  376. mqc_byteout(mqc);
  377. mqc->c <<= mqc->ct;
  378. mqc_byteout(mqc);
  379. if (*mqc->bp != 0xff) {
  380. mqc->bp++;
  381. }
  382. }
  383. void mqc_bypass_init_enc(opj_mqc_t *mqc) {
  384. mqc->c = 0;
  385. mqc->ct = 8;
  386. /*if (*mqc->bp == 0xff) {
  387. mqc->ct = 7;
  388. } */
  389. }
  390. void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
  391. mqc->ct--;
  392. mqc->c = mqc->c + (d << mqc->ct);
  393. if (mqc->ct == 0) {
  394. mqc->bp++;
  395. *mqc->bp = mqc->c;
  396. mqc->ct = 8;
  397. if (*mqc->bp == 0xff) {
  398. mqc->ct = 7;
  399. }
  400. mqc->c = 0;
  401. }
  402. }
  403. int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
  404. unsigned char bit_padding;
  405. bit_padding = 0;
  406. if (mqc->ct != 0) {
  407. while (mqc->ct > 0) {
  408. mqc->ct--;
  409. mqc->c += bit_padding << mqc->ct;
  410. bit_padding = (bit_padding + 1) & 0x01;
  411. }
  412. mqc->bp++;
  413. *mqc->bp = mqc->c;
  414. mqc->ct = 8;
  415. mqc->c = 0;
  416. }
  417. return 1;
  418. }
  419. void mqc_reset_enc(opj_mqc_t *mqc) {
  420. mqc_resetstates(mqc);
  421. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  422. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  423. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  424. }
  425. int mqc_restart_enc(opj_mqc_t *mqc) {
  426. int correction = 1;
  427. /* <flush part> */
  428. int n = 27 - 15 - mqc->ct;
  429. mqc->c <<= mqc->ct;
  430. while (n > 0) {
  431. mqc_byteout(mqc);
  432. n -= mqc->ct;
  433. mqc->c <<= mqc->ct;
  434. }
  435. mqc_byteout(mqc);
  436. return correction;
  437. }
  438. void mqc_restart_init_enc(opj_mqc_t *mqc) {
  439. /* <Re-init part> */
  440. mqc_setcurctx(mqc, 0);
  441. mqc->a = 0x8000;
  442. mqc->c = 0;
  443. mqc->ct = 12;
  444. mqc->bp--;
  445. if (*mqc->bp == 0xff) {
  446. mqc->ct = 13;
  447. }
  448. }
  449. void mqc_erterm_enc(opj_mqc_t *mqc) {
  450. int k = 11 - mqc->ct + 1;
  451. while (k > 0) {
  452. mqc->c <<= mqc->ct;
  453. mqc->ct = 0;
  454. mqc_byteout(mqc);
  455. k -= mqc->ct;
  456. }
  457. if (*mqc->bp != 0xff) {
  458. mqc_byteout(mqc);
  459. }
  460. }
  461. void mqc_segmark_enc(opj_mqc_t *mqc) {
  462. int i;
  463. mqc_setcurctx(mqc, 18);
  464. for (i = 1; i < 5; i++) {
  465. mqc_encode(mqc, i % 2);
  466. }
  467. }
  468. void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
  469. mqc_setcurctx(mqc, 0);
  470. mqc->start = bp;
  471. mqc->end = bp + len;
  472. mqc->bp = bp;
  473. if (len==0) mqc->c = 0xff << 16;
  474. else mqc->c = *mqc->bp << 16;
  475. #ifdef MQC_PERF_OPT
  476. {
  477. unsigned int c;
  478. unsigned int *ip;
  479. unsigned char *end = mqc->end - 1;
  480. mqc->buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(unsigned int));
  481. ip = (unsigned int *) mqc->buffer;
  482. while (bp < end) {
  483. c = *(bp + 1);
  484. if (*bp == 0xff) {
  485. if (c > 0x8f) {
  486. break;
  487. } else {
  488. *ip = 0x00000017 | (c << 9);
  489. }
  490. } else {
  491. *ip = 0x00000018 | (c << 8);
  492. }
  493. bp++;
  494. ip++;
  495. }
  496. /* Handle last byte of data */
  497. c = 0xff;
  498. if (*bp == 0xff) {
  499. *ip = 0x0000ff18;
  500. } else {
  501. bp++;
  502. *ip = 0x00000018 | (c << 8);
  503. }
  504. ip++;
  505. *ip = 0x0000ff08;
  506. mqc->bp = mqc->buffer;
  507. }
  508. #endif
  509. mqc_bytein(mqc);
  510. mqc->c <<= 7;
  511. mqc->ct -= 7;
  512. mqc->a = 0x8000;
  513. }
  514. int mqc_decode(opj_mqc_t *const mqc) {
  515. int d;
  516. mqc->a -= (*mqc->curctx)->qeval;
  517. if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
  518. d = mqc_lpsexchange(mqc);
  519. mqc_renormd(mqc);
  520. } else {
  521. mqc->c -= (*mqc->curctx)->qeval << 16;
  522. if ((mqc->a & 0x8000) == 0) {
  523. d = mqc_mpsexchange(mqc);
  524. mqc_renormd(mqc);
  525. } else {
  526. d = (*mqc->curctx)->mps;
  527. }
  528. }
  529. return d;
  530. }
  531. void mqc_resetstates(opj_mqc_t *mqc) {
  532. int i;
  533. for (i = 0; i < MQC_NUMCTXS; i++) {
  534. mqc->ctxs[i] = mqc_states;
  535. }
  536. }
  537. void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
  538. mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
  539. }