Reformatage : indent -kr $(find . -name '*.c') $(find . -name '*.h')
[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         mqc_bp++;
337         *mqc_bp = mqc_c;
338         mqc_ct = 8;
339         if (*mqc_bp == 0xff) {
340             mqc_ct = 7;
341         }
342         mqc_c = 0;
343     }
344 }
345
346 /* <summary> */
347 /* not fully implemented and tested !! */
348 /* BYPASS mode switch, flush operation */
349 /* </summary> */
350 int mqc_bypass_flush_enc()
351 {
352     unsigned char bit_padding;
353
354     bit_padding = 0;
355
356     if (mqc_ct != 0) {
357         while (mqc_ct > 0) {
358             mqc_ct--;
359             mqc_c += bit_padding << mqc_ct;
360             bit_padding = (bit_padding + 1) & 0x01;
361         }
362         mqc_bp++;
363         *mqc_bp = mqc_c;
364         mqc_ct = 8;
365         mqc_c = 0;
366     }
367
368     return 1;
369 }
370
371 /* <summary> */
372 /* RESET mode switch */
373 /* </summary> */
374 void mqc_reset_enc()
375 {
376     mqc_resetstates();
377     mqc_setstate(18, 0, 46);
378     mqc_setstate(0, 0, 3);
379     mqc_setstate(1, 0, 4);
380 }
381
382 /* <summary> */
383 /* mode switch RESTART (TERMALL) */
384 /* </summary> */
385 int mqc_restart_enc()
386 {
387     int correction = 1;
388
389     /* <flush part> */
390     int n = 27 - 15 - mqc_ct;
391     mqc_c <<= mqc_ct;
392     while (n > 0) {
393         mqc_byteout();
394         n -= mqc_ct;
395         mqc_c <<= mqc_ct;
396     }
397     mqc_byteout();
398
399     return correction;
400 }
401
402 /* <summary> */
403 /* mode switch RESTART (TERMALL) reinitialisation */
404 /* </summary> */
405 void mqc_restart_init_enc()
406 {
407     /* <Re-init part> */
408     mqc_setcurctx(0);
409     mqc_a = 0x8000;
410     mqc_c = 0;
411     mqc_ct = 12;
412     mqc_bp--;
413     if (*mqc_bp == 0xff) {
414         mqc_ct = 13;
415     }
416 }
417
418
419 /* <summary> */
420 /* ERTERM mode switch  */
421 /* </summary> */
422 void mqc_erterm_enc()
423 {
424     int k = 11 - mqc_ct + 1;
425
426     while (k > 0) {
427         mqc_c <<= mqc_ct;
428         mqc_ct = 0;
429         mqc_byteout();
430         k -= mqc_ct;
431     }
432
433     if (*mqc_bp != 0xff) {
434         mqc_byteout();
435     }
436 }
437
438 /* <summary> */
439 /* SEGMARK mode switch (SEGSYM) */
440 /* </summary> */
441 void mqc_segmark_enc()
442 {
443     int i;
444     mqc_setcurctx(18);
445
446     for (i = 1; i < 5; i++) {
447         mqc_encode(i % 2);
448     }
449 }
450
451 /* <summary> */
452 /* </summary> */
453 int mqc_mpsexchange()
454 {
455     int d;
456     if (mqc_a < (*mqc_curctx)->qeval) {
457         d = 1 - (*mqc_curctx)->mps;
458         *mqc_curctx = (*mqc_curctx)->nlps;
459     } else {
460         d = (*mqc_curctx)->mps;
461         *mqc_curctx = (*mqc_curctx)->nmps;
462     }
463     return d;
464 }
465
466 /* <summary> */
467 /* </summary> */
468 int mqc_lpsexchange()
469 {
470     int d;
471     if (mqc_a < (*mqc_curctx)->qeval) {
472         mqc_a = (*mqc_curctx)->qeval;
473         d = (*mqc_curctx)->mps;
474         *mqc_curctx = (*mqc_curctx)->nmps;
475     } else {
476         mqc_a = (*mqc_curctx)->qeval;
477         d = 1 - (*mqc_curctx)->mps;
478         *mqc_curctx = (*mqc_curctx)->nlps;
479     }
480     return d;
481 }
482
483 /* <summary> */
484 /* Input a byte. */
485 /* </summary> */
486 void mqc_bytein()
487 {
488     if (mqc_bp != mqc_end) {
489         unsigned int c;
490         if (mqc_bp + 1 != mqc_end) {
491             c = *(mqc_bp + 1);
492         } else {
493             c = 0xff;
494         }
495         if (*mqc_bp == 0xff) {
496             if (c > 0x8f) {
497                 mqc_c += 0xff00;
498                 mqc_ct = 8;
499             } else {
500                 mqc_bp++;
501                 mqc_c += c << 9;
502                 mqc_ct = 7;
503             }
504         } else {
505             mqc_bp++;
506             mqc_c += c << 8;
507             mqc_ct = 8;
508         }
509     } else {
510         mqc_c += 0xff00;
511         mqc_ct = 8;
512     }
513 }
514
515 /* <summary> */
516 /* Renormalize mqc_a and mqc_c while decoding. */
517 /* </summary> */
518 void mqc_renormd()
519 {
520     do {
521         if (mqc_ct == 0) {
522             mqc_bytein();
523         }
524         mqc_a <<= 1;
525         mqc_c <<= 1;
526         mqc_ct--;
527     } while (mqc_a < 0x8000);
528 }
529
530 /* <summary> */
531 /* Initialize decoder. */
532 /* </summary> */
533 void mqc_init_dec(unsigned char *bp, int len)
534 {
535     mqc_setcurctx(0);
536     mqc_start = bp;
537     mqc_end = bp + len;
538     mqc_bp = bp;
539     mqc_c = *mqc_bp << 16;
540     mqc_bytein();
541     mqc_c <<= 7;
542     mqc_ct -= 7;
543     mqc_a = 0x8000;
544 }
545
546 /* <summary> */
547 /* Decode a symbol. */
548 /* </summary> */
549 int mqc_decode()
550 {
551     int d;
552     mqc_a -= (*mqc_curctx)->qeval;
553     if ((mqc_c >> 16) < (*mqc_curctx)->qeval) {
554         d = mqc_lpsexchange();
555         mqc_renormd();
556     } else {
557         mqc_c -= (*mqc_curctx)->qeval << 16;
558         if ((mqc_a & 0x8000) == 0) {
559             d = mqc_mpsexchange();
560             mqc_renormd();
561         } else {
562             d = (*mqc_curctx)->mps;
563         }
564     }
565     return d;
566 }
567
568 /* <summary> */
569 /* Reset states of all contexts. */
570 /* </summary> */
571 void mqc_resetstates()
572 {
573     int i;
574     for (i = 0; i < MQC_NUMCTXS; i++) {
575         mqc_ctxs[i] = mqc_states;
576     }
577 }
578
579 /* <summary> */
580 /* Set the state for a context. */
581 /* </summary> */
582 /* <param name="ctxno">Context number</param> */
583 /* <param name="msb">Most significant bit</param> */
584 /* <param name="prob">Index to the probability of symbols</param> */
585 void mqc_setstate(int ctxno, int msb, int prob)
586 {
587     mqc_ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
588 }