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