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)
49 if (mqc->a < (*mqc->curctx)->qeval) {
50 d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
51 *mqc->curctx = (*mqc->curctx)->nlps;
53 d = (OPJ_INT32)(*mqc->curctx)->mps;
54 *mqc->curctx = (*mqc->curctx)->nmps;
65 static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc)
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;
73 mqc->a = (*mqc->curctx)->qeval;
74 d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
75 *mqc->curctx = (*mqc->curctx)->nlps;
85 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
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) {
93 if (mqc->bp + 1 != mqc->end) {
98 if (*mqc->bp == 0xff) {
119 Renormalize mqc->a and mqc->c while decoding
120 @param mqc MQC handle
122 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc)
131 } while (mqc->a < 0x8000);
136 @param mqc MQC handle
137 @return Returns the decoded symbol (0 or 1)
139 static INLINE OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc)
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 */
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);
151 mqc->c -= (*mqc->curctx)->qeval << 16;
152 if ((mqc->a & 0x8000) == 0) {
153 d = opj_mqc_mpsexchange(mqc);
154 opj_mqc_renormd(mqc);
156 d = (OPJ_INT32)(*mqc->curctx)->mps;
163 #endif /* __MQC_INL_H */