2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
33 /* This struct defines the state of a context. */
35 typedef struct mqc_state_s {
36 unsigned int qeval; /* the probability of the Least Probable Symbol (0.75->0x8000, 1.5->0xffff) */
37 int mps; /* the Most Probable Symbol (0 or 1) */
38 struct mqc_state_s *nmps; /* next state if the next encoded symbol is the MPS */
39 struct mqc_state_s *nlps; /* next state if the next encoded symbol is the LPS */
43 /* This array defines all the possible states for a context. */
45 mqc_state_t mqc_states[47 * 2] = {
46 {0x5601, 0, &mqc_states[2], &mqc_states[3]},
47 {0x5601, 1, &mqc_states[3], &mqc_states[2]},
48 {0x3401, 0, &mqc_states[4], &mqc_states[12]},
49 {0x3401, 1, &mqc_states[5], &mqc_states[13]},
50 {0x1801, 0, &mqc_states[6], &mqc_states[18]},
51 {0x1801, 1, &mqc_states[7], &mqc_states[19]},
52 {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
53 {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
54 {0x0521, 0, &mqc_states[10], &mqc_states[58]},
55 {0x0521, 1, &mqc_states[11], &mqc_states[59]},
56 {0x0221, 0, &mqc_states[76], &mqc_states[66]},
57 {0x0221, 1, &mqc_states[77], &mqc_states[67]},
58 {0x5601, 0, &mqc_states[14], &mqc_states[13]},
59 {0x5601, 1, &mqc_states[15], &mqc_states[12]},
60 {0x5401, 0, &mqc_states[16], &mqc_states[28]},
61 {0x5401, 1, &mqc_states[17], &mqc_states[29]},
62 {0x4801, 0, &mqc_states[18], &mqc_states[28]},
63 {0x4801, 1, &mqc_states[19], &mqc_states[29]},
64 {0x3801, 0, &mqc_states[20], &mqc_states[28]},
65 {0x3801, 1, &mqc_states[21], &mqc_states[29]},
66 {0x3001, 0, &mqc_states[22], &mqc_states[34]},
67 {0x3001, 1, &mqc_states[23], &mqc_states[35]},
68 {0x2401, 0, &mqc_states[24], &mqc_states[36]},
69 {0x2401, 1, &mqc_states[25], &mqc_states[37]},
70 {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
71 {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
72 {0x1601, 0, &mqc_states[58], &mqc_states[42]},
73 {0x1601, 1, &mqc_states[59], &mqc_states[43]},
74 {0x5601, 0, &mqc_states[30], &mqc_states[29]},
75 {0x5601, 1, &mqc_states[31], &mqc_states[28]},
76 {0x5401, 0, &mqc_states[32], &mqc_states[28]},
77 {0x5401, 1, &mqc_states[33], &mqc_states[29]},
78 {0x5101, 0, &mqc_states[34], &mqc_states[30]},
79 {0x5101, 1, &mqc_states[35], &mqc_states[31]},
80 {0x4801, 0, &mqc_states[36], &mqc_states[32]},
81 {0x4801, 1, &mqc_states[37], &mqc_states[33]},
82 {0x3801, 0, &mqc_states[38], &mqc_states[34]},
83 {0x3801, 1, &mqc_states[39], &mqc_states[35]},
84 {0x3401, 0, &mqc_states[40], &mqc_states[36]},
85 {0x3401, 1, &mqc_states[41], &mqc_states[37]},
86 {0x3001, 0, &mqc_states[42], &mqc_states[38]},
87 {0x3001, 1, &mqc_states[43], &mqc_states[39]},
88 {0x2801, 0, &mqc_states[44], &mqc_states[38]},
89 {0x2801, 1, &mqc_states[45], &mqc_states[39]},
90 {0x2401, 0, &mqc_states[46], &mqc_states[40]},
91 {0x2401, 1, &mqc_states[47], &mqc_states[41]},
92 {0x2201, 0, &mqc_states[48], &mqc_states[42]},
93 {0x2201, 1, &mqc_states[49], &mqc_states[43]},
94 {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
95 {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
96 {0x1801, 0, &mqc_states[52], &mqc_states[46]},
97 {0x1801, 1, &mqc_states[53], &mqc_states[47]},
98 {0x1601, 0, &mqc_states[54], &mqc_states[48]},
99 {0x1601, 1, &mqc_states[55], &mqc_states[49]},
100 {0x1401, 0, &mqc_states[56], &mqc_states[50]},
101 {0x1401, 1, &mqc_states[57], &mqc_states[51]},
102 {0x1201, 0, &mqc_states[58], &mqc_states[52]},
103 {0x1201, 1, &mqc_states[59], &mqc_states[53]},
104 {0x1101, 0, &mqc_states[60], &mqc_states[54]},
105 {0x1101, 1, &mqc_states[61], &mqc_states[55]},
106 {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
107 {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
108 {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
109 {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
110 {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
111 {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
112 {0x0521, 0, &mqc_states[68], &mqc_states[62]},
113 {0x0521, 1, &mqc_states[69], &mqc_states[63]},
114 {0x0441, 0, &mqc_states[70], &mqc_states[64]},
115 {0x0441, 1, &mqc_states[71], &mqc_states[65]},
116 {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
117 {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
118 {0x0221, 0, &mqc_states[74], &mqc_states[68]},
119 {0x0221, 1, &mqc_states[75], &mqc_states[69]},
120 {0x0141, 0, &mqc_states[76], &mqc_states[70]},
121 {0x0141, 1, &mqc_states[77], &mqc_states[71]},
122 {0x0111, 0, &mqc_states[78], &mqc_states[72]},
123 {0x0111, 1, &mqc_states[79], &mqc_states[73]},
124 {0x0085, 0, &mqc_states[80], &mqc_states[74]},
125 {0x0085, 1, &mqc_states[81], &mqc_states[75]},
126 {0x0049, 0, &mqc_states[82], &mqc_states[76]},
127 {0x0049, 1, &mqc_states[83], &mqc_states[77]},
128 {0x0025, 0, &mqc_states[84], &mqc_states[78]},
129 {0x0025, 1, &mqc_states[85], &mqc_states[79]},
130 {0x0015, 0, &mqc_states[86], &mqc_states[80]},
131 {0x0015, 1, &mqc_states[87], &mqc_states[81]},
132 {0x0009, 0, &mqc_states[88], &mqc_states[82]},
133 {0x0009, 1, &mqc_states[89], &mqc_states[83]},
134 {0x0005, 0, &mqc_states[90], &mqc_states[84]},
135 {0x0005, 1, &mqc_states[91], &mqc_states[85]},
136 {0x0001, 0, &mqc_states[90], &mqc_states[86]},
137 {0x0001, 1, &mqc_states[91], &mqc_states[87]},
138 {0x5601, 0, &mqc_states[92], &mqc_states[92]},
139 {0x5601, 1, &mqc_states[93], &mqc_states[93]},
142 #define MQC_NUMCTXS 32
147 unsigned char *mqc_bp;
148 unsigned char *mqc_start;
149 unsigned char *mqc_end;
150 mqc_state_t *mqc_ctxs[MQC_NUMCTXS];
151 mqc_state_t **mqc_curctx;
154 /* Return the number of bytes already encoded. */
158 return mqc_bp - mqc_start;
162 /* Output a byte, doing bit-stuffing if necessary. */
163 /* After a 0xff byte, the next byte must be smaller than 0x90 */
167 if (*mqc_bp == 0xff) {
169 *mqc_bp = mqc_c >> 20;
173 if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */
175 *mqc_bp = mqc_c >> 19;
180 if (*mqc_bp == 0xff) {
183 *mqc_bp = mqc_c >> 20;
188 *mqc_bp = mqc_c >> 19;
197 /* Renormalize mqc_a and mqc_c while encoding, so that mqc_a stays between 0x8000 and 0x10000 */
208 } while ((mqc_a & 0x8000) == 0);
212 /* Encode the most probable symbol. */
216 mqc_a -= (*mqc_curctx)->qeval;
217 if ((mqc_a & 0x8000) == 0) {
218 if (mqc_a < (*mqc_curctx)->qeval) {
219 mqc_a = (*mqc_curctx)->qeval;
221 mqc_c += (*mqc_curctx)->qeval;
223 *mqc_curctx = (*mqc_curctx)->nmps;
226 mqc_c += (*mqc_curctx)->qeval;
231 /* Encode the most least symbol. */
235 mqc_a -= (*mqc_curctx)->qeval;
236 if (mqc_a < (*mqc_curctx)->qeval) {
237 mqc_c += (*mqc_curctx)->qeval;
239 mqc_a = (*mqc_curctx)->qeval;
241 *mqc_curctx = (*mqc_curctx)->nlps;
246 /* Initialize encoder. */
248 /* <param name="bp">Output buffer.</param> */
249 void mqc_init_enc(unsigned char *bp)
256 if (*mqc_bp == 0xff) {
263 /* Set current context. */
265 /* <param name="ctxno">Context number.</param> */
266 void mqc_setcurctx(int ctxno)
268 mqc_curctx = &mqc_ctxs[ctxno];
272 /* Encode a symbol using the MQ-coder. */
274 /* <param name="d"> The symbol to be encoded (0 or 1).</param> */
275 void mqc_encode(int d)
277 if ((*mqc_curctx)->mps == d) {
285 /* Fill mqc_c with 1's for flushing */
289 unsigned int tempc = mqc_c + mqc_a;
291 if (mqc_c >= tempc) {
297 /* Flush encoded data. */
307 if (*mqc_bp != 0xff) {
313 /* not fully implemented and tested !! */
314 /* BYPASS mode switch, initialization operation */
315 /* JPEG 2000 p 505 */
317 void mqc_bypass_init_enc()
321 /*if (*mqc_bp == 0xff) {
327 /* not fully implemented and tested !! */
328 /* BYPASS mode switch, coding operation */
329 /* JPEG 2000 p 505 */
331 void mqc_bypass_enc(int d)
334 mqc_c = mqc_c + (d << mqc_ct);
339 if (*mqc_bp == 0xff) {
347 /* not fully implemented and tested !! */
348 /* BYPASS mode switch, flush operation */
350 int mqc_bypass_flush_enc()
352 unsigned char bit_padding;
359 mqc_c += bit_padding << mqc_ct;
360 bit_padding = (bit_padding + 1) & 0x01;
372 /* RESET mode switch */
377 mqc_setstate(18, 0, 46);
378 mqc_setstate(0, 0, 3);
379 mqc_setstate(1, 0, 4);
383 /* mode switch RESTART (TERMALL) */
385 int mqc_restart_enc()
390 int n = 27 - 15 - mqc_ct;
403 /* mode switch RESTART (TERMALL) reinitialisation */
405 void mqc_restart_init_enc()
413 if (*mqc_bp == 0xff) {
420 /* ERTERM mode switch */
422 void mqc_erterm_enc()
424 int k = 11 - mqc_ct + 1;
433 if (*mqc_bp != 0xff) {
439 /* SEGMARK mode switch (SEGSYM) */
441 void mqc_segmark_enc()
446 for (i = 1; i < 5; i++) {
453 int mqc_mpsexchange()
456 if (mqc_a < (*mqc_curctx)->qeval) {
457 d = 1 - (*mqc_curctx)->mps;
458 *mqc_curctx = (*mqc_curctx)->nlps;
460 d = (*mqc_curctx)->mps;
461 *mqc_curctx = (*mqc_curctx)->nmps;
468 int mqc_lpsexchange()
471 if (mqc_a < (*mqc_curctx)->qeval) {
472 mqc_a = (*mqc_curctx)->qeval;
473 d = (*mqc_curctx)->mps;
474 *mqc_curctx = (*mqc_curctx)->nmps;
476 mqc_a = (*mqc_curctx)->qeval;
477 d = 1 - (*mqc_curctx)->mps;
478 *mqc_curctx = (*mqc_curctx)->nlps;
488 if (mqc_bp != mqc_end) {
490 if (mqc_bp + 1 != mqc_end) {
495 if (*mqc_bp == 0xff) {
516 /* Renormalize mqc_a and mqc_c while decoding. */
527 } while (mqc_a < 0x8000);
531 /* Initialize decoder. */
533 void mqc_init_dec(unsigned char *bp, int len)
539 /*add antonin initbug1*/
540 if (len==0) mqc_c = 0xff << 16;
541 else mqc_c = *mqc_bp << 16;
550 /* Decode a symbol. */
555 mqc_a -= (*mqc_curctx)->qeval;
556 if ((mqc_c >> 16) < (*mqc_curctx)->qeval) {
557 d = mqc_lpsexchange();
560 mqc_c -= (*mqc_curctx)->qeval << 16;
561 if ((mqc_a & 0x8000) == 0) {
562 d = mqc_mpsexchange();
565 d = (*mqc_curctx)->mps;
572 /* Reset states of all contexts. */
574 void mqc_resetstates()
577 for (i = 0; i < MQC_NUMCTXS; i++) {
578 mqc_ctxs[i] = mqc_states;
583 /* Set the state for a context. */
585 /* <param name="ctxno">Context number</param> */
586 /* <param name="msb">Most significant bit</param> */
587 /* <param name="prob">Index to the probability of symbols</param> */
588 void mqc_setstate(int ctxno, int msb, int prob)
590 mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)];