OpenJPEG  2.5.0
mqc_inl.h
Go to the documentation of this file.
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 OPJ_MQC_INL_H
40 #define OPJ_MQC_INL_H
41 
42 /* For internal use of opj_mqc_decode_macro() */
43 #define opj_mqc_mpsexchange_macro(d, curctx, a) \
44 { \
45  if (a < (*curctx)->qeval) { \
46  d = !((*curctx)->mps); \
47  *curctx = (*curctx)->nlps; \
48  } else { \
49  d = (*curctx)->mps; \
50  *curctx = (*curctx)->nmps; \
51  } \
52 }
53 
54 /* For internal use of opj_mqc_decode_macro() */
55 #define opj_mqc_lpsexchange_macro(d, curctx, a) \
56 { \
57  if (a < (*curctx)->qeval) { \
58  a = (*curctx)->qeval; \
59  d = (*curctx)->mps; \
60  *curctx = (*curctx)->nmps; \
61  } else { \
62  a = (*curctx)->qeval; \
63  d = !((*curctx)->mps); \
64  *curctx = (*curctx)->nlps; \
65  } \
66 }
67 
68 
75 {
76  OPJ_UINT32 d;
77  if (mqc->ct == 0) {
78  /* Given opj_mqc_raw_init_dec() we know that at some point we will */
79  /* have a 0xFF 0xFF artificial marker */
80  if (mqc->c == 0xff) {
81  if (*mqc->bp > 0x8f) {
82  mqc->c = 0xff;
83  mqc->ct = 8;
84  } else {
85  mqc->c = *mqc->bp;
86  mqc->bp ++;
87  mqc->ct = 7;
88  }
89  } else {
90  mqc->c = *mqc->bp;
91  mqc->bp ++;
92  mqc->ct = 8;
93  }
94  }
95  mqc->ct--;
96  d = ((OPJ_UINT32)mqc->c >> mqc->ct) & 0x01U;
97 
98  return d;
99 }
100 
101 
102 #define opj_mqc_bytein_macro(mqc, c, ct) \
103 { \
104  OPJ_UINT32 l_c; \
105  /* Given opj_mqc_init_dec() we know that at some point we will */ \
106  /* have a 0xFF 0xFF artificial marker */ \
107  l_c = *(mqc->bp + 1); \
108  if (*mqc->bp == 0xff) { \
109  if (l_c > 0x8f) { \
110  c += 0xff00; \
111  ct = 8; \
112  mqc->end_of_byte_stream_counter ++; \
113  } else { \
114  mqc->bp++; \
115  c += l_c << 9; \
116  ct = 7; \
117  } \
118  } else { \
119  mqc->bp++; \
120  c += l_c << 8; \
121  ct = 8; \
122  } \
123 }
124 
125 /* For internal use of opj_mqc_decode_macro() */
126 #define opj_mqc_renormd_macro(mqc, a, c, ct) \
127 { \
128  do { \
129  if (ct == 0) { \
130  opj_mqc_bytein_macro(mqc, c, ct); \
131  } \
132  a <<= 1; \
133  c <<= 1; \
134  ct--; \
135  } while (a < 0x8000); \
136 }
137 
138 #define opj_mqc_decode_macro(d, mqc, curctx, a, c, ct) \
139 { \
140  /* Implements ISO 15444-1 C.3.2 Decoding a decision (DECODE) */ \
141  /* Note: alternate "J.2 - Decoding an MPS or an LPS in the */ \
142  /* software-conventions decoder" has been tried, but does not bring any */ \
143  /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */ \
144  a -= (*curctx)->qeval; \
145  if ((c >> 16) < (*curctx)->qeval) { \
146  opj_mqc_lpsexchange_macro(d, curctx, a); \
147  opj_mqc_renormd_macro(mqc, a, c, ct); \
148  } else { \
149  c -= (*curctx)->qeval << 16; \
150  if ((a & 0x8000) == 0) { \
151  opj_mqc_mpsexchange_macro(d, curctx, a); \
152  opj_mqc_renormd_macro(mqc, a, c, ct); \
153  } else { \
154  d = (*curctx)->mps; \
155  } \
156  } \
157 }
158 
159 #define DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \
160  register const opj_mqc_state_t **curctx = mqc->curctx; \
161  register OPJ_UINT32 c = mqc->c; \
162  register OPJ_UINT32 a = mqc->a; \
163  register OPJ_UINT32 ct = mqc->ct
164 
165 #define UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \
166  mqc->curctx = curctx; \
167  mqc->c = c; \
168  mqc->a = a; \
169  mqc->ct = ct;
170 
175 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
176 {
177  opj_mqc_bytein_macro(mqc, mqc->c, mqc->ct);
178 }
179 
184 #define opj_mqc_renormd(mqc) \
185  opj_mqc_renormd_macro(mqc, mqc->a, mqc->c, mqc->ct)
186 
193 #define opj_mqc_decode(d, mqc) \
194  opj_mqc_decode_macro(d, mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct)
195 
201 void opj_mqc_byteout(opj_mqc_t *mqc);
202 
210 #define opj_mqc_renorme_macro(mqc, a_, c_, ct_) \
211 { \
212  do { \
213  a_ <<= 1; \
214  c_ <<= 1; \
215  ct_--; \
216  if (ct_ == 0) { \
217  mqc->c = c_; \
218  opj_mqc_byteout(mqc); \
219  c_ = mqc->c; \
220  ct_ = mqc->ct; \
221  } \
222  } while( (a_ & 0x8000) == 0); \
223 }
224 
225 #define opj_mqc_codemps_macro(mqc, curctx, a, c, ct) \
226 { \
227  a -= (*curctx)->qeval; \
228  if ((a & 0x8000) == 0) { \
229  if (a < (*curctx)->qeval) { \
230  a = (*curctx)->qeval; \
231  } else { \
232  c += (*curctx)->qeval; \
233  } \
234  *curctx = (*curctx)->nmps; \
235  opj_mqc_renorme_macro(mqc, a, c, ct); \
236  } else { \
237  c += (*curctx)->qeval; \
238  } \
239 }
240 
241 #define opj_mqc_codelps_macro(mqc, curctx, a, c, ct) \
242 { \
243  a -= (*curctx)->qeval; \
244  if (a < (*curctx)->qeval) { \
245  c += (*curctx)->qeval; \
246  } else { \
247  a = (*curctx)->qeval; \
248  } \
249  *curctx = (*curctx)->nlps; \
250  opj_mqc_renorme_macro(mqc, a, c, ct); \
251 }
252 
253 #define opj_mqc_encode_macro(mqc, curctx, a, c, ct, d) \
254 { \
255  if ((*curctx)->mps == (d)) { \
256  opj_mqc_codemps_macro(mqc, curctx, a, c, ct); \
257  } else { \
258  opj_mqc_codelps_macro(mqc, curctx, a, c, ct); \
259  } \
260 }
261 
262 
263 #define opj_mqc_bypass_enc_macro(mqc, c, ct, d) \
264 {\
265  if (ct == BYPASS_CT_INIT) {\
266  ct = 8;\
267  }\
268  ct--;\
269  c = c + ((d) << ct);\
270  if (ct == 0) {\
271  *mqc->bp = (OPJ_BYTE)c;\
272  ct = 8;\
273  /* If the previous byte was 0xff, make sure that the next msb is 0 */ \
274  if (*mqc->bp == 0xff) {\
275  ct = 7;\
276  }\
277  mqc->bp++;\
278  c = 0;\
279  }\
280 }
281 
282 #endif /* OPJ_MQC_INL_H */
opj_mqc_byteout
void opj_mqc_byteout(opj_mqc_t *mqc)
Output a byte, doing bit-stuffing if necessary.
Definition: mqc.c:492
opj_mqc
MQ coder.
Definition: mqc.h:74
opj_mqc::bp
OPJ_BYTE * bp
pointer to the current position in the buffer
Definition: mqc.h:84
INLINE
#define INLINE
Definition: openjpeg.h:65
opj_mqc_raw_decode
static INLINE OPJ_UINT32 opj_mqc_raw_decode(opj_mqc_t *mqc)
Decode a symbol using raw-decoder.
Definition: mqc_inl.h:74
opj_mqc::c
OPJ_UINT32 c
temporary buffer where bits are coded or decoded
Definition: mqc.h:76
OPJ_UINT32
uint32_t OPJ_UINT32
Definition: openjpeg.h:132
opj_mqc_bytein
static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
Input a byte.
Definition: mqc_inl.h:175
opj_mqc::ct
OPJ_UINT32 ct
number of bits already read or free to write
Definition: mqc.h:80
opj_mqc_bytein_macro
#define opj_mqc_bytein_macro(mqc, c, ct)
Definition: mqc_inl.h:102