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 Output a byte, doing bit-stuffing if necessary.
51 After a 0xff byte, the next byte must be smaller than 0x90.
54 static void opj_mqc_byteout(opj_mqc_t *mqc);
56 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
59 static void opj_mqc_renorme(opj_mqc_t *mqc);
61 Encode the most probable symbol
64 static void opj_mqc_codemps(opj_mqc_t *mqc);
66 Encode the most least symbol
69 static void opj_mqc_codelps(opj_mqc_t *mqc);
71 Fill mqc->c with 1's for flushing
74 static void opj_mqc_setbits(opj_mqc_t *mqc);
80 /* This array defines all the possible states for a context. */
82 static const 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]},
180 ==========================================================
182 ==========================================================
185 static void opj_mqc_byteout(opj_mqc_t *mqc)
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) {
192 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
196 if ((mqc->c & 0x8000000) == 0) {
198 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
203 if (*mqc->bp == 0xff) {
206 *mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
211 *mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
219 static void opj_mqc_renorme(opj_mqc_t *mqc)
226 opj_mqc_byteout(mqc);
228 } while ((mqc->a & 0x8000) == 0);
231 static void opj_mqc_codemps(opj_mqc_t *mqc)
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;
238 mqc->c += (*mqc->curctx)->qeval;
240 *mqc->curctx = (*mqc->curctx)->nmps;
241 opj_mqc_renorme(mqc);
243 mqc->c += (*mqc->curctx)->qeval;
247 static void opj_mqc_codelps(opj_mqc_t *mqc)
249 mqc->a -= (*mqc->curctx)->qeval;
250 if (mqc->a < (*mqc->curctx)->qeval) {
251 mqc->c += (*mqc->curctx)->qeval;
253 mqc->a = (*mqc->curctx)->qeval;
255 *mqc->curctx = (*mqc->curctx)->nlps;
256 opj_mqc_renorme(mqc);
259 static void opj_mqc_setbits(opj_mqc_t *mqc)
261 OPJ_UINT32 tempc = mqc->c + mqc->a;
263 if (mqc->c >= tempc) {
269 ==========================================================
271 ==========================================================
274 OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc)
276 const ptrdiff_t diff = mqc->bp - mqc->start;
278 assert(diff <= 0xffffffff && diff >= 0); /* UINT32_MAX */
280 return (OPJ_UINT32)diff;
283 void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp)
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);
289 /* As specified in Figure C.10 - Initialization of the encoder */
290 /* (C.2.8 Initialization of the encoder (INITENC)) */
293 /* Yes, we point before the start of the buffer, but this is safe */
294 /* given opj_tcd_code_block_enc_allocate_data() */
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);
303 mqc->end_of_byte_stream_counter = 0;
306 void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d)
308 if ((*mqc->curctx)->mps == d) {
309 opj_mqc_codemps(mqc);
311 opj_mqc_codelps(mqc);
315 void opj_mqc_flush(opj_mqc_t *mqc)
317 /* C.2.9 Termination of coding (FLUSH) */
318 /* Figure C.11 – FLUSH procedure */
319 opj_mqc_setbits(mqc);
321 opj_mqc_byteout(mqc);
323 opj_mqc_byteout(mqc);
325 /* It is forbidden that a coding pass ends with 0xff */
326 if (*mqc->bp != 0xff) {
327 /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
332 #define BYPASS_CT_INIT 0xDEADBEEF
334 void opj_mqc_bypass_init_enc(opj_mqc_t *mqc)
336 /* This function is normally called after at least one opj_mqc_flush() */
337 /* which will have advance mqc->bp by at least 2 bytes beyond its */
338 /* initial position */
339 assert(mqc->bp >= mqc->start);
341 /* in theory we should initialize to 8, but use this special value */
342 /* as a hint that opj_mqc_bypass_enc() has never been called, so */
343 /* as to avoid the 0xff 0x7f elimination trick in opj_mqc_bypass_flush_enc() */
344 /* to trigger when we don't have output any bit during this bypass sequence */
345 /* Any value > 8 will do */
346 mqc->ct = BYPASS_CT_INIT;
347 /* Given that we are called after opj_mqc_flush(), the previous byte */
348 /* cannot be 0xff. */
349 assert(mqc->bp[-1] != 0xff);
352 void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d)
354 if (mqc->ct == BYPASS_CT_INIT) {
358 mqc->c = mqc->c + (d << mqc->ct);
360 *mqc->bp = (OPJ_BYTE)mqc->c;
362 /* If the previous byte was 0xff, make sure that the next msb is 0 */
363 if (*mqc->bp == 0xff) {
371 OPJ_UINT32 opj_mqc_bypass_get_extra_bytes(opj_mqc_t *mqc, OPJ_BOOL erterm)
373 return (mqc->ct < 7 ||
374 (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) ? 1 : 0;
377 void opj_mqc_bypass_flush_enc(opj_mqc_t *mqc, OPJ_BOOL erterm)
379 /* Is there any bit remaining to be flushed ? */
380 /* If the last output byte is 0xff, we can discard it, unless */
381 /* erterm is required (I'm not completely sure why in erterm */
382 /* we must output 0xff 0x2a if the last byte was 0xff instead of */
383 /* discarding it, but Kakadu requires it when decoding */
384 /* in -fussy mode) */
385 if (mqc->ct < 7 || (mqc->ct == 7 && (erterm || mqc->bp[-1] != 0xff))) {
386 OPJ_BYTE bit_value = 0;
387 /* If so, fill the remaining lsbs with an alternating sequence of */
389 /* Note: it seems the standard only requires that for a ERTERM flush */
390 /* and doesn't specify what to do for a regular BYPASS flush */
391 while (mqc->ct > 0) {
393 mqc->c += (OPJ_UINT32)(bit_value << mqc->ct);
394 bit_value = (OPJ_BYTE)(1U - bit_value);
396 *mqc->bp = (OPJ_BYTE)mqc->c;
397 /* Advance pointer so that opj_mqc_numbytes() returns a valid value */
399 } else if (mqc->ct == 7 && mqc->bp[-1] == 0xff) {
400 /* Discard last 0xff */
403 } else if (mqc->ct == 8 && !erterm &&
404 mqc->bp[-1] == 0x7f && mqc->bp[-2] == 0xff) {
405 /* Tiny optimization: discard terminating 0xff 0x7f since it is */
406 /* interpreted as 0xff 0x7f [0xff 0xff] by the decoder, and given */
407 /* the bit stuffing, in fact as 0xff 0xff [0xff ..] */
408 /* Happens once on opj_compress -i ../MAPA.tif -o MAPA.j2k -M 1 */
412 assert(mqc->bp[-1] != 0xff);
415 void opj_mqc_reset_enc(opj_mqc_t *mqc)
417 opj_mqc_resetstates(mqc);
418 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
419 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
420 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
424 OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc)
426 OPJ_UINT32 correction = 1;
429 OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
432 opj_mqc_byteout(mqc);
433 n -= (OPJ_INT32)mqc->ct;
436 opj_mqc_byteout(mqc);
442 void opj_mqc_restart_init_enc(opj_mqc_t *mqc)
446 /* As specified in Figure C.10 - Initialization of the encoder */
447 /* (C.2.8 Initialization of the encoder (INITENC)) */
451 /* This function is normally called after at least one opj_mqc_flush() */
452 /* which will have advance mqc->bp by at least 2 bytes beyond its */
453 /* initial position */
455 assert(mqc->bp >= mqc->start - 1);
456 assert(*mqc->bp != 0xff);
457 if (*mqc->bp == 0xff) {
462 void opj_mqc_erterm_enc(opj_mqc_t *mqc)
464 OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
469 opj_mqc_byteout(mqc);
470 k -= (OPJ_INT32)mqc->ct;
473 if (*mqc->bp != 0xff) {
474 opj_mqc_byteout(mqc);
478 void opj_mqc_segmark_enc(opj_mqc_t *mqc)
481 opj_mqc_setcurctx(mqc, 18);
483 for (i = 1; i < 5; i++) {
484 opj_mqc_encode(mqc, i % 2);
488 static void opj_mqc_init_dec_common(opj_mqc_t *mqc,
491 OPJ_UINT32 extra_writable_bytes)
493 (void)extra_writable_bytes;
495 assert(extra_writable_bytes >= OPJ_COMMON_CBLK_DATA_EXTRA);
498 /* Insert an artificial 0xFF 0xFF marker at end of the code block */
499 /* data so that the bytein routines stop on it. This saves us comparing */
500 /* the bp and end pointers */
501 /* But before inserting it, backup th bytes we will overwrite */
502 memcpy(mqc->backup, mqc->end, OPJ_COMMON_CBLK_DATA_EXTRA);
507 void opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
508 OPJ_UINT32 extra_writable_bytes)
510 /* Implements ISO 15444-1 C.3.5 Initialization of the decoder (INITDEC) */
511 /* Note: alternate "J.1 - Initialization of the software-conventions */
512 /* decoder" has been tried, but does */
513 /* not bring any improvement. */
514 /* See https://github.com/uclouvain/openjpeg/issues/921 */
515 opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
516 opj_mqc_setcurctx(mqc, 0);
517 mqc->end_of_byte_stream_counter = 0;
521 mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
531 void opj_mqc_raw_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len,
532 OPJ_UINT32 extra_writable_bytes)
534 opj_mqc_init_dec_common(mqc, bp, len, extra_writable_bytes);
540 void opq_mqc_finish_dec(opj_mqc_t *mqc)
542 /* Restore the bytes overwritten by opj_mqc_init_dec_common() */
543 memcpy(mqc->end, mqc->backup, OPJ_COMMON_CBLK_DATA_EXTRA);
546 void opj_mqc_resetstates(opj_mqc_t *mqc)
549 for (i = 0; i < MQC_NUMCTXS; i++) {
550 mqc->ctxs[i] = mqc_states;
554 void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb,
557 mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];