mqc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. if (mqc) {
  339. mqc->buffer = NULL;
  340. }
  341. #endif
  342. return mqc;
  343. }
  344. void mqc_destroy(opj_mqc_t *mqc) {
  345. if (mqc) {
  346. #ifdef MQC_PERF_OPT
  347. if (mqc->buffer) {
  348. opj_free(mqc->buffer);
  349. }
  350. #endif
  351. opj_free(mqc);
  352. }
  353. }
  354. int mqc_numbytes(opj_mqc_t *mqc) {
  355. return (int)(mqc->bp - mqc->start);
  356. }
  357. void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
  358. mqc_setcurctx(mqc, 0);
  359. mqc->a = 0x8000;
  360. mqc->c = 0;
  361. mqc->bp = bp - 1;
  362. mqc->ct = 12;
  363. if (*mqc->bp == 0xff) {
  364. mqc->ct = 13;
  365. }
  366. mqc->start = bp;
  367. }
  368. void mqc_encode(opj_mqc_t *mqc, int d) {
  369. if ((*mqc->curctx)->mps == d) {
  370. mqc_codemps(mqc);
  371. } else {
  372. mqc_codelps(mqc);
  373. }
  374. }
  375. void mqc_flush(opj_mqc_t *mqc) {
  376. mqc_setbits(mqc);
  377. mqc->c <<= mqc->ct;
  378. mqc_byteout(mqc);
  379. mqc->c <<= mqc->ct;
  380. mqc_byteout(mqc);
  381. if (*mqc->bp != 0xff) {
  382. mqc->bp++;
  383. }
  384. }
  385. void mqc_bypass_init_enc(opj_mqc_t *mqc) {
  386. mqc->c = 0;
  387. mqc->ct = 8;
  388. /*if (*mqc->bp == 0xff) {
  389. mqc->ct = 7;
  390. } */
  391. }
  392. void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
  393. mqc->ct--;
  394. mqc->c = mqc->c + (d << mqc->ct);
  395. if (mqc->ct == 0) {
  396. mqc->bp++;
  397. *mqc->bp = mqc->c;
  398. mqc->ct = 8;
  399. if (*mqc->bp == 0xff) {
  400. mqc->ct = 7;
  401. }
  402. mqc->c = 0;
  403. }
  404. }
  405. int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
  406. unsigned char bit_padding;
  407. bit_padding = 0;
  408. if (mqc->ct != 0) {
  409. while (mqc->ct > 0) {
  410. mqc->ct--;
  411. mqc->c += bit_padding << mqc->ct;
  412. bit_padding = (bit_padding + 1) & 0x01;
  413. }
  414. mqc->bp++;
  415. *mqc->bp = mqc->c;
  416. mqc->ct = 8;
  417. mqc->c = 0;
  418. }
  419. return 1;
  420. }
  421. void mqc_reset_enc(opj_mqc_t *mqc) {
  422. mqc_resetstates(mqc);
  423. mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
  424. mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
  425. mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
  426. }
  427. int mqc_restart_enc(opj_mqc_t *mqc) {
  428. int correction = 1;
  429. /* <flush part> */
  430. int n = 27 - 15 - mqc->ct;
  431. mqc->c <<= mqc->ct;
  432. while (n > 0) {
  433. mqc_byteout(mqc);
  434. n -= mqc->ct;
  435. mqc->c <<= mqc->ct;
  436. }
  437. mqc_byteout(mqc);
  438. return correction;
  439. }
  440. void mqc_restart_init_enc(opj_mqc_t *mqc) {
  441. /* <Re-init part> */
  442. mqc_setcurctx(mqc, 0);
  443. mqc->a = 0x8000;
  444. mqc->c = 0;
  445. mqc->ct = 12;
  446. mqc->bp--;
  447. if (*mqc->bp == 0xff) {
  448. mqc->ct = 13;
  449. }
  450. }
  451. void mqc_erterm_enc(opj_mqc_t *mqc) {
  452. int k = 11 - mqc->ct + 1;
  453. while (k > 0) {
  454. mqc->c <<= mqc->ct;
  455. mqc->ct = 0;
  456. mqc_byteout(mqc);
  457. k -= mqc->ct;
  458. }
  459. if (*mqc->bp != 0xff) {
  460. mqc_byteout(mqc);
  461. }
  462. }
  463. void mqc_segmark_enc(opj_mqc_t *mqc) {
  464. int i;
  465. mqc_setcurctx(mqc, 18);
  466. for (i = 1; i < 5; i++) {
  467. mqc_encode(mqc, i % 2);
  468. }
  469. }
  470. void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
  471. mqc_setcurctx(mqc, 0);
  472. mqc->start = bp;
  473. mqc->end = bp + len;
  474. mqc->bp = bp;
  475. if (len==0) mqc->c = 0xff << 16;
  476. else mqc->c = *mqc->bp << 16;
  477. #ifdef MQC_PERF_OPT
  478. {
  479. unsigned int c;
  480. unsigned int *ip;
  481. unsigned char *end = mqc->end - 1;
  482. mqc->buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(unsigned int));
  483. ip = (unsigned int *) mqc->buffer;
  484. if (!ip) {
  485. opj_event_msg(cinfo, EVT_ERROR, "out of memory\n");
  486. return;
  487. }
  488. while (bp < end) {
  489. c = *(bp + 1);
  490. if (*bp == 0xff) {
  491. if (c > 0x8f) {
  492. break;
  493. } else {
  494. *ip = 0x00000017 | (c << 9);
  495. }
  496. } else {
  497. *ip = 0x00000018 | (c << 8);
  498. }
  499. bp++;
  500. ip++;
  501. }
  502. /* Handle last byte of data */
  503. c = 0xff;
  504. if (*bp == 0xff) {
  505. *ip = 0x0000ff18;
  506. } else {
  507. bp++;
  508. *ip = 0x00000018 | (c << 8);
  509. }
  510. ip++;
  511. *ip = 0x0000ff08;
  512. mqc->bp = mqc->buffer;
  513. }
  514. #endif
  515. mqc_bytein(mqc);
  516. mqc->c <<= 7;
  517. mqc->ct -= 7;
  518. mqc->a = 0x8000;
  519. }
  520. int mqc_decode(opj_mqc_t *const mqc) {
  521. int d;
  522. mqc->a -= (*mqc->curctx)->qeval;
  523. if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
  524. d = mqc_lpsexchange(mqc);
  525. mqc_renormd(mqc);
  526. } else {
  527. mqc->c -= (*mqc->curctx)->qeval << 16;
  528. if ((mqc->a & 0x8000) == 0) {
  529. d = mqc_mpsexchange(mqc);
  530. mqc_renormd(mqc);
  531. } else {
  532. d = (*mqc->curctx)->mps;
  533. }
  534. }
  535. return d;
  536. }
  537. void mqc_resetstates(opj_mqc_t *mqc) {
  538. int i;
  539. for (i = 0; i < MQC_NUMCTXS; i++) {
  540. mqc->ctxs[i] = mqc_states;
  541. }
  542. }
  543. void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
  544. mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
  545. }