xref: /aosp_15_r20/external/pdfium/third_party/libopenjpeg/mqc.c (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 <[email protected]>
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 
opj_mqc_setbits(opj_mqc_t * mqc)164 static void opj_mqc_setbits(opj_mqc_t *mqc)
165 {
166     OPJ_UINT32 tempc = mqc->c + mqc->a;
167     mqc->c |= 0xffff;
168     if (mqc->c >= tempc) {
169         mqc->c -= 0x8000;
170     }
171 }
172 
173 /*
174 ==========================================================
175    MQ-Coder interface
176 ==========================================================
177 */
178 
opj_mqc_numbytes(opj_mqc_t * mqc)179 OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc)
180 {
181     const ptrdiff_t diff = mqc->bp - mqc->start;
182 #if 0
183     assert(diff <= 0xffffffff && diff >= 0);   /* UINT32_MAX */
184 #endif
185     return (OPJ_UINT32)diff;
186 }
187 
opj_mqc_init_enc(opj_mqc_t * mqc,OPJ_BYTE * bp)188 void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp)
189 {
190     /* To avoid the curctx pointer to be dangling, but not strictly */
191     /* required as the current context is always set before encoding */
192     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     mqc->a = 0x8000;
197     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     mqc->bp = bp - 1;
201     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     assert(*(mqc->bp) != 0xff);
206 
207     mqc->start = bp;
208     mqc->end_of_byte_stream_counter = 0;
209 }
210 
211 
opj_mqc_flush(opj_mqc_t * mqc)212 void opj_mqc_flush(opj_mqc_t *mqc)
213 {
214     /* C.2.9 Termination of coding (FLUSH) */
215     /* Figure C.11 – FLUSH procedure */
216     opj_mqc_setbits(mqc);
217     mqc->c <<= mqc->ct;
218     opj_mqc_byteout(mqc);
219     mqc->c <<= mqc->ct;
220     opj_mqc_byteout(mqc);
221 
222     /* It is forbidden that a coding pass ends with 0xff */
223     if (*mqc->bp != 0xff) {
224         /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
225         mqc->bp++;
226     }
227 }
228 
opj_mqc_bypass_init_enc(opj_mqc_t * mqc)229 void opj_mqc_bypass_init_enc(opj_mqc_t *mqc)
230 {
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     assert(mqc->bp >= mqc->start);
235     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     mqc->ct = BYPASS_CT_INIT;
242     /* Given that we are called after opj_mqc_flush(), the previous byte */
243     /* cannot be 0xff. */
244     assert(mqc->bp[-1] != 0xff);
245 }
246 
opj_mqc_bypass_enc(opj_mqc_t * mqc,OPJ_UINT32 d)247 void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d)
248 {
249     if (mqc->ct == BYPASS_CT_INIT) {
250         mqc->ct = 8;
251     }
252     mqc->ct--;
253     mqc->c = mqc->c + (d << mqc->ct);
254     if (mqc->ct == 0) {
255         *mqc->bp = (OPJ_BYTE)mqc->c;
256         mqc->ct = 8;
257         /* If the previous byte was 0xff, make sure that the next msb is 0 */
258         if (*mqc->bp == 0xff) {
259             mqc->ct = 7;
260         }
261         mqc->bp++;
262         mqc->c = 0;
263     }
264 }
265 
opj_mqc_bypass_get_extra_bytes(opj_mqc_t * mqc,OPJ_BOOL erterm)266 OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm)
267 {
268     return (mqc->ct < 7 ||
269             (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0;
270 }
271 
opj_mqc_bypass_flush_enc(opj_mqc_t * mqc,OPJ_BOOL erterm)272 void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm)
273 {
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     if (mqc->ct < 7 || (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) {
281         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         while (mqc->ct > 0) {
287             mqc->ct--;
288             mqc->c += (OPJ_UINT32)(bit_value << mqc->ct);
289             bit_value = (OPJ_BYTE)(1U - bit_value);
290         }
291         *mqc->bp = (OPJ_BYTE)mqc->c;
292         /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
293         mqc->bp++;
294     } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) {
295         /* Discard last 0xff */
296         assert(!erterm);
297         mqc->bp --;
298     } else if (mqc->ct == 8 && !erterm &&
299                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         mqc->bp -= 2;
305     }
306 
307     assert(mqc->bp[-1] != 0xff);
308 }
309 
opj_mqc_reset_enc(opj_mqc_t * mqc)310 void opj_mqc_reset_enc(opj_mqc_t *mqc)
311 {
312     opj_mqc_resetstates(mqc);
313     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
314     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
315     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
316 }
317 
318 #ifdef notdef
opj_mqc_restart_enc(opj_mqc_t * mqc)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 
opj_mqc_restart_init_enc(opj_mqc_t * mqc)337 void opj_mqc_restart_init_enc(opj_mqc_t *mqc)
338 {
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     mqc->a = 0x8000;
344     mqc->c = 0;
345     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     mqc->bp --;
350     assert(mqc->bp >= mqc->start - 1);
351     assert(*mqc->bp != 0xff);
352     if (*mqc->bp == 0xff) {
353         mqc->ct = 13;
354     }
355 }
356 
opj_mqc_erterm_enc(opj_mqc_t * mqc)357 void opj_mqc_erterm_enc(opj_mqc_t *mqc)
358 {
359     OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
360 
361     while (k > 0) {
362         mqc->c <<= mqc->ct;
363         mqc->ct = 0;
364         opj_mqc_byteout(mqc);
365         k -= (OPJ_INT32)mqc->ct;
366     }
367 
368     if (*mqc->bp != 0xff) {
369         opj_mqc_byteout(mqc);
370     }
371 }
372 
373 /**
374 Encode the most probable symbol
375 @param mqc MQC handle
376 */
opj_mqc_codemps(opj_mqc_t * mqc)377 static INLINE void opj_mqc_codemps(opj_mqc_t *mqc)
378 {
379     opj_mqc_codemps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
380 }
381 
382 /**
383 Encode the most least symbol
384 @param mqc MQC handle
385 */
opj_mqc_codelps(opj_mqc_t * mqc)386 static INLINE void opj_mqc_codelps(opj_mqc_t *mqc)
387 {
388     opj_mqc_codelps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
389 }
390 
391 /**
392 Encode a symbol using the MQ-coder
393 @param mqc MQC handle
394 @param d The symbol to be encoded (0 or 1)
395 */
opj_mqc_encode(opj_mqc_t * mqc,OPJ_UINT32 d)396 static INLINE void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d)
397 {
398     if ((*mqc->curctx)->mps == d) {
399         opj_mqc_codemps(mqc);
400     } else {
401         opj_mqc_codelps(mqc);
402     }
403 }
404 
opj_mqc_segmark_enc(opj_mqc_t * mqc)405 void opj_mqc_segmark_enc(opj_mqc_t *mqc)
406 {
407     OPJ_UINT32 i;
408     opj_mqc_setcurctx(mqc, 18);
409 
410     for (i = 1; i < 5; i++) {
411         opj_mqc_encode(mqc, i % 2);
412     }
413 }
414 
opj_mqc_init_dec_common(opj_mqc_t * mqc,OPJ_BYTE * bp,OPJ_UINT32 len,OPJ_UINT32 extra_writable_bytes)415 static void opj_mqc_init_dec_common(opj_mqc_t *mqc,
416                                     OPJ_BYTE *bp,
417                                     OPJ_UINT32 len,
418                                     OPJ_UINT32 extra_writable_bytes)
419 {
420     (void)extra_writable_bytes;
421 
422     assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA);
423     mqc->start = bp;
424     mqc->end = bp + len;
425     /* Insert an artificial 0xFF 0xFF marker at end of the code block */
426     /* data so that the bytein routines stop on it. This saves us comparing */
427     /* the bp and end pointers */
428     /* But before inserting it, backup th bytes we will overwrite */
429     memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA);
430     mqc->end[0] = 0xFF;
431     mqc->end[1] = 0xFF;
432     mqc->bp = bp;
433 }
opj_mqc_init_dec(opj_mqc_t * mqc,OPJ_BYTE * bp,OPJ_UINT32 len,OPJ_UINT32 extra_writable_bytes)434 void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
435                       OPJ_UINT32 extra_writable_bytes)
436 {
437     /* Implements ISO 15444-1 C.3.5 Initialization of the decoder (INITDEC) */
438     /* Note: alternate "J.1 - Initialization of the software-conventions */
439     /* decoder" has been tried, but does */
440     /* not bring any improvement. */
441     /* See https://github.com/uclouvain/openjpeg/issues/921 */
442     opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
443     opj_mqc_setcurctx(mqc, 0);
444     mqc->end_of_byte_stream_counter = 0;
445     if (len == 0) {
446         mqc->c = 0xff << 16;
447     } else {
448         mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
449     }
450 
451     opj_mqc_bytein(mqc);
452     mqc->c <<= 7;
453     mqc->ct -= 7;
454     mqc->a = 0x8000;
455 }
456 
457 
opj_mqc_raw_init_dec(opj_mqc_t * mqc,OPJ_BYTE * bp,OPJ_UINT32 len,OPJ_UINT32 extra_writable_bytes)458 void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
459                           OPJ_UINT32 extra_writable_bytes)
460 {
461     opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
462     mqc->c = 0;
463     mqc->ct = 0;
464 }
465 
466 
opq_mqc_finish_dec(opj_mqc_t * mqc)467 void opq_mqc_finish_dec(opj_mqc_t *mqc)
468 {
469     /* Restore the bytes overwritten by opj_mqc_init_dec_common() */
470     memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA);
471 }
472 
opj_mqc_resetstates(opj_mqc_t * mqc)473 void opj_mqc_resetstates(opj_mqc_t *mqc)
474 {
475     OPJ_UINT32 i;
476     for (i = 0; i < MQC_NUMCTXS; i++) {
477         mqc->ctxs[i] = mqc_states;
478     }
479 }
480 
opj_mqc_setstate(opj_mqc_t * mqc,OPJ_UINT32 ctxno,OPJ_UINT32 msb,OPJ_INT32 prob)481 void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb,
482                       OPJ_INT32 prob)
483 {
484     mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
485 }
486 
opj_mqc_byteout(opj_mqc_t * mqc)487 void opj_mqc_byteout(opj_mqc_t *mqc)
488 {
489     /* bp is initialized to start - 1 in opj_mqc_init_enc() */
490     /* but this is safe, see opj_tcd_code_block_enc_allocate_data() */
491     assert(mqc->bp >= mqc->start - 1);
492     if (*mqc->bp == 0xff) {
493         mqc->bp++;
494         *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
495         mqc->c &= 0xfffff;
496         mqc->ct = 7;
497     } else {
498         if ((mqc->c & 0x8000000) == 0) {
499             mqc->bp++;
500             *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
501             mqc->c &= 0x7ffff;
502             mqc->ct = 8;
503         } else {
504             (*mqc->bp)++;
505             if (*mqc->bp == 0xff) {
506                 mqc->c &= 0x7ffffff;
507                 mqc->bp++;
508                 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
509                 mqc->c &= 0xfffff;
510                 mqc->ct = 7;
511             } else {
512                 mqc->bp++;
513                 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
514                 mqc->c &= 0x7ffff;
515                 mqc->ct = 8;
516             }
517         }
518     }
519 }