Move some MQC functions into a header for speed
[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         OPJ_INT32 d;
48         if (mqc->a < (*mqc->curctx)->qeval) {
49                 d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
50                 *mqc->curctx = (*mqc->curctx)->nlps;
51         } else {
52                 d = (OPJ_INT32)(*mqc->curctx)->mps;
53                 *mqc->curctx = (*mqc->curctx)->nmps;
54         }
55
56         return d;
57 }
58
59 /**
60 FIXME DOC
61 @param mqc MQC handle
62 @return
63 */
64 static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc) {
65         OPJ_INT32 d;
66         if (mqc->a < (*mqc->curctx)->qeval) {
67                 mqc->a = (*mqc->curctx)->qeval;
68                 d = (OPJ_INT32)(*mqc->curctx)->mps;
69                 *mqc->curctx = (*mqc->curctx)->nmps;
70         } else {
71                 mqc->a = (*mqc->curctx)->qeval;
72                 d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
73                 *mqc->curctx = (*mqc->curctx)->nlps;
74         }
75
76         return d;
77 }
78
79 /**
80 Input a byte
81 @param mqc MQC handle
82 */
83 #ifdef MQC_PERF_OPT
84 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc) {
85         unsigned int i = *((unsigned int *) mqc->bp);
86         mqc->c += i & 0xffff00;
87         mqc->ct = i & 0x0f;
88         mqc->bp += (i >> 2) & 0x04;
89 }
90 #else
91 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc) {
92         if (mqc->bp != mqc->end) {
93                 OPJ_UINT32 c;
94                 if (mqc->bp + 1 != mqc->end) {
95                         c = *(mqc->bp + 1);
96                 } else {
97                         c = 0xff;
98                 }
99                 if (*mqc->bp == 0xff) {
100                         if (c > 0x8f) {
101                                 mqc->c += 0xff00;
102                                 mqc->ct = 8;
103                         } else {
104                                 mqc->bp++;
105                                 mqc->c += c << 9;
106                                 mqc->ct = 7;
107                         }
108                 } else {
109                         mqc->bp++;
110                         mqc->c += c << 8;
111                         mqc->ct = 8;
112                 }
113         } else {
114                 mqc->c += 0xff00;
115                 mqc->ct = 8;
116         }
117 }
118 #endif
119
120 /**
121 Renormalize mqc->a and mqc->c while decoding
122 @param mqc MQC handle
123 */
124 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc) {
125         do {
126                 if (mqc->ct == 0) {
127                         opj_mqc_bytein(mqc);
128                 }
129                 mqc->a <<= 1;
130                 mqc->c <<= 1;
131                 mqc->ct--;
132         } while (mqc->a < 0x8000);
133 }
134
135 /**
136 Decode a symbol
137 @param mqc MQC handle
138 @return Returns the decoded symbol (0 or 1)
139 */
140 static INLINE OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc) {
141         OPJ_INT32 d;
142         mqc->a -= (*mqc->curctx)->qeval;
143         if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
144                 d = opj_mqc_lpsexchange(mqc);
145                 opj_mqc_renormd(mqc);
146         } else {
147                 mqc->c -= (*mqc->curctx)->qeval << 16;
148                 if ((mqc->a & 0x8000) == 0) {
149                         d = opj_mqc_mpsexchange(mqc);
150                         opj_mqc_renormd(mqc);
151                 } else {
152                         d = (OPJ_INT32)(*mqc->curctx)->mps;
153                 }
154         }
155
156         return d;
157 }
158
159 #endif /* __MQC_INL_H */