2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
33 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
36 /** @name Local static functions */
40 Output a byte, doing bit-stuffing if necessary.
41 After a 0xff byte, the next byte must be smaller than 0x90.
44 static void mqc_byteout(opj_mqc_t *mqc);
46 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
49 static void mqc_renorme(opj_mqc_t *mqc);
51 Encode the most probable symbol
54 static void mqc_codemps(opj_mqc_t *mqc);
56 Encode the most least symbol
59 static void mqc_codelps(opj_mqc_t *mqc);
61 Fill mqc->c with 1's for flushing
64 static void mqc_setbits(opj_mqc_t *mqc);
66 FIXME: documentation ???
70 static int mqc_mpsexchange(opj_mqc_t *mqc);
72 FIXME: documentation ???
76 static int mqc_lpsexchange(opj_mqc_t *mqc);
81 static void mqc_bytein(opj_mqc_t *mqc);
83 Renormalize mqc->a and mqc->c while decoding
86 static void mqc_renormd(opj_mqc_t *mqc);
93 /* This array defines all the possible states for a context. */
95 static opj_mqc_state_t mqc_states[47 * 2] = {
96 {0x5601, 0, &mqc_states[2], &mqc_states[3]},
97 {0x5601, 1, &mqc_states[3], &mqc_states[2]},
98 {0x3401, 0, &mqc_states[4], &mqc_states[12]},
99 {0x3401, 1, &mqc_states[5], &mqc_states[13]},
100 {0x1801, 0, &mqc_states[6], &mqc_states[18]},
101 {0x1801, 1, &mqc_states[7], &mqc_states[19]},
102 {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
103 {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
104 {0x0521, 0, &mqc_states[10], &mqc_states[58]},
105 {0x0521, 1, &mqc_states[11], &mqc_states[59]},
106 {0x0221, 0, &mqc_states[76], &mqc_states[66]},
107 {0x0221, 1, &mqc_states[77], &mqc_states[67]},
108 {0x5601, 0, &mqc_states[14], &mqc_states[13]},
109 {0x5601, 1, &mqc_states[15], &mqc_states[12]},
110 {0x5401, 0, &mqc_states[16], &mqc_states[28]},
111 {0x5401, 1, &mqc_states[17], &mqc_states[29]},
112 {0x4801, 0, &mqc_states[18], &mqc_states[28]},
113 {0x4801, 1, &mqc_states[19], &mqc_states[29]},
114 {0x3801, 0, &mqc_states[20], &mqc_states[28]},
115 {0x3801, 1, &mqc_states[21], &mqc_states[29]},
116 {0x3001, 0, &mqc_states[22], &mqc_states[34]},
117 {0x3001, 1, &mqc_states[23], &mqc_states[35]},
118 {0x2401, 0, &mqc_states[24], &mqc_states[36]},
119 {0x2401, 1, &mqc_states[25], &mqc_states[37]},
120 {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
121 {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
122 {0x1601, 0, &mqc_states[58], &mqc_states[42]},
123 {0x1601, 1, &mqc_states[59], &mqc_states[43]},
124 {0x5601, 0, &mqc_states[30], &mqc_states[29]},
125 {0x5601, 1, &mqc_states[31], &mqc_states[28]},
126 {0x5401, 0, &mqc_states[32], &mqc_states[28]},
127 {0x5401, 1, &mqc_states[33], &mqc_states[29]},
128 {0x5101, 0, &mqc_states[34], &mqc_states[30]},
129 {0x5101, 1, &mqc_states[35], &mqc_states[31]},
130 {0x4801, 0, &mqc_states[36], &mqc_states[32]},
131 {0x4801, 1, &mqc_states[37], &mqc_states[33]},
132 {0x3801, 0, &mqc_states[38], &mqc_states[34]},
133 {0x3801, 1, &mqc_states[39], &mqc_states[35]},
134 {0x3401, 0, &mqc_states[40], &mqc_states[36]},
135 {0x3401, 1, &mqc_states[41], &mqc_states[37]},
136 {0x3001, 0, &mqc_states[42], &mqc_states[38]},
137 {0x3001, 1, &mqc_states[43], &mqc_states[39]},
138 {0x2801, 0, &mqc_states[44], &mqc_states[38]},
139 {0x2801, 1, &mqc_states[45], &mqc_states[39]},
140 {0x2401, 0, &mqc_states[46], &mqc_states[40]},
141 {0x2401, 1, &mqc_states[47], &mqc_states[41]},
142 {0x2201, 0, &mqc_states[48], &mqc_states[42]},
143 {0x2201, 1, &mqc_states[49], &mqc_states[43]},
144 {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
145 {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
146 {0x1801, 0, &mqc_states[52], &mqc_states[46]},
147 {0x1801, 1, &mqc_states[53], &mqc_states[47]},
148 {0x1601, 0, &mqc_states[54], &mqc_states[48]},
149 {0x1601, 1, &mqc_states[55], &mqc_states[49]},
150 {0x1401, 0, &mqc_states[56], &mqc_states[50]},
151 {0x1401, 1, &mqc_states[57], &mqc_states[51]},
152 {0x1201, 0, &mqc_states[58], &mqc_states[52]},
153 {0x1201, 1, &mqc_states[59], &mqc_states[53]},
154 {0x1101, 0, &mqc_states[60], &mqc_states[54]},
155 {0x1101, 1, &mqc_states[61], &mqc_states[55]},
156 {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
157 {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
158 {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
159 {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
160 {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
161 {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
162 {0x0521, 0, &mqc_states[68], &mqc_states[62]},
163 {0x0521, 1, &mqc_states[69], &mqc_states[63]},
164 {0x0441, 0, &mqc_states[70], &mqc_states[64]},
165 {0x0441, 1, &mqc_states[71], &mqc_states[65]},
166 {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
167 {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
168 {0x0221, 0, &mqc_states[74], &mqc_states[68]},
169 {0x0221, 1, &mqc_states[75], &mqc_states[69]},
170 {0x0141, 0, &mqc_states[76], &mqc_states[70]},
171 {0x0141, 1, &mqc_states[77], &mqc_states[71]},
172 {0x0111, 0, &mqc_states[78], &mqc_states[72]},
173 {0x0111, 1, &mqc_states[79], &mqc_states[73]},
174 {0x0085, 0, &mqc_states[80], &mqc_states[74]},
175 {0x0085, 1, &mqc_states[81], &mqc_states[75]},
176 {0x0049, 0, &mqc_states[82], &mqc_states[76]},
177 {0x0049, 1, &mqc_states[83], &mqc_states[77]},
178 {0x0025, 0, &mqc_states[84], &mqc_states[78]},
179 {0x0025, 1, &mqc_states[85], &mqc_states[79]},
180 {0x0015, 0, &mqc_states[86], &mqc_states[80]},
181 {0x0015, 1, &mqc_states[87], &mqc_states[81]},
182 {0x0009, 0, &mqc_states[88], &mqc_states[82]},
183 {0x0009, 1, &mqc_states[89], &mqc_states[83]},
184 {0x0005, 0, &mqc_states[90], &mqc_states[84]},
185 {0x0005, 1, &mqc_states[91], &mqc_states[85]},
186 {0x0001, 0, &mqc_states[90], &mqc_states[86]},
187 {0x0001, 1, &mqc_states[91], &mqc_states[87]},
188 {0x5601, 0, &mqc_states[92], &mqc_states[92]},
189 {0x5601, 1, &mqc_states[93], &mqc_states[93]},
193 ==========================================================
195 ==========================================================
198 static void mqc_byteout(opj_mqc_t *mqc) {
199 if (*mqc->bp == 0xff) {
201 *mqc->bp = mqc->c >> 20;
205 if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */
207 *mqc->bp = mqc->c >> 19;
212 if (*mqc->bp == 0xff) {
215 *mqc->bp = mqc->c >> 20;
220 *mqc->bp = mqc->c >> 19;
228 static void mqc_renorme(opj_mqc_t *mqc) {
236 } while ((mqc->a & 0x8000) == 0);
239 static void mqc_codemps(opj_mqc_t *mqc) {
240 mqc->a -= (*mqc->curctx)->qeval;
241 if ((mqc->a & 0x8000) == 0) {
242 if (mqc->a < (*mqc->curctx)->qeval) {
243 mqc->a = (*mqc->curctx)->qeval;
245 mqc->c += (*mqc->curctx)->qeval;
247 *mqc->curctx = (*mqc->curctx)->nmps;
250 mqc->c += (*mqc->curctx)->qeval;
254 static void mqc_codelps(opj_mqc_t *mqc) {
255 mqc->a -= (*mqc->curctx)->qeval;
256 if (mqc->a < (*mqc->curctx)->qeval) {
257 mqc->c += (*mqc->curctx)->qeval;
259 mqc->a = (*mqc->curctx)->qeval;
261 *mqc->curctx = (*mqc->curctx)->nlps;
265 static void mqc_setbits(opj_mqc_t *mqc) {
266 unsigned int tempc = mqc->c + mqc->a;
268 if (mqc->c >= tempc) {
273 static int mqc_mpsexchange(opj_mqc_t *mqc) {
275 if (mqc->a < (*mqc->curctx)->qeval) {
276 d = 1 - (*mqc->curctx)->mps;
277 *mqc->curctx = (*mqc->curctx)->nlps;
279 d = (*mqc->curctx)->mps;
280 *mqc->curctx = (*mqc->curctx)->nmps;
286 static int mqc_lpsexchange(opj_mqc_t *mqc) {
288 if (mqc->a < (*mqc->curctx)->qeval) {
289 mqc->a = (*mqc->curctx)->qeval;
290 d = (*mqc->curctx)->mps;
291 *mqc->curctx = (*mqc->curctx)->nmps;
293 mqc->a = (*mqc->curctx)->qeval;
294 d = 1 - (*mqc->curctx)->mps;
295 *mqc->curctx = (*mqc->curctx)->nlps;
301 static void mqc_bytein(opj_mqc_t *mqc) {
302 if (mqc->bp != mqc->end) {
304 if (mqc->bp + 1 != mqc->end) {
309 if (*mqc->bp == 0xff) {
329 static void mqc_renormd(opj_mqc_t *mqc) {
337 } while (mqc->a < 0x8000);
341 ==========================================================
343 ==========================================================
346 opj_mqc_t* mqc_create() {
347 opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
351 void mqc_destroy(opj_mqc_t *mqc) {
357 int mqc_numbytes(opj_mqc_t *mqc) {
358 return mqc->bp - mqc->start;
361 void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
362 mqc_setcurctx(mqc, 0);
367 if (*mqc->bp == 0xff) {
373 void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) {
374 mqc->curctx = &mqc->ctxs[ctxno];
377 void mqc_encode(opj_mqc_t *mqc, int d) {
378 if ((*mqc->curctx)->mps == d) {
385 void mqc_flush(opj_mqc_t *mqc) {
392 if (*mqc->bp != 0xff) {
397 void mqc_bypass_init_enc(opj_mqc_t *mqc) {
400 /*if (*mqc->bp == 0xff) {
405 void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
407 mqc->c = mqc->c + (d << mqc->ct);
412 if (*mqc->bp == 0xff) {
419 int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
420 unsigned char bit_padding;
425 while (mqc->ct > 0) {
427 mqc->c += bit_padding << mqc->ct;
428 bit_padding = (bit_padding + 1) & 0x01;
439 void mqc_reset_enc(opj_mqc_t *mqc) {
440 mqc_resetstates(mqc);
441 mqc_setstate(mqc, 18, 0, 46);
442 mqc_setstate(mqc, 0, 0, 3);
443 mqc_setstate(mqc, 1, 0, 4);
446 int mqc_restart_enc(opj_mqc_t *mqc) {
450 int n = 27 - 15 - mqc->ct;
462 void mqc_restart_init_enc(opj_mqc_t *mqc) {
464 mqc_setcurctx(mqc, 0);
469 if (*mqc->bp == 0xff) {
474 void mqc_erterm_enc(opj_mqc_t *mqc) {
475 int k = 11 - mqc->ct + 1;
484 if (*mqc->bp != 0xff) {
489 void mqc_segmark_enc(opj_mqc_t *mqc) {
491 mqc_setcurctx(mqc, 18);
493 for (i = 1; i < 5; i++) {
494 mqc_encode(mqc, i % 2);
498 void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
499 mqc_setcurctx(mqc, 0);
503 if (len==0) mqc->c = 0xff << 16;
504 else mqc->c = *mqc->bp << 16;
511 int mqc_decode(opj_mqc_t *mqc) {
513 mqc->a -= (*mqc->curctx)->qeval;
514 if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
515 d = mqc_lpsexchange(mqc);
518 mqc->c -= (*mqc->curctx)->qeval << 16;
519 if ((mqc->a & 0x8000) == 0) {
520 d = mqc_mpsexchange(mqc);
523 d = (*mqc->curctx)->mps;
530 void mqc_resetstates(opj_mqc_t *mqc) {
532 for (i = 0; i < MQC_NUMCTXS; i++) {
533 mqc->ctxs[i] = mqc_states;
537 void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
538 mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];