Coverage Report

Created: 2025-06-12 06:51

/src/opencv/3rdparty/openjpeg/openjp2/mqc.c
Line
Count
Source (jump to first uncovered line)
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
#include "opj_includes.h"
40
41
#include <assert.h>
42
43
/** @defgroup MQC MQC - Implementation of an MQ-Coder */
44
/*@{*/
45
46
/** @name Local static functions */
47
/*@{*/
48
49
/**
50
Fill mqc->c with 1's for flushing
51
@param mqc MQC handle
52
*/
53
static void opj_mqc_setbits(opj_mqc_t *mqc);
54
/*@}*/
55
56
/*@}*/
57
58
/* <summary> */
59
/* This array defines all the possible states for a context. */
60
/* </summary> */
61
static const opj_mqc_state_t mqc_states[47 * 2] = {
62
    {0x5601, 0, &mqc_states[2], &mqc_states[3]},
63
    {0x5601, 1, &mqc_states[3], &mqc_states[2]},
64
    {0x3401, 0, &mqc_states[4], &mqc_states[12]},
65
    {0x3401, 1, &mqc_states[5], &mqc_states[13]},
66
    {0x1801, 0, &mqc_states[6], &mqc_states[18]},
67
    {0x1801, 1, &mqc_states[7], &mqc_states[19]},
68
    {0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
69
    {0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
70
    {0x0521, 0, &mqc_states[10], &mqc_states[58]},
71
    {0x0521, 1, &mqc_states[11], &mqc_states[59]},
72
    {0x0221, 0, &mqc_states[76], &mqc_states[66]},
73
    {0x0221, 1, &mqc_states[77], &mqc_states[67]},
74
    {0x5601, 0, &mqc_states[14], &mqc_states[13]},
75
    {0x5601, 1, &mqc_states[15], &mqc_states[12]},
76
    {0x5401, 0, &mqc_states[16], &mqc_states[28]},
77
    {0x5401, 1, &mqc_states[17], &mqc_states[29]},
78
    {0x4801, 0, &mqc_states[18], &mqc_states[28]},
79
    {0x4801, 1, &mqc_states[19], &mqc_states[29]},
80
    {0x3801, 0, &mqc_states[20], &mqc_states[28]},
81
    {0x3801, 1, &mqc_states[21], &mqc_states[29]},
82
    {0x3001, 0, &mqc_states[22], &mqc_states[34]},
83
    {0x3001, 1, &mqc_states[23], &mqc_states[35]},
84
    {0x2401, 0, &mqc_states[24], &mqc_states[36]},
85
    {0x2401, 1, &mqc_states[25], &mqc_states[37]},
86
    {0x1c01, 0, &mqc_states[26], &mqc_states[40]},
87
    {0x1c01, 1, &mqc_states[27], &mqc_states[41]},
88
    {0x1601, 0, &mqc_states[58], &mqc_states[42]},
89
    {0x1601, 1, &mqc_states[59], &mqc_states[43]},
90
    {0x5601, 0, &mqc_states[30], &mqc_states[29]},
91
    {0x5601, 1, &mqc_states[31], &mqc_states[28]},
92
    {0x5401, 0, &mqc_states[32], &mqc_states[28]},
93
    {0x5401, 1, &mqc_states[33], &mqc_states[29]},
94
    {0x5101, 0, &mqc_states[34], &mqc_states[30]},
95
    {0x5101, 1, &mqc_states[35], &mqc_states[31]},
96
    {0x4801, 0, &mqc_states[36], &mqc_states[32]},
97
    {0x4801, 1, &mqc_states[37], &mqc_states[33]},
98
    {0x3801, 0, &mqc_states[38], &mqc_states[34]},
99
    {0x3801, 1, &mqc_states[39], &mqc_states[35]},
100
    {0x3401, 0, &mqc_states[40], &mqc_states[36]},
101
    {0x3401, 1, &mqc_states[41], &mqc_states[37]},
102
    {0x3001, 0, &mqc_states[42], &mqc_states[38]},
103
    {0x3001, 1, &mqc_states[43], &mqc_states[39]},
104
    {0x2801, 0, &mqc_states[44], &mqc_states[38]},
105
    {0x2801, 1, &mqc_states[45], &mqc_states[39]},
106
    {0x2401, 0, &mqc_states[46], &mqc_states[40]},
107
    {0x2401, 1, &mqc_states[47], &mqc_states[41]},
108
    {0x2201, 0, &mqc_states[48], &mqc_states[42]},
109
    {0x2201, 1, &mqc_states[49], &mqc_states[43]},
110
    {0x1c01, 0, &mqc_states[50], &mqc_states[44]},
111
    {0x1c01, 1, &mqc_states[51], &mqc_states[45]},
112
    {0x1801, 0, &mqc_states[52], &mqc_states[46]},
113
    {0x1801, 1, &mqc_states[53], &mqc_states[47]},
114
    {0x1601, 0, &mqc_states[54], &mqc_states[48]},
115
    {0x1601, 1, &mqc_states[55], &mqc_states[49]},
116
    {0x1401, 0, &mqc_states[56], &mqc_states[50]},
117
    {0x1401, 1, &mqc_states[57], &mqc_states[51]},
118
    {0x1201, 0, &mqc_states[58], &mqc_states[52]},
119
    {0x1201, 1, &mqc_states[59], &mqc_states[53]},
120
    {0x1101, 0, &mqc_states[60], &mqc_states[54]},
121
    {0x1101, 1, &mqc_states[61], &mqc_states[55]},
122
    {0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
123
    {0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
124
    {0x09c1, 0, &mqc_states[64], &mqc_states[58]},
125
    {0x09c1, 1, &mqc_states[65], &mqc_states[59]},
126
    {0x08a1, 0, &mqc_states[66], &mqc_states[60]},
127
    {0x08a1, 1, &mqc_states[67], &mqc_states[61]},
128
    {0x0521, 0, &mqc_states[68], &mqc_states[62]},
129
    {0x0521, 1, &mqc_states[69], &mqc_states[63]},
130
    {0x0441, 0, &mqc_states[70], &mqc_states[64]},
131
    {0x0441, 1, &mqc_states[71], &mqc_states[65]},
132
    {0x02a1, 0, &mqc_states[72], &mqc_states[66]},
133
    {0x02a1, 1, &mqc_states[73], &mqc_states[67]},
134
    {0x0221, 0, &mqc_states[74], &mqc_states[68]},
135
    {0x0221, 1, &mqc_states[75], &mqc_states[69]},
136
    {0x0141, 0, &mqc_states[76], &mqc_states[70]},
137
    {0x0141, 1, &mqc_states[77], &mqc_states[71]},
138
    {0x0111, 0, &mqc_states[78], &mqc_states[72]},
139
    {0x0111, 1, &mqc_states[79], &mqc_states[73]},
140
    {0x0085, 0, &mqc_states[80], &mqc_states[74]},
141
    {0x0085, 1, &mqc_states[81], &mqc_states[75]},
142
    {0x0049, 0, &mqc_states[82], &mqc_states[76]},
143
    {0x0049, 1, &mqc_states[83], &mqc_states[77]},
144
    {0x0025, 0, &mqc_states[84], &mqc_states[78]},
145
    {0x0025, 1, &mqc_states[85], &mqc_states[79]},
146
    {0x0015, 0, &mqc_states[86], &mqc_states[80]},
147
    {0x0015, 1, &mqc_states[87], &mqc_states[81]},
148
    {0x0009, 0, &mqc_states[88], &mqc_states[82]},
149
    {0x0009, 1, &mqc_states[89], &mqc_states[83]},
150
    {0x0005, 0, &mqc_states[90], &mqc_states[84]},
151
    {0x0005, 1, &mqc_states[91], &mqc_states[85]},
152
    {0x0001, 0, &mqc_states[90], &mqc_states[86]},
153
    {0x0001, 1, &mqc_states[91], &mqc_states[87]},
154
    {0x5601, 0, &mqc_states[92], &mqc_states[92]},
155
    {0x5601, 1, &mqc_states[93], &mqc_states[93]},
156
};
157
158
/*
159
==========================================================
160
   local functions
161
==========================================================
162
*/
163
164
static void opj_mqc_setbits(opj_mqc_t *mqc)
165
0
{
166
0
    OPJ_UINT32 tempc = mqc->c + mqc->a;
167
0
    mqc->c |= 0xffff;
168
0
    if (mqc->c >= tempc) {
169
0
        mqc->c -= 0x8000;
170
0
    }
171
0
}
172
173
/*
174
==========================================================
175
   MQ-Coder interface
176
==========================================================
177
*/
178
179
OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc)
180
0
{
181
0
    const ptrdiff_t diff = mqc->bp - mqc->start;
182
#if 0
183
    assert(diff <= 0xffffffff && diff >= 0);   /* UINT32_MAX */
184
#endif
185
0
    return (OPJ_UINT32)diff;
186
0
}
187
188
void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp)
189
0
{
190
    /* To avoid the curctx pointer to be dangling, but not strictly */
191
    /* required as the current context is always set before encoding */
192
0
    opj_mqc_setcurctx(mqc, 0);
193
194
    /* As specified in Figure C.10 - Initialization of the encoder */
195
    /* (C.2.8 Initialization of the encoder (INITENC)) */
196
0
    mqc->a = 0x8000;
197
0
    mqc->c = 0;
198
    /* Yes, we point before the start of the buffer, but this is safe */
199
    /* given opj_tcd_code_block_enc_allocate_data() */
200
0
    mqc->bp = bp - 1;
201
0
    mqc->ct = 12;
202
    /* At this point we should test *(mqc->bp) against 0xFF, but this is not */
203
    /* necessary, as this is only used at the beginning of the code block */
204
    /* and our initial fake byte is set at 0 */
205
0
    assert(*(mqc->bp) != 0xff);
206
207
0
    mqc->start = bp;
208
0
    mqc->end_of_byte_stream_counter = 0;
209
0
}
210
211
212
void opj_mqc_flush(opj_mqc_t *mqc)
213
0
{
214
    /* C.2.9 Termination of coding (FLUSH) */
215
    /* Figure C.11 – FLUSH procedure */
216
0
    opj_mqc_setbits(mqc);
217
0
    mqc->c <<= mqc->ct;
218
0
    opj_mqc_byteout(mqc);
219
0
    mqc->c <<= mqc->ct;
220
0
    opj_mqc_byteout(mqc);
221
222
    /* It is forbidden that a coding pass ends with 0xff */
223
0
    if (*mqc->bp != 0xff) {
224
        /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
225
0
        mqc->bp++;
226
0
    }
227
0
}
228
229
void opj_mqc_bypass_init_enc(opj_mqc_t *mqc)
230
0
{
231
    /* This function is normally called after at least one opj_mqc_flush() */
232
    /* which will have advance mqc->bp by at least 2 bytes beyond its */
233
    /* initial position */
234
0
    assert(mqc->bp >= mqc->start);
235
0
    mqc->c = 0;
236
    /* in theory we should initialize to 8, but use this special value */
237
    /* as a hint that opj_mqc_bypass_enc() has never been called, so */
238
    /* as to avoid the 0xff 0x7f elimination trick in opj_mqc_bypass_flush_enc() */
239
    /* to trigger when we don't have output any bit during this bypass sequence */
240
    /* Any value > 8 will do */
241
0
    mqc->ct = BYPASS_CT_INIT;
242
    /* Given that we are called after opj_mqc_flush(), the previous byte */
243
    /* cannot be 0xff. */
244
0
    assert(mqc->bp[-1] != 0xff);
245
0
}
246
247
void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d)
248
0
{
249
0
    if (mqc->ct == BYPASS_CT_INIT) {
250
0
        mqc->ct = 8;
251
0
    }
252
0
    mqc->ct--;
253
0
    mqc->c = mqc->c + (d << mqc->ct);
254
0
    if (mqc->ct == 0) {
255
0
        *mqc->bp = (OPJ_BYTE)mqc->c;
256
0
        mqc->ct = 8;
257
        /* If the previous byte was 0xff, make sure that the next msb is 0 */
258
0
        if (*mqc->bp == 0xff) {
259
0
            mqc->ct = 7;
260
0
        }
261
0
        mqc->bp++;
262
0
        mqc->c = 0;
263
0
    }
264
0
}
265
266
OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm)
267
0
{
268
0
    return (mqc->ct < 7 ||
269
0
            (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0;
270
0
}
271
272
void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm)
273
0
{
274
    /* Is there any bit remaining to be flushed ? */
275
    /* If the last output byte is 0xff, we can discard it, unless */
276
    /* erterm is required (I'm not completely sure why in erterm */
277
    /* we must output 0xff 0x2a if the last byte was 0xff instead of */
278
    /* discarding it, but Kakadu requires it when decoding */
279
    /* in -fussy mode) */
280
0
    if (mqc->ct < 7 || (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) {
281
0
        OPJ_BYTE bit_value = 0;
282
        /* If so, fill the remaining lsbs with an alternating sequence of */
283
        /* 0,1,... */
284
        /* Note: it seems the standard only requires that for a ERTERM flush */
285
        /* and doesn't specify what to do for a regular BYPASS flush */
286
0
        while (mqc->ct > 0) {
287
0
            mqc->ct--;
288
0
            mqc->c += (OPJ_UINT32)(bit_value << mqc->ct);
289
0
            bit_value = (OPJ_BYTE)(1U - bit_value);
290
0
        }
291
0
        *mqc->bp = (OPJ_BYTE)mqc->c;
292
        /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
293
0
        mqc->bp++;
294
0
    } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) {
295
        /* Discard last 0xff */
296
0
        assert(!erterm);
297
0
        mqc->bp --;
298
0
    } else if (mqc->ct == 8 && !erterm &&
299
0
               mqc->bp[-1] == 0x7f && mqc->bp[-2] == 0xff) {
300
        /* Tiny optimization: discard terminating 0xff 0x7f since it is */
301
        /* interpreted as 0xff 0x7f [0xff 0xff] by the decoder, and given */
302
        /* the bit stuffing, in fact as 0xff 0xff [0xff ..] */
303
        /* Happens once on opj_compress -i ../MAPA.tif -o MAPA.j2k  -M 1 */
304
0
        mqc->bp -= 2;
305
0
    }
306
307
0
    assert(mqc->bp[-1] != 0xff);
308
0
}
309
310
void opj_mqc_reset_enc(opj_mqc_t *mqc)
311
0
{
312
0
    opj_mqc_resetstates(mqc);
313
0
    opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
314
0
    opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
315
0
    opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
316
0
}
317
318
#ifdef notdef
319
OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc)
320
{
321
    OPJ_UINT32 correction = 1;
322
323
    /* <flush part> */
324
    OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
325
    mqc->c <<= mqc->ct;
326
    while (n > 0) {
327
        opj_mqc_byteout(mqc);
328
        n -= (OPJ_INT32)mqc->ct;
329
        mqc->c <<= mqc->ct;
330
    }
331
    opj_mqc_byteout(mqc);
332
333
    return correction;
334
}
335
#endif
336
337
void opj_mqc_restart_init_enc(opj_mqc_t *mqc)
338
0
{
339
    /* <Re-init part> */
340
341
    /* As specified in Figure C.10 - Initialization of the encoder */
342
    /* (C.2.8 Initialization of the encoder (INITENC)) */
343
0
    mqc->a = 0x8000;
344
0
    mqc->c = 0;
345
0
    mqc->ct = 12;
346
    /* This function is normally called after at least one opj_mqc_flush() */
347
    /* which will have advance mqc->bp by at least 2 bytes beyond its */
348
    /* initial position */
349
0
    mqc->bp --;
350
0
    assert(mqc->bp >= mqc->start - 1);
351
0
    assert(*mqc->bp != 0xff);
352
0
    if (*mqc->bp == 0xff) {
353
0
        mqc->ct = 13;
354
0
    }
355
0
}
356
357
void opj_mqc_erterm_enc(opj_mqc_t *mqc)
358
0
{
359
0
    OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
360
361
0
    while (k > 0) {
362
0
        mqc->c <<= mqc->ct;
363
0
        mqc->ct = 0;
364
0
        opj_mqc_byteout(mqc);
365
0
        k -= (OPJ_INT32)mqc->ct;
366
0
    }
367
368
0
    if (*mqc->bp != 0xff) {
369
0
        opj_mqc_byteout(mqc);
370
0
    }
371
0
}
372
373
static INLINE void opj_mqc_renorme(opj_mqc_t *mqc)
374
0
{
375
0
    opj_mqc_renorme_macro(mqc, mqc->a, mqc->c, mqc->ct);
376
0
}
377
378
/**
379
Encode the most probable symbol
380
@param mqc MQC handle
381
*/
382
static INLINE void opj_mqc_codemps(opj_mqc_t *mqc)
383
0
{
384
0
    opj_mqc_codemps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
385
0
}
386
387
/**
388
Encode the most least symbol
389
@param mqc MQC handle
390
*/
391
static INLINE void opj_mqc_codelps(opj_mqc_t *mqc)
392
0
{
393
0
    opj_mqc_codelps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
394
0
}
395
396
/**
397
Encode a symbol using the MQ-coder
398
@param mqc MQC handle
399
@param d The symbol to be encoded (0 or 1)
400
*/
401
static INLINE void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d)
402
0
{
403
0
    if ((*mqc->curctx)->mps == d) {
404
0
        opj_mqc_codemps(mqc);
405
0
    } else {
406
0
        opj_mqc_codelps(mqc);
407
0
    }
408
0
}
409
410
void opj_mqc_segmark_enc(opj_mqc_t *mqc)
411
0
{
412
0
    OPJ_UINT32 i;
413
0
    opj_mqc_setcurctx(mqc, 18);
414
415
0
    for (i = 1; i < 5; i++) {
416
0
        opj_mqc_encode(mqc, i % 2);
417
0
    }
418
0
}
419
420
static void opj_mqc_init_dec_common(opj_mqc_t *mqc,
421
                                    OPJ_BYTE *bp,
422
                                    OPJ_UINT32 len,
423
                                    OPJ_UINT32 extra_writable_bytes)
424
184k
{
425
184k
    (void)extra_writable_bytes;
426
427
184k
    assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA);
428
184k
    mqc->start = bp;
429
184k
    mqc->end = bp + len;
430
    /* Insert an artificial 0xFF 0xFF marker at end of the code block */
431
    /* data so that the bytein routines stop on it. This saves us comparing */
432
    /* the bp and end pointers */
433
    /* But before inserting it, backup th bytes we will overwrite */
434
184k
    memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA);
435
184k
    mqc->end[0] = 0xFF;
436
184k
    mqc->end[1] = 0xFF;
437
184k
    mqc->bp = bp;
438
184k
}
439
void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
440
                      OPJ_UINT32 extra_writable_bytes)
441
161k
{
442
    /* Implements ISO 15444-1 C.3.5 Initialization of the decoder (INITDEC) */
443
    /* Note: alternate "J.1 - Initialization of the software-conventions */
444
    /* decoder" has been tried, but does */
445
    /* not bring any improvement. */
446
    /* See https://github.com/uclouvain/openjpeg/issues/921 */
447
161k
    opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
448
161k
    opj_mqc_setcurctx(mqc, 0);
449
161k
    mqc->end_of_byte_stream_counter = 0;
450
161k
    if (len == 0) {
451
21.8k
        mqc->c = 0xff << 16;
452
139k
    } else {
453
139k
        mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
454
139k
    }
455
456
161k
    opj_mqc_bytein(mqc);
457
161k
    mqc->c <<= 7;
458
161k
    mqc->ct -= 7;
459
161k
    mqc->a = 0x8000;
460
161k
}
461
462
463
void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
464
                          OPJ_UINT32 extra_writable_bytes)
465
22.7k
{
466
22.7k
    opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
467
22.7k
    mqc->c = 0;
468
22.7k
    mqc->ct = 0;
469
22.7k
}
470
471
472
void opq_mqc_finish_dec(opj_mqc_t *mqc)
473
184k
{
474
    /* Restore the bytes overwritten by opj_mqc_init_dec_common() */
475
184k
    memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA);
476
184k
}
477
478
void opj_mqc_resetstates(opj_mqc_t *mqc)
479
1.29M
{
480
1.29M
    OPJ_UINT32 i;
481
25.9M
    for (i = 0; i < MQC_NUMCTXS; i++) {
482
24.6M
        mqc->ctxs[i] = mqc_states;
483
24.6M
    }
484
1.29M
}
485
486
void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb,
487
                      OPJ_INT32 prob)
488
3.89M
{
489
3.89M
    mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
490
3.89M
}
491
492
void opj_mqc_byteout(opj_mqc_t *mqc)
493
0
{
494
    /* bp is initialized to start - 1 in opj_mqc_init_enc() */
495
    /* but this is safe, see opj_tcd_code_block_enc_allocate_data() */
496
0
    assert(mqc->bp >= mqc->start - 1);
497
0
    if (*mqc->bp == 0xff) {
498
0
        mqc->bp++;
499
0
        *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
500
0
        mqc->c &= 0xfffff;
501
0
        mqc->ct = 7;
502
0
    } else {
503
0
        if ((mqc->c & 0x8000000) == 0) {
504
0
            mqc->bp++;
505
0
            *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
506
0
            mqc->c &= 0x7ffff;
507
0
            mqc->ct = 8;
508
0
        } else {
509
0
            (*mqc->bp)++;
510
0
            if (*mqc->bp == 0xff) {
511
0
                mqc->c &= 0x7ffffff;
512
0
                mqc->bp++;
513
0
                *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
514
0
                mqc->c &= 0xfffff;
515
0
                mqc->ct = 7;
516
0
            } else {
517
0
                mqc->bp++;
518
0
                *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
519
0
                mqc->c &= 0x7ffff;
520
0
                mqc->ct = 8;
521
0
            }
522
0
        }
523
0
    }
524
0
}