Add comments about non successfull attempt of implementing alternate INITDEC, DECODE...
[openjpeg.git] / src / lib / openjp2 / mqc_inl.h
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) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #ifndef __MQC_INL_H
40 #define __MQC_INL_H
41 /**
42 FIXME DOC
43 @param mqc MQC handle
44 @return
45 */
46 static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc)
47 {
48     OPJ_INT32 d;
49     if (mqc->a < (*mqc->curctx)->qeval) {
50         d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
51         *mqc->curctx = (*mqc->curctx)->nlps;
52     } else {
53         d = (OPJ_INT32)(*mqc->curctx)->mps;
54         *mqc->curctx = (*mqc->curctx)->nmps;
55     }
56
57     return d;
58 }
59
60 /**
61 FIXME DOC
62 @param mqc MQC handle
63 @return
64 */
65 static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc)
66 {
67     OPJ_INT32 d;
68     if (mqc->a < (*mqc->curctx)->qeval) {
69         mqc->a = (*mqc->curctx)->qeval;
70         d = (OPJ_INT32)(*mqc->curctx)->mps;
71         *mqc->curctx = (*mqc->curctx)->nmps;
72     } else {
73         mqc->a = (*mqc->curctx)->qeval;
74         d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
75         *mqc->curctx = (*mqc->curctx)->nlps;
76     }
77
78     return d;
79 }
80
81 /**
82 Input a byte
83 @param mqc MQC handle
84 */
85 #ifdef MQC_PERF_OPT
86 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
87 {
88     unsigned int i = *((unsigned int *) mqc->bp);
89     mqc->c += i & 0xffff00;
90     mqc->ct = i & 0x0f;
91     mqc->bp += (i >> 2) & 0x04;
92 }
93 #else
94 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
95 {
96     /* Implements ISO 15444-1 C.3.4 Compressed image data input (BYTEIN) */
97     /* Note: alternate "J.3 - Inserting a new byte into the C register in the */
98     /* software-conventions decoder" has been tried, but does not bring any */
99     /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */
100     if (mqc->bp != mqc->end) {
101         OPJ_UINT32 c;
102         if (mqc->bp + 1 != mqc->end) {
103             c = *(mqc->bp + 1);
104         } else {
105             c = 0xff;
106         }
107         if (*mqc->bp == 0xff) {
108             if (c > 0x8f) {
109                 mqc->c += 0xff00;
110                 mqc->ct = 8;
111             } else {
112                 mqc->bp++;
113                 mqc->c += c << 9;
114                 mqc->ct = 7;
115             }
116         } else {
117             mqc->bp++;
118             mqc->c += c << 8;
119             mqc->ct = 8;
120         }
121     } else {
122         mqc->c += 0xff00;
123         mqc->ct = 8;
124     }
125 }
126 #endif
127
128 /**
129 Renormalize mqc->a and mqc->c while decoding
130 @param mqc MQC handle
131 */
132 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc)
133 {
134     do {
135         if (mqc->ct == 0) {
136             opj_mqc_bytein(mqc);
137         }
138         mqc->a <<= 1;
139         mqc->c <<= 1;
140         mqc->ct--;
141     } while (mqc->a < 0x8000);
142 }
143
144 /**
145 Decode a symbol
146 @param mqc MQC handle
147 @return Returns the decoded symbol (0 or 1)
148 */
149 static INLINE OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc)
150 {
151     /* Implements ISO 15444-1 C.3.2 Decoding a decision (DECODE) */
152     /* Note: alternate "J.2 - Decoding an MPS or an LPS in the */
153     /* software-conventions decoder" has been tried, but does not bring any */
154     /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */
155     OPJ_INT32 d;
156     mqc->a -= (*mqc->curctx)->qeval;
157     if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
158         d = opj_mqc_lpsexchange(mqc);
159         opj_mqc_renormd(mqc);
160     } else {
161         mqc->c -= (*mqc->curctx)->qeval << 16;
162         if ((mqc->a & 0x8000) == 0) {
163             d = opj_mqc_mpsexchange(mqc);
164             opj_mqc_renormd(mqc);
165         } else {
166             d = (OPJ_INT32)(*mqc->curctx)->mps;
167         }
168     }
169
170     return d;
171 }
172
173 #endif /* __MQC_INL_H */