• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /** @defgroup MQC MQC - Implementation of an MQ-Coder */
42 /*@{*/
43 
44 /** @name Local static functions */
45 /*@{*/
46 
47 /**
48 Output a byte, doing bit-stuffing if necessary.
49 After a 0xff byte, the next byte must be smaller than 0x90.
50 @param mqc MQC handle
51 */
52 static void opj_mqc_byteout(opj_mqc_t *mqc);
53 /**
54 Renormalize mqc->a and mqc->c while encoding, so that mqc->a stays between 0x8000 and 0x10000
55 @param mqc MQC handle
56 */
57 static void opj_mqc_renorme(opj_mqc_t *mqc);
58 /**
59 Encode the most probable symbol
60 @param mqc MQC handle
61 */
62 static void opj_mqc_codemps(opj_mqc_t *mqc);
63 /**
64 Encode the most least symbol
65 @param mqc MQC handle
66 */
67 static void opj_mqc_codelps(opj_mqc_t *mqc);
68 /**
69 Fill mqc->c with 1's for flushing
70 @param mqc MQC handle
71 */
72 static void opj_mqc_setbits(opj_mqc_t *mqc);
73 /**
74 FIXME DOC
75 @param mqc MQC handle
76 @return
77 */
78 static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc);
79 /**
80 FIXME DOC
81 @param mqc MQC handle
82 @return
83 */
84 static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc);
85 /**
86 Input a byte
87 @param mqc MQC handle
88 */
89 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc);
90 /**
91 Renormalize mqc->a and mqc->c while decoding
92 @param mqc MQC handle
93 */
94 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc);
95 /*@}*/
96 
97 /*@}*/
98 
99 /* <summary> */
100 /* This array defines all the possible states for a context. */
101 /* </summary> */
102 static opj_mqc_state_t mqc_states[47 * 2] = {
103 	{0x5601, 0, &mqc_states[2], &mqc_states[3]},
104 	{0x5601, 1, &mqc_states[3], &mqc_states[2]},
105 	{0x3401, 0, &mqc_states[4], &mqc_states[12]},
106 	{0x3401, 1, &mqc_states[5], &mqc_states[13]},
107 	{0x1801, 0, &mqc_states[6], &mqc_states[18]},
108 	{0x1801, 1, &mqc_states[7], &mqc_states[19]},
109 	{0x0ac1, 0, &mqc_states[8], &mqc_states[24]},
110 	{0x0ac1, 1, &mqc_states[9], &mqc_states[25]},
111 	{0x0521, 0, &mqc_states[10], &mqc_states[58]},
112 	{0x0521, 1, &mqc_states[11], &mqc_states[59]},
113 	{0x0221, 0, &mqc_states[76], &mqc_states[66]},
114 	{0x0221, 1, &mqc_states[77], &mqc_states[67]},
115 	{0x5601, 0, &mqc_states[14], &mqc_states[13]},
116 	{0x5601, 1, &mqc_states[15], &mqc_states[12]},
117 	{0x5401, 0, &mqc_states[16], &mqc_states[28]},
118 	{0x5401, 1, &mqc_states[17], &mqc_states[29]},
119 	{0x4801, 0, &mqc_states[18], &mqc_states[28]},
120 	{0x4801, 1, &mqc_states[19], &mqc_states[29]},
121 	{0x3801, 0, &mqc_states[20], &mqc_states[28]},
122 	{0x3801, 1, &mqc_states[21], &mqc_states[29]},
123 	{0x3001, 0, &mqc_states[22], &mqc_states[34]},
124 	{0x3001, 1, &mqc_states[23], &mqc_states[35]},
125 	{0x2401, 0, &mqc_states[24], &mqc_states[36]},
126 	{0x2401, 1, &mqc_states[25], &mqc_states[37]},
127 	{0x1c01, 0, &mqc_states[26], &mqc_states[40]},
128 	{0x1c01, 1, &mqc_states[27], &mqc_states[41]},
129 	{0x1601, 0, &mqc_states[58], &mqc_states[42]},
130 	{0x1601, 1, &mqc_states[59], &mqc_states[43]},
131 	{0x5601, 0, &mqc_states[30], &mqc_states[29]},
132 	{0x5601, 1, &mqc_states[31], &mqc_states[28]},
133 	{0x5401, 0, &mqc_states[32], &mqc_states[28]},
134 	{0x5401, 1, &mqc_states[33], &mqc_states[29]},
135 	{0x5101, 0, &mqc_states[34], &mqc_states[30]},
136 	{0x5101, 1, &mqc_states[35], &mqc_states[31]},
137 	{0x4801, 0, &mqc_states[36], &mqc_states[32]},
138 	{0x4801, 1, &mqc_states[37], &mqc_states[33]},
139 	{0x3801, 0, &mqc_states[38], &mqc_states[34]},
140 	{0x3801, 1, &mqc_states[39], &mqc_states[35]},
141 	{0x3401, 0, &mqc_states[40], &mqc_states[36]},
142 	{0x3401, 1, &mqc_states[41], &mqc_states[37]},
143 	{0x3001, 0, &mqc_states[42], &mqc_states[38]},
144 	{0x3001, 1, &mqc_states[43], &mqc_states[39]},
145 	{0x2801, 0, &mqc_states[44], &mqc_states[38]},
146 	{0x2801, 1, &mqc_states[45], &mqc_states[39]},
147 	{0x2401, 0, &mqc_states[46], &mqc_states[40]},
148 	{0x2401, 1, &mqc_states[47], &mqc_states[41]},
149 	{0x2201, 0, &mqc_states[48], &mqc_states[42]},
150 	{0x2201, 1, &mqc_states[49], &mqc_states[43]},
151 	{0x1c01, 0, &mqc_states[50], &mqc_states[44]},
152 	{0x1c01, 1, &mqc_states[51], &mqc_states[45]},
153 	{0x1801, 0, &mqc_states[52], &mqc_states[46]},
154 	{0x1801, 1, &mqc_states[53], &mqc_states[47]},
155 	{0x1601, 0, &mqc_states[54], &mqc_states[48]},
156 	{0x1601, 1, &mqc_states[55], &mqc_states[49]},
157 	{0x1401, 0, &mqc_states[56], &mqc_states[50]},
158 	{0x1401, 1, &mqc_states[57], &mqc_states[51]},
159 	{0x1201, 0, &mqc_states[58], &mqc_states[52]},
160 	{0x1201, 1, &mqc_states[59], &mqc_states[53]},
161 	{0x1101, 0, &mqc_states[60], &mqc_states[54]},
162 	{0x1101, 1, &mqc_states[61], &mqc_states[55]},
163 	{0x0ac1, 0, &mqc_states[62], &mqc_states[56]},
164 	{0x0ac1, 1, &mqc_states[63], &mqc_states[57]},
165 	{0x09c1, 0, &mqc_states[64], &mqc_states[58]},
166 	{0x09c1, 1, &mqc_states[65], &mqc_states[59]},
167 	{0x08a1, 0, &mqc_states[66], &mqc_states[60]},
168 	{0x08a1, 1, &mqc_states[67], &mqc_states[61]},
169 	{0x0521, 0, &mqc_states[68], &mqc_states[62]},
170 	{0x0521, 1, &mqc_states[69], &mqc_states[63]},
171 	{0x0441, 0, &mqc_states[70], &mqc_states[64]},
172 	{0x0441, 1, &mqc_states[71], &mqc_states[65]},
173 	{0x02a1, 0, &mqc_states[72], &mqc_states[66]},
174 	{0x02a1, 1, &mqc_states[73], &mqc_states[67]},
175 	{0x0221, 0, &mqc_states[74], &mqc_states[68]},
176 	{0x0221, 1, &mqc_states[75], &mqc_states[69]},
177 	{0x0141, 0, &mqc_states[76], &mqc_states[70]},
178 	{0x0141, 1, &mqc_states[77], &mqc_states[71]},
179 	{0x0111, 0, &mqc_states[78], &mqc_states[72]},
180 	{0x0111, 1, &mqc_states[79], &mqc_states[73]},
181 	{0x0085, 0, &mqc_states[80], &mqc_states[74]},
182 	{0x0085, 1, &mqc_states[81], &mqc_states[75]},
183 	{0x0049, 0, &mqc_states[82], &mqc_states[76]},
184 	{0x0049, 1, &mqc_states[83], &mqc_states[77]},
185 	{0x0025, 0, &mqc_states[84], &mqc_states[78]},
186 	{0x0025, 1, &mqc_states[85], &mqc_states[79]},
187 	{0x0015, 0, &mqc_states[86], &mqc_states[80]},
188 	{0x0015, 1, &mqc_states[87], &mqc_states[81]},
189 	{0x0009, 0, &mqc_states[88], &mqc_states[82]},
190 	{0x0009, 1, &mqc_states[89], &mqc_states[83]},
191 	{0x0005, 0, &mqc_states[90], &mqc_states[84]},
192 	{0x0005, 1, &mqc_states[91], &mqc_states[85]},
193 	{0x0001, 0, &mqc_states[90], &mqc_states[86]},
194 	{0x0001, 1, &mqc_states[91], &mqc_states[87]},
195 	{0x5601, 0, &mqc_states[92], &mqc_states[92]},
196 	{0x5601, 1, &mqc_states[93], &mqc_states[93]},
197 };
198 
199 /*
200 ==========================================================
201    local functions
202 ==========================================================
203 */
204 
opj_mqc_byteout(opj_mqc_t * mqc)205 static void opj_mqc_byteout(opj_mqc_t *mqc) {
206 	if (*mqc->bp == 0xff) {
207 		mqc->bp++;
208 		*mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
209 		mqc->c &= 0xfffff;
210 		mqc->ct = 7;
211 	} else {
212 		if ((mqc->c & 0x8000000) == 0) {	/* ((mqc->c&0x8000000)==0) CHANGE */
213 			mqc->bp++;
214 			*mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
215 			mqc->c &= 0x7ffff;
216 			mqc->ct = 8;
217 		} else {
218 			(*mqc->bp)++;
219 			if (*mqc->bp == 0xff) {
220 				mqc->c &= 0x7ffffff;
221 				mqc->bp++;
222 				*mqc->bp = (OPJ_BYTE)(mqc->c >> 20);
223 				mqc->c &= 0xfffff;
224 				mqc->ct = 7;
225 			} else {
226 				mqc->bp++;
227 				*mqc->bp = (OPJ_BYTE)(mqc->c >> 19);
228 				mqc->c &= 0x7ffff;
229 				mqc->ct = 8;
230 			}
231 		}
232 	}
233 }
234 
opj_mqc_renorme(opj_mqc_t * mqc)235 static void opj_mqc_renorme(opj_mqc_t *mqc) {
236 	do {
237 		mqc->a <<= 1;
238 		mqc->c <<= 1;
239 		mqc->ct--;
240 		if (mqc->ct == 0) {
241 			opj_mqc_byteout(mqc);
242 		}
243 	} while ((mqc->a & 0x8000) == 0);
244 }
245 
opj_mqc_codemps(opj_mqc_t * mqc)246 static void opj_mqc_codemps(opj_mqc_t *mqc) {
247 	mqc->a -= (*mqc->curctx)->qeval;
248 	if ((mqc->a & 0x8000) == 0) {
249 		if (mqc->a < (*mqc->curctx)->qeval) {
250 			mqc->a = (*mqc->curctx)->qeval;
251 		} else {
252 			mqc->c += (*mqc->curctx)->qeval;
253 		}
254 		*mqc->curctx = (*mqc->curctx)->nmps;
255 		opj_mqc_renorme(mqc);
256 	} else {
257 		mqc->c += (*mqc->curctx)->qeval;
258 	}
259 }
260 
opj_mqc_codelps(opj_mqc_t * mqc)261 static void opj_mqc_codelps(opj_mqc_t *mqc) {
262 	mqc->a -= (*mqc->curctx)->qeval;
263 	if (mqc->a < (*mqc->curctx)->qeval) {
264 		mqc->c += (*mqc->curctx)->qeval;
265 	} else {
266 		mqc->a = (*mqc->curctx)->qeval;
267 	}
268 	*mqc->curctx = (*mqc->curctx)->nlps;
269 	opj_mqc_renorme(mqc);
270 }
271 
opj_mqc_setbits(opj_mqc_t * mqc)272 static void opj_mqc_setbits(opj_mqc_t *mqc) {
273 	OPJ_UINT32 tempc = mqc->c + mqc->a;
274 	mqc->c |= 0xffff;
275 	if (mqc->c >= tempc) {
276 		mqc->c -= 0x8000;
277 	}
278 }
279 
opj_mqc_mpsexchange(opj_mqc_t * const mqc)280 static INLINE OPJ_INT32 opj_mqc_mpsexchange(opj_mqc_t *const mqc) {
281 	OPJ_INT32 d;
282 	if (mqc->a < (*mqc->curctx)->qeval) {
283 		d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
284 		*mqc->curctx = (*mqc->curctx)->nlps;
285 	} else {
286 		d = (OPJ_INT32)(*mqc->curctx)->mps;
287 		*mqc->curctx = (*mqc->curctx)->nmps;
288 	}
289 
290 	return d;
291 }
292 
opj_mqc_lpsexchange(opj_mqc_t * const mqc)293 static INLINE OPJ_INT32 opj_mqc_lpsexchange(opj_mqc_t *const mqc) {
294 	OPJ_INT32 d;
295 	if (mqc->a < (*mqc->curctx)->qeval) {
296 		mqc->a = (*mqc->curctx)->qeval;
297 		d = (OPJ_INT32)(*mqc->curctx)->mps;
298 		*mqc->curctx = (*mqc->curctx)->nmps;
299 	} else {
300 		mqc->a = (*mqc->curctx)->qeval;
301 		d = (OPJ_INT32)(1 - (*mqc->curctx)->mps);
302 		*mqc->curctx = (*mqc->curctx)->nlps;
303 	}
304 
305 	return d;
306 }
307 
308 #ifdef MQC_PERF_OPT
opj_mqc_bytein(opj_mqc_t * const mqc)309 static INLINE void opj_mqc_bytein(opj_mqc_t *const mqc) {
310 	unsigned int i = *((unsigned int *) mqc->bp);
311 	mqc->c += i & 0xffff00;
312 	mqc->ct = i & 0x0f;
313 	mqc->bp += (i >> 2) & 0x04;
314 }
315 #else
opj_mqc_bytein(opj_mqc_t * const mqc)316 static void opj_mqc_bytein(opj_mqc_t *const mqc) {
317 	if (mqc->bp != mqc->end) {
318 		OPJ_UINT32 c;
319 		if (mqc->bp + 1 != mqc->end) {
320 			c = *(mqc->bp + 1);
321 		} else {
322 			c = 0xff;
323 		}
324 		if (*mqc->bp == 0xff) {
325 			if (c > 0x8f) {
326 				mqc->c += 0xff00;
327 				mqc->ct = 8;
328 			} else {
329 				mqc->bp++;
330 				mqc->c += c << 9;
331 				mqc->ct = 7;
332 			}
333 		} else {
334 			mqc->bp++;
335 			mqc->c += c << 8;
336 			mqc->ct = 8;
337 		}
338 	} else {
339 		mqc->c += 0xff00;
340 		mqc->ct = 8;
341 	}
342 }
343 #endif
344 
opj_mqc_renormd(opj_mqc_t * const mqc)345 static INLINE void opj_mqc_renormd(opj_mqc_t *const mqc) {
346 	do {
347 		if (mqc->ct == 0) {
348 			opj_mqc_bytein(mqc);
349 		}
350 		mqc->a <<= 1;
351 		mqc->c <<= 1;
352 		mqc->ct--;
353 	} while (mqc->a < 0x8000);
354 }
355 
356 /*
357 ==========================================================
358    MQ-Coder interface
359 ==========================================================
360 */
361 
opj_mqc_create(void)362 opj_mqc_t* opj_mqc_create(void) {
363 	opj_mqc_t *mqc = (opj_mqc_t*)opj_malloc(sizeof(opj_mqc_t));
364 #ifdef MQC_PERF_OPT
365 	if (mqc) {
366 		mqc->buffer = NULL;
367 	}
368 #endif
369 	return mqc;
370 }
371 
opj_mqc_destroy(opj_mqc_t * mqc)372 void opj_mqc_destroy(opj_mqc_t *mqc) {
373 	if(mqc) {
374 #ifdef MQC_PERF_OPT
375 		if (mqc->buffer) {
376 			opj_free(mqc->buffer);
377 		}
378 #endif
379 		opj_free(mqc);
380 	}
381 }
382 
opj_mqc_numbytes(opj_mqc_t * mqc)383 OPJ_UINT32 opj_mqc_numbytes(opj_mqc_t *mqc) {
384 	const ptrdiff_t diff = mqc->bp - mqc->start;
385 #if 0
386   assert( diff <= 0xffffffff && diff >= 0 ); /* UINT32_MAX */
387 #endif
388 	return (OPJ_UINT32)diff;
389 }
390 
opj_mqc_init_enc(opj_mqc_t * mqc,OPJ_BYTE * bp)391 void opj_mqc_init_enc(opj_mqc_t *mqc, OPJ_BYTE *bp) {
392     /* TODO MSD: need to take a look to the v2 version */
393 	opj_mqc_setcurctx(mqc, 0);
394 	mqc->a = 0x8000;
395 	mqc->c = 0;
396 	mqc->bp = bp - 1;
397 	mqc->ct = 12;
398 	if (*mqc->bp == 0xff) {
399 		mqc->ct = 13;
400 	}
401 	mqc->start = bp;
402 }
403 
opj_mqc_encode(opj_mqc_t * mqc,OPJ_UINT32 d)404 void opj_mqc_encode(opj_mqc_t *mqc, OPJ_UINT32 d) {
405 	if ((*mqc->curctx)->mps == d) {
406 		opj_mqc_codemps(mqc);
407 	} else {
408 		opj_mqc_codelps(mqc);
409 	}
410 }
411 
opj_mqc_flush(opj_mqc_t * mqc)412 void opj_mqc_flush(opj_mqc_t *mqc) {
413 	opj_mqc_setbits(mqc);
414 	mqc->c <<= mqc->ct;
415 	opj_mqc_byteout(mqc);
416 	mqc->c <<= mqc->ct;
417 	opj_mqc_byteout(mqc);
418 
419 	if (*mqc->bp != 0xff) {
420 		mqc->bp++;
421 	}
422 }
423 
opj_mqc_bypass_init_enc(opj_mqc_t * mqc)424 void opj_mqc_bypass_init_enc(opj_mqc_t *mqc) {
425 	mqc->c = 0;
426 	mqc->ct = 8;
427 	/*if (*mqc->bp == 0xff) {
428 	mqc->ct = 7;
429      } */
430 }
431 
opj_mqc_bypass_enc(opj_mqc_t * mqc,OPJ_UINT32 d)432 void opj_mqc_bypass_enc(opj_mqc_t *mqc, OPJ_UINT32 d) {
433 	mqc->ct--;
434 	mqc->c = mqc->c + (d << mqc->ct);
435 	if (mqc->ct == 0) {
436 		mqc->bp++;
437 		*mqc->bp = (OPJ_BYTE)mqc->c;
438 		mqc->ct = 8;
439 		if (*mqc->bp == 0xff) {
440 			mqc->ct = 7;
441 		}
442 		mqc->c = 0;
443 	}
444 }
445 
opj_mqc_bypass_flush_enc(opj_mqc_t * mqc)446 OPJ_UINT32 opj_mqc_bypass_flush_enc(opj_mqc_t *mqc) {
447 	OPJ_BYTE bit_padding;
448 
449 	bit_padding = 0;
450 
451 	if (mqc->ct != 0) {
452 		while (mqc->ct > 0) {
453 			mqc->ct--;
454 			mqc->c += (OPJ_UINT32)(bit_padding << mqc->ct);
455 			bit_padding = (bit_padding + 1) & 0x01;
456 		}
457 		mqc->bp++;
458 		*mqc->bp = (OPJ_BYTE)mqc->c;
459 		mqc->ct = 8;
460 		mqc->c = 0;
461 	}
462 
463 	return 1;
464 }
465 
opj_mqc_reset_enc(opj_mqc_t * mqc)466 void opj_mqc_reset_enc(opj_mqc_t *mqc) {
467 	opj_mqc_resetstates(mqc);
468 	opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
469 	opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
470 	opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
471 }
472 
opj_mqc_restart_enc(opj_mqc_t * mqc)473 OPJ_UINT32 opj_mqc_restart_enc(opj_mqc_t *mqc) {
474 	OPJ_UINT32 correction = 1;
475 
476 	/* <flush part> */
477 	OPJ_INT32 n = (OPJ_INT32)(27 - 15 - mqc->ct);
478 	mqc->c <<= mqc->ct;
479 	while (n > 0) {
480 		opj_mqc_byteout(mqc);
481 		n -= (OPJ_INT32)mqc->ct;
482 		mqc->c <<= mqc->ct;
483 	}
484 	opj_mqc_byteout(mqc);
485 
486 	return correction;
487 }
488 
opj_mqc_restart_init_enc(opj_mqc_t * mqc)489 void opj_mqc_restart_init_enc(opj_mqc_t *mqc) {
490 	/* <Re-init part> */
491 	opj_mqc_setcurctx(mqc, 0);
492 	mqc->a = 0x8000;
493 	mqc->c = 0;
494 	mqc->ct = 12;
495 	mqc->bp--;
496 	if (*mqc->bp == 0xff) {
497 		mqc->ct = 13;
498 	}
499 }
500 
opj_mqc_erterm_enc(opj_mqc_t * mqc)501 void opj_mqc_erterm_enc(opj_mqc_t *mqc) {
502 	OPJ_INT32 k = (OPJ_INT32)(11 - mqc->ct + 1);
503 
504 	while (k > 0) {
505 		mqc->c <<= mqc->ct;
506 		mqc->ct = 0;
507 		opj_mqc_byteout(mqc);
508 		k -= (OPJ_INT32)mqc->ct;
509 	}
510 
511 	if (*mqc->bp != 0xff) {
512 		opj_mqc_byteout(mqc);
513 	}
514 }
515 
opj_mqc_segmark_enc(opj_mqc_t * mqc)516 void opj_mqc_segmark_enc(opj_mqc_t *mqc) {
517 	OPJ_UINT32 i;
518 	opj_mqc_setcurctx(mqc, 18);
519 
520 	for (i = 1; i < 5; i++) {
521 		opj_mqc_encode(mqc, i % 2);
522 	}
523 }
524 
opj_mqc_init_dec(opj_mqc_t * mqc,OPJ_BYTE * bp,OPJ_UINT32 len)525 OPJ_BOOL opj_mqc_init_dec(opj_mqc_t *mqc, OPJ_BYTE *bp, OPJ_UINT32 len) {
526 	opj_mqc_setcurctx(mqc, 0);
527 	mqc->start = bp;
528 	mqc->end = bp + len;
529 	mqc->bp = bp;
530 	if (len==0) mqc->c = 0xff << 16;
531 	else mqc->c = (OPJ_UINT32)(*mqc->bp << 16);
532 
533 #ifdef MQC_PERF_OPT /* TODO_MSD: check this option and put in experimental */
534 	{
535         OPJ_UINT32 c;
536 		OPJ_UINT32 *ip;
537 		OPJ_BYTE *end = mqc->end - 1;
538         void* new_buffer = opj_realloc(mqc->buffer, (len + 1) * sizeof(OPJ_UINT32));
539         if (! new_buffer) {
540             opj_free(mqc->buffer);
541             mqc->buffer = NULL;
542             return OPJ_FALSE;
543         }
544         mqc->buffer = new_buffer;
545 
546         ip = (OPJ_UINT32 *) mqc->buffer;
547 
548 		while (bp < end) {
549 			c = *(bp + 1);
550 			if (*bp == 0xff) {
551 				if (c > 0x8f) {
552 					break;
553 				} else {
554 					*ip = 0x00000017 | (c << 9);
555 				}
556 			} else {
557 				*ip = 0x00000018 | (c << 8);
558 			}
559 			bp++;
560 			ip++;
561 		}
562 
563 		/* Handle last byte of data */
564 		c = 0xff;
565 		if (*bp == 0xff) {
566 			*ip = 0x0000ff18;
567 		} else {
568 			bp++;
569 			*ip = 0x00000018 | (c << 8);
570 		}
571 		ip++;
572 
573 		*ip = 0x0000ff08;
574 		mqc->bp = mqc->buffer;
575 	}
576 #endif
577 	opj_mqc_bytein(mqc);
578 	mqc->c <<= 7;
579 	mqc->ct -= 7;
580 	mqc->a = 0x8000;
581         return OPJ_TRUE;
582 }
583 
opj_mqc_decode(opj_mqc_t * const mqc)584 OPJ_INT32 opj_mqc_decode(opj_mqc_t *const mqc) {
585 	OPJ_INT32 d;
586 	mqc->a -= (*mqc->curctx)->qeval;
587 	if ((mqc->c >> 16) < (*mqc->curctx)->qeval) {
588 		d = opj_mqc_lpsexchange(mqc);
589 		opj_mqc_renormd(mqc);
590 	} else {
591 		mqc->c -= (*mqc->curctx)->qeval << 16;
592 		if ((mqc->a & 0x8000) == 0) {
593 			d = opj_mqc_mpsexchange(mqc);
594 			opj_mqc_renormd(mqc);
595 		} else {
596 			d = (OPJ_INT32)(*mqc->curctx)->mps;
597 		}
598 	}
599 
600 	return d;
601 }
602 
opj_mqc_resetstates(opj_mqc_t * mqc)603 void opj_mqc_resetstates(opj_mqc_t *mqc) {
604 	OPJ_UINT32 i;
605 	for (i = 0; i < MQC_NUMCTXS; i++) {
606 		mqc->ctxs[i] = mqc_states;
607 	}
608 }
609 
opj_mqc_setstate(opj_mqc_t * mqc,OPJ_UINT32 ctxno,OPJ_UINT32 msb,OPJ_INT32 prob)610 void opj_mqc_setstate(opj_mqc_t *mqc, OPJ_UINT32 ctxno, OPJ_UINT32 msb, OPJ_INT32 prob) {
611 	mqc->ctxs[ctxno] = &mqc_states[msb + (OPJ_UINT32)(prob << 1)];
612 }
613 
614 
615