Update for version 0.8
[openjpeg.git] / libopenjpeg / mqc.c
1 /*
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
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28
29 #include "mqc.h"
30 #include <stdio.h>
31
32 /* <summary> */
33 /* This struct defines the state of a context. */
34 /* </summary> */
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 */
40 } mqc_state_t;
41
42 /* <summary> */
43 /* This array defines all the possible states for a context. */
44 /* </summary> */
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]},
140 };
141
142 #define MQC_NUMCTXS 32
143
144 unsigned int mqc_c;
145 unsigned int mqc_a;
146 unsigned int mqc_ct;
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;
152
153 /* <summary> */
154 /* Return the number of bytes already encoded. */
155 /* </summary> */
156 int mqc_numbytes()
157 {
158         return mqc_bp - mqc_start;
159 }
160
161 /* <summary> */
162 /* Output a byte, doing bit-stuffing if necessary. */
163 /* After a 0xff byte, the next byte must be smaller than 0x90 */
164 /* </summary> */
165 void mqc_byteout()
166 {
167         if (*mqc_bp == 0xff) {
168                 mqc_bp++;
169                 *mqc_bp = mqc_c >> 20;
170                 mqc_c &= 0xfffff;
171                 mqc_ct = 7;
172         } else {
173                 if ((mqc_c & 0x8000000) == 0) { /* ((mqc_c&0x8000000)==0) CHANGE */
174                         mqc_bp++;
175                         *mqc_bp = mqc_c >> 19;
176                         mqc_c &= 0x7ffff;
177                         mqc_ct = 8;
178                 } else {
179                         (*mqc_bp)++;
180                         if (*mqc_bp == 0xff) {
181                                 mqc_c &= 0x7ffffff;
182                                 mqc_bp++;
183                                 *mqc_bp = mqc_c >> 20;
184                                 mqc_c &= 0xfffff;
185                                 mqc_ct = 7;
186                         } else {
187                                 mqc_bp++;
188                                 *mqc_bp = mqc_c >> 19;
189                                 mqc_c &= 0x7ffff;
190                                 mqc_ct = 8;
191                         }
192                 }
193         }
194 }
195
196 /* <summary> */
197 /* Renormalize mqc_a and mqc_c while encoding, so that mqc_a stays between 0x8000 and 0x10000 */
198 /* </summary> */
199 void mqc_renorme()
200 {
201         do {
202                 mqc_a <<= 1;
203                 mqc_c <<= 1;
204                 mqc_ct--;
205                 if (mqc_ct == 0) {
206                         mqc_byteout();
207                 }
208         } while ((mqc_a & 0x8000) == 0);
209 }
210
211 /* <summary> */
212 /* Encode the most probable symbol. */
213 /* </summary> */
214 void mqc_codemps()
215 {
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;
220                 } else {
221                         mqc_c += (*mqc_curctx)->qeval;
222                 }
223                 *mqc_curctx = (*mqc_curctx)->nmps;
224                 mqc_renorme();
225         } else {
226                 mqc_c += (*mqc_curctx)->qeval;
227         }
228 }
229
230 /* <summary> */
231 /* Encode the most least symbol. */
232 /* </summary> */
233 void mqc_codelps()
234 {
235         mqc_a -= (*mqc_curctx)->qeval;
236         if (mqc_a < (*mqc_curctx)->qeval) {
237                 mqc_c += (*mqc_curctx)->qeval;
238         } else {
239                 mqc_a = (*mqc_curctx)->qeval;
240         }
241         *mqc_curctx = (*mqc_curctx)->nlps;
242         mqc_renorme();
243 }
244
245 /* <summary> */
246 /* Initialize encoder. */
247 /* </summary> */
248 /* <param name="bp">Output buffer.</param> */
249 void mqc_init_enc(unsigned char *bp)
250 {
251         mqc_setcurctx(0);
252         mqc_a = 0x8000;
253         mqc_c = 0;
254         mqc_bp = bp - 1;
255         mqc_ct = 12;
256         if (*mqc_bp == 0xff) {
257                 mqc_ct = 13;
258         }
259         mqc_start = bp;
260 }
261
262 /* <summary> */
263 /* Set current context. */
264 /* </summary> */
265 /* <param name="ctxno">Context number.</param> */
266 void mqc_setcurctx(int ctxno)
267 {
268         mqc_curctx = &mqc_ctxs[ctxno];
269 }
270
271 /* <summary> */
272 /* Encode a symbol using the MQ-coder. */
273 /* </summary> */
274 /* <param name="d"> The symbol to be encoded (0 or 1).</param> */
275 void mqc_encode(int d)
276 {
277         if ((*mqc_curctx)->mps == d) {
278                 mqc_codemps();
279         } else {
280                 mqc_codelps();
281         }
282 }
283
284 /* <summary> */
285 /* Fill mqc_c with 1's for flushing */
286 /* </summary> */
287 void mqc_setbits()
288 {
289         unsigned int tempc = mqc_c + mqc_a;
290         mqc_c |= 0xffff;
291         if (mqc_c >= tempc) {
292                 mqc_c -= 0x8000;
293         }
294 }
295
296 /* <summary> */
297 /* Flush encoded data. */
298 /* </summary> */
299 void mqc_flush()
300 {
301         mqc_setbits();
302         mqc_c <<= mqc_ct;
303         mqc_byteout();
304         mqc_c <<= mqc_ct;
305         mqc_byteout();
306
307         if (*mqc_bp != 0xff) {
308                 mqc_bp++;
309         }
310 }
311
312 /* <summary> */
313 /* not fully implemented and tested !! */
314 /* BYPASS mode switch, initialization operation */
315 /* JPEG 2000 p 505 */
316 /* </summary> */
317 void mqc_bypass_init_enc()
318 {
319         mqc_c = 0;
320         mqc_ct = 8;
321         /*if (*mqc_bp == 0xff) {
322           mqc_ct = 7;
323           }*/
324 }
325
326 /* <summary> */
327 /* not fully implemented and tested !! */
328 /* BYPASS mode switch, coding operation */
329 /* JPEG 2000 p 505 */
330 /* </summary> */
331 void mqc_bypass_enc(int d)
332 {
333         mqc_ct--;
334         mqc_c = mqc_c + (d << mqc_ct);
335         if (mqc_ct == 0)
336           {
337             mqc_bp++;
338             *mqc_bp = mqc_c;
339             mqc_ct = 8;
340             if (*mqc_bp == 0xff) {
341               mqc_ct = 7;
342             }
343             mqc_c = 0;
344           }
345 }
346
347 /* <summary> */
348 /* not fully implemented and tested !! */
349 /* BYPASS mode switch, flush operation */
350 /* </summary> */
351 int mqc_bypass_flush_enc()
352 {
353         unsigned char bit_padding;
354
355         bit_padding = 0;
356
357         if (mqc_ct != 0) {
358                 while (mqc_ct > 0) {
359                         mqc_ct--;
360                         mqc_c += bit_padding << mqc_ct;
361                         bit_padding = (bit_padding + 1) & 0x01;
362                 }
363                 mqc_bp++;
364                 *mqc_bp = mqc_c;
365                 mqc_ct = 8;
366                 mqc_c = 0;
367         }
368
369         return 1;
370 }
371
372 /* <summary> */
373 /* RESET mode switch */
374 /* </summary> */
375 void mqc_reset_enc()
376 {
377         mqc_resetstates();
378         mqc_setstate(18, 0, 46);
379         mqc_setstate(0, 0, 3);
380         mqc_setstate(1, 0, 4);
381 }
382
383 /* <summary> */
384 /* mode switch RESTART (TERMALL) */
385 /* </summary> */
386 int mqc_restart_enc()
387 {
388         int correction = 1;
389
390         /* <flush part> */
391         int n = 27 - 15 - mqc_ct;
392         mqc_c <<= mqc_ct;
393         while (n > 0) {
394                 mqc_byteout();
395                 n -= mqc_ct;
396                 mqc_c <<= mqc_ct;
397         }
398         mqc_byteout();
399
400         return correction;
401 }
402
403 /* <summary> */
404 /* mode switch RESTART (TERMALL) reinitialisation */
405 /* </summary> */
406 void mqc_restart_init_enc()
407 {
408         /* <Re-init part> */
409         mqc_setcurctx(0);
410         mqc_a = 0x8000;
411         mqc_c = 0;
412         mqc_ct = 12;
413         mqc_bp--;
414         if (*mqc_bp == 0xff) {
415                 mqc_ct = 13;
416         }
417 }
418
419
420 /* <summary> */
421 /* ERTERM mode switch  */
422 /* </summary> */
423 void mqc_erterm_enc()
424 {
425         int k = 11 - mqc_ct + 1;
426
427         while (k > 0) {
428                 mqc_c <<= mqc_ct;
429                 mqc_ct = 0;
430                 mqc_byteout();
431                 k -= mqc_ct;
432         }
433
434         if (*mqc_bp != 0xff) {
435                 mqc_byteout();
436         }
437 }
438
439 /* <summary> */
440 /* SEGMARK mode switch (SEGSYM) */
441 /* </summary> */
442 void mqc_segmark_enc()
443 {
444         int i;
445         mqc_setcurctx(18);
446
447         for (i = 1; i < 5; i++) {
448                 mqc_encode(i % 2);
449         }
450 }
451
452 /* <summary> */
453 /* </summary> */
454 int mqc_mpsexchange()
455 {
456         int d;
457         if (mqc_a < (*mqc_curctx)->qeval) {
458                 d = 1 - (*mqc_curctx)->mps;
459                 *mqc_curctx = (*mqc_curctx)->nlps;
460         } else {
461                 d = (*mqc_curctx)->mps;
462                 *mqc_curctx = (*mqc_curctx)->nmps;
463         }
464         return d;
465 }
466
467 /* <summary> */
468 /* </summary> */
469 int mqc_lpsexchange()
470 {
471         int d;
472         if (mqc_a < (*mqc_curctx)->qeval) {
473                 mqc_a = (*mqc_curctx)->qeval;
474                 d = (*mqc_curctx)->mps;
475                 *mqc_curctx = (*mqc_curctx)->nmps;
476         } else {
477                 mqc_a = (*mqc_curctx)->qeval;
478                 d = 1 - (*mqc_curctx)->mps;
479                 *mqc_curctx = (*mqc_curctx)->nlps;
480         }
481         return d;
482 }
483
484 /* <summary> */
485 /* Input a byte. */
486 /* </summary> */
487 void mqc_bytein()
488 {
489         if (mqc_bp != mqc_end) {
490                 unsigned int c;
491                 if (mqc_bp + 1 != mqc_end) {
492                         c = *(mqc_bp + 1);
493                 } else {
494                         c = 0xff;
495                 }
496                 if (*mqc_bp == 0xff) {
497                         if (c > 0x8f) {
498                                 mqc_c += 0xff00;
499                                 mqc_ct = 8;
500                         } else {
501                                 mqc_bp++;
502                                 mqc_c += c << 9;
503                                 mqc_ct = 7;
504                         }
505                 } else {
506                         mqc_bp++;
507                         mqc_c += c << 8;
508                         mqc_ct = 8;
509                 }
510         } else {
511                 mqc_c += 0xff00;
512                 mqc_ct = 8;
513         }
514 }
515
516 /* <summary> */
517 /* Renormalize mqc_a and mqc_c while decoding. */
518 /* </summary> */
519 void mqc_renormd()
520 {
521         do {
522                 if (mqc_ct == 0) {
523                         mqc_bytein();
524                 }
525                 mqc_a <<= 1;
526                 mqc_c <<= 1;
527                 mqc_ct--;
528         } while (mqc_a < 0x8000);
529 }
530
531 /* <summary> */
532 /* Initialize decoder. */
533 /* </summary> */
534 void mqc_init_dec(unsigned char *bp, int len)
535 {
536         mqc_setcurctx(0);
537         mqc_start = bp;
538         mqc_end = bp + len;
539         mqc_bp = bp;
540         mqc_c = *mqc_bp << 16;
541         mqc_bytein();
542         mqc_c <<= 7;
543         mqc_ct -= 7;
544         mqc_a = 0x8000;
545 }
546
547 /* <summary> */
548 /* Decode a symbol. */
549 /* </summary> */
550 int mqc_decode()
551 {
552         int d;
553         mqc_a -= (*mqc_curctx)->qeval;
554         if ((mqc_c >> 16) < (*mqc_curctx)->qeval) {
555                 d = mqc_lpsexchange();
556                 mqc_renormd();
557         } else {
558                 mqc_c -= (*mqc_curctx)->qeval << 16;
559                 if ((mqc_a & 0x8000) == 0) {
560                         d = mqc_mpsexchange();
561                         mqc_renormd();
562                 } else {
563                         d = (*mqc_curctx)->mps;
564                 }
565         }
566         return d;
567 }
568
569 /* <summary> */
570 /* Reset states of all contexts. */
571 /* </summary> */
572 void mqc_resetstates()
573 {
574         int i;
575         for (i = 0; i < MQC_NUMCTXS; i++) {
576                 mqc_ctxs[i] = mqc_states;
577         }
578 }
579
580 /* <summary> */
581 /* Set the state for a context. */
582 /* </summary> */
583 /* <param name="ctxno">Context number</param> */
584 /* <param name="msb">Most significant bit</param> */
585 /* <param name="prob">Index to the probability of symbols</param> */
586 void mqc_setstate(int ctxno, int msb, int prob)
587 {
588         mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
589 }