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"
34 /* This array defines all the possible states for a context. */
36 static opj_mqc_state_t mqc_states[47 * 2] = {
37 {0x5601, 0, &mqc_states[2], &mqc_states[3]},
38 {0x5601, 1, &mqc_states[3], &mqc_states[2]},
39 {0x3401, 0, &mqc_states[4], &mqc_states[12]},
40 {0x3401, 1, &mqc_states[5], &mqc_states[13]},
41 {0x1801, 0, &mqc_states[6], &mqc_states[18]},
42 {0x1801, 1, &mqc_states[7], &mqc_states[19]},
43 {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
44 {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
45 {0x0521, 0, &mqc_states[10], &mqc_states[58]},
46 {0x0521, 1, &mqc_states[11], &mqc_states[59]},
47 {0x0221, 0, &mqc_states[76], &mqc_states[66]},
48 {0x0221, 1, &mqc_states[77], &mqc_states[67]},
49 {0x5601, 0, &mqc_states[14], &mqc_states[13]},
50 {0x5601, 1, &mqc_states[15], &mqc_states[12]},
51 {0x5401, 0, &mqc_states[16], &mqc_states[28]},
52 {0x5401, 1, &mqc_states[17], &mqc_states[29]},
53 {0x4801, 0, &mqc_states[18], &mqc_states[28]},
54 {0x4801, 1, &mqc_states[19], &mqc_states[29]},
55 {0x3801, 0, &mqc_states[20], &mqc_states[28]},
56 {0x3801, 1, &mqc_states[21], &mqc_states[29]},
57 {0x3001, 0, &mqc_states[22], &mqc_states[34]},
58 {0x3001, 1, &mqc_states[23], &mqc_states[35]},
59 {0x2401, 0, &mqc_states[24], &mqc_states[36]},
60 {0x2401, 1, &mqc_states[25], &mqc_states[37]},
61 {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
62 {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
63 {0x1601, 0, &mqc_states[58], &mqc_states[42]},
64 {0x1601, 1, &mqc_states[59], &mqc_states[43]},
65 {0x5601, 0, &mqc_states[30], &mqc_states[29]},
66 {0x5601, 1, &mqc_states[31], &mqc_states[28]},
67 {0x5401, 0, &mqc_states[32], &mqc_states[28]},
68 {0x5401, 1, &mqc_states[33], &mqc_states[29]},
69 {0x5101, 0, &mqc_states[34], &mqc_states[30]},
70 {0x5101, 1, &mqc_states[35], &mqc_states[31]},
71 {0x4801, 0, &mqc_states[36], &mqc_states[32]},
72 {0x4801, 1, &mqc_states[37], &mqc_states[33]},
73 {0x3801, 0, &mqc_states[38], &mqc_states[34]},
74 {0x3801, 1, &mqc_states[39], &mqc_states[35]},
75 {0x3401, 0, &mqc_states[40], &mqc_states[36]},
76 {0x3401, 1, &mqc_states[41], &mqc_states[37]},
77 {0x3001, 0, &mqc_states[42], &mqc_states[38]},
78 {0x3001, 1, &mqc_states[43], &mqc_states[39]},
79 {0x2801, 0, &mqc_states[44], &mqc_states[38]},
80 {0x2801, 1, &mqc_states[45], &mqc_states[39]},
81 {0x2401, 0, &mqc_states[46], &mqc_states[40]},
82 {0x2401, 1, &mqc_states[47], &mqc_states[41]},
83 {0x2201, 0, &mqc_states[48], &mqc_states[42]},
84 {0x2201, 1, &mqc_states[49], &mqc_states[43]},
85 {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
86 {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
87 {0x1801, 0, &mqc_states[52], &mqc_states[46]},
88 {0x1801, 1, &mqc_states[53], &mqc_states[47]},
89 {0x1601, 0, &mqc_states[54], &mqc_states[48]},
90 {0x1601, 1, &mqc_states[55], &mqc_states[49]},
91 {0x1401, 0, &mqc_states[56], &mqc_states[50]},
92 {0x1401, 1, &mqc_states[57], &mqc_states[51]},
93 {0x1201, 0, &mqc_states[58], &mqc_states[52]},
94 {0x1201, 1, &mqc_states[59], &mqc_states[53]},
95 {0x1101, 0, &mqc_states[60], &mqc_states[54]},
96 {0x1101, 1, &mqc_states[61], &mqc_states[55]},
97 {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
98 {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
99 {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
100 {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
101 {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
102 {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
103 {0x0521, 0, &mqc_states[68], &mqc_states[62]},
104 {0x0521, 1, &mqc_states[69], &mqc_states[63]},
105 {0x0441, 0, &mqc_states[70], &mqc_states[64]},
106 {0x0441, 1, &mqc_states[71], &mqc_states[65]},
107 {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
108 {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
109 {0x0221, 0, &mqc_states[74], &mqc_states[68]},
110 {0x0221, 1, &mqc_states[75], &mqc_states[69]},
111 {0x0141, 0, &mqc_states[76], &mqc_states[70]},
112 {0x0141, 1, &mqc_states[77], &mqc_states[71]},
113 {0x0111, 0, &mqc_states[78], &mqc_states[72]},
114 {0x0111, 1, &mqc_states[79], &mqc_states[73]},
115 {0x0085, 0, &mqc_states[80], &mqc_states[74]},
116 {0x0085, 1, &mqc_states[81], &mqc_states[75]},
117 {0x0049, 0, &mqc_states[82], &mqc_states[76]},
118 {0x0049, 1, &mqc_states[83], &mqc_states[77]},
119 {0x0025, 0, &mqc_states[84], &mqc_states[78]},
120 {0x0025, 1, &mqc_states[85], &mqc_states[79]},
121 {0x0015, 0, &mqc_states[86], &mqc_states[80]},
122 {0x0015, 1, &mqc_states[87], &mqc_states[81]},
123 {0x0009, 0, &mqc_states[88], &mqc_states[82]},
124 {0x0009, 1, &mqc_states[89], &mqc_states[83]},
125 {0x0005, 0, &mqc_states[90], &mqc_states[84]},
126 {0x0005, 1, &mqc_states[91], &mqc_states[85]},
127 {0x0001, 0, &mqc_states[90], &mqc_states[86]},
128 {0x0001, 1, &mqc_states[91], &mqc_states[87]},
129 {0x5601, 0, &mqc_states[92], &mqc_states[92]},
130 {0x5601, 1, &mqc_states[93], &mqc_states[93]},
134 ==========================================================
136 ==========================================================
139 static void mqc_byteout(opj_mqc_t *mqc) {
140 if (*mqc->bp == 0xff) {
142 *mqc->bp = mqc->c >> 20;
146 if ((mqc->c & 0x8000000) == 0) { /* ((mqc->c&0x8000000)==0) CHANGE */
148 *mqc->bp = mqc->c >> 19;
153 if (*mqc->bp == 0xff) {
156 *mqc->bp = mqc->c >> 20;
161 *mqc->bp = mqc->c >> 19;
169 static void mqc_renorme(opj_mqc_t *mqc) {
177 } while ((mqc->a & 0x8000) == 0);
180 static void mqc_codemps(opj_mqc_t *mqc) {
181 mqc->a -= (*mqc->curctx)->qeval;
182 if ((mqc->a & 0x8000) == 0) {
183 if (mqc->a < (*mqc->curctx)->qeval) {
184 mqc->a = (*mqc->curctx)->qeval;
186 mqc->c += (*mqc->curctx)->qeval;
188 *mqc->curctx = (*mqc->curctx)->nmps;
191 mqc->c += (*mqc->curctx)->qeval;
195 static void mqc_codelps(opj_mqc_t *mqc) {
196 mqc->a -= (*mqc->curctx)->qeval;
197 if (mqc->a < (*mqc->curctx)->qeval) {
198 mqc->c += (*mqc->curctx)->qeval;
200 mqc->a = (*mqc->curctx)->qeval;
202 *mqc->curctx = (*mqc->curctx)->nlps;
206 static void mqc_setbits(opj_mqc_t *mqc) {
207 unsigned int tempc = mqc->c + mqc->a;
209 if (mqc->c >= tempc) {
214 static int mqc_mpsexchange(opj_mqc_t *mqc) {
216 if (mqc->a < (*mqc->curctx)->qeval) {
217 d = 1 - (*mqc->curctx)->mps;
218 *mqc->curctx = (*mqc->curctx)->nlps;
220 d = (*mqc->curctx)->mps;
221 *mqc->curctx = (*mqc->curctx)->nmps;
227 static int mqc_lpsexchange(opj_mqc_t *mqc) {
229 if (mqc->a < (*mqc->curctx)->qeval) {
230 mqc->a = (*mqc->curctx)->qeval;
231 d = (*mqc->curctx)->mps;
232 *mqc->curctx = (*mqc->curctx)->nmps;
234 mqc->a = (*mqc->curctx)->qeval;
235 d = 1 - (*mqc->curctx)->mps;
236 *mqc->curctx = (*mqc->curctx)->nlps;
242 static void mqc_bytein(opj_mqc_t *mqc) {
243 if (mqc->bp != mqc->end) {
245 if (mqc->bp + 1 != mqc->end) {
250 if (*mqc->bp == 0xff) {
270 static void mqc_renormd(opj_mqc_t *mqc) {
278 } while (mqc->a < 0x8000);
282 ==========================================================
284 ==========================================================
287 opj_mqc_t* mqc_create() {
288 opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
292 void mqc_destroy(opj_mqc_t *mqc) {
298 int mqc_numbytes(opj_mqc_t *mqc) {
299 return mqc->bp - mqc->start;
302 void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
303 mqc_setcurctx(mqc, 0);
308 if (*mqc->bp == 0xff) {
314 void mqc_setcurctx(opj_mqc_t *mqc, int ctxno) {
315 mqc->curctx = &mqc->ctxs[ctxno];
318 void mqc_encode(opj_mqc_t *mqc, int d) {
319 if ((*mqc->curctx)->mps == d) {
326 void mqc_flush(opj_mqc_t *mqc) {
333 if (*mqc->bp != 0xff) {
338 void mqc_bypass_init_enc(opj_mqc_t *mqc) {
341 /*if (*mqc->bp == 0xff) {
346 void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
348 mqc->c = mqc->c + (d << mqc->ct);
353 if (*mqc->bp == 0xff) {
360 int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
361 unsigned char bit_padding;
366 while (mqc->ct > 0) {
368 mqc->c += bit_padding << mqc->ct;
369 bit_padding = (bit_padding + 1) & 0x01;
380 void mqc_reset_enc(opj_mqc_t *mqc) {
381 mqc_resetstates(mqc);
382 mqc_setstate(mqc, 18, 0, 46);
383 mqc_setstate(mqc, 0, 0, 3);
384 mqc_setstate(mqc, 1, 0, 4);
387 int mqc_restart_enc(opj_mqc_t *mqc) {
391 int n = 27 - 15 - mqc->ct;
403 void mqc_restart_init_enc(opj_mqc_t *mqc) {
405 mqc_setcurctx(mqc, 0);
410 if (*mqc->bp == 0xff) {
415 void mqc_erterm_enc(opj_mqc_t *mqc) {
416 int k = 11 - mqc->ct + 1;
425 if (*mqc->bp != 0xff) {
430 void mqc_segmark_enc(opj_mqc_t *mqc) {
432 mqc_setcurctx(mqc, 18);
434 for (i = 1; i < 5; i++) {
435 mqc_encode(mqc, i % 2);
439 void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
440 mqc_setcurctx(mqc, 0);
444 if (len==0) mqc->c = 0xff << 16;
445 else mqc->c = *mqc->bp << 16;
452 int mqc_decode(opj_mqc_t *mqc) {
454 mqc->a -= (*mqc->curctx)->qeval;
455 if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
456 d = mqc_lpsexchange(mqc);
459 mqc->c -= (*mqc->curctx)->qeval << 16;
460 if ((mqc->a & 0x8000) == 0) {
461 d = mqc_mpsexchange(mqc);
464 d = (*mqc->curctx)->mps;
471 void mqc_resetstates(opj_mqc_t *mqc) {
473 for (i = 0; i < MQC_NUMCTXS; i++) {
474 mqc->ctxs[i] = mqc_states;
478 void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
479 mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];