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