-/*\r
- * Copyright (c) 2001-2003, David Janssens\r
- * Copyright (c) 2002-2003, Yannick Verschueren\r
- * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe\r
- * Copyright (c) 2005, Herve Drolon, FreeImage Team\r
- * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
- * POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-\r
-#include "opj_includes.h"\r
-\r
-/** @defgroup MQC MQC - Implementation of an MQ-Coder */\r
-/*@{*/\r
-\r
-/** @name Local static functions */\r
-/*@{*/\r
-\r
-/**\r
-Output a byte, doing bit-stuffing if necessary.\r
-After a 0xff byte, the next byte must be smaller than 0x90.\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_byteout(opj_mqc_t *mqc);\r
-/**\r
-Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_renorme(opj_mqc_t *mqc);\r
-/**\r
-Encode the most probable symbol\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_codemps(opj_mqc_t *mqc);\r
-/**\r
-Encode the most least symbol\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_codelps(opj_mqc_t *mqc);\r
-/**\r
-Fill mqc->c with 1's for flushing\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_setbits(opj_mqc_t *mqc);\r
-/**\r
-Exchange MPS with LPS\r
-@param mqc MQC handle\r
-@return \r
-*/\r
-static int mqc_mpsexchange(opj_mqc_t *mqc);\r
-/**\r
-Exchange LPS with MPS\r
-@param mqc MQC handle\r
-@return \r
-*/\r
-static int mqc_lpsexchange(opj_mqc_t *mqc);\r
-/**\r
-Input a byte\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_bytein(opj_mqc_t *mqc);\r
-/**\r
-Renormalize mqc->a and mqc->c while decoding\r
-@param mqc MQC handle\r
-*/\r
-static void mqc_renormd(opj_mqc_t *mqc);\r
-\r
-/*@}*/\r
-\r
-/*@}*/\r
-\r
-/* <summary> */\r
-/* This array defines all the possible states for a context. */\r
-/* </summary> */\r
-static opj_mqc_state_t mqc_states[47 * 2] = {\r
- {0x5601, 0, &mqc_states[2], &mqc_states[3]},\r
- {0x5601, 1, &mqc_states[3], &mqc_states[2]},\r
- {0x3401, 0, &mqc_states[4], &mqc_states[12]},\r
- {0x3401, 1, &mqc_states[5], &mqc_states[13]},\r
- {0x1801, 0, &mqc_states[6], &mqc_states[18]},\r
- {0x1801, 1, &mqc_states[7], &mqc_states[19]},\r
- {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},\r
- {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},\r
- {0x0521, 0, &mqc_states[10], &mqc_states[58]},\r
- {0x0521, 1, &mqc_states[11], &mqc_states[59]},\r
- {0x0221, 0, &mqc_states[76], &mqc_states[66]},\r
- {0x0221, 1, &mqc_states[77], &mqc_states[67]},\r
- {0x5601, 0, &mqc_states[14], &mqc_states[13]},\r
- {0x5601, 1, &mqc_states[15], &mqc_states[12]},\r
- {0x5401, 0, &mqc_states[16], &mqc_states[28]},\r
- {0x5401, 1, &mqc_states[17], &mqc_states[29]},\r
- {0x4801, 0, &mqc_states[18], &mqc_states[28]},\r
- {0x4801, 1, &mqc_states[19], &mqc_states[29]},\r
- {0x3801, 0, &mqc_states[20], &mqc_states[28]},\r
- {0x3801, 1, &mqc_states[21], &mqc_states[29]},\r
- {0x3001, 0, &mqc_states[22], &mqc_states[34]},\r
- {0x3001, 1, &mqc_states[23], &mqc_states[35]},\r
- {0x2401, 0, &mqc_states[24], &mqc_states[36]},\r
- {0x2401, 1, &mqc_states[25], &mqc_states[37]},\r
- {0x1c01, 0, &mqc_states[26], &mqc_states[40]},\r
- {0x1c01, 1, &mqc_states[27], &mqc_states[41]},\r
- {0x1601, 0, &mqc_states[58], &mqc_states[42]},\r
- {0x1601, 1, &mqc_states[59], &mqc_states[43]},\r
- {0x5601, 0, &mqc_states[30], &mqc_states[29]},\r
- {0x5601, 1, &mqc_states[31], &mqc_states[28]},\r
- {0x5401, 0, &mqc_states[32], &mqc_states[28]},\r
- {0x5401, 1, &mqc_states[33], &mqc_states[29]},\r
- {0x5101, 0, &mqc_states[34], &mqc_states[30]},\r
- {0x5101, 1, &mqc_states[35], &mqc_states[31]},\r
- {0x4801, 0, &mqc_states[36], &mqc_states[32]},\r
- {0x4801, 1, &mqc_states[37], &mqc_states[33]},\r
- {0x3801, 0, &mqc_states[38], &mqc_states[34]},\r
- {0x3801, 1, &mqc_states[39], &mqc_states[35]},\r
- {0x3401, 0, &mqc_states[40], &mqc_states[36]},\r
- {0x3401, 1, &mqc_states[41], &mqc_states[37]},\r
- {0x3001, 0, &mqc_states[42], &mqc_states[38]},\r
- {0x3001, 1, &mqc_states[43], &mqc_states[39]},\r
- {0x2801, 0, &mqc_states[44], &mqc_states[38]},\r
- {0x2801, 1, &mqc_states[45], &mqc_states[39]},\r
- {0x2401, 0, &mqc_states[46], &mqc_states[40]},\r
- {0x2401, 1, &mqc_states[47], &mqc_states[41]},\r
- {0x2201, 0, &mqc_states[48], &mqc_states[42]},\r
- {0x2201, 1, &mqc_states[49], &mqc_states[43]},\r
- {0x1c01, 0, &mqc_states[50], &mqc_states[44]},\r
- {0x1c01, 1, &mqc_states[51], &mqc_states[45]},\r
- {0x1801, 0, &mqc_states[52], &mqc_states[46]},\r
- {0x1801, 1, &mqc_states[53], &mqc_states[47]},\r
- {0x1601, 0, &mqc_states[54], &mqc_states[48]},\r
- {0x1601, 1, &mqc_states[55], &mqc_states[49]},\r
- {0x1401, 0, &mqc_states[56], &mqc_states[50]},\r
- {0x1401, 1, &mqc_states[57], &mqc_states[51]},\r
- {0x1201, 0, &mqc_states[58], &mqc_states[52]},\r
- {0x1201, 1, &mqc_states[59], &mqc_states[53]},\r
- {0x1101, 0, &mqc_states[60], &mqc_states[54]},\r
- {0x1101, 1, &mqc_states[61], &mqc_states[55]},\r
- {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},\r
- {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},\r
- {0x09c1, 0, &mqc_states[64], &mqc_states[58]},\r
- {0x09c1, 1, &mqc_states[65], &mqc_states[59]},\r
- {0x08a1, 0, &mqc_states[66], &mqc_states[60]},\r
- {0x08a1, 1, &mqc_states[67], &mqc_states[61]},\r
- {0x0521, 0, &mqc_states[68], &mqc_states[62]},\r
- {0x0521, 1, &mqc_states[69], &mqc_states[63]},\r
- {0x0441, 0, &mqc_states[70], &mqc_states[64]},\r
- {0x0441, 1, &mqc_states[71], &mqc_states[65]},\r
- {0x02a1, 0, &mqc_states[72], &mqc_states[66]},\r
- {0x02a1, 1, &mqc_states[73], &mqc_states[67]},\r
- {0x0221, 0, &mqc_states[74], &mqc_states[68]},\r
- {0x0221, 1, &mqc_states[75], &mqc_states[69]},\r
- {0x0141, 0, &mqc_states[76], &mqc_states[70]},\r
- {0x0141, 1, &mqc_states[77], &mqc_states[71]},\r
- {0x0111, 0, &mqc_states[78], &mqc_states[72]},\r
- {0x0111, 1, &mqc_states[79], &mqc_states[73]},\r
- {0x0085, 0, &mqc_states[80], &mqc_states[74]},\r
- {0x0085, 1, &mqc_states[81], &mqc_states[75]},\r
- {0x0049, 0, &mqc_states[82], &mqc_states[76]},\r
- {0x0049, 1, &mqc_states[83], &mqc_states[77]},\r
- {0x0025, 0, &mqc_states[84], &mqc_states[78]},\r
- {0x0025, 1, &mqc_states[85], &mqc_states[79]},\r
- {0x0015, 0, &mqc_states[86], &mqc_states[80]},\r
- {0x0015, 1, &mqc_states[87], &mqc_states[81]},\r
- {0x0009, 0, &mqc_states[88], &mqc_states[82]},\r
- {0x0009, 1, &mqc_states[89], &mqc_states[83]},\r
- {0x0005, 0, &mqc_states[90], &mqc_states[84]},\r
- {0x0005, 1, &mqc_states[91], &mqc_states[85]},\r
- {0x0001, 0, &mqc_states[90], &mqc_states[86]},\r
- {0x0001, 1, &mqc_states[91], &mqc_states[87]},\r
- {0x5601, 0, &mqc_states[92], &mqc_states[92]},\r
- {0x5601, 1, &mqc_states[93], &mqc_states[93]},\r
-};\r
-\r
-/* \r
-==========================================================\r
- local functions\r
-==========================================================\r
-*/\r
-\r
-static void mqc_byteout(opj_mqc_t *mqc) {\r
- if (*mqc->bp == 0xff) {\r
- mqc->bp++;\r
- *mqc->bp = mqc->c >> 20;\r
- mqc->c &= 0xfffff;\r
- mqc->ct = 7;\r
- } else {\r
- if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */\r
- mqc->bp++;\r
- *mqc->bp = mqc->c >> 19;\r
- mqc->c &= 0x7ffff;\r
- mqc->ct = 8;\r
- } else {\r
- (*mqc->bp)++;\r
- if (*mqc->bp == 0xff) {\r
- mqc->c &= 0x7ffffff;\r
- mqc->bp++;\r
- *mqc->bp = mqc->c >> 20;\r
- mqc->c &= 0xfffff;\r
- mqc->ct = 7;\r
- } else {\r
- mqc->bp++;\r
- *mqc->bp = mqc->c >> 19;\r
- mqc->c &= 0x7ffff;\r
- mqc->ct = 8;\r
- }\r
- }\r
- }\r
-}\r
-\r
-static void mqc_renorme(opj_mqc_t *mqc) {\r
- do {\r
- mqc->a <<= 1;\r
- mqc->c <<= 1;\r
- mqc->ct--;\r
- if (mqc->ct == 0) {\r
- mqc_byteout(mqc);\r
- }\r
- } while ((mqc->a & 0x8000) == 0);\r
-}\r
-\r
-static void mqc_codemps(opj_mqc_t *mqc) {\r
- mqc->a -= (*mqc->curctx)->qeval;\r
- if ((mqc->a & 0x8000) == 0) {\r
- if (mqc->a < (*mqc->curctx)->qeval) {\r
- mqc->a = (*mqc->curctx)->qeval;\r
- } else {\r
- mqc->c += (*mqc->curctx)->qeval;\r
- }\r
- *mqc->curctx = (*mqc->curctx)->nmps;\r
- mqc_renorme(mqc);\r
- } else {\r
- mqc->c += (*mqc->curctx)->qeval;\r
- }\r
-}\r
-\r
-static void mqc_codelps(opj_mqc_t *mqc) {\r
- mqc->a -= (*mqc->curctx)->qeval;\r
- if (mqc->a < (*mqc->curctx)->qeval) {\r
- mqc->c += (*mqc->curctx)->qeval;\r
- } else {\r
- mqc->a = (*mqc->curctx)->qeval;\r
- }\r
- *mqc->curctx = (*mqc->curctx)->nlps;\r
- mqc_renorme(mqc);\r
-}\r
-\r
-static void mqc_setbits(opj_mqc_t *mqc) {\r
- unsigned int tempc = mqc->c + mqc->a;\r
- mqc->c |= 0xffff;\r
- if (mqc->c >= tempc) {\r
- mqc->c -= 0x8000;\r
- }\r
-}\r
-\r
-static int mqc_mpsexchange(opj_mqc_t *mqc) {\r
- int d;\r
- if (mqc->a < (*mqc->curctx)->qeval) {\r
- d = 1 - (*mqc->curctx)->mps;\r
- *mqc->curctx = (*mqc->curctx)->nlps;\r
- } else {\r
- d = (*mqc->curctx)->mps;\r
- *mqc->curctx = (*mqc->curctx)->nmps;\r
- }\r
- \r
- return d;\r
-}\r
-\r
-static int mqc_lpsexchange(opj_mqc_t *mqc) {\r
- int d;\r
- if (mqc->a < (*mqc->curctx)->qeval) {\r
- mqc->a = (*mqc->curctx)->qeval;\r
- d = (*mqc->curctx)->mps;\r
- *mqc->curctx = (*mqc->curctx)->nmps;\r
- } else {\r
- mqc->a = (*mqc->curctx)->qeval;\r
- d = 1 - (*mqc->curctx)->mps;\r
- *mqc->curctx = (*mqc->curctx)->nlps;\r
- }\r
- \r
- return d;\r
-}\r
-\r
-static void mqc_bytein(opj_mqc_t *mqc) {\r
- if (mqc->bp != mqc->end) {\r
- unsigned int c;\r
- if (mqc->bp + 1 != mqc->end) {\r
- c = *(mqc->bp + 1);\r
- } else {\r
- c = 0xff;\r
- }\r
- if (*mqc->bp == 0xff) {\r
- if (c > 0x8f) {\r
- mqc->c += 0xff00;\r
- mqc->ct = 8;\r
- } else {\r
- mqc->bp++;\r
- mqc->c += c << 9;\r
- mqc->ct = 7;\r
- }\r
- } else {\r
- mqc->bp++;\r
- mqc->c += c << 8;\r
- mqc->ct = 8;\r
- }\r
- } else {\r
- mqc->c += 0xff00;\r
- mqc->ct = 8;\r
- }\r
-}\r
-\r
-static void mqc_renormd(opj_mqc_t *mqc) {\r
- do {\r
- if (mqc->ct == 0) {\r
- mqc_bytein(mqc);\r
- }\r
- mqc->a <<= 1;\r
- mqc->c <<= 1;\r
- mqc->ct--;\r
- } while (mqc->a < 0x8000);\r
-}\r
-\r
-/* \r
-==========================================================\r
- MQ-Coder interface\r
-==========================================================\r
-*/\r
-\r
-opj_mqc_t* mqc_create() {\r
- opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));\r
- return mqc;\r
-}\r
-\r
-void mqc_destroy(opj_mqc_t *mqc) {\r
- if(mqc) {\r
- opj_free(mqc);\r
- }\r
-}\r
-\r
-int mqc_numbytes(opj_mqc_t *mqc) {\r
- return mqc->bp - mqc->start;\r
-}\r
-\r
-void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {\r
- mqc_setcurctx(mqc, 0);\r
- mqc->a = 0x8000;\r
- mqc->c = 0;\r
- mqc->bp = bp - 1;\r
- mqc->ct = 12;\r
- if (*mqc->bp == 0xff) {\r
- mqc->ct = 13;\r
- }\r
- mqc->start = bp;\r
-}\r
-\r
-void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) {\r
- mqc->curctx = &mqc->ctxs[ctxno];\r
-}\r
-\r
-void mqc_encode(opj_mqc_t *mqc, int d) {\r
- if ((*mqc->curctx)->mps == d) {\r
- mqc_codemps(mqc);\r
- } else {\r
- mqc_codelps(mqc);\r
- }\r
-}\r
-\r
-void mqc_flush(opj_mqc_t *mqc) {\r
- mqc_setbits(mqc);\r
- mqc->c <<= mqc->ct;\r
- mqc_byteout(mqc);\r
- mqc->c <<= mqc->ct;\r
- mqc_byteout(mqc);\r
- \r
- if (*mqc->bp != 0xff) {\r
- mqc->bp++;\r
- }\r
-}\r
-\r
-void mqc_bypass_init_enc(opj_mqc_t *mqc) {\r
- mqc->c = 0;\r
- mqc->ct = 8;\r
- /*if (*mqc->bp == 0xff) {\r
- mqc->ct = 7;\r
- } */\r
-}\r
-\r
-void mqc_bypass_enc(opj_mqc_t *mqc, int d) {\r
- mqc->ct--;\r
- mqc->c = mqc->c + (d << mqc->ct);\r
- if (mqc->ct == 0) {\r
- mqc->bp++;\r
- *mqc->bp = mqc->c;\r
- mqc->ct = 8;\r
- if (*mqc->bp == 0xff) {\r
- mqc->ct = 7;\r
- }\r
- mqc->c = 0;\r
- }\r
-}\r
-\r
-int mqc_bypass_flush_enc(opj_mqc_t *mqc) {\r
- unsigned char bit_padding;\r
- \r
- bit_padding = 0;\r
- \r
- if (mqc->ct != 0) {\r
- while (mqc->ct > 0) {\r
- mqc->ct--;\r
- mqc->c += bit_padding << mqc->ct;\r
- bit_padding = (bit_padding + 1) & 0x01;\r
- }\r
- mqc->bp++;\r
- *mqc->bp = mqc->c;\r
- mqc->ct = 8;\r
- mqc->c = 0;\r
- }\r
- \r
- return 1;\r
-}\r
-\r
-void mqc_reset_enc(opj_mqc_t *mqc) {\r
- mqc_resetstates(mqc);\r
- mqc_setstate(mqc, 18, 0, 46);\r
- mqc_setstate(mqc, 0, 0, 3);\r
- mqc_setstate(mqc, 1, 0, 4);\r
-}\r
-\r
-void mqc_reset_enc_3(opj_mqc_t *mqc) {\r
- mqc_resetstates(mqc);\r
- mqc_setstate(mqc, T1_3D_CTXNO_UNI, 0, 46);\r
- mqc_setstate(mqc, T1_3D_CTXNO_AGG, 0, 3);\r
- mqc_setstate(mqc, T1_3D_CTXNO_ZC, 0, 4);\r
-}\r
-\r
-int mqc_restart_enc(opj_mqc_t *mqc) {\r
- int correction = 1;\r
- \r
- /* <flush part> */\r
- int n = 27 - 15 - mqc->ct;\r
- mqc->c <<= mqc->ct;\r
- while (n > 0) {\r
- mqc_byteout(mqc);\r
- n -= mqc->ct;\r
- mqc->c <<= mqc->ct;\r
- }\r
- mqc_byteout(mqc);\r
- \r
- return correction;\r
-}\r
-\r
-void mqc_restart_init_enc(opj_mqc_t *mqc) {\r
- /* <Re-init part> */\r
- mqc_setcurctx(mqc, 0);\r
- mqc->a = 0x8000;\r
- mqc->c = 0;\r
- mqc->ct = 12;\r
- mqc->bp--;\r
- if (*mqc->bp == 0xff) {\r
- mqc->ct = 13;\r
- }\r
-}\r
-\r
-void mqc_erterm_enc(opj_mqc_t *mqc) {\r
- int k = 11 - mqc->ct + 1;\r
- \r
- while (k > 0) {\r
- mqc->c <<= mqc->ct;\r
- mqc->ct = 0;\r
- mqc_byteout(mqc);\r
- k -= mqc->ct;\r
- }\r
- \r
- if (*mqc->bp != 0xff) {\r
- mqc_byteout(mqc);\r
- }\r
-}\r
-\r
-void mqc_segmark_enc(opj_mqc_t *mqc) {\r
- int i;\r
- mqc_setcurctx(mqc, 18);\r
- \r
- for (i = 1; i < 5; i++) {\r
- mqc_encode(mqc, i % 2);\r
- }\r
-}\r
-\r
-void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {\r
- mqc_setcurctx(mqc, 0);\r
- mqc->start = bp;\r
- mqc->end = bp + len;\r
- mqc->bp = bp;\r
- if (len==0) mqc->c = 0xff << 16;\r
- else mqc->c = *mqc->bp << 16;\r
- mqc_bytein(mqc);\r
- mqc->c <<= 7;\r
- mqc->ct -= 7;\r
- mqc->a = 0x8000;\r
-}\r
-\r
-int mqc_decode(opj_mqc_t *mqc) {\r
- int d;\r
- mqc->a -= (*mqc->curctx)->qeval;\r
- if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {\r
- d = mqc_lpsexchange(mqc);\r
- mqc_renormd(mqc);\r
- } else {\r
- mqc->c -= (*mqc->curctx)->qeval << 16;\r
- if ((mqc->a & 0x8000) == 0) {\r
- d = mqc_mpsexchange(mqc);\r
- mqc_renormd(mqc);\r
- } else {\r
- d = (*mqc->curctx)->mps;\r
- }\r
- }\r
-\r
- return d;\r
-}\r
-\r
-void mqc_resetstates(opj_mqc_t *mqc) {\r
- int i;\r
- for (i = 0; i < MQC_NUMCTXS; i++) {\r
- mqc->ctxs[i] = mqc_states;\r
- }\r
-}\r
-\r
-void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {\r
- mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];\r
-}\r
-\r
-\r
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2001-2003, David Janssens
+ * Copyright (c) 2002-2003, Yannick Verschueren
+ * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
+ * Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "opj_includes.h"
+
+/** @defgroup MQC MQC - Implementation of an MQ-Coder */
+/*@{*/
+
+/** @name Local static functions */
+/*@{*/
+
+/**
+Output a byte, doing bit-stuffing if necessary.
+After a 0xff byte, the next byte must be smaller than 0x90.
+@param mqc MQC handle
+*/
+static void mqc_byteout(opj_mqc_t *mqc);
+/**
+Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
+@param mqc MQC handle
+*/
+static void mqc_renorme(opj_mqc_t *mqc);
+/**
+Encode the most probable symbol
+@param mqc MQC handle
+*/
+static void mqc_codemps(opj_mqc_t *mqc);
+/**
+Encode the most least symbol
+@param mqc MQC handle
+*/
+static void mqc_codelps(opj_mqc_t *mqc);
+/**
+Fill mqc->c with 1's for flushing
+@param mqc MQC handle
+*/
+static void mqc_setbits(opj_mqc_t *mqc);
+/**
+Exchange MPS with LPS
+@param mqc MQC handle
+@return
+*/
+static int mqc_mpsexchange(opj_mqc_t *mqc);
+/**
+Exchange LPS with MPS
+@param mqc MQC handle
+@return
+*/
+static int mqc_lpsexchange(opj_mqc_t *mqc);
+/**
+Input a byte
+@param mqc MQC handle
+*/
+static void mqc_bytein(opj_mqc_t *mqc);
+/**
+Renormalize mqc->a and mqc->c while decoding
+@param mqc MQC handle
+*/
+static void mqc_renormd(opj_mqc_t *mqc);
+
+/*@}*/
+
+/*@}*/
+
+/* <summary> */
+/* This array defines all the possible states for a context. */
+/* </summary> */
+static opj_mqc_state_t mqc_states[47 * 2] = {
+ {0x5601, 0, &mqc_states[2], &mqc_states[3]},
+ {0x5601, 1, &mqc_states[3], &mqc_states[2]},
+ {0x3401, 0, &mqc_states[4], &mqc_states[12]},
+ {0x3401, 1, &mqc_states[5], &mqc_states[13]},
+ {0x1801, 0, &mqc_states[6], &mqc_states[18]},
+ {0x1801, 1, &mqc_states[7], &mqc_states[19]},
+ {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
+ {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
+ {0x0521, 0, &mqc_states[10], &mqc_states[58]},
+ {0x0521, 1, &mqc_states[11], &mqc_states[59]},
+ {0x0221, 0, &mqc_states[76], &mqc_states[66]},
+ {0x0221, 1, &mqc_states[77], &mqc_states[67]},
+ {0x5601, 0, &mqc_states[14], &mqc_states[13]},
+ {0x5601, 1, &mqc_states[15], &mqc_states[12]},
+ {0x5401, 0, &mqc_states[16], &mqc_states[28]},
+ {0x5401, 1, &mqc_states[17], &mqc_states[29]},
+ {0x4801, 0, &mqc_states[18], &mqc_states[28]},
+ {0x4801, 1, &mqc_states[19], &mqc_states[29]},
+ {0x3801, 0, &mqc_states[20], &mqc_states[28]},
+ {0x3801, 1, &mqc_states[21], &mqc_states[29]},
+ {0x3001, 0, &mqc_states[22], &mqc_states[34]},
+ {0x3001, 1, &mqc_states[23], &mqc_states[35]},
+ {0x2401, 0, &mqc_states[24], &mqc_states[36]},
+ {0x2401, 1, &mqc_states[25], &mqc_states[37]},
+ {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
+ {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
+ {0x1601, 0, &mqc_states[58], &mqc_states[42]},
+ {0x1601, 1, &mqc_states[59], &mqc_states[43]},
+ {0x5601, 0, &mqc_states[30], &mqc_states[29]},
+ {0x5601, 1, &mqc_states[31], &mqc_states[28]},
+ {0x5401, 0, &mqc_states[32], &mqc_states[28]},
+ {0x5401, 1, &mqc_states[33], &mqc_states[29]},
+ {0x5101, 0, &mqc_states[34], &mqc_states[30]},
+ {0x5101, 1, &mqc_states[35], &mqc_states[31]},
+ {0x4801, 0, &mqc_states[36], &mqc_states[32]},
+ {0x4801, 1, &mqc_states[37], &mqc_states[33]},
+ {0x3801, 0, &mqc_states[38], &mqc_states[34]},
+ {0x3801, 1, &mqc_states[39], &mqc_states[35]},
+ {0x3401, 0, &mqc_states[40], &mqc_states[36]},
+ {0x3401, 1, &mqc_states[41], &mqc_states[37]},
+ {0x3001, 0, &mqc_states[42], &mqc_states[38]},
+ {0x3001, 1, &mqc_states[43], &mqc_states[39]},
+ {0x2801, 0, &mqc_states[44], &mqc_states[38]},
+ {0x2801, 1, &mqc_states[45], &mqc_states[39]},
+ {0x2401, 0, &mqc_states[46], &mqc_states[40]},
+ {0x2401, 1, &mqc_states[47], &mqc_states[41]},
+ {0x2201, 0, &mqc_states[48], &mqc_states[42]},
+ {0x2201, 1, &mqc_states[49], &mqc_states[43]},
+ {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
+ {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
+ {0x1801, 0, &mqc_states[52], &mqc_states[46]},
+ {0x1801, 1, &mqc_states[53], &mqc_states[47]},
+ {0x1601, 0, &mqc_states[54], &mqc_states[48]},
+ {0x1601, 1, &mqc_states[55], &mqc_states[49]},
+ {0x1401, 0, &mqc_states[56], &mqc_states[50]},
+ {0x1401, 1, &mqc_states[57], &mqc_states[51]},
+ {0x1201, 0, &mqc_states[58], &mqc_states[52]},
+ {0x1201, 1, &mqc_states[59], &mqc_states[53]},
+ {0x1101, 0, &mqc_states[60], &mqc_states[54]},
+ {0x1101, 1, &mqc_states[61], &mqc_states[55]},
+ {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
+ {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
+ {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
+ {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
+ {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
+ {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
+ {0x0521, 0, &mqc_states[68], &mqc_states[62]},
+ {0x0521, 1, &mqc_states[69], &mqc_states[63]},
+ {0x0441, 0, &mqc_states[70], &mqc_states[64]},
+ {0x0441, 1, &mqc_states[71], &mqc_states[65]},
+ {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
+ {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
+ {0x0221, 0, &mqc_states[74], &mqc_states[68]},
+ {0x0221, 1, &mqc_states[75], &mqc_states[69]},
+ {0x0141, 0, &mqc_states[76], &mqc_states[70]},
+ {0x0141, 1, &mqc_states[77], &mqc_states[71]},
+ {0x0111, 0, &mqc_states[78], &mqc_states[72]},
+ {0x0111, 1, &mqc_states[79], &mqc_states[73]},
+ {0x0085, 0, &mqc_states[80], &mqc_states[74]},
+ {0x0085, 1, &mqc_states[81], &mqc_states[75]},
+ {0x0049, 0, &mqc_states[82], &mqc_states[76]},
+ {0x0049, 1, &mqc_states[83], &mqc_states[77]},
+ {0x0025, 0, &mqc_states[84], &mqc_states[78]},
+ {0x0025, 1, &mqc_states[85], &mqc_states[79]},
+ {0x0015, 0, &mqc_states[86], &mqc_states[80]},
+ {0x0015, 1, &mqc_states[87], &mqc_states[81]},
+ {0x0009, 0, &mqc_states[88], &mqc_states[82]},
+ {0x0009, 1, &mqc_states[89], &mqc_states[83]},
+ {0x0005, 0, &mqc_states[90], &mqc_states[84]},
+ {0x0005, 1, &mqc_states[91], &mqc_states[85]},
+ {0x0001, 0, &mqc_states[90], &mqc_states[86]},
+ {0x0001, 1, &mqc_states[91], &mqc_states[87]},
+ {0x5601, 0, &mqc_states[92], &mqc_states[92]},
+ {0x5601, 1, &mqc_states[93], &mqc_states[93]},
+};
+
+/*
+==========================================================
+ local functions
+==========================================================
+*/
+
+static void mqc_byteout(opj_mqc_t *mqc) {
+ if (*mqc->bp == 0xff) {
+ mqc->bp++;
+ *mqc->bp = mqc->c >> 20;
+ mqc->c &= 0xfffff;
+ mqc->ct = 7;
+ } else {
+ if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */
+ mqc->bp++;
+ *mqc->bp = mqc->c >> 19;
+ mqc->c &= 0x7ffff;
+ mqc->ct = 8;
+ } else {
+ (*mqc->bp)++;
+ if (*mqc->bp == 0xff) {
+ mqc->c &= 0x7ffffff;
+ mqc->bp++;
+ *mqc->bp = mqc->c >> 20;
+ mqc->c &= 0xfffff;
+ mqc->ct = 7;
+ } else {
+ mqc->bp++;
+ *mqc->bp = mqc->c >> 19;
+ mqc->c &= 0x7ffff;
+ mqc->ct = 8;
+ }
+ }
+ }
+}
+
+static void mqc_renorme(opj_mqc_t *mqc) {
+ do {
+ mqc->a <<= 1;
+ mqc->c <<= 1;
+ mqc->ct--;
+ if (mqc->ct == 0) {
+ mqc_byteout(mqc);
+ }
+ } while ((mqc->a & 0x8000) == 0);
+}
+
+static void mqc_codemps(opj_mqc_t *mqc) {
+ mqc->a -= (*mqc->curctx)->qeval;
+ if ((mqc->a & 0x8000) == 0) {
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ mqc->a = (*mqc->curctx)->qeval;
+ } else {
+ mqc->c += (*mqc->curctx)->qeval;
+ }
+ *mqc->curctx = (*mqc->curctx)->nmps;
+ mqc_renorme(mqc);
+ } else {
+ mqc->c += (*mqc->curctx)->qeval;
+ }
+}
+
+static void mqc_codelps(opj_mqc_t *mqc) {
+ mqc->a -= (*mqc->curctx)->qeval;
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ mqc->c += (*mqc->curctx)->qeval;
+ } else {
+ mqc->a = (*mqc->curctx)->qeval;
+ }
+ *mqc->curctx = (*mqc->curctx)->nlps;
+ mqc_renorme(mqc);
+}
+
+static void mqc_setbits(opj_mqc_t *mqc) {
+ unsigned int tempc = mqc->c + mqc->a;
+ mqc->c |= 0xffff;
+ if (mqc->c >= tempc) {
+ mqc->c -= 0x8000;
+ }
+}
+
+static int mqc_mpsexchange(opj_mqc_t *mqc) {
+ int d;
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ d = 1 - (*mqc->curctx)->mps;
+ *mqc->curctx = (*mqc->curctx)->nlps;
+ } else {
+ d = (*mqc->curctx)->mps;
+ *mqc->curctx = (*mqc->curctx)->nmps;
+ }
+
+ return d;
+}
+
+static int mqc_lpsexchange(opj_mqc_t *mqc) {
+ int d;
+ if (mqc->a < (*mqc->curctx)->qeval) {
+ mqc->a = (*mqc->curctx)->qeval;
+ d = (*mqc->curctx)->mps;
+ *mqc->curctx = (*mqc->curctx)->nmps;
+ } else {
+ mqc->a = (*mqc->curctx)->qeval;
+ d = 1 - (*mqc->curctx)->mps;
+ *mqc->curctx = (*mqc->curctx)->nlps;
+ }
+
+ return d;
+}
+
+static void mqc_bytein(opj_mqc_t *mqc) {
+ if (mqc->bp != mqc->end) {
+ unsigned int c;
+ if (mqc->bp + 1 != mqc->end) {
+ c = *(mqc->bp + 1);
+ } else {
+ c = 0xff;
+ }
+ if (*mqc->bp == 0xff) {
+ if (c > 0x8f) {
+ mqc->c += 0xff00;
+ mqc->ct = 8;
+ } else {
+ mqc->bp++;
+ mqc->c += c << 9;
+ mqc->ct = 7;
+ }
+ } else {
+ mqc->bp++;
+ mqc->c += c << 8;
+ mqc->ct = 8;
+ }
+ } else {
+ mqc->c += 0xff00;
+ mqc->ct = 8;
+ }
+}
+
+static void mqc_renormd(opj_mqc_t *mqc) {
+ do {
+ if (mqc->ct == 0) {
+ mqc_bytein(mqc);
+ }
+ mqc->a <<= 1;
+ mqc->c <<= 1;
+ mqc->ct--;
+ } while (mqc->a < 0x8000);
+}
+
+/*
+==========================================================
+ MQ-Coder interface
+==========================================================
+*/
+
+opj_mqc_t* mqc_create() {
+ opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
+ return mqc;
+}
+
+void mqc_destroy(opj_mqc_t *mqc) {
+ if(mqc) {
+ opj_free(mqc);
+ }
+}
+
+int mqc_numbytes(opj_mqc_t *mqc) {
+ return mqc->bp - mqc->start;
+}
+
+void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
+ mqc_setcurctx(mqc, 0);
+ mqc->a = 0x8000;
+ mqc->c = 0;
+ mqc->bp = bp - 1;
+ mqc->ct = 12;
+ if (*mqc->bp == 0xff) {
+ mqc->ct = 13;
+ }
+ mqc->start = bp;
+}
+
+void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) {
+ mqc->curctx = &mqc->ctxs[ctxno];
+}
+
+void mqc_encode(opj_mqc_t *mqc, int d) {
+ if ((*mqc->curctx)->mps == d) {
+ mqc_codemps(mqc);
+ } else {
+ mqc_codelps(mqc);
+ }
+}
+
+void mqc_flush(opj_mqc_t *mqc) {
+ mqc_setbits(mqc);
+ mqc->c <<= mqc->ct;
+ mqc_byteout(mqc);
+ mqc->c <<= mqc->ct;
+ mqc_byteout(mqc);
+
+ if (*mqc->bp != 0xff) {
+ mqc->bp++;
+ }
+}
+
+void mqc_bypass_init_enc(opj_mqc_t *mqc) {
+ mqc->c = 0;
+ mqc->ct = 8;
+ /*if (*mqc->bp == 0xff) {
+ mqc->ct = 7;
+ } */
+}
+
+void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
+ mqc->ct--;
+ mqc->c = mqc->c + (d << mqc->ct);
+ if (mqc->ct == 0) {
+ mqc->bp++;
+ *mqc->bp = mqc->c;
+ mqc->ct = 8;
+ if (*mqc->bp == 0xff) {
+ mqc->ct = 7;
+ }
+ mqc->c = 0;
+ }
+}
+
+int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
+ unsigned char bit_padding;
+
+ bit_padding = 0;
+
+ if (mqc->ct != 0) {
+ while (mqc->ct > 0) {
+ mqc->ct--;
+ mqc->c += bit_padding << mqc->ct;
+ bit_padding = (bit_padding + 1) & 0x01;
+ }
+ mqc->bp++;
+ *mqc->bp = mqc->c;
+ mqc->ct = 8;
+ mqc->c = 0;
+ }
+
+ return 1;
+}
+
+void mqc_reset_enc(opj_mqc_t *mqc) {
+ mqc_resetstates(mqc);
+ mqc_setstate(mqc, 18, 0, 46);
+ mqc_setstate(mqc, 0, 0, 3);
+ mqc_setstate(mqc, 1, 0, 4);
+}
+
+void mqc_reset_enc_3(opj_mqc_t *mqc) {
+ mqc_resetstates(mqc);
+ mqc_setstate(mqc, T1_3D_CTXNO_UNI, 0, 46);
+ mqc_setstate(mqc, T1_3D_CTXNO_AGG, 0, 3);
+ mqc_setstate(mqc, T1_3D_CTXNO_ZC, 0, 4);
+}
+
+int mqc_restart_enc(opj_mqc_t *mqc) {
+ int correction = 1;
+
+ /* <flush part> */
+ int n = 27 - 15 - mqc->ct;
+ mqc->c <<= mqc->ct;
+ while (n > 0) {
+ mqc_byteout(mqc);
+ n -= mqc->ct;
+ mqc->c <<= mqc->ct;
+ }
+ mqc_byteout(mqc);
+
+ return correction;
+}
+
+void mqc_restart_init_enc(opj_mqc_t *mqc) {
+ /* <Re-init part> */
+ mqc_setcurctx(mqc, 0);
+ mqc->a = 0x8000;
+ mqc->c = 0;
+ mqc->ct = 12;
+ mqc->bp--;
+ if (*mqc->bp == 0xff) {
+ mqc->ct = 13;
+ }
+}
+
+void mqc_erterm_enc(opj_mqc_t *mqc) {
+ int k = 11 - mqc->ct + 1;
+
+ while (k > 0) {
+ mqc->c <<= mqc->ct;
+ mqc->ct = 0;
+ mqc_byteout(mqc);
+ k -= mqc->ct;
+ }
+
+ if (*mqc->bp != 0xff) {
+ mqc_byteout(mqc);
+ }
+}
+
+void mqc_segmark_enc(opj_mqc_t *mqc) {
+ int i;
+ mqc_setcurctx(mqc, 18);
+
+ for (i = 1; i < 5; i++) {
+ mqc_encode(mqc, i % 2);
+ }
+}
+
+void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
+ mqc_setcurctx(mqc, 0);
+ mqc->start = bp;
+ mqc->end = bp + len;
+ mqc->bp = bp;
+ if (len==0) mqc->c = 0xff << 16;
+ else mqc->c = *mqc->bp << 16;
+ mqc_bytein(mqc);
+ mqc->c <<= 7;
+ mqc->ct -= 7;
+ mqc->a = 0x8000;
+}
+
+int mqc_decode(opj_mqc_t *mqc) {
+ int d;
+ mqc->a -= (*mqc->curctx)->qeval;
+ if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
+ d = mqc_lpsexchange(mqc);
+ mqc_renormd(mqc);
+ } else {
+ mqc->c -= (*mqc->curctx)->qeval << 16;
+ if ((mqc->a & 0x8000) == 0) {
+ d = mqc_mpsexchange(mqc);
+ mqc_renormd(mqc);
+ } else {
+ d = (*mqc->curctx)->mps;
+ }
+ }
+
+ return d;
+}
+
+void mqc_resetstates(opj_mqc_t *mqc) {
+ int i;
+ for (i = 0; i < MQC_NUMCTXS; i++) {
+ mqc->ctxs[i] = mqc_states;
+ }
+}
+
+void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
+ mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
+}
+
+