track all declarations after statement for C90 compilers (VS2005)
[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 INLINE int mqc_mpsexchange(opj_mqc_t *const mqc);
72 /**
73 FIXME: documentation ???
74 @param mqc MQC handle
75 @return 
76 */
77 static INLINE int mqc_lpsexchange(opj_mqc_t *const mqc);
78 /**
79 Input a byte
80 @param mqc MQC handle
81 */
82 static INLINE void mqc_bytein(opj_mqc_t *const mqc);
83 /**
84 Renormalize mqc->a and mqc->c while decoding
85 @param mqc MQC handle
86 */
87 static INLINE void mqc_renormd(opj_mqc_t *const mqc);
88 /*@}*/
89
90 /*@}*/
91
92 /* <summary> */
93 /* This array defines all the possible states for a context. */
94 /* </summary> */
95 static opj_mqc_state_t mqc_states[47 * 2] = {
96         {0x5601, 0, &mqc_states[2], &mqc_states[3]},
97         {0x5601, 1, &mqc_states[3], &mqc_states[2]},
98         {0x3401, 0, &mqc_states[4], &mqc_states[12]},
99         {0x3401, 1, &mqc_states[5], &mqc_states[13]},
100         {0x1801, 0, &mqc_states[6], &mqc_states[18]},
101         {0x1801, 1, &mqc_states[7], &mqc_states[19]},
102         {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
103         {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
104         {0x0521, 0, &mqc_states[10], &mqc_states[58]},
105         {0x0521, 1, &mqc_states[11], &mqc_states[59]},
106         {0x0221, 0, &mqc_states[76], &mqc_states[66]},
107         {0x0221, 1, &mqc_states[77], &mqc_states[67]},
108         {0x5601, 0, &mqc_states[14], &mqc_states[13]},
109         {0x5601, 1, &mqc_states[15], &mqc_states[12]},
110         {0x5401, 0, &mqc_states[16], &mqc_states[28]},
111         {0x5401, 1, &mqc_states[17], &mqc_states[29]},
112         {0x4801, 0, &mqc_states[18], &mqc_states[28]},
113         {0x4801, 1, &mqc_states[19], &mqc_states[29]},
114         {0x3801, 0, &mqc_states[20], &mqc_states[28]},
115         {0x3801, 1, &mqc_states[21], &mqc_states[29]},
116         {0x3001, 0, &mqc_states[22], &mqc_states[34]},
117         {0x3001, 1, &mqc_states[23], &mqc_states[35]},
118         {0x2401, 0, &mqc_states[24], &mqc_states[36]},
119         {0x2401, 1, &mqc_states[25], &mqc_states[37]},
120         {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
121         {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
122         {0x1601, 0, &mqc_states[58], &mqc_states[42]},
123         {0x1601, 1, &mqc_states[59], &mqc_states[43]},
124         {0x5601, 0, &mqc_states[30], &mqc_states[29]},
125         {0x5601, 1, &mqc_states[31], &mqc_states[28]},
126         {0x5401, 0, &mqc_states[32], &mqc_states[28]},
127         {0x5401, 1, &mqc_states[33], &mqc_states[29]},
128         {0x5101, 0, &mqc_states[34], &mqc_states[30]},
129         {0x5101, 1, &mqc_states[35], &mqc_states[31]},
130         {0x4801, 0, &mqc_states[36], &mqc_states[32]},
131         {0x4801, 1, &mqc_states[37], &mqc_states[33]},
132         {0x3801, 0, &mqc_states[38], &mqc_states[34]},
133         {0x3801, 1, &mqc_states[39], &mqc_states[35]},
134         {0x3401, 0, &mqc_states[40], &mqc_states[36]},
135         {0x3401, 1, &mqc_states[41], &mqc_states[37]},
136         {0x3001, 0, &mqc_states[42], &mqc_states[38]},
137         {0x3001, 1, &mqc_states[43], &mqc_states[39]},
138         {0x2801, 0, &mqc_states[44], &mqc_states[38]},
139         {0x2801, 1, &mqc_states[45], &mqc_states[39]},
140         {0x2401, 0, &mqc_states[46], &mqc_states[40]},
141         {0x2401, 1, &mqc_states[47], &mqc_states[41]},
142         {0x2201, 0, &mqc_states[48], &mqc_states[42]},
143         {0x2201, 1, &mqc_states[49], &mqc_states[43]},
144         {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
145         {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
146         {0x1801, 0, &mqc_states[52], &mqc_states[46]},
147         {0x1801, 1, &mqc_states[53], &mqc_states[47]},
148         {0x1601, 0, &mqc_states[54], &mqc_states[48]},
149         {0x1601, 1, &mqc_states[55], &mqc_states[49]},
150         {0x1401, 0, &mqc_states[56], &mqc_states[50]},
151         {0x1401, 1, &mqc_states[57], &mqc_states[51]},
152         {0x1201, 0, &mqc_states[58], &mqc_states[52]},
153         {0x1201, 1, &mqc_states[59], &mqc_states[53]},
154         {0x1101, 0, &mqc_states[60], &mqc_states[54]},
155         {0x1101, 1, &mqc_states[61], &mqc_states[55]},
156         {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
157         {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
158         {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
159         {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
160         {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
161         {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
162         {0x0521, 0, &mqc_states[68], &mqc_states[62]},
163         {0x0521, 1, &mqc_states[69], &mqc_states[63]},
164         {0x0441, 0, &mqc_states[70], &mqc_states[64]},
165         {0x0441, 1, &mqc_states[71], &mqc_states[65]},
166         {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
167         {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
168         {0x0221, 0, &mqc_states[74], &mqc_states[68]},
169         {0x0221, 1, &mqc_states[75], &mqc_states[69]},
170         {0x0141, 0, &mqc_states[76], &mqc_states[70]},
171         {0x0141, 1, &mqc_states[77], &mqc_states[71]},
172         {0x0111, 0, &mqc_states[78], &mqc_states[72]},
173         {0x0111, 1, &mqc_states[79], &mqc_states[73]},
174         {0x0085, 0, &mqc_states[80], &mqc_states[74]},
175         {0x0085, 1, &mqc_states[81], &mqc_states[75]},
176         {0x0049, 0, &mqc_states[82], &mqc_states[76]},
177         {0x0049, 1, &mqc_states[83], &mqc_states[77]},
178         {0x0025, 0, &mqc_states[84], &mqc_states[78]},
179         {0x0025, 1, &mqc_states[85], &mqc_states[79]},
180         {0x0015, 0, &mqc_states[86], &mqc_states[80]},
181         {0x0015, 1, &mqc_states[87], &mqc_states[81]},
182         {0x0009, 0, &mqc_states[88], &mqc_states[82]},
183         {0x0009, 1, &mqc_states[89], &mqc_states[83]},
184         {0x0005, 0, &mqc_states[90], &mqc_states[84]},
185         {0x0005, 1, &mqc_states[91], &mqc_states[85]},
186         {0x0001, 0, &mqc_states[90], &mqc_states[86]},
187         {0x0001, 1, &mqc_states[91], &mqc_states[87]},
188         {0x5601, 0, &mqc_states[92], &mqc_states[92]},
189         {0x5601, 1, &mqc_states[93], &mqc_states[93]},
190 };
191
192 /* 
193 ==========================================================
194    local functions
195 ==========================================================
196 */
197
198 static void mqc_byteout(opj_mqc_t *mqc) {
199         if (*mqc->bp == 0xff) {
200                 mqc->bp++;
201                 *mqc->bp = mqc->c >> 20;
202                 mqc->c &= 0xfffff;
203                 mqc->ct = 7;
204         } else {
205                 if ((mqc->c & 0x8000000) == 0) {        /* ((mqc->c&0x8000000)==0) CHANGE */
206                         mqc->bp++;
207                         *mqc->bp = mqc->c >> 19;
208                         mqc->c &= 0x7ffff;
209                         mqc->ct = 8;
210                 } else {
211                         (*mqc->bp)++;
212                         if (*mqc->bp == 0xff) {
213                                 mqc->c &= 0x7ffffff;
214                                 mqc->bp++;
215                                 *mqc->bp = mqc->c >> 20;
216                                 mqc->c &= 0xfffff;
217                                 mqc->ct = 7;
218                         } else {
219                                 mqc->bp++;
220                                 *mqc->bp = mqc->c >> 19;
221                                 mqc->c &= 0x7ffff;
222                                 mqc->ct = 8;
223                         }
224                 }
225         }
226 }
227
228 static void mqc_renorme(opj_mqc_t *mqc) {
229         do {
230                 mqc->a <<= 1;
231                 mqc->c <<= 1;
232                 mqc->ct--;
233                 if (mqc->ct == 0) {
234                         mqc_byteout(mqc);
235                 }
236         } while ((mqc->a & 0x8000) == 0);
237 }
238
239 static void mqc_codemps(opj_mqc_t *mqc) {
240         mqc->a -= (*mqc->curctx)->qeval;
241         if ((mqc->a & 0x8000) == 0) {
242                 if (mqc->a < (*mqc->curctx)->qeval) {
243                         mqc->a = (*mqc->curctx)->qeval;
244                 } else {
245                         mqc->c += (*mqc->curctx)->qeval;
246                 }
247                 *mqc->curctx = (*mqc->curctx)->nmps;
248                 mqc_renorme(mqc);
249         } else {
250                 mqc->c += (*mqc->curctx)->qeval;
251         }
252 }
253
254 static void mqc_codelps(opj_mqc_t *mqc) {
255         mqc->a -= (*mqc->curctx)->qeval;
256         if (mqc->a < (*mqc->curctx)->qeval) {
257                 mqc->c += (*mqc->curctx)->qeval;
258         } else {
259                 mqc->a = (*mqc->curctx)->qeval;
260         }
261         *mqc->curctx = (*mqc->curctx)->nlps;
262         mqc_renorme(mqc);
263 }
264
265 static void mqc_setbits(opj_mqc_t *mqc) {
266         unsigned int tempc = mqc->c + mqc->a;
267         mqc->c |= 0xffff;
268         if (mqc->c >= tempc) {
269                 mqc->c -= 0x8000;
270         }
271 }
272
273 static INLINE int mqc_mpsexchange(opj_mqc_t *const mqc) {
274         int d;
275         if (mqc->a < (*mqc->curctx)->qeval) {
276                 d = 1 - (*mqc->curctx)->mps;
277                 *mqc->curctx = (*mqc->curctx)->nlps;
278         } else {
279                 d = (*mqc->curctx)->mps;
280                 *mqc->curctx = (*mqc->curctx)->nmps;
281         }
282         
283         return d;
284 }
285
286 static INLINE int mqc_lpsexchange(opj_mqc_t *const mqc) {
287         int d;
288         if (mqc->a < (*mqc->curctx)->qeval) {
289                 mqc->a = (*mqc->curctx)->qeval;
290                 d = (*mqc->curctx)->mps;
291                 *mqc->curctx = (*mqc->curctx)->nmps;
292         } else {
293                 mqc->a = (*mqc->curctx)->qeval;
294                 d = 1 - (*mqc->curctx)->mps;
295                 *mqc->curctx = (*mqc->curctx)->nlps;
296         }
297         
298         return d;
299 }
300
301 #ifdef MQC_PERF_OPT
302 static INLINE void mqc_bytein(opj_mqc_t *const mqc) {
303         unsigned int i = *((unsigned int *) mqc->bp);
304         mqc->c += i & 0xffff00;
305         mqc->ct = i & 0x0f;
306         mqc->bp += (i >> 2) & 0x04;
307 }
308 #else
309 static void mqc_bytein(opj_mqc_t *const mqc) {
310         if (mqc->bp != mqc->end) {
311                 unsigned int c;
312                 if (mqc->bp + 1 != mqc->end) {
313                         c = *(mqc->bp + 1);
314                 } else {
315                         c = 0xff;
316                 }
317                 if (*mqc->bp == 0xff) {
318                         if (c > 0x8f) {
319                                 mqc->c += 0xff00;
320                                 mqc->ct = 8;
321                         } else {
322                                 mqc->bp++;
323                                 mqc->c += c << 9;
324                                 mqc->ct = 7;
325                         }
326                 } else {
327                         mqc->bp++;
328                         mqc->c += c << 8;
329                         mqc->ct = 8;
330                 }
331         } else {
332                 mqc->c += 0xff00;
333                 mqc->ct = 8;
334         }
335 }
336 #endif
337
338 static INLINE void mqc_renormd(opj_mqc_t *const mqc) {
339         do {
340                 if (mqc->ct == 0) {
341                         mqc_bytein(mqc);
342                 }
343                 mqc->a <<= 1;
344                 mqc->c <<= 1;
345                 mqc->ct--;
346         } while (mqc->a < 0x8000);
347 }
348
349 /* 
350 ==========================================================
351    MQ-Coder interface
352 ==========================================================
353 */
354
355 opj_mqc_t* mqc_create(void) {
356         opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
357 #ifdef MQC_PERF_OPT
358         mqc->buffer = NULL;
359 #endif
360         return mqc;
361 }
362
363 void mqc_destroy(opj_mqc_t *mqc) {
364         if(mqc) {
365 #ifdef MQC_PERF_OPT
366                 if (mqc->buffer) {
367                         opj_free(mqc->buffer);
368                 }
369 #endif
370                 opj_free(mqc);
371         }
372 }
373
374 int mqc_numbytes(opj_mqc_t *mqc) {
375         return mqc->bp - mqc->start;
376 }
377
378 void mqc_init_enc(opj_mqc_t *mqc, unsigned char *bp) {
379         mqc_setcurctx(mqc, 0);
380         mqc->a = 0x8000;
381         mqc->c = 0;
382         mqc->bp = bp - 1;
383         mqc->ct = 12;
384         if (*mqc->bp == 0xff) {
385                 mqc->ct = 13;
386         }
387         mqc->start = bp;
388 }
389
390 void mqc_encode(opj_mqc_t *mqc, int d) {
391         if ((*mqc->curctx)->mps == d) {
392                 mqc_codemps(mqc);
393         } else {
394                 mqc_codelps(mqc);
395         }
396 }
397
398 void mqc_flush(opj_mqc_t *mqc) {
399         mqc_setbits(mqc);
400         mqc->c <<= mqc->ct;
401         mqc_byteout(mqc);
402         mqc->c <<= mqc->ct;
403         mqc_byteout(mqc);
404         
405         if (*mqc->bp != 0xff) {
406                 mqc->bp++;
407         }
408 }
409
410 void mqc_bypass_init_enc(opj_mqc_t *mqc) {
411         mqc->c = 0;
412         mqc->ct = 8;
413         /*if (*mqc->bp == 0xff) {
414         mqc->ct = 7;
415      } */
416 }
417
418 void mqc_bypass_enc(opj_mqc_t *mqc, int d) {
419         mqc->ct--;
420         mqc->c = mqc->c + (d << mqc->ct);
421         if (mqc->ct == 0) {
422                 mqc->bp++;
423                 *mqc->bp = mqc->c;
424                 mqc->ct = 8;
425                 if (*mqc->bp == 0xff) {
426                         mqc->ct = 7;
427                 }
428                 mqc->c = 0;
429         }
430 }
431
432 int mqc_bypass_flush_enc(opj_mqc_t *mqc) {
433         unsigned char bit_padding;
434         
435         bit_padding = 0;
436         
437         if (mqc->ct != 0) {
438                 while (mqc->ct > 0) {
439                         mqc->ct--;
440                         mqc->c += bit_padding << mqc->ct;
441                         bit_padding = (bit_padding + 1) & 0x01;
442                 }
443                 mqc->bp++;
444                 *mqc->bp = mqc->c;
445                 mqc->ct = 8;
446                 mqc->c = 0;
447         }
448         
449         return 1;
450 }
451
452 void mqc_reset_enc(opj_mqc_t *mqc) {
453         mqc_resetstates(mqc);
454         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
455         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
456         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
457 }
458
459 int mqc_restart_enc(opj_mqc_t *mqc) {
460         int correction = 1;
461         
462         /* <flush part> */
463         int n = 27 - 15 - mqc->ct;
464         mqc->c <<= mqc->ct;
465         while (n > 0) {
466                 mqc_byteout(mqc);
467                 n -= mqc->ct;
468                 mqc->c <<= mqc->ct;
469         }
470         mqc_byteout(mqc);
471         
472         return correction;
473 }
474
475 void mqc_restart_init_enc(opj_mqc_t *mqc) {
476         /* <Re-init part> */
477         mqc_setcurctx(mqc, 0);
478         mqc->a = 0x8000;
479         mqc->c = 0;
480         mqc->ct = 12;
481         mqc->bp--;
482         if (*mqc->bp == 0xff) {
483                 mqc->ct = 13;
484         }
485 }
486
487 void mqc_erterm_enc(opj_mqc_t *mqc) {
488         int k = 11 - mqc->ct + 1;
489         
490         while (k > 0) {
491                 mqc->c <<= mqc->ct;
492                 mqc->ct = 0;
493                 mqc_byteout(mqc);
494                 k -= mqc->ct;
495         }
496         
497         if (*mqc->bp != 0xff) {
498                 mqc_byteout(mqc);
499         }
500 }
501
502 void mqc_segmark_enc(opj_mqc_t *mqc) {
503         int i;
504         mqc_setcurctx(mqc, 18);
505         
506         for (i = 1; i < 5; i++) {
507                 mqc_encode(mqc, i % 2);
508         }
509 }
510
511 void mqc_init_dec(opj_mqc_t *mqc, unsigned char *bp, int len) {
512         mqc_setcurctx(mqc, 0);
513         mqc->start = bp;
514         mqc->end = bp + len;
515         mqc->bp = bp;
516         if (len==0) mqc->c = 0xff << 16;
517         else mqc->c = *mqc->bp << 16;
518
519 #ifdef MQC_PERF_OPT
520         {
521                 unsigned int c;
522                 unsigned int *ip;
523                 unsigned char *end = mqc->end - 1;
524                 mqc->buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(unsigned int));
525                 ip = (unsigned int *) mqc->buffer;
526
527                 while (bp < end) {
528                         c = *(bp + 1);
529                         if (*bp == 0xff) {
530                                 if (c > 0x8f) {
531                                         break;
532                                 } else {
533                                         *ip = 0x00000017 | (c << 9);
534                                 }
535                         } else {
536                                 *ip = 0x00000018 | (c << 8);
537                         }
538                         bp++;
539                         ip++;
540                 }
541
542                 /* Handle last byte of data */
543                 c = 0xff;
544                 if (*bp == 0xff) {
545                         *ip = 0x0000ff18;
546                 } else {
547                         bp++;
548                         *ip = 0x00000018 | (c << 8);
549                 }
550                 ip++;
551
552                 *ip = 0x0000ff08;
553                 mqc->bp = mqc->buffer;
554         }
555 #endif
556         mqc_bytein(mqc);
557         mqc->c <<= 7;
558         mqc->ct -= 7;
559         mqc->a = 0x8000;
560 }
561
562 int mqc_decode(opj_mqc_t *const mqc) {
563         int d;
564         mqc->a -= (*mqc->curctx)->qeval;
565         if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
566                 d = mqc_lpsexchange(mqc);
567                 mqc_renormd(mqc);
568         } else {
569                 mqc->c -= (*mqc->curctx)->qeval << 16;
570                 if ((mqc->a & 0x8000) == 0) {
571                         d = mqc_mpsexchange(mqc);
572                         mqc_renormd(mqc);
573                 } else {
574                         d = (*mqc->curctx)->mps;
575                 }
576         }
577
578         return d;
579 }
580
581 void mqc_resetstates(opj_mqc_t *mqc) {
582         int i;
583         for (i = 0; i < MQC_NUMCTXS; i++) {
584                 mqc->ctxs[i] = mqc_states;
585         }
586 }
587
588 void mqc_setstate(opj_mqc_t *mqc, int ctxno, int msb, int prob) {
589         mqc->ctxs[ctxno] = &mqc_states[msb + (prob << 1)];
590 }
591
592