MQC: remove disabled MQC_PERF_OPT mode, which brings no performance improvements...
[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 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
86 {
87     /* Implements ISO 15444-1 C.3.4 Compressed image data input (BYTEIN) */
88     /* Note: alternate "J.3 - Inserting a new byte into the C register in the */
89     /* software-conventions decoder" has been tried, but does not bring any */
90     /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */
91     if (mqc->bp != mqc->end) {
92         OPJ_UINT32 c;
93         if (mqc->bp + 1 != mqc->end) {
94             c = *(mqc->bp + 1);
95         } else {
96             c = 0xff;
97         }
98         if (*mqc->bp == 0xff) {
99             if (c > 0x8f) {
100                 mqc->c += 0xff00;
101                 mqc->ct = 8;
102             } else {
103                 mqc->bp++;
104                 mqc->c += c << 9;
105                 mqc->ct = 7;
106             }
107         } else {
108             mqc->bp++;
109             mqc->c += c << 8;
110             mqc->ct = 8;
111         }
112     } else {
113         mqc->c += 0xff00;
114         mqc->ct = 8;
115     }
116 }
117
118 /**
119 Renormalize mqc->a and mqc->c while decoding
120 @param mqc MQC handle
121 */
122 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc)
123 {
124     do {
125         if (mqc->ct == 0) {
126             opj_mqc_bytein(mqc);
127         }
128         mqc->a <<= 1;
129         mqc->c <<= 1;
130         mqc->ct--;
131     } while (mqc->a < 0x8000);
132 }
133
134 /**
135 Decode a symbol
136 @param mqc MQC handle
137 @return Returns the decoded symbol (0 or 1)
138 */
139 static INLINE OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc)
140 {
141     /* Implements ISO 15444-1 C.3.2 Decoding a decision (DECODE) */
142     /* Note: alternate "J.2 - Decoding an MPS or an LPS in the */
143     /* software-conventions decoder" has been tried, but does not bring any */
144     /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */
145     OPJ_INT32 d;
146     mqc->a -= (*mqc->curctx)->qeval;
147     if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
148         d = opj_mqc_lpsexchange(mqc);
149         opj_mqc_renormd(mqc);
150     } else {
151         mqc->c -= (*mqc->curctx)->qeval << 16;
152         if ((mqc->a & 0x8000) == 0) {
153             d = opj_mqc_mpsexchange(mqc);
154             opj_mqc_renormd(mqc);
155         } else {
156             d = (OPJ_INT32)(*mqc->curctx)->mps;
157         }
158     }
159
160     return d;
161 }
162
163 #endif /* __MQC_INL_H */