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.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
46 static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc) {
48 if (mqc->a < (*mqc->curctx)->qeval) {
49 d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
50 *mqc->curctx = (*mqc->curctx)->nlps;
52 d = (OPJ_INT32)(*mqc->curctx)->mps;
53 *mqc->curctx = (*mqc->curctx)->nmps;
64 static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc) {
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;
71 mqc->a = (*mqc->curctx)->qeval;
72 d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
73 *mqc->curctx = (*mqc->curctx)->nlps;
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;
88 mqc->bp += (i >> 2) & 0x04;
91 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc) {
92 if (mqc->bp != mqc->end) {
94 if (mqc->bp + 1 != mqc->end) {
99 if (*mqc->bp == 0xff) {
121 Renormalize mqc->a and mqc->c while decoding
122 @param mqc MQC handle
124 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc) {
132 } while (mqc->a < 0x8000);
137 @param mqc MQC handle
138 @return Returns the decoded symbol (0 or 1)
140 static INLINE OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc) {
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);
147 mqc->c -= (*mqc->curctx)->qeval << 16;
148 if ((mqc->a & 0x8000) == 0) {
149 d = opj_mqc_mpsexchange(mqc);
150 opj_mqc_renormd(mqc);
152 d = (OPJ_INT32)(*mqc->curctx)->mps;
159 #endif /* __MQC_INL_H */