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