• 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) 2007, Callum Lerwick <seg@haxxed.com>
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 #include "t1_luts.h"
41 
42 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
43 /*@{*/
44 
45 /** @name Local static functions */
46 /*@{*/
47 
48 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
49 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
50 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
52 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
53 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
54 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
55 /**
56 Encode significant pass
57 */
58 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
59                                     opj_flag_t *flagsp,
60                                     OPJ_INT32 *datap,
61                                     OPJ_UINT32 orient,
62                                     OPJ_INT32 bpno,
63                                     OPJ_INT32 one,
64                                     OPJ_INT32 *nmsedec,
65                                     OPJ_BYTE type,
66                                     OPJ_UINT32 vsc);
67 
68 /**
69 Decode significant pass
70 */
71 #if 0
72 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
73                                     opj_flag_t *flagsp,
74                                     OPJ_INT32 *datap,
75                                     OPJ_UINT32 orient,
76                                     OPJ_INT32 oneplushalf,
77                                     OPJ_BYTE type,
78                                     OPJ_UINT32 vsc);
79 #endif
80 
81 static INLINE void opj_t1_dec_sigpass_step_raw(
82                 opj_t1_t *t1,
83                 opj_flag_t *flagsp,
84                 OPJ_INT32 *datap,
85                 OPJ_INT32 orient,
86                 OPJ_INT32 oneplushalf,
87                 OPJ_INT32 vsc);
88 static INLINE void opj_t1_dec_sigpass_step_mqc(
89                 opj_t1_t *t1,
90                 opj_flag_t *flagsp,
91                 OPJ_INT32 *datap,
92                 OPJ_INT32 orient,
93                 OPJ_INT32 oneplushalf);
94 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
95                 opj_t1_t *t1,
96                 opj_flag_t *flagsp,
97                 OPJ_INT32 *datap,
98                 OPJ_INT32 orient,
99                 OPJ_INT32 oneplushalf,
100                 OPJ_INT32 vsc);
101 
102 
103 /**
104 Encode significant pass
105 */
106 static void opj_t1_enc_sigpass( opj_t1_t *t1,
107                                 OPJ_INT32 bpno,
108                                 OPJ_UINT32 orient,
109                                 OPJ_INT32 *nmsedec,
110                                 OPJ_BYTE type,
111                                 OPJ_UINT32 cblksty);
112 
113 /**
114 Decode significant pass
115 */
116 static void opj_t1_dec_sigpass_raw(
117                 opj_t1_t *t1,
118                 OPJ_INT32 bpno,
119                 OPJ_INT32 orient,
120                 OPJ_INT32 cblksty);
121 static void opj_t1_dec_sigpass_mqc(
122                 opj_t1_t *t1,
123                 OPJ_INT32 bpno,
124                 OPJ_INT32 orient);
125 static void opj_t1_dec_sigpass_mqc_vsc(
126                 opj_t1_t *t1,
127                 OPJ_INT32 bpno,
128                 OPJ_INT32 orient);
129 
130 
131 
132 /**
133 Encode refinement pass
134 */
135 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
136                                     opj_flag_t *flagsp,
137                                     OPJ_INT32 *datap,
138                                     OPJ_INT32 bpno,
139                                     OPJ_INT32 one,
140                                     OPJ_INT32 *nmsedec,
141                                     OPJ_BYTE type,
142                                     OPJ_UINT32 vsc);
143 
144 
145 /**
146 Encode refinement pass
147 */
148 static void opj_t1_enc_refpass( opj_t1_t *t1,
149                                 OPJ_INT32 bpno,
150                                 OPJ_INT32 *nmsedec,
151                                 OPJ_BYTE type,
152                                 OPJ_UINT32 cblksty);
153 
154 /**
155 Decode refinement pass
156 */
157 static void opj_t1_dec_refpass_raw(
158                 opj_t1_t *t1,
159                 OPJ_INT32 bpno,
160                 OPJ_INT32 cblksty);
161 static void opj_t1_dec_refpass_mqc(
162                 opj_t1_t *t1,
163                 OPJ_INT32 bpno);
164 static void opj_t1_dec_refpass_mqc_vsc(
165                 opj_t1_t *t1,
166                 OPJ_INT32 bpno);
167 
168 
169 /**
170 Decode refinement pass
171 */
172 #if 0
173 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
174                                     opj_flag_t *flagsp,
175                                     OPJ_INT32 *datap,
176                                     OPJ_INT32 poshalf,
177                                     OPJ_INT32 neghalf,
178                                     OPJ_BYTE type,
179                                     OPJ_UINT32 vsc);
180 #endif
181 
182 static INLINE void  opj_t1_dec_refpass_step_raw(
183                 opj_t1_t *t1,
184                 opj_flag_t *flagsp,
185                 OPJ_INT32 *datap,
186                 OPJ_INT32 poshalf,
187                 OPJ_INT32 neghalf,
188                 OPJ_INT32 vsc);
189 static INLINE void opj_t1_dec_refpass_step_mqc(
190                 opj_t1_t *t1,
191                 opj_flag_t *flagsp,
192                 OPJ_INT32 *datap,
193                 OPJ_INT32 poshalf,
194                 OPJ_INT32 neghalf);
195 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
196                 opj_t1_t *t1,
197                 opj_flag_t *flagsp,
198                 OPJ_INT32 *datap,
199                 OPJ_INT32 poshalf,
200                 OPJ_INT32 neghalf,
201                 OPJ_INT32 vsc);
202 
203 
204 
205 /**
206 Encode clean-up pass
207 */
208 static void opj_t1_enc_clnpass_step(
209 		opj_t1_t *t1,
210 		opj_flag_t *flagsp,
211 		OPJ_INT32 *datap,
212 		OPJ_UINT32 orient,
213 		OPJ_INT32 bpno,
214 		OPJ_INT32 one,
215 		OPJ_INT32 *nmsedec,
216 		OPJ_UINT32 partial,
217 		OPJ_UINT32 vsc);
218 /**
219 Decode clean-up pass
220 */
221 static void opj_t1_dec_clnpass_step_partial(
222 		opj_t1_t *t1,
223 		opj_flag_t *flagsp,
224 		OPJ_INT32 *datap,
225 		OPJ_INT32 orient,
226 		OPJ_INT32 oneplushalf);
227 static void opj_t1_dec_clnpass_step(
228 		opj_t1_t *t1,
229 		opj_flag_t *flagsp,
230 		OPJ_INT32 *datap,
231 		OPJ_INT32 orient,
232 		OPJ_INT32 oneplushalf);
233 static void opj_t1_dec_clnpass_step_vsc(
234 		opj_t1_t *t1,
235 		opj_flag_t *flagsp,
236 		OPJ_INT32 *datap,
237 		OPJ_INT32 orient,
238 		OPJ_INT32 oneplushalf,
239 		OPJ_INT32 partial,
240 		OPJ_INT32 vsc);
241 /**
242 Encode clean-up pass
243 */
244 static void opj_t1_enc_clnpass(
245 		opj_t1_t *t1,
246 		OPJ_INT32 bpno,
247 		OPJ_UINT32 orient,
248 		OPJ_INT32 *nmsedec,
249 		OPJ_UINT32 cblksty);
250 /**
251 Decode clean-up pass
252 */
253 static void opj_t1_dec_clnpass(
254 		opj_t1_t *t1,
255 		OPJ_INT32 bpno,
256 		OPJ_INT32 orient,
257 		OPJ_INT32 cblksty);
258 
259 static OPJ_FLOAT64 opj_t1_getwmsedec(
260 		OPJ_INT32 nmsedec,
261 		OPJ_UINT32 compno,
262 		OPJ_UINT32 level,
263 		OPJ_UINT32 orient,
264 		OPJ_INT32 bpno,
265 		OPJ_UINT32 qmfbid,
266 		OPJ_FLOAT64 stepsize,
267 		OPJ_UINT32 numcomps,
268 		const OPJ_FLOAT64 * mct_norms,
269 		OPJ_UINT32 mct_numcomps);
270 
271 static void opj_t1_encode_cblk( opj_t1_t *t1,
272                                 opj_tcd_cblk_enc_t* cblk,
273                                 OPJ_UINT32 orient,
274                                 OPJ_UINT32 compno,
275                                 OPJ_UINT32 level,
276                                 OPJ_UINT32 qmfbid,
277                                 OPJ_FLOAT64 stepsize,
278                                 OPJ_UINT32 cblksty,
279                                 OPJ_UINT32 numcomps,
280                                 opj_tcd_tile_t * tile,
281                                 const OPJ_FLOAT64 * mct_norms,
282                                 OPJ_UINT32 mct_numcomps);
283 
284 /**
285 Decode 1 code-block
286 @param t1 T1 handle
287 @param cblk Code-block coding parameters
288 @param orient
289 @param roishift Region of interest shifting value
290 @param cblksty Code-block style
291 */
292 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
293                                     opj_tcd_cblk_dec_t* cblk,
294                                     OPJ_UINT32 orient,
295                                     OPJ_UINT32 roishift,
296                                     OPJ_UINT32 cblksty);
297 
298 OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
299                                     OPJ_UINT32 w,
300                                     OPJ_UINT32 h);
301 
302 /*@}*/
303 
304 /*@}*/
305 
306 /* ----------------------------------------------------------------------- */
307 
opj_t1_getctxno_zc(OPJ_UINT32 f,OPJ_UINT32 orient)308 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
309 	return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
310 }
311 
opj_t1_getctxno_sc(OPJ_UINT32 f)312 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
313 	return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
314 }
315 
opj_t1_getctxno_mag(OPJ_UINT32 f)316 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
317 	OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
318 	OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
319 	return (tmp2);
320 }
321 
opj_t1_getspb(OPJ_UINT32 f)322 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
323 	return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
324 }
325 
opj_t1_getnmsedec_sig(OPJ_UINT32 x,OPJ_UINT32 bitpos)326 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
327 	if (bitpos > T1_NMSEDEC_FRACBITS) {
328 		return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
329 	}
330 
331 	return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
332 }
333 
opj_t1_getnmsedec_ref(OPJ_UINT32 x,OPJ_UINT32 bitpos)334 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
335 	if (bitpos > T1_NMSEDEC_FRACBITS) {
336 		return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
337 	}
338 
339     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
340 }
341 
opj_t1_updateflags(opj_flag_t * flagsp,OPJ_UINT32 s,OPJ_UINT32 stride)342 void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
343 	opj_flag_t *np = flagsp - stride;
344 	opj_flag_t *sp = flagsp + stride;
345 
346 	static const opj_flag_t mod[] = {
347 		T1_SIG_S, T1_SIG_S|T1_SGN_S,
348 		T1_SIG_E, T1_SIG_E|T1_SGN_E,
349 		T1_SIG_W, T1_SIG_W|T1_SGN_W,
350 		T1_SIG_N, T1_SIG_N|T1_SGN_N
351 	};
352 
353 	np[-1] |= T1_SIG_SE;
354 	np[0]  |= mod[s];
355 	np[1]  |= T1_SIG_SW;
356 
357 	flagsp[-1] |= mod[s+2];
358 	flagsp[0]  |= T1_SIG;
359 	flagsp[1]  |= mod[s+4];
360 
361 	sp[-1] |= T1_SIG_NE;
362 	sp[0]  |= mod[s+6];
363 	sp[1]  |= T1_SIG_NW;
364 }
365 
opj_t1_enc_sigpass_step(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_UINT32 orient,OPJ_INT32 bpno,OPJ_INT32 one,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 vsc)366 void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
367                                 opj_flag_t *flagsp,
368                                 OPJ_INT32 *datap,
369                                 OPJ_UINT32 orient,
370                                 OPJ_INT32 bpno,
371                                 OPJ_INT32 one,
372                                 OPJ_INT32 *nmsedec,
373                                 OPJ_BYTE type,
374                                 OPJ_UINT32 vsc
375                                 )
376 {
377 	OPJ_INT32 v;
378     OPJ_UINT32 flag;
379 
380 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
381 
382 	flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
383 	if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
384 		v = opj_int_abs(*datap) & one ? 1 : 0;
385 		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));	/* ESSAI */
386 		if (type == T1_TYPE_RAW) {	/* BYPASS/LAZY MODE */
387 			opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
388 		} else {
389 			opj_mqc_encode(mqc, (OPJ_UINT32)v);
390 		}
391 		if (v) {
392 			v = *datap < 0 ? 1 : 0;
393 			*nmsedec +=	opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
394 			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));	/* ESSAI */
395 			if (type == T1_TYPE_RAW) {	/* BYPASS/LAZY MODE */
396 				opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
397 			} else {
398 				opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
399 			}
400 			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
401 		}
402 		*flagsp |= T1_VISIT;
403 	}
404 }
405 
406 
opj_t1_dec_sigpass_step_raw(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 orient,OPJ_INT32 oneplushalf,OPJ_INT32 vsc)407 static INLINE void opj_t1_dec_sigpass_step_raw(
408                 opj_t1_t *t1,
409                 opj_flag_t *flagsp,
410                 OPJ_INT32 *datap,
411                 OPJ_INT32 orient,
412                 OPJ_INT32 oneplushalf,
413                 OPJ_INT32 vsc)
414 {
415         OPJ_INT32 v, flag;
416         opj_raw_t *raw = t1->raw;       /* RAW component */
417         OPJ_ARG_NOT_USED(orient);
418 
419         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
420         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
421                         if (opj_raw_decode(raw)) {
422                                 v = (OPJ_INT32)opj_raw_decode(raw);    /* ESSAI */
423                                 *datap = v ? -oneplushalf : oneplushalf;
424                                 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
425                         }
426                 *flagsp |= T1_VISIT;
427         }
428 }
429 
opj_t1_dec_sigpass_step_mqc(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 orient,OPJ_INT32 oneplushalf)430 INLINE void opj_t1_dec_sigpass_step_mqc(
431                 opj_t1_t *t1,
432                 opj_flag_t *flagsp,
433                 OPJ_INT32 *datap,
434                 OPJ_INT32 orient,
435                 OPJ_INT32 oneplushalf)
436 {
437         OPJ_INT32 v, flag;
438 
439         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
440 
441         flag = *flagsp;
442         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
443                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
444                         if (opj_mqc_decode(mqc)) {
445                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
446                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
447                                 *datap = v ? -oneplushalf : oneplushalf;
448                                 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
449                         }
450                 *flagsp |= T1_VISIT;
451         }
452 }                               /* VSC and  BYPASS by Antonin */
453 
opj_t1_dec_sigpass_step_mqc_vsc(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 orient,OPJ_INT32 oneplushalf,OPJ_INT32 vsc)454 INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
455                 opj_t1_t *t1,
456                 opj_flag_t *flagsp,
457                 OPJ_INT32 *datap,
458                 OPJ_INT32 orient,
459                 OPJ_INT32 oneplushalf,
460                 OPJ_INT32 vsc)
461 {
462         OPJ_INT32 v, flag;
463 
464         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
465 
466         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
467         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
468                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
469                 if (opj_mqc_decode(mqc)) {
470                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
471                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
472                         *datap = v ? -oneplushalf : oneplushalf;
473                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
474                 }
475                 *flagsp |= T1_VISIT;
476         }
477 }                               /* VSC and  BYPASS by Antonin */
478 
479 
480 
opj_t1_enc_sigpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_UINT32 orient,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 cblksty)481 void opj_t1_enc_sigpass(opj_t1_t *t1,
482                         OPJ_INT32 bpno,
483                         OPJ_UINT32 orient,
484                         OPJ_INT32 *nmsedec,
485                         OPJ_BYTE type,
486                         OPJ_UINT32 cblksty
487                         )
488 {
489 	OPJ_UINT32 i, j, k, vsc;
490     OPJ_INT32 one;
491 
492 	*nmsedec = 0;
493 	one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
494 	for (k = 0; k < t1->h; k += 4) {
495 		for (i = 0; i < t1->w; ++i) {
496 			for (j = k; j < k + 4 && j < t1->h; ++j) {
497 				vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
498 				opj_t1_enc_sigpass_step(
499 						t1,
500 						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
501 						&t1->data[(j * t1->data_stride) + i],
502 						orient,
503 						bpno,
504 						one,
505 						nmsedec,
506 						type,
507 						vsc);
508 			}
509 		}
510 	}
511 }
512 
opj_t1_dec_sigpass_raw(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 orient,OPJ_INT32 cblksty)513 void opj_t1_dec_sigpass_raw(
514                 opj_t1_t *t1,
515                 OPJ_INT32 bpno,
516                 OPJ_INT32 orient,
517                 OPJ_INT32 cblksty)
518 {
519         OPJ_INT32 one, half, oneplushalf, vsc;
520         OPJ_UINT32 i, j, k;
521         one = 1 << bpno;
522         half = one >> 1;
523         oneplushalf = one | half;
524         for (k = 0; k < t1->h; k += 4) {
525                 for (i = 0; i < t1->w; ++i) {
526                         for (j = k; j < k + 4 && j < t1->h; ++j) {
527                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
528                                 opj_t1_dec_sigpass_step_raw(
529                                                 t1,
530                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
531                                                 &t1->data[(j * t1->w) + i],
532                                                 orient,
533                                                 oneplushalf,
534                                                 vsc);
535                         }
536                 }
537         }
538 }                               /* VSC and  BYPASS by Antonin */
539 
opj_t1_dec_sigpass_mqc(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 orient)540 void opj_t1_dec_sigpass_mqc(
541                 opj_t1_t *t1,
542                 OPJ_INT32 bpno,
543                 OPJ_INT32 orient)
544 {
545         OPJ_INT32 one, half, oneplushalf;
546         OPJ_UINT32 i, j, k;
547         OPJ_INT32 *data1 = t1->data;
548         opj_flag_t *flags1 = &t1->flags[1];
549         one = 1 << bpno;
550         half = one >> 1;
551         oneplushalf = one | half;
552         for (k = 0; k < (t1->h & ~3u); k += 4) {
553                 for (i = 0; i < t1->w; ++i) {
554                         OPJ_INT32 *data2 = data1 + i;
555                         opj_flag_t *flags2 = flags1 + i;
556                         flags2 += t1->flags_stride;
557                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
558                         data2 += t1->w;
559                         flags2 += t1->flags_stride;
560                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
561                         data2 += t1->w;
562                         flags2 += t1->flags_stride;
563                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
564                         data2 += t1->w;
565                         flags2 += t1->flags_stride;
566                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
567                         data2 += t1->w;
568                 }
569                 data1 += t1->w << 2;
570                 flags1 += t1->flags_stride << 2;
571         }
572         for (i = 0; i < t1->w; ++i) {
573                 OPJ_INT32 *data2 = data1 + i;
574                 opj_flag_t *flags2 = flags1 + i;
575                 for (j = k; j < t1->h; ++j) {
576                         flags2 += t1->flags_stride;
577                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
578                         data2 += t1->w;
579                 }
580         }
581 }                               /* VSC and  BYPASS by Antonin */
582 
opj_t1_dec_sigpass_mqc_vsc(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 orient)583 void opj_t1_dec_sigpass_mqc_vsc(
584                 opj_t1_t *t1,
585                 OPJ_INT32 bpno,
586                 OPJ_INT32 orient)
587 {
588         OPJ_INT32 one, half, oneplushalf, vsc;
589         OPJ_UINT32 i, j, k;
590         one = 1 << bpno;
591         half = one >> 1;
592         oneplushalf = one | half;
593         for (k = 0; k < t1->h; k += 4) {
594                 for (i = 0; i < t1->w; ++i) {
595                         for (j = k; j < k + 4 && j < t1->h; ++j) {
596                                 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
597                                 opj_t1_dec_sigpass_step_mqc_vsc(
598                                                 t1,
599                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
600                                                 &t1->data[(j * t1->w) + i],
601                                                 orient,
602                                                 oneplushalf,
603                                                 vsc);
604                         }
605                 }
606         }
607 }                               /* VSC and  BYPASS by Antonin */
608 
609 
610 
opj_t1_enc_refpass_step(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 bpno,OPJ_INT32 one,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 vsc)611 void opj_t1_enc_refpass_step(   opj_t1_t *t1,
612                                 opj_flag_t *flagsp,
613                                 OPJ_INT32 *datap,
614                                 OPJ_INT32 bpno,
615                                 OPJ_INT32 one,
616                                 OPJ_INT32 *nmsedec,
617                                 OPJ_BYTE type,
618                                 OPJ_UINT32 vsc)
619 {
620 	OPJ_INT32 v;
621 	OPJ_UINT32 flag;
622 
623 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
624 
625 	flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
626 	if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
627 		*nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
628 		v = opj_int_abs(*datap) & one ? 1 : 0;
629 		opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));	/* ESSAI */
630 		if (type == T1_TYPE_RAW) {	/* BYPASS/LAZY MODE */
631 			opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
632 		} else {
633 			opj_mqc_encode(mqc, (OPJ_UINT32)v);
634 		}
635 		*flagsp |= T1_REFINE;
636 	}
637 }
638 
opj_t1_dec_refpass_step_raw(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 poshalf,OPJ_INT32 neghalf,OPJ_INT32 vsc)639 INLINE void opj_t1_dec_refpass_step_raw(
640                 opj_t1_t *t1,
641                 opj_flag_t *flagsp,
642                 OPJ_INT32 *datap,
643                 OPJ_INT32 poshalf,
644                 OPJ_INT32 neghalf,
645                 OPJ_INT32 vsc)
646 {
647         OPJ_INT32 v, t, flag;
648 
649         opj_raw_t *raw = t1->raw;       /* RAW component */
650 
651         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
652         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
653                         v = (OPJ_INT32)opj_raw_decode(raw);
654                 t = v ? poshalf : neghalf;
655                 *datap += *datap < 0 ? -t : t;
656                 *flagsp |= T1_REFINE;
657         }
658 }                               /* VSC and  BYPASS by Antonin  */
659 
opj_t1_dec_refpass_step_mqc(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 poshalf,OPJ_INT32 neghalf)660 INLINE void opj_t1_dec_refpass_step_mqc(
661                 opj_t1_t *t1,
662                 opj_flag_t *flagsp,
663                 OPJ_INT32 *datap,
664                 OPJ_INT32 poshalf,
665                 OPJ_INT32 neghalf)
666 {
667         OPJ_INT32 v, t, flag;
668 
669         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
670 
671         flag = *flagsp;
672         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
673                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag));      /* ESSAI */
674                         v = opj_mqc_decode(mqc);
675                 t = v ? poshalf : neghalf;
676                 *datap += *datap < 0 ? -t : t;
677                 *flagsp |= T1_REFINE;
678                 }
679 }                               /* VSC and  BYPASS by Antonin  */
680 
opj_t1_dec_refpass_step_mqc_vsc(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 poshalf,OPJ_INT32 neghalf,OPJ_INT32 vsc)681 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
682                 opj_t1_t *t1,
683                 opj_flag_t *flagsp,
684                 OPJ_INT32 *datap,
685                 OPJ_INT32 poshalf,
686                 OPJ_INT32 neghalf,
687                 OPJ_INT32 vsc)
688 {
689         OPJ_INT32 v, t, flag;
690 
691         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
692 
693         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
694         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
695                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag));      /* ESSAI */
696                 v = opj_mqc_decode(mqc);
697                 t = v ? poshalf : neghalf;
698                 *datap += *datap < 0 ? -t : t;
699                 *flagsp |= T1_REFINE;
700         }
701 }                               /* VSC and  BYPASS by Antonin  */
702 
703 
opj_t1_enc_refpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 * nmsedec,OPJ_BYTE type,OPJ_UINT32 cblksty)704 void opj_t1_enc_refpass(
705 		opj_t1_t *t1,
706 		OPJ_INT32 bpno,
707 		OPJ_INT32 *nmsedec,
708 		OPJ_BYTE type,
709 		OPJ_UINT32 cblksty)
710 {
711 	OPJ_UINT32 i, j, k, vsc;
712     OPJ_INT32 one;
713 
714 	*nmsedec = 0;
715 	one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
716 	for (k = 0; k < t1->h; k += 4) {
717 		for (i = 0; i < t1->w; ++i) {
718 			for (j = k; j < k + 4 && j < t1->h; ++j) {
719 				vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
720 				opj_t1_enc_refpass_step(
721 						t1,
722 						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
723 						&t1->data[(j * t1->data_stride) + i],
724 						bpno,
725 						one,
726 						nmsedec,
727 						type,
728 						vsc);
729 			}
730 		}
731 	}
732 }
733 
opj_t1_dec_refpass_raw(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 cblksty)734 void opj_t1_dec_refpass_raw(
735                 opj_t1_t *t1,
736                 OPJ_INT32 bpno,
737                 OPJ_INT32 cblksty)
738 {
739         OPJ_INT32 one, poshalf, neghalf;
740         OPJ_UINT32 i, j, k;
741         OPJ_INT32 vsc;
742         one = 1 << bpno;
743         poshalf = one >> 1;
744         neghalf = bpno > 0 ? -poshalf : -1;
745         for (k = 0; k < t1->h; k += 4) {
746                 for (i = 0; i < t1->w; ++i) {
747                         for (j = k; j < k + 4 && j < t1->h; ++j) {
748                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
749                                 opj_t1_dec_refpass_step_raw(
750                                                 t1,
751                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
752                                                 &t1->data[(j * t1->w) + i],
753                                                 poshalf,
754                                                 neghalf,
755                                                 vsc);
756                         }
757                 }
758         }
759 }                               /* VSC and  BYPASS by Antonin */
760 
opj_t1_dec_refpass_mqc(opj_t1_t * t1,OPJ_INT32 bpno)761 void opj_t1_dec_refpass_mqc(
762                 opj_t1_t *t1,
763                 OPJ_INT32 bpno)
764 {
765         OPJ_INT32 one, poshalf, neghalf;
766         OPJ_UINT32 i, j, k;
767         OPJ_INT32 *data1 = t1->data;
768         opj_flag_t *flags1 = &t1->flags[1];
769         one = 1 << bpno;
770         poshalf = one >> 1;
771         neghalf = bpno > 0 ? -poshalf : -1;
772         for (k = 0; k < (t1->h & ~3u); k += 4) {
773                 for (i = 0; i < t1->w; ++i) {
774                         OPJ_INT32 *data2 = data1 + i;
775                         opj_flag_t *flags2 = flags1 + i;
776                         flags2 += t1->flags_stride;
777                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
778                         data2 += t1->w;
779                         flags2 += t1->flags_stride;
780                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
781                         data2 += t1->w;
782                         flags2 += t1->flags_stride;
783                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
784                         data2 += t1->w;
785                         flags2 += t1->flags_stride;
786                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
787                         data2 += t1->w;
788                 }
789                 data1 += t1->w << 2;
790                 flags1 += t1->flags_stride << 2;
791         }
792         for (i = 0; i < t1->w; ++i) {
793                 OPJ_INT32 *data2 = data1 + i;
794                 opj_flag_t *flags2 = flags1 + i;
795                 for (j = k; j < t1->h; ++j) {
796                         flags2 += t1->flags_stride;
797                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
798                         data2 += t1->w;
799                 }
800         }
801 }                               /* VSC and  BYPASS by Antonin */
802 
opj_t1_dec_refpass_mqc_vsc(opj_t1_t * t1,OPJ_INT32 bpno)803 void opj_t1_dec_refpass_mqc_vsc(
804                 opj_t1_t *t1,
805                 OPJ_INT32 bpno)
806 {
807         OPJ_INT32 one, poshalf, neghalf;
808         OPJ_UINT32 i, j, k;
809         OPJ_INT32 vsc;
810         one = 1 << bpno;
811         poshalf = one >> 1;
812         neghalf = bpno > 0 ? -poshalf : -1;
813         for (k = 0; k < t1->h; k += 4) {
814                 for (i = 0; i < t1->w; ++i) {
815                         for (j = k; j < k + 4 && j < t1->h; ++j) {
816                                 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
817                                 opj_t1_dec_refpass_step_mqc_vsc(
818                                                 t1,
819                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
820                                                 &t1->data[(j * t1->w) + i],
821                                                 poshalf,
822                                                 neghalf,
823                                                 vsc);
824                         }
825                 }
826         }
827 }                               /* VSC and  BYPASS by Antonin */
828 
829 
opj_t1_enc_clnpass_step(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_UINT32 orient,OPJ_INT32 bpno,OPJ_INT32 one,OPJ_INT32 * nmsedec,OPJ_UINT32 partial,OPJ_UINT32 vsc)830 void opj_t1_enc_clnpass_step(
831 		opj_t1_t *t1,
832 		opj_flag_t *flagsp,
833 		OPJ_INT32 *datap,
834 		OPJ_UINT32 orient,
835 		OPJ_INT32 bpno,
836 		OPJ_INT32 one,
837 		OPJ_INT32 *nmsedec,
838 		OPJ_UINT32 partial,
839 		OPJ_UINT32 vsc)
840 {
841 	OPJ_INT32 v;
842 	OPJ_UINT32 flag;
843 
844 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
845 
846 	flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
847 	if (partial) {
848 		goto LABEL_PARTIAL;
849 	}
850 	if (!(*flagsp & (T1_SIG | T1_VISIT))) {
851 		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
852 		v = opj_int_abs(*datap) & one ? 1 : 0;
853 		opj_mqc_encode(mqc, (OPJ_UINT32)v);
854 		if (v) {
855 LABEL_PARTIAL:
856 			*nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
857 			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
858 			v = *datap < 0 ? 1 : 0;
859 			opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
860 			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
861 		}
862 	}
863 	*flagsp &= ~T1_VISIT;
864 }
865 
opj_t1_dec_clnpass_step_partial(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 orient,OPJ_INT32 oneplushalf)866 static void opj_t1_dec_clnpass_step_partial(
867 		opj_t1_t *t1,
868 		opj_flag_t *flagsp,
869 		OPJ_INT32 *datap,
870 		OPJ_INT32 orient,
871 		OPJ_INT32 oneplushalf)
872 {
873 	OPJ_INT32 v, flag;
874 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
875 
876 	OPJ_ARG_NOT_USED(orient);
877 
878 	flag = *flagsp;
879 	opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
880 	v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
881 	*datap = v ? -oneplushalf : oneplushalf;
882 	opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
883 	*flagsp &= ~T1_VISIT;
884 }				/* VSC and  BYPASS by Antonin */
885 
opj_t1_dec_clnpass_step(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 orient,OPJ_INT32 oneplushalf)886 static void opj_t1_dec_clnpass_step(
887 		opj_t1_t *t1,
888 		opj_flag_t *flagsp,
889 		OPJ_INT32 *datap,
890 		OPJ_INT32 orient,
891 		OPJ_INT32 oneplushalf)
892 {
893 	OPJ_INT32 v, flag;
894 
895 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
896 
897 	flag = *flagsp;
898 	if (!(flag & (T1_SIG | T1_VISIT))) {
899 		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
900 		if (opj_mqc_decode(mqc)) {
901 			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
902 			v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
903 			*datap = v ? -oneplushalf : oneplushalf;
904 			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
905 		}
906 	}
907 	*flagsp &= ~T1_VISIT;
908 }				/* VSC and  BYPASS by Antonin */
909 
opj_t1_dec_clnpass_step_vsc(opj_t1_t * t1,opj_flag_t * flagsp,OPJ_INT32 * datap,OPJ_INT32 orient,OPJ_INT32 oneplushalf,OPJ_INT32 partial,OPJ_INT32 vsc)910 static void opj_t1_dec_clnpass_step_vsc(
911 		opj_t1_t *t1,
912 		opj_flag_t *flagsp,
913 		OPJ_INT32 *datap,
914 		OPJ_INT32 orient,
915 		OPJ_INT32 oneplushalf,
916 		OPJ_INT32 partial,
917 		OPJ_INT32 vsc)
918 {
919 	OPJ_INT32 v, flag;
920 
921 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
922 
923 	flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
924 	if (partial) {
925 		goto LABEL_PARTIAL;
926 	}
927 	if (!(flag & (T1_SIG | T1_VISIT))) {
928 		opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
929 		if (opj_mqc_decode(mqc)) {
930 LABEL_PARTIAL:
931 			opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
932 			v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
933 			*datap = v ? -oneplushalf : oneplushalf;
934 			opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
935 		}
936 	}
937 	*flagsp &= ~T1_VISIT;
938 }
939 
opj_t1_enc_clnpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_UINT32 orient,OPJ_INT32 * nmsedec,OPJ_UINT32 cblksty)940 void opj_t1_enc_clnpass(
941 		opj_t1_t *t1,
942 		OPJ_INT32 bpno,
943 		OPJ_UINT32 orient,
944 		OPJ_INT32 *nmsedec,
945 		OPJ_UINT32 cblksty)
946 {
947 	OPJ_UINT32 i, j, k;
948 	OPJ_INT32 one;
949 	OPJ_UINT32 agg, runlen, vsc;
950 
951 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
952 
953 	*nmsedec = 0;
954 	one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
955 	for (k = 0; k < t1->h; k += 4) {
956 		for (i = 0; i < t1->w; ++i) {
957 			if (k + 3 < t1->h) {
958 				if (cblksty & J2K_CCP_CBLKSTY_VSC) {
959 					agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960 						|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
961 						|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
962 						|| (MACRO_t1_flags(1 + k + 3,1 + i)
963 						& (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |	T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
964 				} else {
965 					agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
966 						|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
967 						|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
968 						|| MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
969 				}
970 			} else {
971 				agg = 0;
972 			}
973 			if (agg) {
974 				for (runlen = 0; runlen < 4; ++runlen) {
975 					if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
976 						break;
977 				}
978 				opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
979 				opj_mqc_encode(mqc, runlen != 4);
980 				if (runlen == 4) {
981 					continue;
982 				}
983 				opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
984 				opj_mqc_encode(mqc, runlen >> 1);
985 				opj_mqc_encode(mqc, runlen & 1);
986 			} else {
987 				runlen = 0;
988 			}
989 			for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
990 				vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
991 				opj_t1_enc_clnpass_step(
992 						t1,
993 						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
994 						&t1->data[(j * t1->data_stride) + i],
995 						orient,
996 						bpno,
997 						one,
998 						nmsedec,
999 						agg && (j == k + runlen),
1000 						vsc);
1001 			}
1002 		}
1003 	}
1004 }
1005 
opj_t1_dec_clnpass(opj_t1_t * t1,OPJ_INT32 bpno,OPJ_INT32 orient,OPJ_INT32 cblksty)1006 static void opj_t1_dec_clnpass(
1007 		opj_t1_t *t1,
1008 		OPJ_INT32 bpno,
1009 		OPJ_INT32 orient,
1010 		OPJ_INT32 cblksty)
1011 {
1012 	OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
1013     OPJ_UINT32 i, j, k;
1014 	OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1015 
1016 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1017 
1018 	one = 1 << bpno;
1019 	half = one >> 1;
1020 	oneplushalf = one | half;
1021 	if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1022 	for (k = 0; k < t1->h; k += 4) {
1023 		for (i = 0; i < t1->w; ++i) {
1024 			if (k + 3 < t1->h) {
1025 					agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1026 						|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1027 						|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1028 						|| (MACRO_t1_flags(1 + k + 3,1 + i)
1029 						& (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |	T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1030 				} else {
1031 				agg = 0;
1032 			}
1033 			if (agg) {
1034 				opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1035 				if (!opj_mqc_decode(mqc)) {
1036 					continue;
1037 				}
1038 				opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1039 				runlen = opj_mqc_decode(mqc);
1040 				runlen = (runlen << 1) | opj_mqc_decode(mqc);
1041 			} else {
1042 				runlen = 0;
1043 			}
1044 			for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1045 					vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1046 					opj_t1_dec_clnpass_step_vsc(
1047 						t1,
1048 						&t1->flags[((j+1) * t1->flags_stride) + i + 1],
1049 						&t1->data[(j * t1->w) + i],
1050 						orient,
1051 						oneplushalf,
1052 						agg && (j == k + (OPJ_UINT32)runlen),
1053 						vsc);
1054 			}
1055 		}
1056 	}
1057 	} else {
1058 		OPJ_INT32 *data1 = t1->data;
1059 		opj_flag_t *flags1 = &t1->flags[1];
1060 		for (k = 0; k < (t1->h & ~3u); k += 4) {
1061 			for (i = 0; i < t1->w; ++i) {
1062 				OPJ_INT32 *data2 = data1 + i;
1063 				opj_flag_t *flags2 = flags1 + i;
1064 				agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1065 					|| MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1066 					|| MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1067 					|| MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1068 				if (agg) {
1069 					opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1070 					if (!opj_mqc_decode(mqc)) {
1071 						continue;
1072 					}
1073 					opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1074 					runlen = opj_mqc_decode(mqc);
1075 					runlen = (runlen << 1) | opj_mqc_decode(mqc);
1076 					flags2 += (OPJ_UINT32)runlen * t1->flags_stride;
1077 					data2 += (OPJ_UINT32)runlen * t1->w;
1078 					for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1079 						flags2 += t1->flags_stride;
1080 						if (agg && (j == k + (OPJ_UINT32)runlen)) {
1081 							opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1082 						} else {
1083 							opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1084 						}
1085 						data2 += t1->w;
1086 					}
1087 				} else {
1088 					flags2 += t1->flags_stride;
1089 					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1090 					data2 += t1->w;
1091 					flags2 += t1->flags_stride;
1092 					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1093 					data2 += t1->w;
1094 					flags2 += t1->flags_stride;
1095 					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1096 					data2 += t1->w;
1097 					flags2 += t1->flags_stride;
1098 					opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1099 					data2 += t1->w;
1100 				}
1101 			}
1102 			data1 += t1->w << 2;
1103 			flags1 += t1->flags_stride << 2;
1104 		}
1105 		for (i = 0; i < t1->w; ++i) {
1106 			OPJ_INT32 *data2 = data1 + i;
1107 			opj_flag_t *flags2 = flags1 + i;
1108 			for (j = k; j < t1->h; ++j) {
1109 				flags2 += t1->flags_stride;
1110 				opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1111 				data2 += t1->w;
1112 			}
1113 		}
1114 	}
1115 
1116 	if (segsym) {
1117 		OPJ_INT32 v = 0;
1118 		opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1119 		v = opj_mqc_decode(mqc);
1120 		v = (v << 1) | opj_mqc_decode(mqc);
1121 		v = (v << 1) | opj_mqc_decode(mqc);
1122 		v = (v << 1) | opj_mqc_decode(mqc);
1123 		/*
1124 		if (v!=0xa) {
1125 			opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1126 		}
1127 		*/
1128 	}
1129 }				/* VSC and  BYPASS by Antonin */
1130 
1131 
1132 /** mod fixed_quality */
opj_t1_getwmsedec(OPJ_INT32 nmsedec,OPJ_UINT32 compno,OPJ_UINT32 level,OPJ_UINT32 orient,OPJ_INT32 bpno,OPJ_UINT32 qmfbid,OPJ_FLOAT64 stepsize,OPJ_UINT32 numcomps,const OPJ_FLOAT64 * mct_norms,OPJ_UINT32 mct_numcomps)1133 static OPJ_FLOAT64 opj_t1_getwmsedec(
1134 		OPJ_INT32 nmsedec,
1135 		OPJ_UINT32 compno,
1136 		OPJ_UINT32 level,
1137 		OPJ_UINT32 orient,
1138 		OPJ_INT32 bpno,
1139 		OPJ_UINT32 qmfbid,
1140 		OPJ_FLOAT64 stepsize,
1141 		OPJ_UINT32 numcomps,
1142 		const OPJ_FLOAT64 * mct_norms,
1143 		OPJ_UINT32 mct_numcomps)
1144 {
1145 	OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1146 	OPJ_ARG_NOT_USED(numcomps);
1147 
1148 	if (mct_norms && (compno < mct_numcomps)) {
1149 		w1 = mct_norms[compno];
1150 	}
1151 
1152 	if (qmfbid == 1) {
1153 		w2 = opj_dwt_getnorm(level, orient);
1154 	} else {	/* if (qmfbid == 0) */
1155 		w2 = opj_dwt_getnorm_real(level, orient);
1156 	}
1157 
1158 	wmsedec = w1 * w2 * stepsize * (1 << bpno);
1159 	wmsedec *= wmsedec * nmsedec / 8192.0;
1160 
1161 	return wmsedec;
1162 }
1163 
opj_t1_allocate_buffers(opj_t1_t * t1,OPJ_UINT32 w,OPJ_UINT32 h)1164 OPJ_BOOL opj_t1_allocate_buffers(
1165 		opj_t1_t *t1,
1166 		OPJ_UINT32 w,
1167 		OPJ_UINT32 h)
1168 {
1169 	OPJ_UINT32 datasize=w * h;
1170 	OPJ_UINT32 flagssize;
1171 
1172 	/* encoder uses tile buffer, so no need to allocate */
1173 	if (!t1->encoder) {
1174 		if(datasize > t1->datasize){
1175 			opj_aligned_free(t1->data);
1176 			t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1177 			if(!t1->data){
1178 				/* FIXME event manager error callback */
1179 				return OPJ_FALSE;
1180 			}
1181 			t1->datasize=datasize;
1182 		}
1183 		memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1184 	}
1185 	t1->flags_stride=w+2;
1186 	flagssize=t1->flags_stride * (h+2);
1187 
1188 	if(flagssize > t1->flagssize){
1189 		opj_aligned_free(t1->flags);
1190 		t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1191 		if(!t1->flags){
1192 			/* FIXME event manager error callback */
1193 			return OPJ_FALSE;
1194 		}
1195 		t1->flagssize=flagssize;
1196 	}
1197 	memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1198 
1199 	t1->w=w;
1200 	t1->h=h;
1201 
1202 	return OPJ_TRUE;
1203 }
1204 
1205 /* ----------------------------------------------------------------------- */
1206 
1207 /* ----------------------------------------------------------------------- */
1208 /**
1209  * Creates a new Tier 1 handle
1210  * and initializes the look-up tables of the Tier-1 coder/decoder
1211  * @return a new T1 handle if successful, returns NULL otherwise
1212 */
opj_t1_create(OPJ_BOOL isEncoder)1213 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1214 {
1215 	opj_t1_t *l_t1 = 00;
1216 
1217 	l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1218 	if (!l_t1) {
1219 		return 00;
1220 	}
1221 
1222 	/* create MQC and RAW handles */
1223 	l_t1->mqc = opj_mqc_create();
1224 	if (! l_t1->mqc) {
1225 		opj_t1_destroy(l_t1);
1226 		return 00;
1227 	}
1228 
1229 	l_t1->raw = opj_raw_create();
1230 	if (! l_t1->raw) {
1231 		opj_t1_destroy(l_t1);
1232 		return 00;
1233 	}
1234 	l_t1->encoder = isEncoder;
1235 
1236 	return l_t1;
1237 }
1238 
1239 
1240 /**
1241  * Destroys a previously created T1 handle
1242  *
1243  * @param p_t1 Tier 1 handle to destroy
1244 */
opj_t1_destroy(opj_t1_t * p_t1)1245 void opj_t1_destroy(opj_t1_t *p_t1)
1246 {
1247 	if (! p_t1) {
1248 		return;
1249 	}
1250 
1251 	/* destroy MQC and RAW handles */
1252 	opj_mqc_destroy(p_t1->mqc);
1253 	p_t1->mqc = 00;
1254 	opj_raw_destroy(p_t1->raw);
1255 	p_t1->raw = 00;
1256 
1257 	/* encoder uses tile buffer, so no need to free */
1258 	if (!p_t1->encoder && p_t1->data) {
1259 		opj_aligned_free(p_t1->data);
1260 		p_t1->data = 00;
1261 	}
1262 
1263 	if (p_t1->flags) {
1264 		opj_aligned_free(p_t1->flags);
1265 		p_t1->flags = 00;
1266 	}
1267 
1268 	opj_free(p_t1);
1269 }
1270 
opj_t1_decode_cblks(opj_t1_t * t1,opj_tcd_tilecomp_t * tilec,opj_tccp_t * tccp)1271 OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
1272                             opj_tcd_tilecomp_t* tilec,
1273                             opj_tccp_t* tccp
1274                             )
1275 {
1276 	OPJ_UINT32 resno, bandno, precno, cblkno;
1277 	OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1278 
1279 	for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1280 		opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1281 
1282 		for (bandno = 0; bandno < res->numbands; ++bandno) {
1283 			opj_tcd_band_t* restrict band = &res->bands[bandno];
1284 
1285 			for (precno = 0; precno < res->pw * res->ph; ++precno) {
1286 				opj_tcd_precinct_t* precinct = &band->precincts[precno];
1287 
1288 				for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1289 					opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1290 					OPJ_INT32* restrict datap;
1291 					/*void* restrict tiledp;*/
1292 					OPJ_UINT32 cblk_w, cblk_h;
1293 					OPJ_INT32 x, y;
1294 					OPJ_UINT32 i, j;
1295 
1296                     if (OPJ_FALSE == opj_t1_decode_cblk(
1297                                             t1,
1298                                             cblk,
1299                                             band->bandno,
1300                                             (OPJ_UINT32)tccp->roishift,
1301                                             tccp->cblksty)) {
1302                             return OPJ_FALSE;
1303                     }
1304 
1305 					x = cblk->x0 - band->x0;
1306 					y = cblk->y0 - band->y0;
1307 					if (band->bandno & 1) {
1308 						opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1309 						x += pres->x1 - pres->x0;
1310 					}
1311 					if (band->bandno & 2) {
1312 						opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1313 						y += pres->y1 - pres->y0;
1314 					}
1315 
1316 					datap=t1->data;
1317 					cblk_w = t1->w;
1318 					cblk_h = t1->h;
1319 
1320 					if (tccp->roishift) {
1321 						OPJ_INT32 thresh = 1 << tccp->roishift;
1322 						for (j = 0; j < cblk_h; ++j) {
1323 							for (i = 0; i < cblk_w; ++i) {
1324 								OPJ_INT32 val = datap[(j * cblk_w) + i];
1325 								OPJ_INT32 mag = abs(val);
1326 								if (mag >= thresh) {
1327 									mag >>= tccp->roishift;
1328 									datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1329 								}
1330 							}
1331 						}
1332 					}
1333 
1334 					/*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1335 					if (tccp->qmfbid == 1) {
1336                         OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1337 						for (j = 0; j < cblk_h; ++j) {
1338 							for (i = 0; i < cblk_w; ++i) {
1339 								OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1340 								((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1341 							}
1342 						}
1343 					} else {		/* if (tccp->qmfbid == 0) */
1344                         OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1345 						for (j = 0; j < cblk_h; ++j) {
1346                             OPJ_FLOAT32* restrict tiledp2 = tiledp;
1347 							for (i = 0; i < cblk_w; ++i) {
1348                                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1349                                 *tiledp2 = tmp;
1350                                 datap++;
1351                                 tiledp2++;
1352 								/*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1353 								((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1354 
1355 							}
1356                             tiledp += tile_w;
1357 						}
1358 					}
1359                     /*opj_free(cblk->data);
1360 					opj_free(cblk->segs);*/
1361 					/*cblk->segs = 00;*/
1362 				} /* cblkno */
1363                 /*opj_free(precinct->cblks.dec);*/
1364 			} /* precno */
1365 		} /* bandno */
1366 	} /* resno */
1367         return OPJ_TRUE;
1368 }
1369 
1370 
opj_t1_decode_cblk(opj_t1_t * t1,opj_tcd_cblk_dec_t * cblk,OPJ_UINT32 orient,OPJ_UINT32 roishift,OPJ_UINT32 cblksty)1371 OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1372                             opj_tcd_cblk_dec_t* cblk,
1373                             OPJ_UINT32 orient,
1374                             OPJ_UINT32 roishift,
1375                             OPJ_UINT32 cblksty)
1376 {
1377 	opj_raw_t *raw = t1->raw;	/* RAW component */
1378 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1379 
1380 	OPJ_INT32 bpno;
1381 	OPJ_UINT32 passtype;
1382 	OPJ_UINT32 segno, passno;
1383 	OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1384 
1385 	if(!opj_t1_allocate_buffers(
1386 				t1,
1387 				(OPJ_UINT32)(cblk->x1 - cblk->x0),
1388 				(OPJ_UINT32)(cblk->y1 - cblk->y0)))
1389 	{
1390 		return OPJ_FALSE;
1391 	}
1392 
1393 	bpno = (OPJ_INT32)(roishift + cblk->numbps - 1);
1394 	passtype = 2;
1395 
1396 	opj_mqc_resetstates(mqc);
1397 	opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1398 	opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1399 	opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1400 
1401 	for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1402 		opj_tcd_seg_t *seg = &cblk->segs[segno];
1403 
1404 		/* BYPASS mode */
1405 		type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1406 		/* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1407 		if(seg->data == 00){
1408 			continue;
1409 		}
1410 		if (type == T1_TYPE_RAW) {
1411 			opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1412 		} else {
1413             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1414                     return OPJ_FALSE;
1415             }
1416 		}
1417 
1418 		for (passno = 0; passno < seg->real_num_passes; ++passno) {
1419             switch (passtype) {
1420                 case 0:
1421                     if (type == T1_TYPE_RAW) {
1422                         opj_t1_dec_sigpass_raw(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1423                     } else {
1424                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1425                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, (OPJ_INT32)orient);
1426                         } else {
1427                             opj_t1_dec_sigpass_mqc(t1, bpno+1, (OPJ_INT32)orient);
1428                         }
1429                     }
1430                     break;
1431                 case 1:
1432                     if (type == T1_TYPE_RAW) {
1433                             opj_t1_dec_refpass_raw(t1, bpno+1, (OPJ_INT32)cblksty);
1434                     } else {
1435                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1436                             opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1437                         } else {
1438                             opj_t1_dec_refpass_mqc(t1, bpno+1);
1439                         }
1440                     }
1441                     break;
1442                 case 2:
1443                     opj_t1_dec_clnpass(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1444                     break;
1445             }
1446 
1447 			if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1448 				opj_mqc_resetstates(mqc);
1449 				opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1450 				opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1451 				opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1452 			}
1453 			if (++passtype == 3) {
1454 				passtype = 0;
1455 				bpno--;
1456 			}
1457 		}
1458 	}
1459         return OPJ_TRUE;
1460 }
1461 
1462 
1463 
1464 
opj_t1_encode_cblks(opj_t1_t * t1,opj_tcd_tile_t * tile,opj_tcp_t * tcp,const OPJ_FLOAT64 * mct_norms,OPJ_UINT32 mct_numcomps)1465 OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
1466                                 opj_tcd_tile_t *tile,
1467                                 opj_tcp_t *tcp,
1468                                 const OPJ_FLOAT64 * mct_norms,
1469                                 OPJ_UINT32 mct_numcomps
1470                                 )
1471 {
1472 	OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1473 
1474 	tile->distotile = 0;		/* fixed_quality */
1475 
1476 	for (compno = 0; compno < tile->numcomps; ++compno) {
1477 		opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1478 		opj_tccp_t* tccp = &tcp->tccps[compno];
1479 		OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1480 
1481 		for (resno = 0; resno < tilec->numresolutions; ++resno) {
1482 			opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1483 
1484 			for (bandno = 0; bandno < res->numbands; ++bandno) {
1485 				opj_tcd_band_t* restrict band = &res->bands[bandno];
1486                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1487 
1488 				for (precno = 0; precno < res->pw * res->ph; ++precno) {
1489 					opj_tcd_precinct_t *prc = &band->precincts[precno];
1490 
1491 					for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1492 						opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1493 						OPJ_INT32* restrict tiledp;
1494 						OPJ_UINT32 cblk_w;
1495 						OPJ_UINT32 cblk_h;
1496 						OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1497 
1498 						OPJ_INT32 x = cblk->x0 - band->x0;
1499 						OPJ_INT32 y = cblk->y0 - band->y0;
1500 						if (band->bandno & 1) {
1501 							opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1502 							x += pres->x1 - pres->x0;
1503 						}
1504 						if (band->bandno & 2) {
1505 							opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1506 							y += pres->y1 - pres->y0;
1507 						}
1508 
1509 						if(!opj_t1_allocate_buffers(
1510 									t1,
1511 									(OPJ_UINT32)(cblk->x1 - cblk->x0),
1512 									(OPJ_UINT32)(cblk->y1 - cblk->y0)))
1513 						{
1514 							return OPJ_FALSE;
1515 						}
1516 
1517 						cblk_w = t1->w;
1518 						cblk_h = t1->h;
1519 						tileLineAdvance = tile_w - cblk_w;
1520 
1521 						tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1522 						t1->data = tiledp;
1523 						t1->data_stride = tile_w;
1524 						if (tccp->qmfbid == 1) {
1525 							for (j = 0; j < cblk_h; ++j) {
1526 								for (i = 0; i < cblk_w; ++i) {
1527 									tiledp[tileIndex] <<= T1_NMSEDEC_FRACBITS;
1528 									tileIndex++;
1529 								}
1530 								tileIndex += tileLineAdvance;
1531 							}
1532 						} else {		/* if (tccp->qmfbid == 0) */
1533 							for (j = 0; j < cblk_h; ++j) {
1534 								for (i = 0; i < cblk_w; ++i) {
1535 									OPJ_INT32 tmp = tiledp[tileIndex];
1536 									tiledp[tileIndex] =
1537 										opj_int_fix_mul_t1(
1538 										tmp,
1539 										bandconst);
1540 									tileIndex++;
1541 								}
1542 								tileIndex += tileLineAdvance;
1543 							}
1544 						}
1545 
1546 						opj_t1_encode_cblk(
1547 								t1,
1548 								cblk,
1549 								band->bandno,
1550 								compno,
1551 								tilec->numresolutions - 1 - resno,
1552 								tccp->qmfbid,
1553 								band->stepsize,
1554 								tccp->cblksty,
1555 								tile->numcomps,
1556 								tile,
1557 								mct_norms,
1558 								mct_numcomps);
1559 
1560 					} /* cblkno */
1561 				} /* precno */
1562 			} /* bandno */
1563 		} /* resno  */
1564 	} /* compno  */
1565 	return OPJ_TRUE;
1566 }
1567 
1568 /** mod fixed_quality */
opj_t1_encode_cblk(opj_t1_t * t1,opj_tcd_cblk_enc_t * cblk,OPJ_UINT32 orient,OPJ_UINT32 compno,OPJ_UINT32 level,OPJ_UINT32 qmfbid,OPJ_FLOAT64 stepsize,OPJ_UINT32 cblksty,OPJ_UINT32 numcomps,opj_tcd_tile_t * tile,const OPJ_FLOAT64 * mct_norms,OPJ_UINT32 mct_numcomps)1569 void opj_t1_encode_cblk(opj_t1_t *t1,
1570                         opj_tcd_cblk_enc_t* cblk,
1571                         OPJ_UINT32 orient,
1572                         OPJ_UINT32 compno,
1573                         OPJ_UINT32 level,
1574                         OPJ_UINT32 qmfbid,
1575                         OPJ_FLOAT64 stepsize,
1576                         OPJ_UINT32 cblksty,
1577                         OPJ_UINT32 numcomps,
1578                         opj_tcd_tile_t * tile,
1579                         const OPJ_FLOAT64 * mct_norms,
1580                         OPJ_UINT32 mct_numcomps)
1581 {
1582 	OPJ_FLOAT64 cumwmsedec = 0.0;
1583 
1584 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1585 
1586 	OPJ_UINT32 passno;
1587 	OPJ_INT32 bpno;
1588 	OPJ_UINT32 passtype;
1589 	OPJ_INT32 nmsedec = 0;
1590 	OPJ_INT32 max;
1591 	OPJ_UINT32 i, j;
1592 	OPJ_BYTE type = T1_TYPE_MQ;
1593 	OPJ_FLOAT64 tempwmsedec;
1594 
1595 	max = 0;
1596 	for (i = 0; i < t1->w; ++i) {
1597 		for (j = 0; j < t1->h; ++j) {
1598 			OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1599 			max = opj_int_max(max, tmp);
1600 		}
1601 	}
1602 
1603 	cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1604 
1605 	bpno = (OPJ_INT32)(cblk->numbps - 1);
1606 	passtype = 2;
1607 
1608 	opj_mqc_resetstates(mqc);
1609 	opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1610 	opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1611 	opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1612 	opj_mqc_init_enc(mqc, cblk->data);
1613 
1614 	for (passno = 0; bpno >= 0; ++passno) {
1615 		opj_tcd_pass_t *pass = &cblk->passes[passno];
1616 		OPJ_UINT32 correction = 3;
1617 		type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1618 
1619 		switch (passtype) {
1620 			case 0:
1621 				opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1622 				break;
1623 			case 1:
1624 				opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1625 				break;
1626 			case 2:
1627 				opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1628 				/* code switch SEGMARK (i.e. SEGSYM) */
1629 				if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1630 					opj_mqc_segmark_enc(mqc);
1631 				break;
1632 		}
1633 
1634 		/* fixed_quality */
1635 		tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
1636 		cumwmsedec += tempwmsedec;
1637 		tile->distotile += tempwmsedec;
1638 
1639 		/* Code switch "RESTART" (i.e. TERMALL) */
1640 		if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)	&& !((passtype == 2) && (bpno - 1 < 0))) {
1641 			if (type == T1_TYPE_RAW) {
1642 				opj_mqc_flush(mqc);
1643 				correction = 1;
1644 				/* correction = mqc_bypass_flush_enc(); */
1645 			} else {			/* correction = mqc_restart_enc(); */
1646 				opj_mqc_flush(mqc);
1647 				correction = 1;
1648 			}
1649 			pass->term = 1;
1650 		} else {
1651 			if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1652 				|| ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1653 				if (type == T1_TYPE_RAW) {
1654 					opj_mqc_flush(mqc);
1655 					correction = 1;
1656 					/* correction = mqc_bypass_flush_enc(); */
1657 				} else {		/* correction = mqc_restart_enc(); */
1658 					opj_mqc_flush(mqc);
1659 					correction = 1;
1660 				}
1661 				pass->term = 1;
1662 			} else {
1663 				pass->term = 0;
1664 			}
1665 		}
1666 
1667 		if (++passtype == 3) {
1668 			passtype = 0;
1669 			bpno--;
1670 		}
1671 
1672 		if (pass->term && bpno > 0) {
1673 			type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1674 			if (type == T1_TYPE_RAW)
1675 				opj_mqc_bypass_init_enc(mqc);
1676 			else
1677 				opj_mqc_restart_init_enc(mqc);
1678 		}
1679 
1680 		pass->distortiondec = cumwmsedec;
1681 		pass->rate = opj_mqc_numbytes(mqc) + correction;	/* FIXME */
1682 
1683 		/* Code-switch "RESET" */
1684 		if (cblksty & J2K_CCP_CBLKSTY_RESET)
1685 			opj_mqc_reset_enc(mqc);
1686 	}
1687 
1688 	/* Code switch "ERTERM" (i.e. PTERM) */
1689 	if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1690 		opj_mqc_erterm_enc(mqc);
1691 	else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1692 		opj_mqc_flush(mqc);
1693 
1694 	cblk->totalpasses = passno;
1695 
1696 	for (passno = 0; passno<cblk->totalpasses; passno++) {
1697 		opj_tcd_pass_t *pass = &cblk->passes[passno];
1698 		if (pass->rate > opj_mqc_numbytes(mqc))
1699 			pass->rate = opj_mqc_numbytes(mqc);
1700 		/*Preventing generation of FF as last data byte of a pass*/
1701 		if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1702 			pass->rate--;
1703 		}
1704 		pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1705 	}
1706 }
1707 
1708 #if 0
1709 void opj_t1_dec_refpass_step(   opj_t1_t *t1,
1710                                 opj_flag_t *flagsp,
1711                                 OPJ_INT32 *datap,
1712                                 OPJ_INT32 poshalf,
1713                                 OPJ_INT32 neghalf,
1714                                 OPJ_BYTE type,
1715                                 OPJ_UINT32 vsc)
1716 {
1717 	OPJ_INT32  t;
1718 	OPJ_UINT32 v,flag;
1719 
1720 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1721 	opj_raw_t *raw = t1->raw;	/* RAW component */
1722 
1723 	flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1724 	if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1725 		opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));	/* ESSAI */
1726 		if (type == T1_TYPE_RAW) {
1727 			v = opj_raw_decode(raw);
1728 		} else {
1729 			v = opj_mqc_decode(mqc);
1730 		}
1731 		t = v ? poshalf : neghalf;
1732 		*datap += *datap < 0 ? -t : t;
1733 		*flagsp |= T1_REFINE;
1734 	}
1735 }				/* VSC and  BYPASS by Antonin  */
1736 #endif
1737 
1738 
1739 
1740 #if 0
1741 void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
1742                                 opj_flag_t *flagsp,
1743                                 OPJ_INT32 *datap,
1744                                 OPJ_UINT32 orient,
1745                                 OPJ_INT32 oneplushalf,
1746                                 OPJ_BYTE type,
1747                                 OPJ_UINT32 vsc)
1748 {
1749 	OPJ_UINT32 v, flag;
1750 
1751 	opj_raw_t *raw = t1->raw;	/* RAW component */
1752 	opj_mqc_t *mqc = t1->mqc;	/* MQC component */
1753 
1754 	flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1755 	if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1756 		if (type == T1_TYPE_RAW) {
1757 			if (opj_raw_decode(raw)) {
1758 				v = opj_raw_decode(raw);	/* ESSAI */
1759 				*datap = v ? -oneplushalf : oneplushalf;
1760 				opj_t1_updateflags(flagsp, v, t1->flags_stride);
1761 			}
1762 		} else {
1763 			opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1764 			if (opj_mqc_decode(mqc)) {
1765 				opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1766 				v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1767 				*datap = v ? -oneplushalf : oneplushalf;
1768 				opj_t1_updateflags(flagsp, v, t1->flags_stride);
1769 			}
1770 		}
1771 		*flagsp |= T1_VISIT;
1772 	}
1773 }				/* VSC and  BYPASS by Antonin */
1774 #endif
1775