Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/openjpeg/src/lib/openjp2/mqc_inl.h
Line
Count
Source
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
88.6M
#define opj_mqc_mpsexchange_macro(d, curctx, a) \
44
88.6M
{ \
45
88.6M
    if (a < (*curctx)->qeval) { \
46
14.0M
        d = !((*curctx)->mps); \
47
14.0M
        *curctx = (*curctx)->nlps; \
48
74.6M
    } else { \
49
74.6M
        d = (*curctx)->mps; \
50
74.6M
        *curctx = (*curctx)->nmps; \
51
74.6M
    } \
52
88.6M
}
53
54
/* For internal use of opj_mqc_decode_macro() */
55
83.5M
#define opj_mqc_lpsexchange_macro(d, curctx, a) \
56
83.5M
{ \
57
83.5M
    if (a < (*curctx)->qeval) { \
58
17.4M
        a = (*curctx)->qeval; \
59
17.4M
        d = (*curctx)->mps; \
60
17.4M
        *curctx = (*curctx)->nmps; \
61
66.1M
    } else { \
62
66.1M
        a = (*curctx)->qeval; \
63
66.1M
        d = !((*curctx)->mps); \
64
66.1M
        *curctx = (*curctx)->nlps; \
65
66.1M
    } \
66
83.5M
}
67
68
69
/**
70
Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
71
@param mqc MQC handle
72
@return Returns the decoded symbol (0 or 1)
73
*/
74
static INLINE OPJ_UINT32 opj_mqc_raw_decode(opj_mqc_t *mqc)
75
16.3M
{
76
16.3M
    OPJ_UINT32 d;
77
16.3M
    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
2.04M
        if (mqc->c == 0xff) {
81
1.93M
            if (*mqc->bp  > 0x8f) {
82
1.92M
                mqc->c = 0xff;
83
1.92M
                mqc->ct = 8;
84
1.92M
            } else {
85
2.74k
                mqc->c = *mqc->bp;
86
2.74k
                mqc->bp ++;
87
2.74k
                mqc->ct = 7;
88
2.74k
            }
89
1.93M
        } else {
90
118k
            mqc->c = *mqc->bp;
91
118k
            mqc->bp ++;
92
118k
            mqc->ct = 8;
93
118k
        }
94
2.04M
    }
95
16.3M
    mqc->ct--;
96
16.3M
    d = ((OPJ_UINT32)mqc->c >> mqc->ct) & 0x01U;
97
98
16.3M
    return d;
99
16.3M
}
Unexecuted instantiation: cio.c:opj_mqc_raw_decode
Unexecuted instantiation: event.c:opj_mqc_raw_decode
Unexecuted instantiation: image.c:opj_mqc_raw_decode
Unexecuted instantiation: openjpeg.c:opj_mqc_raw_decode
Unexecuted instantiation: opj_malloc.c:opj_mqc_raw_decode
Unexecuted instantiation: j2k.c:opj_mqc_raw_decode
Unexecuted instantiation: jp2.c:opj_mqc_raw_decode
Unexecuted instantiation: mct.c:opj_mqc_raw_decode
Unexecuted instantiation: pi.c:opj_mqc_raw_decode
Unexecuted instantiation: tcd.c:opj_mqc_raw_decode
Unexecuted instantiation: tgt.c:opj_mqc_raw_decode
Unexecuted instantiation: function_list.c:opj_mqc_raw_decode
Unexecuted instantiation: thread.c:opj_mqc_raw_decode
Unexecuted instantiation: bio.c:opj_mqc_raw_decode
Unexecuted instantiation: dwt.c:opj_mqc_raw_decode
Unexecuted instantiation: invert.c:opj_mqc_raw_decode
t1.c:opj_mqc_raw_decode
Line
Count
Source
75
16.3M
{
76
16.3M
    OPJ_UINT32 d;
77
16.3M
    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
2.04M
        if (mqc->c == 0xff) {
81
1.93M
            if (*mqc->bp  > 0x8f) {
82
1.92M
                mqc->c = 0xff;
83
1.92M
                mqc->ct = 8;
84
1.92M
            } else {
85
2.74k
                mqc->c = *mqc->bp;
86
2.74k
                mqc->bp ++;
87
2.74k
                mqc->ct = 7;
88
2.74k
            }
89
1.93M
        } else {
90
118k
            mqc->c = *mqc->bp;
91
118k
            mqc->bp ++;
92
118k
            mqc->ct = 8;
93
118k
        }
94
2.04M
    }
95
16.3M
    mqc->ct--;
96
16.3M
    d = ((OPJ_UINT32)mqc->c >> mqc->ct) & 0x01U;
97
98
16.3M
    return d;
99
16.3M
}
Unexecuted instantiation: t2.c:opj_mqc_raw_decode
Unexecuted instantiation: sparse_array.c:opj_mqc_raw_decode
Unexecuted instantiation: ht_dec.c:opj_mqc_raw_decode
Unexecuted instantiation: mqc.c:opj_mqc_raw_decode
100
101
102
29.1M
#define opj_mqc_bytein_macro(mqc, c, ct) \
103
29.1M
{ \
104
29.1M
        OPJ_UINT32 l_c;  \
105
29.1M
        /* Given opj_mqc_init_dec() we know that at some point we will */ \
106
29.1M
        /* have a 0xFF 0xFF artificial marker */ \
107
29.1M
        l_c = *(mqc->bp + 1); \
108
29.1M
        if (*mqc->bp == 0xff) { \
109
24.1M
            if (l_c > 0x8f) { \
110
24.0M
                c += 0xff00; \
111
24.0M
                ct = 8; \
112
24.0M
                mqc->end_of_byte_stream_counter ++; \
113
24.0M
            } else { \
114
113k
                mqc->bp++; \
115
113k
                c += l_c << 9; \
116
113k
                ct = 7; \
117
113k
            } \
118
24.1M
        } else { \
119
5.03M
            mqc->bp++; \
120
5.03M
            c += l_c << 8; \
121
5.03M
            ct = 8; \
122
5.03M
        } \
123
29.1M
}
124
125
/* For internal use of opj_mqc_decode_macro() */
126
172M
#define opj_mqc_renormd_macro(mqc, a, c, ct) \
127
172M
{ \
128
232M
    do { \
129
232M
        if (ct == 0) { \
130
29.0M
            opj_mqc_bytein_macro(mqc, c, ct); \
131
29.0M
        } \
132
232M
        a <<= 1; \
133
232M
        c <<= 1; \
134
232M
        ct--; \
135
232M
    } while (a < 0x8000); \
136
172M
}
137
138
396M
#define opj_mqc_decode_macro(d, mqc, curctx, a, c, ct) \
139
396M
{ \
140
396M
    /* Implements ISO 15444-1 C.3.2 Decoding a decision (DECODE) */ \
141
396M
    /* Note: alternate "J.2 - Decoding an MPS or an LPS in the */ \
142
396M
    /* software-conventions decoder" has been tried, but does not bring any */ \
143
396M
    /* improvement. See https://github.com/uclouvain/openjpeg/issues/921 */ \
144
396M
    a -= (*curctx)->qeval;  \
145
396M
    if ((c >> 16) < (*curctx)->qeval) {  \
146
83.5M
        opj_mqc_lpsexchange_macro(d, curctx, a);  \
147
83.5M
        opj_mqc_renormd_macro(mqc, a, c, ct);  \
148
312M
    } else {  \
149
312M
        c -= (*curctx)->qeval << 16;  \
150
312M
        if ((a & 0x8000) == 0) { \
151
88.6M
            opj_mqc_mpsexchange_macro(d, curctx, a); \
152
88.6M
            opj_mqc_renormd_macro(mqc, a, c, ct); \
153
224M
        } else { \
154
224M
            d = (*curctx)->mps; \
155
224M
        } \
156
312M
    } \
157
396M
}
158
159
#define DOWNLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \
160
566k
        register const opj_mqc_state_t **curctx = mqc->curctx; \
161
566k
        register OPJ_UINT32 c = mqc->c; \
162
566k
        register OPJ_UINT32 a = mqc->a; \
163
566k
        register OPJ_UINT32 ct = mqc->ct
164
165
#define UPLOAD_MQC_VARIABLES(mqc, curctx, a, c, ct) \
166
566k
        mqc->curctx = curctx; \
167
566k
        mqc->c = c; \
168
566k
        mqc->a = a; \
169
566k
        mqc->ct = ct;
170
171
/**
172
Input a byte
173
@param mqc MQC handle
174
*/
175
static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc)
176
97.4k
{
177
97.4k
    opj_mqc_bytein_macro(mqc, mqc->c, mqc->ct);
178
97.4k
}
Unexecuted instantiation: cio.c:opj_mqc_bytein
Unexecuted instantiation: event.c:opj_mqc_bytein
Unexecuted instantiation: image.c:opj_mqc_bytein
Unexecuted instantiation: openjpeg.c:opj_mqc_bytein
Unexecuted instantiation: opj_malloc.c:opj_mqc_bytein
Unexecuted instantiation: j2k.c:opj_mqc_bytein
Unexecuted instantiation: jp2.c:opj_mqc_bytein
Unexecuted instantiation: mct.c:opj_mqc_bytein
Unexecuted instantiation: pi.c:opj_mqc_bytein
Unexecuted instantiation: tcd.c:opj_mqc_bytein
Unexecuted instantiation: tgt.c:opj_mqc_bytein
Unexecuted instantiation: function_list.c:opj_mqc_bytein
Unexecuted instantiation: thread.c:opj_mqc_bytein
Unexecuted instantiation: bio.c:opj_mqc_bytein
Unexecuted instantiation: dwt.c:opj_mqc_bytein
Unexecuted instantiation: invert.c:opj_mqc_bytein
Unexecuted instantiation: t1.c:opj_mqc_bytein
Unexecuted instantiation: t2.c:opj_mqc_bytein
Unexecuted instantiation: sparse_array.c:opj_mqc_bytein
Unexecuted instantiation: ht_dec.c:opj_mqc_bytein
mqc.c:opj_mqc_bytein
Line
Count
Source
176
97.4k
{
177
97.4k
    opj_mqc_bytein_macro(mqc, mqc->c, mqc->ct);
178
97.4k
}
179
180
/**
181
Renormalize mqc->a and mqc->c while decoding
182
@param mqc MQC handle
183
*/
184
#define opj_mqc_renormd(mqc) \
185
    opj_mqc_renormd_macro(mqc, mqc->a, mqc->c, mqc->ct)
186
187
/**
188
Decode a symbol
189
@param d OPJ_UINT32 value where to store the decoded symbol
190
@param mqc MQC handle
191
@return Returns the decoded symbol (0 or 1) in d
192
*/
193
#define opj_mqc_decode(d, mqc) \
194
359k
    opj_mqc_decode_macro(d, mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct)
195
196
/**
197
Output a byte, doing bit-stuffing if necessary.
198
After a 0xff byte, the next byte must be smaller than 0x90.
199
@param mqc MQC handle
200
*/
201
void opj_mqc_byteout(opj_mqc_t *mqc);
202
203
/**
204
Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
205
@param mqc MQC handle
206
@param a_ value of mqc->a
207
@param c_ value of mqc->c_
208
@param ct_ value of mqc->ct_
209
*/
210
55.5k
#define opj_mqc_renorme_macro(mqc, a_, c_, ct_) \
211
55.5k
{ \
212
78.7k
    do { \
213
78.7k
        a_ <<= 1; \
214
78.7k
        c_ <<= 1; \
215
78.7k
        ct_--; \
216
78.7k
        if (ct_ == 0) { \
217
9.12k
            mqc->c = c_; \
218
9.12k
            opj_mqc_byteout(mqc); \
219
9.12k
            c_ = mqc->c; \
220
9.12k
            ct_ = mqc->ct; \
221
9.12k
        } \
222
78.7k
    } while( (a_ & 0x8000) == 0); \
223
55.5k
}
224
225
211k
#define opj_mqc_codemps_macro(mqc, curctx, a, c, ct) \
226
211k
{ \
227
211k
    a -= (*curctx)->qeval; \
228
211k
    if ((a & 0x8000) == 0) { \
229
34.2k
        if (a < (*curctx)->qeval) { \
230
3.07k
            a = (*curctx)->qeval; \
231
31.1k
        } else { \
232
31.1k
            c += (*curctx)->qeval; \
233
31.1k
        } \
234
34.2k
        *curctx = (*curctx)->nmps; \
235
34.2k
        opj_mqc_renorme_macro(mqc, a, c, ct); \
236
177k
    } else { \
237
177k
        c += (*curctx)->qeval; \
238
177k
    } \
239
211k
}
240
241
21.3k
#define opj_mqc_codelps_macro(mqc, curctx, a, c, ct) \
242
21.3k
{ \
243
21.3k
    a -= (*curctx)->qeval; \
244
21.3k
    if (a < (*curctx)->qeval) { \
245
4.14k
        c += (*curctx)->qeval; \
246
17.1k
    } else { \
247
17.1k
        a = (*curctx)->qeval; \
248
17.1k
    } \
249
21.3k
    *curctx = (*curctx)->nlps; \
250
21.3k
    opj_mqc_renorme_macro(mqc, a, c, ct); \
251
21.3k
}
252
253
232k
#define opj_mqc_encode_macro(mqc, curctx, a, c, ct, d) \
254
232k
{ \
255
232k
    if ((*curctx)->mps == (d)) { \
256
211k
        opj_mqc_codemps_macro(mqc, curctx, a, c, ct); \
257
211k
    } else { \
258
21.3k
        opj_mqc_codelps_macro(mqc, curctx, a, c, ct); \
259
21.3k
    } \
260
232k
}
261
262
263
0
#define opj_mqc_bypass_enc_macro(mqc, c, ct, d) \
264
0
{\
265
0
    if (ct == BYPASS_CT_INIT) {\
266
0
        ct = 8;\
267
0
    }\
268
0
    ct--;\
269
0
    c = c + ((d) << ct);\
270
0
    if (ct == 0) {\
271
0
        *mqc->bp = (OPJ_BYTE)c;\
272
0
        ct = 8;\
273
0
        /* If the previous byte was 0xff, make sure that the next msb is 0 */ \
274
0
        if (*mqc->bp == 0xff) {\
275
0
            ct = 7;\
276
0
        }\
277
0
        mqc->bp++;\
278
0
        c = 0;\
279
0
    }\
280
0
}
281
282
#endif /* OPJ_MQC_INL_H */