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