(no commit message)
[openjpeg.git] / libopenjpeg / mqc.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "mqc.h"
34 #include "t1.h"
35 #include "opj_malloc.h"
36
37 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
38 /*@{*/
39
40 /** @name Local static functions */
41 /*@{*/
42
43 /**
44 Output a byte, doing bit-stuffing if necessary.
45 After a 0xff byte, the next byte must be smaller than 0x90.
46 @param mqc MQC handle
47 */
48 static void mqc_byteout(opj_mqc_t *mqc);
49 /**
50 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
51 @param mqc MQC handle
52 */
53 static void mqc_renorme(opj_mqc_t *mqc);
54 /**
55 Encode the most probable symbol
56 @param mqc MQC handle
57 */
58 static void mqc_codemps(opj_mqc_t *mqc);
59 /**
60 Encode the most least symbol
61 @param mqc MQC handle
62 */
63 static void mqc_codelps(opj_mqc_t *mqc);
64 /**
65 Fill mqc->c with 1's for flushing
66 @param mqc MQC handle
67 */
68 static void mqc_setbits(opj_mqc_t *mqc);
69 /**
70 FIXME: documentation ???
71 @param mqc MQC handle
72 @return 
73 */
74 static OPJ_INT32 mqc_mpsexchange(opj_mqc_t *mqc);
75 /**
76 FIXME: documentation ???
77 @param mqc MQC handle
78 @return 
79 */
80 static OPJ_INT32 mqc_lpsexchange(opj_mqc_t *mqc);
81 /**
82 Input a byte
83 @param mqc MQC handle
84 */
85 static void mqc_bytein(opj_mqc_t *mqc);
86 /**
87 Renormalize mqc->a and mqc->c while decoding
88 @param mqc MQC handle
89 */
90 static void mqc_renormd(opj_mqc_t *mqc);
91
92 /*@}*/
93
94 /*@}*/
95
96 /* <summary> */
97 /* This array defines all the possible states for a context. */
98 /* </summary> */
99 static opj_mqc_state_t mqc_states[47 * 2] = {
100         {0x5601, 0, &mqc_states[2], &mqc_states[3]},
101         {0x5601, 1, &mqc_states[3], &mqc_states[2]},
102         {0x3401, 0, &mqc_states[4], &mqc_states[12]},
103         {0x3401, 1, &mqc_states[5], &mqc_states[13]},
104         {0x1801, 0, &mqc_states[6], &mqc_states[18]},
105         {0x1801, 1, &mqc_states[7], &mqc_states[19]},
106         {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
107         {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
108         {0x0521, 0, &mqc_states[10], &mqc_states[58]},
109         {0x0521, 1, &mqc_states[11], &mqc_states[59]},
110         {0x0221, 0, &mqc_states[76], &mqc_states[66]},
111         {0x0221, 1, &mqc_states[77], &mqc_states[67]},
112         {0x5601, 0, &mqc_states[14], &mqc_states[13]},
113         {0x5601, 1, &mqc_states[15], &mqc_states[12]},
114         {0x5401, 0, &mqc_states[16], &mqc_states[28]},
115         {0x5401, 1, &mqc_states[17], &mqc_states[29]},
116         {0x4801, 0, &mqc_states[18], &mqc_states[28]},
117         {0x4801, 1, &mqc_states[19], &mqc_states[29]},
118         {0x3801, 0, &mqc_states[20], &mqc_states[28]},
119         {0x3801, 1, &mqc_states[21], &mqc_states[29]},
120         {0x3001, 0, &mqc_states[22], &mqc_states[34]},
121         {0x3001, 1, &mqc_states[23], &mqc_states[35]},
122         {0x2401, 0, &mqc_states[24], &mqc_states[36]},
123         {0x2401, 1, &mqc_states[25], &mqc_states[37]},
124         {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
125         {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
126         {0x1601, 0, &mqc_states[58], &mqc_states[42]},
127         {0x1601, 1, &mqc_states[59], &mqc_states[43]},
128         {0x5601, 0, &mqc_states[30], &mqc_states[29]},
129         {0x5601, 1, &mqc_states[31], &mqc_states[28]},
130         {0x5401, 0, &mqc_states[32], &mqc_states[28]},
131         {0x5401, 1, &mqc_states[33], &mqc_states[29]},
132         {0x5101, 0, &mqc_states[34], &mqc_states[30]},
133         {0x5101, 1, &mqc_states[35], &mqc_states[31]},
134         {0x4801, 0, &mqc_states[36], &mqc_states[32]},
135         {0x4801, 1, &mqc_states[37], &mqc_states[33]},
136         {0x3801, 0, &mqc_states[38], &mqc_states[34]},
137         {0x3801, 1, &mqc_states[39], &mqc_states[35]},
138         {0x3401, 0, &mqc_states[40], &mqc_states[36]},
139         {0x3401, 1, &mqc_states[41], &mqc_states[37]},
140         {0x3001, 0, &mqc_states[42], &mqc_states[38]},
141         {0x3001, 1, &mqc_states[43], &mqc_states[39]},
142         {0x2801, 0, &mqc_states[44], &mqc_states[38]},
143         {0x2801, 1, &mqc_states[45], &mqc_states[39]},
144         {0x2401, 0, &mqc_states[46], &mqc_states[40]},
145         {0x2401, 1, &mqc_states[47], &mqc_states[41]},
146         {0x2201, 0, &mqc_states[48], &mqc_states[42]},
147         {0x2201, 1, &mqc_states[49], &mqc_states[43]},
148         {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
149         {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
150         {0x1801, 0, &mqc_states[52], &mqc_states[46]},
151         {0x1801, 1, &mqc_states[53], &mqc_states[47]},
152         {0x1601, 0, &mqc_states[54], &mqc_states[48]},
153         {0x1601, 1, &mqc_states[55], &mqc_states[49]},
154         {0x1401, 0, &mqc_states[56], &mqc_states[50]},
155         {0x1401, 1, &mqc_states[57], &mqc_states[51]},
156         {0x1201, 0, &mqc_states[58], &mqc_states[52]},
157         {0x1201, 1, &mqc_states[59], &mqc_states[53]},
158         {0x1101, 0, &mqc_states[60], &mqc_states[54]},
159         {0x1101, 1, &mqc_states[61], &mqc_states[55]},
160         {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
161         {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
162         {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
163         {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
164         {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
165         {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
166         {0x0521, 0, &mqc_states[68], &mqc_states[62]},
167         {0x0521, 1, &mqc_states[69], &mqc_states[63]},
168         {0x0441, 0, &mqc_states[70], &mqc_states[64]},
169         {0x0441, 1, &mqc_states[71], &mqc_states[65]},
170         {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
171         {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
172         {0x0221, 0, &mqc_states[74], &mqc_states[68]},
173         {0x0221, 1, &mqc_states[75], &mqc_states[69]},
174         {0x0141, 0, &mqc_states[76], &mqc_states[70]},
175         {0x0141, 1, &mqc_states[77], &mqc_states[71]},
176         {0x0111, 0, &mqc_states[78], &mqc_states[72]},
177         {0x0111, 1, &mqc_states[79], &mqc_states[73]},
178         {0x0085, 0, &mqc_states[80], &mqc_states[74]},
179         {0x0085, 1, &mqc_states[81], &mqc_states[75]},
180         {0x0049, 0, &mqc_states[82], &mqc_states[76]},
181         {0x0049, 1, &mqc_states[83], &mqc_states[77]},
182         {0x0025, 0, &mqc_states[84], &mqc_states[78]},
183         {0x0025, 1, &mqc_states[85], &mqc_states[79]},
184         {0x0015, 0, &mqc_states[86], &mqc_states[80]},
185         {0x0015, 1, &mqc_states[87], &mqc_states[81]},
186         {0x0009, 0, &mqc_states[88], &mqc_states[82]},
187         {0x0009, 1, &mqc_states[89], &mqc_states[83]},
188         {0x0005, 0, &mqc_states[90], &mqc_states[84]},
189         {0x0005, 1, &mqc_states[91], &mqc_states[85]},
190         {0x0001, 0, &mqc_states[90], &mqc_states[86]},
191         {0x0001, 1, &mqc_states[91], &mqc_states[87]},
192         {0x5601, 0, &mqc_states[92], &mqc_states[92]},
193         {0x5601, 1, &mqc_states[93], &mqc_states[93]},
194 };
195
196 /* 
197 ==========================================================
198    local functions
199 ==========================================================
200 */
201
202 static void mqc_byteout(opj_mqc_t *mqc) {
203         if (*mqc->bp == 0xff) {
204                 mqc->bp++;
205                 *mqc->bp = mqc->c >> 20;
206                 mqc->c &= 0xfffff;
207                 mqc->ct = 7;
208         } else {
209                 if ((mqc->c & 0x8000000) == 0) {        /* ((mqc->c&0x8000000)==0) CHANGE */
210                         mqc->bp++;
211                         *mqc->bp = mqc->c >> 19;
212                         mqc->c &= 0x7ffff;
213                         mqc->ct = 8;
214                 } else {
215                         (*mqc->bp)++;
216                         if (*mqc->bp == 0xff) {
217                                 mqc->c &= 0x7ffffff;
218                                 mqc->bp++;
219                                 *mqc->bp = mqc->c >> 20;
220                                 mqc->c &= 0xfffff;
221                                 mqc->ct = 7;
222                         } else {
223                                 mqc->bp++;
224                                 *mqc->bp = mqc->c >> 19;
225                                 mqc->c &= 0x7ffff;
226                                 mqc->ct = 8;
227                         }
228                 }
229         }
230 }
231
232 static void mqc_renorme(opj_mqc_t *mqc) {
233         do {
234                 mqc->a <<= 1;
235                 mqc->c <<= 1;
236                 mqc->ct--;
237                 if (mqc->ct == 0) {
238                         mqc_byteout(mqc);
239                 }
240         } while ((mqc->a & 0x8000) == 0);
241 }
242
243 static void mqc_codemps(opj_mqc_t *mqc) {
244         mqc->a -= (*mqc->curctx)->qeval;
245         if ((mqc->a & 0x8000) == 0) {
246                 if (mqc->a < (*mqc->curctx)->qeval) {
247                         mqc->a = (*mqc->curctx)->qeval;
248                 } else {
249                         mqc->c += (*mqc->curctx)->qeval;
250                 }
251                 *mqc->curctx = (*mqc->curctx)->nmps;
252                 mqc_renorme(mqc);
253         } else {
254                 mqc->c += (*mqc->curctx)->qeval;
255         }
256 }
257
258 static void mqc_codelps(opj_mqc_t *mqc) {
259         mqc->a -= (*mqc->curctx)->qeval;
260         if (mqc->a < (*mqc->curctx)->qeval) {
261                 mqc->c += (*mqc->curctx)->qeval;
262         } else {
263                 mqc->a = (*mqc->curctx)->qeval;
264         }
265         *mqc->curctx = (*mqc->curctx)->nlps;
266         mqc_renorme(mqc);
267 }
268
269 static void mqc_setbits(opj_mqc_t *mqc) {
270         OPJ_UINT32 tempc = mqc->c + mqc->a;
271         mqc->c |= 0xffff;
272         if (mqc->c >= tempc) {
273                 mqc->c -= 0x8000;
274         }
275 }
276
277 static OPJ_INT32 mqc_mpsexchange(opj_mqc_t *mqc) {
278         OPJ_INT32 d;
279         if (mqc->a < (*mqc->curctx)->qeval) {
280                 d = 1 - (*mqc->curctx)->mps;
281                 *mqc->curctx = (*mqc->curctx)->nlps;
282         } else {
283                 d = (*mqc->curctx)->mps;
284                 *mqc->curctx = (*mqc->curctx)->nmps;
285         }
286         
287         return d;
288 }
289
290 static OPJ_INT32 mqc_lpsexchange(opj_mqc_t *mqc) {
291         OPJ_INT32 d;
292         if (mqc->a < (*mqc->curctx)->qeval) {
293                 mqc->a = (*mqc->curctx)->qeval;
294                 d = (*mqc->curctx)->mps;
295                 *mqc->curctx = (*mqc->curctx)->nmps;
296         } else {
297                 mqc->a = (*mqc->curctx)->qeval;
298                 d = 1 - (*mqc->curctx)->mps;
299                 *mqc->curctx = (*mqc->curctx)->nlps;
300         }
301         
302         return d;
303 }
304
305 static void mqc_bytein(opj_mqc_t *mqc) {
306         if (mqc->bp != mqc->end) {
307                 OPJ_UINT32 c;
308                 if (mqc->bp + 1 != mqc->end) {
309                         c = *(mqc->bp + 1);
310                 } else {
311                         c = 0xff;
312                 }
313                 if (*mqc->bp == 0xff) {
314                         if (c > 0x8f) {
315                                 mqc->c += 0xff00;
316                                 mqc->ct = 8;
317                         } else {
318                                 mqc->bp++;
319                                 mqc->c += c << 9;
320                                 mqc->ct = 7;
321                         }
322                 } else {
323                         mqc->bp++;
324                         mqc->c += c << 8;
325                         mqc->ct = 8;
326                 }
327         } else {
328                 mqc->c += 0xff00;
329                 mqc->ct = 8;
330         }
331 }
332
333 static void mqc_renormd(opj_mqc_t *mqc) {
334         do {
335                 if (mqc->ct == 0) {
336                         mqc_bytein(mqc);
337                 }
338                 mqc->a <<= 1;
339                 mqc->c <<= 1;
340                 mqc->ct--;
341         } while (mqc->a < 0x8000);
342 }
343
344 /* 
345 ==========================================================
346    MQ-Coder interface
347 ==========================================================
348 */
349
350 opj_mqc_t* mqc_create(void) {
351         opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
352         return mqc;
353 }
354
355 void mqc_destroy(opj_mqc_t *mqc) {
356         if
357                 (mqc) 
358         {
359                 opj_free(mqc);
360         }
361 }
362
363 OPJ_UINT32 mqc_numbytes(opj_mqc_t *mqc) {
364         return mqc->bp - mqc->start;
365 }
366
367 void mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) {
368         mqc_setcurctx(mqc, 0);
369         mqc->a = 0x8000;
370         mqc->c = 0;
371         mqc->bp = bp - 1;
372         *(mqc->bp) = 0;
373         mqc->ct = 12;
374         /*if (*mqc->bp == 0xff) {
375                 mqc->ct = 13;
376         }*/
377         mqc->start = bp;
378 }
379
380 void mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) {
381         if ((*mqc->curctx)->mps == d) {
382                 mqc_codemps(mqc);
383         } else {
384                 mqc_codelps(mqc);
385         }
386 }
387
388 void mqc_flush(opj_mqc_t *mqc) {
389         mqc_setbits(mqc);
390         mqc->c <<= mqc->ct;
391         mqc_byteout(mqc);
392         mqc->c <<= mqc->ct;
393         mqc_byteout(mqc);
394         
395         if (*mqc->bp != 0xff) {
396                 mqc->bp++;
397         }
398 }
399
400 void mqc_bypass_init_enc(opj_mqc_t *mqc) {
401         mqc->c = 0;
402         mqc->ct = 8;
403         /*if (*mqc->bp == 0xff) {
404         mqc->ct = 7;
405      } */
406 }
407
408 void mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) {
409         mqc->ct--;
410         mqc->c = mqc->c + (d << mqc->ct);
411         if (mqc->ct == 0) {
412                 mqc->bp++;
413                 *mqc->bp = mqc->c;
414                 mqc->ct = 8;
415                 if (*mqc->bp == 0xff) {
416                         mqc->ct = 7;
417                 }
418                 mqc->c = 0;
419         }
420 }
421
422 OPJ_UINT32 mqc_bypass_flush_enc(opj_mqc_t *mqc) {
423         OPJ_BYTE bit_padding;
424         
425         bit_padding = 0;
426         
427         if (mqc->ct != 0) {
428                 while (mqc->ct > 0) {
429                         mqc->ct--;
430                         mqc->c += bit_padding << mqc->ct;
431                         bit_padding = (bit_padding + 1) & 0x01;
432                 }
433                 mqc->bp++;
434                 *mqc->bp = mqc->c;
435                 mqc->ct = 8;
436                 mqc->c = 0;
437         }
438         
439         return 1;
440 }
441
442 void mqc_reset_enc(opj_mqc_t *mqc) {
443         mqc_resetstates(mqc);
444         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
445         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
446         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
447 }
448
449 OPJ_UINT32 mqc_restart_enc(opj_mqc_t *mqc) {
450         OPJ_UINT32 correction = 1;
451         
452         /* <flush part> */
453         OPJ_INT32 n = 27 - 15 - mqc->ct;
454         mqc->c <<= mqc->ct;
455         while (n > 0) {
456                 mqc_byteout(mqc);
457                 n -= mqc->ct;
458                 mqc->c <<= mqc->ct;
459         }
460         mqc_byteout(mqc);
461         
462         return correction;
463 }
464
465 void mqc_restart_init_enc(opj_mqc_t *mqc) {
466         /* <Re-init part> */
467         mqc_setcurctx(mqc, 0);
468         mqc->a = 0x8000;
469         mqc->c = 0;
470         mqc->ct = 12;
471         mqc->bp--;
472         if (*mqc->bp == 0xff) {
473                 mqc->ct = 13;
474         }
475 }
476
477 void mqc_erterm_enc(opj_mqc_t *mqc) {
478         OPJ_INT32 k = 11 - mqc->ct + 1;
479         
480         while (k > 0) {
481                 mqc->c <<= mqc->ct;
482                 mqc->ct = 0;
483                 mqc_byteout(mqc);
484                 k -= mqc->ct;
485         }
486         
487         if (*mqc->bp != 0xff) {
488                 mqc_byteout(mqc);
489         }
490 }
491
492 void mqc_segmark_enc(opj_mqc_t *mqc) {
493         OPJ_UINT32 i;
494         mqc_setcurctx(mqc, 18);
495         
496         for (i = 1; i < 5; i++) {
497                 mqc_encode(mqc, i % 2);
498         }
499 }
500
501 void mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len) {
502         mqc_setcurctx(mqc, 0);
503         mqc->start = bp;
504         mqc->end = bp + len;
505         mqc->bp = bp;
506         if (len==0) mqc->c = 0xff << 16;
507         else mqc->c = *mqc->bp << 16;
508         mqc_bytein(mqc);
509         mqc->c <<= 7;
510         mqc->ct -= 7;
511         mqc->a = 0x8000;
512 }
513
514 OPJ_UINT32 mqc_decode(opj_mqc_t *mqc) {
515         OPJ_INT32 d;
516         mqc->a -= (*mqc->curctx)->qeval;
517         if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
518                 d = mqc_lpsexchange(mqc);
519                 mqc_renormd(mqc);
520         } else {
521                 mqc->c -= (*mqc->curctx)->qeval << 16;
522                 if ((mqc->a & 0x8000) == 0) {
523                         d = mqc_mpsexchange(mqc);
524                         mqc_renormd(mqc);
525                 } else {
526                         d = (*mqc->curctx)->mps;
527                 }
528         }
529
530         return (OPJ_UINT32)d;
531 }
532
533 void mqc_resetstates(opj_mqc_t *mqc) {
534         OPJ_UINT32 i;
535         for (i = 0; i < MQC_NUMCTXS; i++) {
536                 mqc->ctxs[i] = mqc_states;
537         }
538 }
539
540 void mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob) {
541         mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
542 }
543
544