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.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
39 #include "opj_includes.h"
43 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
46 /** @name Local static functions */
50 Fill mqc->c with 1's for flushing
53 static void opj_mqc_setbits(opj_mqc_t *mqc);
59 /* This array defines all the possible states for a context. */
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]},
159 ==========================================================
161 ==========================================================
164 static void opj_mqc_setbits(opj_mqc_t *mqc)
166 OPJ_UINT32 tempc = mqc->c + mqc->a;
168 if (mqc->c >= tempc) {
174 ==========================================================
176 ==========================================================
179 OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc)
181 const ptrdiff_t diff = mqc->bp - mqc->start;
183 assert(diff <= 0xffffffff && diff >= 0); /* UINT32_MAX */
185 return (OPJ_UINT32)diff;
188 void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp)
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);
194 /* As specified in Figure C.10 - Initialization of the encoder */
195 /* (C.2.8 Initialization of the encoder (INITENC)) */
198 /* Yes, we point before the start of the buffer, but this is safe */
199 /* given opj_tcd_code_block_enc_allocate_data() */
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);
208 mqc->end_of_byte_stream_counter = 0;
212 void opj_mqc_flush(opj_mqc_t *mqc)
214 /* C.2.9 Termination of coding (FLUSH) */
215 /* Figure C.11 – FLUSH procedure */
216 opj_mqc_setbits(mqc);
218 opj_mqc_byteout(mqc);
220 opj_mqc_byteout(mqc);
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 */
229 void opj_mqc_bypass_init_enc(opj_mqc_t *mqc)
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);
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);
247 void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d)
249 if (mqc->ct == BYPASS_CT_INIT) {
253 mqc->c = mqc->c + (d << mqc->ct);
255 *mqc->bp = (OPJ_BYTE)mqc->c;
257 /* If the previous byte was 0xff, make sure that the next msb is 0 */
258 if (*mqc->bp == 0xff) {
266 OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm)
268 return (mqc->ct < 7 ||
269 (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0;
272 void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm)
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 */
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) {
288 mqc->c += (OPJ_UINT32)(bit_value << mqc->ct);
289 bit_value = (OPJ_BYTE)(1U - bit_value);
291 *mqc->bp = (OPJ_BYTE)mqc->c;
292 /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
294 } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) {
295 /* Discard last 0xff */
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 */
307 assert(mqc->bp[-1] != 0xff);
310 void opj_mqc_reset_enc(opj_mqc_t *mqc)
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);
319 OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc)
321 OPJ_UINT32 correction = 1;
324 OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
327 opj_mqc_byteout(mqc);
328 n -= (OPJ_INT32)mqc->ct;
331 opj_mqc_byteout(mqc);
337 void opj_mqc_restart_init_enc(opj_mqc_t *mqc)
341 /* As specified in Figure C.10 - Initialization of the encoder */
342 /* (C.2.8 Initialization of the encoder (INITENC)) */
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 */
350 assert(mqc->bp >= mqc->start - 1);
351 assert(*mqc->bp != 0xff);
352 if (*mqc->bp == 0xff) {
357 void opj_mqc_erterm_enc(opj_mqc_t *mqc)
359 OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
364 opj_mqc_byteout(mqc);
365 k -= (OPJ_INT32)mqc->ct;
368 if (*mqc->bp != 0xff) {
369 opj_mqc_byteout(mqc);
373 static INLINE void opj_mqc_renorme(opj_mqc_t *mqc)
375 opj_mqc_renorme_macro(mqc, mqc->a, mqc->c, mqc->ct);
379 Encode the most probable symbol
380 @param mqc MQC handle
382 static INLINE void opj_mqc_codemps(opj_mqc_t *mqc)
384 opj_mqc_codemps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
388 Encode the most least symbol
389 @param mqc MQC handle
391 static INLINE void opj_mqc_codelps(opj_mqc_t *mqc)
393 opj_mqc_codelps_macro(mqc, mqc->curctx, mqc->a, mqc->c, mqc->ct);
397 Encode a symbol using the MQ-coder
398 @param mqc MQC handle
399 @param d The symbol to be encoded (0 or 1)
401 static INLINE void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d)
403 if ((*mqc->curctx)->mps == d) {
404 opj_mqc_codemps(mqc);
406 opj_mqc_codelps(mqc);
410 void opj_mqc_segmark_enc(opj_mqc_t *mqc)
413 opj_mqc_setcurctx(mqc, 18);
415 for (i = 1; i < 5; i++) {
416 opj_mqc_encode(mqc, i % 2);
420 static void opj_mqc_init_dec_common(opj_mqc_t *mqc,
423 OPJ_UINT32 extra_writable_bytes)
425 (void)extra_writable_bytes;
427 assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA);
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 memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA);
439 void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
440 OPJ_UINT32 extra_writable_bytes)
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 opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
448 opj_mqc_setcurctx(mqc, 0);
449 mqc->end_of_byte_stream_counter = 0;
453 mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
463 void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
464 OPJ_UINT32 extra_writable_bytes)
466 opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
472 void opq_mqc_finish_dec(opj_mqc_t *mqc)
474 /* Restore the bytes overwritten by opj_mqc_init_dec_common() */
475 memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA);
478 void opj_mqc_resetstates(opj_mqc_t *mqc)
481 for (i = 0; i < MQC_NUMCTXS; i++) {
482 mqc->ctxs[i] = mqc_states;
486 void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb,
489 mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
492 void opj_mqc_byteout(opj_mqc_t *mqc)
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 assert(mqc->bp >= mqc->start - 1);
497 if (*mqc->bp == 0xff) {
499 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
503 if ((mqc->c & 0x8000000) == 0) {
505 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
510 if (*mqc->bp == 0xff) {
513 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
518 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);