Flags in T1 shall be unsigned (#840)
[openjpeg.git] / src / lib / openjp2 / t1.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 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 /* #define CONSISTENCY_CHECK */
43
44 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
45 /*@{*/
46
47 /** @name Local static functions */
48 /*@{*/
49
50 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
52 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
53 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
54 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
55 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
56 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
57 /**
58 Encode significant pass
59 */
60 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
61                                     opj_flag_t *flagsp,
62                                     OPJ_INT32 *datap,
63                                     OPJ_INT32 bpno,
64                                     OPJ_INT32 one,
65                                     OPJ_INT32 *nmsedec,
66                                     OPJ_BYTE type,
67                                     OPJ_UINT32 vsc);
68
69 /**
70 Decode significant pass
71 */
72 #if 0
73 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
74                                     opj_flag_t *flagsp,
75                                     OPJ_INT32 *datap,
76                                     OPJ_UINT32 orient,
77                                     OPJ_INT32 oneplushalf,
78                                     OPJ_BYTE type,
79                                     OPJ_UINT32 vsc);
80 #endif
81
82 static INLINE void opj_t1_dec_sigpass_step_raw(
83                 opj_t1_t *t1,
84                 opj_flag_t *flagsp,
85                 opj_colflag_t* colflagsp,
86                 OPJ_INT32 *datap,
87                 OPJ_INT32 oneplushalf,
88                 OPJ_INT32 vsc,
89                 OPJ_UINT32 row);
90 static INLINE void opj_t1_dec_sigpass_step_mqc(
91                 opj_t1_t *t1,
92                 opj_flag_t *flagsp,
93                 opj_colflag_t* colflagsp,
94                 OPJ_INT32 *datap,
95                 OPJ_INT32 oneplushalf,
96                 OPJ_UINT32 row,
97                 OPJ_UINT32 flags_stride);
98 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
99                 opj_t1_t *t1,
100                 opj_flag_t *flagsp,
101                 opj_colflag_t* colflagsp,
102                 OPJ_INT32 *datap,
103                 OPJ_INT32 oneplushalf,
104                 OPJ_INT32 vsc,
105                 OPJ_UINT32 row);
106
107
108 /**
109 Encode significant pass
110 */
111 static void opj_t1_enc_sigpass( opj_t1_t *t1,
112                                 OPJ_INT32 bpno,
113                                 OPJ_INT32 *nmsedec,
114                                 OPJ_BYTE type,
115                                 OPJ_UINT32 cblksty);
116
117 /**
118 Decode significant pass
119 */
120 static void opj_t1_dec_sigpass_raw(
121                 opj_t1_t *t1,
122                 OPJ_INT32 bpno,
123                 OPJ_INT32 cblksty);
124 static void opj_t1_dec_sigpass_mqc_vsc(
125                 opj_t1_t *t1,
126                 OPJ_INT32 bpno);
127
128
129
130 /**
131 Encode refinement pass
132 */
133 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
134                                     opj_flag_t *flagsp,
135                                     OPJ_INT32 *datap,
136                                     OPJ_INT32 bpno,
137                                     OPJ_INT32 one,
138                                     OPJ_INT32 *nmsedec,
139                                     OPJ_BYTE type,
140                                     OPJ_UINT32 vsc);
141
142
143 /**
144 Encode refinement pass
145 */
146 static void opj_t1_enc_refpass( opj_t1_t *t1,
147                                 OPJ_INT32 bpno,
148                                 OPJ_INT32 *nmsedec,
149                                 OPJ_BYTE type,
150                                 OPJ_UINT32 cblksty);
151
152 /**
153 Decode refinement pass
154 */
155 static void opj_t1_dec_refpass_raw(
156                 opj_t1_t *t1,
157                 OPJ_INT32 bpno);
158 static void opj_t1_dec_refpass_mqc_vsc(
159                 opj_t1_t *t1,
160                 OPJ_INT32 bpno);
161
162
163 /**
164 Decode refinement pass
165 */
166 #if 0
167 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
168                                     opj_flag_t *flagsp,
169                                     OPJ_INT32 *datap,
170                                     OPJ_INT32 poshalf,
171                                     OPJ_INT32 neghalf,
172                                     OPJ_BYTE type,
173                                     OPJ_UINT32 vsc);
174 #endif
175
176 static INLINE void  opj_t1_dec_refpass_step_raw(
177                 opj_t1_t *t1,
178                 opj_colflag_t *colflagsp,
179                 OPJ_INT32 *datap,
180                 OPJ_INT32 poshalf,
181                 OPJ_INT32 neghalf,
182                 OPJ_UINT32 row);
183 static INLINE void opj_t1_dec_refpass_step_mqc(
184                 opj_t1_t *t1,
185                 opj_flag_t *flagsp,
186                 opj_colflag_t *colflagsp,
187                 OPJ_INT32 *datap,
188                 OPJ_INT32 poshalf,
189                 OPJ_INT32 neghalf,
190                 OPJ_UINT32 row);
191 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
192                 opj_t1_t *t1,
193                 opj_flag_t *flagsp,
194                 opj_colflag_t *colflagsp,
195                 OPJ_INT32 *datap,
196                 OPJ_INT32 poshalf,
197                 OPJ_INT32 neghalf,
198                 OPJ_INT32 vsc,
199                 OPJ_UINT32 row);
200
201
202
203 /**
204 Encode clean-up pass
205 */
206 static void opj_t1_enc_clnpass_step(
207                 opj_t1_t *t1,
208                 opj_flag_t *flagsp,
209                 OPJ_INT32 *datap,
210                 OPJ_INT32 bpno,
211                 OPJ_INT32 one,
212                 OPJ_INT32 *nmsedec,
213                 OPJ_UINT32 partial,
214                 OPJ_UINT32 vsc);
215 /**
216 Decode clean-up pass
217 */
218 static void opj_t1_dec_clnpass_step_partial(
219                 opj_t1_t *t1,
220                 opj_flag_t *flagsp,
221                 opj_colflag_t *colflagsp,
222                 OPJ_INT32 *datap,
223                 OPJ_INT32 oneplushalf,
224                 OPJ_UINT32 row);
225 static void opj_t1_dec_clnpass_step(
226                 opj_t1_t *t1,
227                 opj_flag_t *flagsp,
228                 opj_colflag_t *colflagsp,
229                 OPJ_INT32 *datap,
230                 OPJ_INT32 oneplushalf,
231                 OPJ_UINT32 row);
232 static void opj_t1_dec_clnpass_step_vsc(
233                 opj_t1_t *t1,
234                 opj_flag_t *flagsp,
235                 opj_colflag_t *colflagsp,
236                 OPJ_INT32 *datap,
237                 OPJ_INT32 oneplushalf,
238                 OPJ_INT32 partial,
239                 OPJ_INT32 vsc,
240                 OPJ_UINT32 row);
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_INT32 *nmsedec,
248                 OPJ_UINT32 cblksty);
249
250 static OPJ_FLOAT64 opj_t1_getwmsedec(
251                 OPJ_INT32 nmsedec,
252                 OPJ_UINT32 compno,
253                 OPJ_UINT32 level,
254                 OPJ_UINT32 orient,
255                 OPJ_INT32 bpno,
256                 OPJ_UINT32 qmfbid,
257                 OPJ_FLOAT64 stepsize,
258                 OPJ_UINT32 numcomps,
259                 const OPJ_FLOAT64 * mct_norms,
260                 OPJ_UINT32 mct_numcomps);
261
262 static void opj_t1_encode_cblk( opj_t1_t *t1,
263                                 opj_tcd_cblk_enc_t* cblk,
264                                 OPJ_UINT32 orient,
265                                 OPJ_UINT32 compno,
266                                 OPJ_UINT32 level,
267                                 OPJ_UINT32 qmfbid,
268                                 OPJ_FLOAT64 stepsize,
269                                 OPJ_UINT32 cblksty,
270                                 OPJ_UINT32 numcomps,
271                                 opj_tcd_tile_t * tile,
272                                 const OPJ_FLOAT64 * mct_norms,
273                                 OPJ_UINT32 mct_numcomps);
274
275 /**
276 Decode 1 code-block
277 @param t1 T1 handle
278 @param cblk Code-block coding parameters
279 @param orient
280 @param roishift Region of interest shifting value
281 @param cblksty Code-block style
282 */
283 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
284                                     opj_tcd_cblk_dec_t* cblk,
285                                     OPJ_UINT32 orient,
286                                     OPJ_UINT32 roishift,
287                                     OPJ_UINT32 cblksty);
288
289 static OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
290                                     OPJ_UINT32 w,
291                                     OPJ_UINT32 h);
292
293 /*@}*/
294
295 /*@}*/
296
297 /* ----------------------------------------------------------------------- */
298
299 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f) {
300         return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
301 }
302
303 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
304         return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
305 }
306
307 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
308         OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
309         OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
310         return (tmp2);
311 }
312
313 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
314         return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
315 }
316
317 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
318         if (bitpos > 0) {
319                 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
320         }
321         
322         return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
323 }
324
325 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
326         if (bitpos > 0) {
327                 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
328         }
329
330     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
331 }
332
333 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
334         opj_flag_t *np = flagsp - stride;
335         opj_flag_t *sp = flagsp + stride;
336
337         /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
338         /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
339         /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
340
341         unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
342
343         np[-1] |= T1_SIG_SE;
344         np[0]  |= (opj_flag_t)(flag_N << 2U);
345         np[1]  |= T1_SIG_SW;
346
347         flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
348         flagsp[0]  |= T1_SIG;
349         flagsp[1]  |= (opj_flag_t)(flag_N << 3U);
350
351         sp[-1] |= T1_SIG_NE;
352         sp[0]  |= (opj_flag_t)flag_N;
353         sp[1]  |= T1_SIG_NW;
354 }
355
356 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp, opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
357 {
358         opj_t1_updateflags(flagsp, s, stride);
359         if( row == 0U )
360         {
361                         *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 <<  (T1_COLFLAG_RBS * row)) |
362                                                   (T1_COLFLAG_SIG_OTHER_ROW_0 <<  (T1_COLFLAG_RBS * (row+1U))));
363                         *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
364                                                             (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
365                         *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
366                                                                 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
367                         *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
368                         *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
369                         *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
370         }
371         else if( row == 3U )
372         {
373                         *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 <<  (T1_COLFLAG_RBS * row)) |
374                                                   (T1_COLFLAG_SIG_OTHER_ROW_0 <<  (T1_COLFLAG_RBS * (row-1U))));
375                         *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
376                                                             (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))));
377                         *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
378                                                                 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS* (row-1))));
379                         *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
380                         *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
381                         *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
382         }
383         else
384         {
385                         *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
386                                                                 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))) |
387                                                                 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
388                         *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 <<  (T1_COLFLAG_RBS * row)) |
389                                                   (T1_COLFLAG_SIG_OTHER_ROW_0 <<  (T1_COLFLAG_RBS * (row-1U))) |
390                                                   (T1_COLFLAG_SIG_OTHER_ROW_0 <<  (T1_COLFLAG_RBS * (row+1U))));
391                         *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) |
392                                                                 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row-1U))) |
393                                                                 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row+1U))));
394         }
395 }
396
397 static void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
398                                 opj_flag_t *flagsp,
399                                 OPJ_INT32 *datap,
400                                 OPJ_INT32 bpno,
401                                 OPJ_INT32 one,
402                                 OPJ_INT32 *nmsedec,
403                                 OPJ_BYTE type,
404                                 OPJ_UINT32 vsc
405                                 )
406 {
407         OPJ_INT32 v;
408     OPJ_UINT32 flag;
409         
410         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
411         
412         flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
413         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
414                 v = (opj_int_abs(*datap) & one) ? 1 : 0;
415                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));  /* ESSAI */
416                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
417                         opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
418                 } else {
419                         opj_mqc_encode(mqc, (OPJ_UINT32)v);
420                 }
421                 if (v) {
422                         v = *datap < 0 ? 1 : 0;
423                         *nmsedec +=     opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
424                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));       /* ESSAI */
425                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
426                                 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
427                         } else {
428                                 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
429                         }
430                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
431                 }
432                 *flagsp |= T1_VISIT;
433         }
434 }
435
436
437 static INLINE void opj_t1_dec_sigpass_step_raw(
438                 opj_t1_t *t1,
439                 opj_flag_t *flagsp,
440                 opj_colflag_t* colflagsp,
441                 OPJ_INT32 *datap,
442                 OPJ_INT32 oneplushalf,
443                 OPJ_INT32 vsc,
444                 OPJ_UINT32 row)
445 {
446         OPJ_UINT32 v, flag;
447         opj_raw_t *raw = t1->raw;       /* RAW component */
448
449         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
450         if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
451                         if (opj_raw_decode(raw)) {
452                                 v = opj_raw_decode(raw);    /* ESSAI */
453                                 *datap = v ? -oneplushalf : oneplushalf;
454                                 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
455                         }
456 #ifdef CONSISTENCY_CHECK
457                 *flagsp |= T1_VISIT;
458 #endif
459                 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
460         }
461 }      
462
463 static INLINE void opj_t1_dec_sigpass_step_mqc(
464                 opj_t1_t *t1,
465                 opj_flag_t *flagsp,
466                 opj_colflag_t* colflagsp,
467                 OPJ_INT32 *datap,
468                 OPJ_INT32 oneplushalf,
469                 OPJ_UINT32 row,
470                 OPJ_UINT32 flags_stride)
471 {
472         OPJ_INT32 v, flag;
473        
474         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
475 #ifdef CONSISTENCY_CHECK
476                 assert( ((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
477                                 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
478                                   (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) );
479 #endif
480         if( (*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
481             (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row)) ) {
482                         flag = *flagsp;
483                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
484                         if (opj_mqc_decode(mqc)) {
485                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
486                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
487                                 *datap = v ? -oneplushalf : oneplushalf;
488                                 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
489                         }
490 #ifdef CONSISTENCY_CHECK
491                 *flagsp |= T1_VISIT;
492 #endif
493                 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
494         }
495 }                               /* VSC and  BYPASS by Antonin */
496
497 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
498                 opj_t1_t *t1,
499                 opj_flag_t *flagsp,
500                 opj_colflag_t* colflagsp,
501                 OPJ_INT32 *datap,
502                 OPJ_INT32 oneplushalf,
503                 OPJ_INT32 vsc,
504                 OPJ_UINT32 row)
505 {
506         OPJ_UINT32 v, flag;
507        
508         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
509        
510         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
511         if ((flag & T1_SIG_OTH) && !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
512                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
513                 if (opj_mqc_decode(mqc)) {
514                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
515                         v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
516                         *datap = v ? -oneplushalf : oneplushalf;
517                         opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
518                 }
519 #ifdef CONSISTENCY_CHECK
520                 *flagsp |= T1_VISIT;
521 #endif
522                 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_VISIT_ROW_0 << (T1_COLFLAG_RBS * row));
523         }
524 }                               /* VSC and  BYPASS by Antonin */
525
526
527
528 static void opj_t1_enc_sigpass(opj_t1_t *t1,
529                         OPJ_INT32 bpno,
530                         OPJ_INT32 *nmsedec,
531                         OPJ_BYTE type,
532                         OPJ_UINT32 cblksty
533                         )
534 {
535         OPJ_UINT32 i, j, k, vsc; 
536     OPJ_INT32 one;
537
538         *nmsedec = 0;
539         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
540         for (k = 0; k < t1->h; k += 4) {
541                 for (i = 0; i < t1->w; ++i) {
542                         for (j = k; j < k + 4 && j < t1->h; ++j) {
543                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
544                                 opj_t1_enc_sigpass_step(
545                                                 t1,
546                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
547                                                 &t1->data[(j * t1->data_stride) + i],
548                                                 bpno,
549                                                 one,
550                                                 nmsedec,
551                                                 type,
552                                                 vsc);
553                         }
554                 }
555         }
556 }
557
558 static void opj_t1_dec_sigpass_raw(
559                 opj_t1_t *t1,
560                 OPJ_INT32 bpno,
561                 OPJ_INT32 cblksty)
562 {
563         OPJ_INT32 one, half, oneplushalf, vsc;
564         OPJ_UINT32 i, j, k; 
565         opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
566         one = 1 << bpno;
567         half = one >> 1;
568         oneplushalf = one | half;
569         for (k = 0; k < t1->h; k += 4) {
570                 for (i = 0; i < t1->w; ++i) {
571                         opj_colflag_t *colflags2 = colflags1 + i;
572                         for (j = k; j < k + 4 && j < t1->h; ++j) {
573                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
574                                 opj_t1_dec_sigpass_step_raw(
575                                                 t1,
576                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
577                                                 colflags2,
578                                                 &t1->data[(j * t1->w) + i],
579                                                 oneplushalf,
580                                                 vsc,
581                                                 j - k);
582                         }
583                 }
584                 colflags1 += t1->flags_stride;
585         }
586 }                               /* VSC and  BYPASS by Antonin */
587
588 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
589 { \
590         OPJ_INT32 one, half, oneplushalf; \
591         OPJ_UINT32 i, j, k; \
592         OPJ_INT32 *data1 = t1->data; \
593         opj_flag_t *flags1 = &t1->flags[1]; \
594         opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
595         one = 1 << bpno; \
596         half = one >> 1; \
597         oneplushalf = one | half; \
598         for (k = 0; k < (h & ~3u); k += 4) { \
599                 for (i = 0; i < w; ++i) { \
600                         OPJ_INT32 *data2 = data1 + i; \
601                         opj_flag_t *flags2 = flags1 + i; \
602                         opj_colflag_t *colflags2 = colflags1 + i; \
603                         if( *colflags2 == 0 ) continue; \
604                         flags2 += flags_stride; \
605                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
606                         data2 += w; \
607                         flags2 += flags_stride; \
608                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
609                         data2 += w; \
610                         flags2 += flags_stride; \
611                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
612                         data2 += w; \
613                         flags2 += flags_stride; \
614                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
615                         data2 += w; \
616                 } \
617                 data1 += w << 2; \
618                 flags1 += flags_stride << 2; \
619                 colflags1 += flags_stride; \
620         } \
621         for (i = 0; i < w; ++i) { \
622                 OPJ_INT32 *data2 = data1 + i; \
623                 opj_flag_t *flags2 = flags1 + i; \
624                 opj_colflag_t *colflags2 = colflags1 + i; \
625                 for (j = k; j < h; ++j) { \
626                         flags2 += flags_stride; \
627                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
628                         data2 += w; \
629                 } \
630         } \
631 }
632
633 static void opj_t1_dec_sigpass_mqc_64x64(
634                 opj_t1_t *t1,
635                 OPJ_INT32 bpno)
636 {
637         opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
638 }
639
640 static void opj_t1_dec_sigpass_mqc_generic(
641                 opj_t1_t *t1,
642                 OPJ_INT32 bpno)
643 {
644         opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
645 }
646
647 /* VSC and  BYPASS by Antonin */
648 static void opj_t1_dec_sigpass_mqc_vsc(
649                 opj_t1_t *t1,
650                 OPJ_INT32 bpno)
651 {
652         OPJ_INT32 one, half, oneplushalf, vsc;
653         OPJ_UINT32 i, j, k;
654         OPJ_INT32 *data1 = t1->data;
655         opj_flag_t *flags1 = &t1->flags[1];
656         opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
657         one = 1 << bpno;
658         half = one >> 1;
659         oneplushalf = one | half;
660         for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
661                 for (i = 0; i < t1->w; ++i) {
662                         OPJ_INT32 *data2 = data1 + i;
663                         opj_flag_t *flags2 = flags1 + i;
664                         opj_colflag_t *colflags2 = colflags1 + i;
665                         flags2 += t1->flags_stride;
666                         opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 0U);
667                         data2 += t1->w;
668                         flags2 += t1->flags_stride;
669                         opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 1U);
670                         data2 += t1->w;
671                         flags2 += t1->flags_stride;
672                         opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0, 2U);
673                         data2 += t1->w;
674                         flags2 += t1->flags_stride;
675                         opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1, 3U);
676                         data2 += t1->w;
677                 }
678                 data1 += t1->w << 2;
679                 flags1 += t1->flags_stride << 2;
680                 colflags1 += t1->flags_stride;
681         }
682         for (i = 0; i < t1->w; ++i) {
683                 opj_colflag_t *colflags2 = colflags1 + i;
684                 for (j = k; j < t1->h; ++j) {
685                         vsc = (j == t1->h - 1) ? 1 : 0;
686                         opj_t1_dec_sigpass_step_mqc_vsc(
687                                         t1,
688                                         &t1->flags[((j+1) * t1->flags_stride) + i + 1],
689                                         colflags2,
690                                         &t1->data[(j * t1->w) + i],
691                                         oneplushalf,
692                                         vsc,
693                                         j - k);
694                 }
695         }
696 }                               /* VSC and  BYPASS by Antonin */
697
698
699
700 static void opj_t1_enc_refpass_step(   opj_t1_t *t1,
701                                 opj_flag_t *flagsp,
702                                 OPJ_INT32 *datap,
703                                 OPJ_INT32 bpno,
704                                 OPJ_INT32 one,
705                                 OPJ_INT32 *nmsedec,
706                                 OPJ_BYTE type,
707                                 OPJ_UINT32 vsc)
708 {
709         OPJ_INT32 v;
710         OPJ_UINT32 flag;
711         
712         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
713         
714         flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
715         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
716                 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
717                 v = (opj_int_abs(*datap) & one) ? 1 : 0;
718                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
719                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
720                         opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
721                 } else {
722                         opj_mqc_encode(mqc, (OPJ_UINT32)v);
723                 }
724                 *flagsp |= T1_REFINE;
725         }
726 }
727
728 static INLINE void opj_t1_dec_refpass_step_raw(
729                 opj_t1_t *t1,
730                 opj_colflag_t *colflagsp,
731                 OPJ_INT32 *datap,
732                 OPJ_INT32 poshalf,
733                 OPJ_INT32 neghalf,
734                 OPJ_UINT32 row)
735 {
736         OPJ_INT32 v, t;
737        
738         opj_raw_t *raw = t1->raw;       /* RAW component */
739        
740         if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
741             ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
742                         v = (OPJ_INT32)opj_raw_decode(raw);
743                 t = v ? poshalf : neghalf;
744                 *datap += *datap < 0 ? -t : t;
745                 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
746         }
747 }                               /* VSC and  BYPASS by Antonin  */
748
749 static INLINE void opj_t1_dec_refpass_step_mqc(
750                 opj_t1_t *t1,
751 #ifdef CONSISTENCY_CHECK
752                 opj_flag_t *flagsp,
753 #else
754                 opj_flag_t *flagsp_unused,
755 #endif
756                 opj_colflag_t *colflagsp,
757                 OPJ_INT32 *datap,
758                 OPJ_INT32 poshalf,
759                 OPJ_INT32 neghalf,
760                 OPJ_UINT32 row)
761 {
762         OPJ_INT32 v, t;
763        
764         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
765 #ifdef CONSISTENCY_CHECK
766                 assert( ((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) == 
767                                 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) );
768 #else
769         (void)flagsp_unused;
770 #endif
771         if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
772             ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
773                 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
774                 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
775                 opj_mqc_setcurctx(mqc, tmp2);      /* ESSAI */
776                         v = opj_mqc_decode(mqc);
777                 t = v ? poshalf : neghalf;
778                 *datap += *datap < 0 ? -t : t;
779                 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
780                 }
781 }                               /* VSC and  BYPASS by Antonin  */
782
783 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
784                 opj_t1_t *t1,
785                 opj_flag_t *flagsp,
786                 opj_colflag_t *colflagsp,
787                 OPJ_INT32 *datap,
788                 OPJ_INT32 poshalf,
789                 OPJ_INT32 neghalf,
790                 OPJ_INT32 vsc,
791                 OPJ_UINT32 row)
792 {
793         OPJ_UINT32 v;
794         OPJ_INT32 t;
795         
796         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
797        
798         if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row))) ==
799                 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
800                 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
801                 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
802                 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
803                 opj_mqc_setcurctx(mqc, tmp2);      /* ESSAI */
804                 v = (OPJ_UINT32)opj_mqc_decode(mqc);
805                 t = v ? poshalf : neghalf;
806                 *datap += *datap < 0 ? -t : t;
807                 *colflagsp |= (opj_colflag_t)(T1_COLFLAG_REFINE_ROW_0 << (T1_COLFLAG_RBS * row));
808         }
809 }                               /* VSC and  BYPASS by Antonin  */
810
811
812 static void opj_t1_enc_refpass(
813                 opj_t1_t *t1,
814                 OPJ_INT32 bpno,
815                 OPJ_INT32 *nmsedec,
816                 OPJ_BYTE type,
817                 OPJ_UINT32 cblksty)
818 {
819         OPJ_UINT32 i, j, k, vsc;
820     OPJ_INT32 one;
821
822         *nmsedec = 0;
823         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
824         for (k = 0; k < t1->h; k += 4) {
825                 for (i = 0; i < t1->w; ++i) {
826                         for (j = k; j < k + 4 && j < t1->h; ++j) {
827                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
828                                 opj_t1_enc_refpass_step(
829                                                 t1,
830                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
831                                                 &t1->data[(j * t1->data_stride) + i],
832                                                 bpno,
833                                                 one,
834                                                 nmsedec,
835                                                 type,
836                                                 vsc);
837                         }
838                 }
839         }
840 }
841
842 static void opj_t1_dec_refpass_raw(
843                 opj_t1_t *t1,
844                 OPJ_INT32 bpno)
845 {
846         OPJ_INT32 one, poshalf, neghalf;
847         OPJ_UINT32 i, j, k;
848         opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
849         one = 1 << bpno;
850         poshalf = one >> 1;
851         neghalf = bpno > 0 ? -poshalf : -1;
852         for (k = 0; k < t1->h; k += 4) {
853                 for (i = 0; i < t1->w; ++i) {
854                         opj_colflag_t *colflags2 = colflags1 + i;
855                         for (j = k; j < k + 4 && j < t1->h; ++j) {
856                                 opj_t1_dec_refpass_step_raw(
857                                                 t1,
858                                                 colflags2,
859                                                 &t1->data[(j * t1->w) + i],
860                                                 poshalf,
861                                                 neghalf, j - k);
862                         }
863                 }
864                 colflags1 += t1->flags_stride;
865         }
866 }                               /* VSC and  BYPASS by Antonin */
867
868 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
869 { \
870         OPJ_INT32 one, poshalf, neghalf; \
871         OPJ_UINT32 i, j, k; \
872         OPJ_INT32 *data1 = t1->data; \
873         opj_flag_t *flags1 = &t1->flags[1]; \
874         opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
875         one = 1 << bpno; \
876         poshalf = one >> 1; \
877         neghalf = bpno > 0 ? -poshalf : -1; \
878         for (k = 0; k < (h & ~3u); k += 4) { \
879                 for (i = 0; i < w; ++i) { \
880                         OPJ_INT32 *data2 = data1 + i; \
881                         opj_flag_t *flags2 = flags1 + i; \
882                         opj_colflag_t *colflags2 = colflags1 + i; \
883                         if( *colflags2 == 0 ) continue; \
884                         flags2 += flags_stride; \
885                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
886                         data2 += w; \
887                         flags2 += flags_stride; \
888                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
889                         data2 += w; \
890                         flags2 += flags_stride; \
891                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
892                         data2 += w; \
893                         flags2 += flags_stride; \
894                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
895                         data2 += w; \
896                 } \
897                 data1 += w << 2; \
898                 flags1 += flags_stride << 2; \
899                 colflags1 += flags_stride; \
900         } \
901         for (i = 0; i < w; ++i) { \
902                 OPJ_INT32 *data2 = data1 + i; \
903                 opj_flag_t *flags2 = flags1 + i; \
904                 opj_colflag_t *colflags2 = colflags1 + i; \
905                 for (j = k; j < h; ++j) { \
906                         flags2 += flags_stride; \
907                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
908                         data2 += w; \
909                 } \
910         } \
911 }
912
913 static void opj_t1_dec_refpass_mqc_64x64(
914                 opj_t1_t *t1,
915                 OPJ_INT32 bpno)
916 {
917         opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
918 }
919
920 static void opj_t1_dec_refpass_mqc_generic(
921                 opj_t1_t *t1,
922                 OPJ_INT32 bpno)
923 {
924         opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
925 }
926
927 /* VSC and  BYPASS by Antonin */
928 static void opj_t1_dec_refpass_mqc_vsc(
929                 opj_t1_t *t1,
930                 OPJ_INT32 bpno)
931 {
932         OPJ_INT32 one, poshalf, neghalf;
933         OPJ_UINT32 i, j, k;
934         OPJ_INT32 vsc;
935         OPJ_INT32 *data1 = t1->data;
936         opj_flag_t *flags1 = &t1->flags[1];
937         opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
938         one = 1 << bpno;
939         poshalf = one >> 1;
940         neghalf = bpno > 0 ? -poshalf : -1;
941         for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
942                 for (i = 0; i < t1->w; ++i) {
943                         OPJ_INT32 *data2 = data1 + i;
944                         opj_flag_t *flags2 = flags1 + i;
945                         opj_colflag_t *colflags2 = colflags1 + i;
946                         flags2 += t1->flags_stride;
947                         opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 0U);
948                         data2 += t1->w;
949                         flags2 += t1->flags_stride;
950                         opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 1U);
951                         data2 += t1->w;
952                         flags2 += t1->flags_stride;
953                         opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 0, 2U);
954                         data2 += t1->w;
955                         flags2 += t1->flags_stride;
956                         opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf, 1, 3U);
957                         data2 += t1->w;
958                 }
959                 data1 += t1->w << 2;
960                 flags1 += t1->flags_stride << 2;
961                 colflags1 += t1->flags_stride;
962         }
963         for (i = 0; i < t1->w; ++i) {
964                 opj_colflag_t *colflags2 = colflags1 + i;
965                 for (j = k; j < t1->h; ++j) {
966                         vsc = (j == t1->h - 1) ? 1 : 0;
967                         opj_t1_dec_refpass_step_mqc_vsc(
968                                         t1,
969                                         &t1->flags[((j+1) * t1->flags_stride) + i + 1],
970                                         colflags2,
971                                         &t1->data[(j * t1->w) + i],
972                                         poshalf, neghalf,
973                                         vsc,
974                                         j - k);
975                 }
976         }
977 }                               /* VSC and  BYPASS by Antonin */
978
979
980 static void opj_t1_enc_clnpass_step(
981                 opj_t1_t *t1,
982                 opj_flag_t *flagsp,
983                 OPJ_INT32 *datap,
984                 OPJ_INT32 bpno,
985                 OPJ_INT32 one,
986                 OPJ_INT32 *nmsedec,
987                 OPJ_UINT32 partial,
988                 OPJ_UINT32 vsc)
989 {
990         OPJ_INT32 v;
991         OPJ_UINT32 flag;
992         
993         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
994         
995         flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
996         if (partial) {
997                 goto LABEL_PARTIAL;
998         }
999         if (!(*flagsp & (T1_SIG | T1_VISIT))) {
1000                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1001                 v = (opj_int_abs(*datap) & one) ? 1 : 0;
1002                 opj_mqc_encode(mqc, (OPJ_UINT32)v);
1003                 if (v) {
1004 LABEL_PARTIAL:
1005                         *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
1006                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1007                         v = *datap < 0 ? 1 : 0;
1008                         opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1009                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1010                 }
1011         }
1012         *flagsp &= (opj_flag_t)~T1_VISIT;
1013 }
1014
1015 static void opj_t1_dec_clnpass_step_partial(
1016                 opj_t1_t *t1,
1017                 opj_flag_t *flagsp,
1018                 opj_colflag_t *colflagsp,
1019                 OPJ_INT32 *datap,
1020                 OPJ_INT32 oneplushalf,
1021                 OPJ_UINT32 row)
1022 {
1023         OPJ_INT32 v, flag;
1024         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1025         
1026         flag = *flagsp;
1027         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1028         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1029         *datap = v ? -oneplushalf : oneplushalf;
1030         opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1031 #ifdef CONSISTENCY_CHECK
1032         *flagsp &= ~T1_VISIT;
1033 #endif
1034 }                               /* VSC and  BYPASS by Antonin */
1035
1036 static void opj_t1_dec_clnpass_step(
1037                 opj_t1_t *t1,
1038                 opj_flag_t *flagsp,
1039                 opj_colflag_t *colflagsp,
1040                 OPJ_INT32 *datap,
1041                 OPJ_INT32 oneplushalf,
1042                 OPJ_UINT32 row)
1043 {
1044         OPJ_INT32 v, flag;
1045         
1046         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1047 #ifdef CONSISTENCY_CHECK
1048         assert( (!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) );
1049 #endif
1050         if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4*row)))) {
1051                 flag = *flagsp;
1052                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1053                 if (opj_mqc_decode(mqc)) {
1054                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1055                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1056                         *datap = v ? -oneplushalf : oneplushalf;
1057                         opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride, row);
1058                 }
1059         }
1060 #ifdef CONSISTENCY_CHECK
1061         *flagsp &= ~T1_VISIT;
1062 #endif
1063 }                               /* VSC and  BYPASS by Antonin */
1064
1065 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1066         opj_t1_t *t1,
1067         opj_flag_t *flagsp,
1068         opj_colflag_t *colflagsp,
1069         OPJ_INT32 *datap,
1070         OPJ_INT32 oneplushalf,
1071         OPJ_UINT32 row,
1072         OPJ_UINT32 flags_stride)
1073 {
1074     OPJ_INT32 v;
1075     OPJ_INT32 flag;
1076
1077     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1078
1079     flag = *flagsp;
1080     /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1081     {
1082         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1083         if (opj_mqc_decode(mqc)) {
1084             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1085             v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1086             *datap = v ? -oneplushalf : oneplushalf;
1087             opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1088         }
1089     }
1090     /*flagsp &= ~T1_VISIT;*/
1091 }
1092
1093 static void opj_t1_dec_clnpass_step_vsc(
1094                 opj_t1_t *t1,
1095                 opj_flag_t *flagsp,
1096                 opj_colflag_t *colflagsp,
1097                 OPJ_INT32 *datap,
1098                 OPJ_INT32 oneplushalf,
1099                 OPJ_INT32 partial,
1100                 OPJ_INT32 vsc,
1101                 OPJ_UINT32 row)
1102 {
1103         OPJ_UINT32 v, flag;
1104         
1105         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1106         
1107         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1108         if (partial) {
1109                 goto LABEL_PARTIAL;
1110         }
1111         if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (T1_COLFLAG_RBS * row)))) {
1112                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1113                 if (opj_mqc_decode(mqc)) {
1114 LABEL_PARTIAL:
1115                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1116                         v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1117                         *datap = v ? -oneplushalf : oneplushalf;
1118                         opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1119                 }
1120         }
1121 #ifdef CONSISTENCY_CHECK
1122         *flagsp &= ~T1_VISIT;
1123 #endif
1124 }
1125
1126 static void opj_t1_enc_clnpass(
1127                 opj_t1_t *t1,
1128                 OPJ_INT32 bpno,
1129                 OPJ_INT32 *nmsedec,
1130                 OPJ_UINT32 cblksty)
1131 {
1132         OPJ_UINT32 i, j, k;
1133         OPJ_INT32 one;
1134         OPJ_UINT32 agg, runlen, vsc;
1135         
1136         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1137         
1138         *nmsedec = 0;
1139         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1140         for (k = 0; k < t1->h; k += 4) {
1141                 for (i = 0; i < t1->w; ++i) {
1142                         if (k + 3 < t1->h) {
1143                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1144                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1145                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1146                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1147                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
1148                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1149                                 } else {
1150                                         agg = !((MACRO_t1_flags(1 + k,1 + i) | 
1151                                                     MACRO_t1_flags(1 + k + 1,1 + i) |
1152                                                     MACRO_t1_flags(1 + k + 2,1 + i) | 
1153                                                      MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1154                                 }
1155                         } else {
1156                                 agg = 0;
1157                         }
1158                         if (agg) {
1159                                 for (runlen = 0; runlen < 4; ++runlen) {
1160                                         if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1161                                                 break;
1162                                 }
1163                                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1164                                 opj_mqc_encode(mqc, runlen != 4);
1165                                 if (runlen == 4) {
1166                                         continue;
1167                                 }
1168                                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1169                                 opj_mqc_encode(mqc, runlen >> 1);
1170                                 opj_mqc_encode(mqc, runlen & 1);
1171                         } else {
1172                                 runlen = 0;
1173                         }
1174                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1175                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1176                                 opj_t1_enc_clnpass_step(
1177                                                 t1,
1178                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1179                                                 &t1->data[(j * t1->data_stride) + i],
1180                                                 bpno,
1181                                                 one,
1182                                                 nmsedec,
1183                                                 agg && (j == k + runlen),
1184                                                 vsc);
1185                         }
1186                 }
1187         }
1188 }
1189
1190 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1191
1192 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1193 { \
1194         OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1195         OPJ_UINT32 i, j, k; \
1196         OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1197          \
1198         opj_mqc_t *mqc = t1->mqc;       /* MQC component */ \
1199          \
1200         one = 1 << bpno; \
1201         half = one >> 1; \
1202         oneplushalf = one | half; \
1203         if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1204         opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1205         for (k = 0; k < h; k += 4) { \
1206                 for (i = 0; i < w; ++i) { \
1207                         opj_colflag_t *colflags2 = colflags1 + i; \
1208                         if (k + 3 < h) { \
1209                                         agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1210                                                                    T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1211                                                                    T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1212                                                                    T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1213                                                   ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1214                                                    & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |     T1_SGN_S))) & (T1_SIG_OTH)))); \
1215                                 } else { \
1216                                 agg = 0; \
1217                         } \
1218                         if (agg) { \
1219                                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1220                                 if (!opj_mqc_decode(mqc)) { \
1221                                         continue; \
1222                                 } \
1223                                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1224                                 runlen = opj_mqc_decode(mqc); \
1225                                 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1226                         } else { \
1227                                 runlen = 0; \
1228                         } \
1229                         for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1230                                         vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1231                                         opj_t1_dec_clnpass_step_vsc( \
1232                                                 t1, \
1233                                                 &t1->flags[((j+1) * flags_stride) + i + 1], \
1234                                                 colflags2, \
1235                                                 &t1->data[(j * w) + i], \
1236                                                 oneplushalf, \
1237                                                 agg && (j == k + (OPJ_UINT32)runlen), \
1238                                                 vsc, j - k); \
1239                         } \
1240                         *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1241                 } \
1242                 colflags1 += flags_stride; \
1243         } \
1244         } else { \
1245                 OPJ_INT32 *data1 = t1->data; \
1246                 opj_flag_t *flags1 = &t1->flags[1]; \
1247                 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1248                 for (k = 0; k < (h & ~3u); k += 4) { \
1249                         for (i = 0; i < w; ++i) { \
1250                                 OPJ_INT32 *data2 = data1 + i; \
1251                                 opj_flag_t *flags2 = flags1 + i; \
1252                                 opj_colflag_t *colflags2 = colflags1 + i; \
1253                                 opj_colflag_t colflags = *colflags2; \
1254                                 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1255                                                                          T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1256                                                                          T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1257                                                                          T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1258                                 if( consistency_check ) { \
1259                                         assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1260                                                                           MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1261                                                                           MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1262                                                                           MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1263                                 } \
1264                                 if (agg) { \
1265                                         opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1266                                         if (!opj_mqc_decode(mqc)) { \
1267                                                 continue; \
1268                                         } \
1269                                         opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1270                                         runlen = opj_mqc_decode(mqc); \
1271                                         runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1272                                         flags2 += (OPJ_UINT32)runlen * flags_stride; \
1273                                         data2 += (OPJ_UINT32)runlen * w; \
1274                                         for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1275                                                 flags2 += flags_stride; \
1276                                                 if (j == (OPJ_UINT32)runlen) { \
1277                                                         opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1278                                                 } else { \
1279                                                         opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1280                                                 } \
1281                                                 data2 += w; \
1282                                         } \
1283                                 } else { \
1284                                         flags2 += flags_stride; \
1285                                         if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1286                                         if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1287                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1288                                         } \
1289                                         if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1290                                         data2 += w; \
1291                                         flags2 += flags_stride; \
1292                                         if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1293                                         if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1294                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1295                                         } \
1296                                         if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1297                                         data2 += w; \
1298                                         flags2 += flags_stride; \
1299                                         if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1300                                         if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1301                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1302                                         } \
1303                                         if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1304                                         data2 += w; \
1305                                         flags2 += flags_stride; \
1306                                         if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1307                                         if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1308                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1309                                         } \
1310                                         if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1311                                         data2 += w; \
1312                                 } \
1313                                 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1314                         } \
1315                         data1 += w << 2; \
1316                         flags1 += flags_stride << 2; \
1317                         colflags1 += flags_stride; \
1318                 } \
1319                 for (i = 0; i < w; ++i) { \
1320                         OPJ_INT32 *data2 = data1 + i; \
1321                         opj_flag_t *flags2 = flags1 + i; \
1322                         opj_colflag_t *colflags2 = colflags1 + i; \
1323                         for (j = k; j < h; ++j) { \
1324                                 flags2 += flags_stride; \
1325                                 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1326                                 data2 += w; \
1327                         } \
1328                         *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1329                 } \
1330         } \
1331  \
1332         if (segsym) { \
1333                 OPJ_INT32 v = 0; \
1334                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1335                 v = opj_mqc_decode(mqc); \
1336                 v = (v << 1) | opj_mqc_decode(mqc); \
1337                 v = (v << 1) | opj_mqc_decode(mqc); \
1338                 v = (v << 1) | opj_mqc_decode(mqc); \
1339                 /* \
1340                 if (v!=0xa) { \
1341                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1342                 } \
1343                 */ \
1344         } \
1345 }                               /* VSC and  BYPASS by Antonin */
1346
1347 static void opj_t1_dec_clnpass_64x64(
1348                 opj_t1_t *t1,
1349                 OPJ_INT32 bpno,
1350                 OPJ_INT32 cblksty)
1351 {
1352 #ifdef CONSISTENCY_CHECK
1353         opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1354 #else
1355         opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1356 #endif
1357 }
1358
1359 static void opj_t1_dec_clnpass_generic(
1360                 opj_t1_t *t1,
1361                 OPJ_INT32 bpno,
1362                 OPJ_INT32 cblksty)
1363 {
1364 #ifdef CONSISTENCY_CHECK
1365         opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1366 #else
1367         opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h, t1->flags_stride);
1368 #endif
1369 }
1370
1371
1372 /** mod fixed_quality */
1373 static OPJ_FLOAT64 opj_t1_getwmsedec(
1374                 OPJ_INT32 nmsedec,
1375                 OPJ_UINT32 compno,
1376                 OPJ_UINT32 level,
1377                 OPJ_UINT32 orient,
1378                 OPJ_INT32 bpno,
1379                 OPJ_UINT32 qmfbid,
1380                 OPJ_FLOAT64 stepsize,
1381                 OPJ_UINT32 numcomps,
1382                 const OPJ_FLOAT64 * mct_norms,
1383                 OPJ_UINT32 mct_numcomps)
1384 {
1385         OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1386         OPJ_ARG_NOT_USED(numcomps);
1387
1388         if (mct_norms && (compno < mct_numcomps)) {
1389                 w1 = mct_norms[compno];
1390         }
1391
1392         if (qmfbid == 1) {
1393                 w2 = opj_dwt_getnorm(level, orient);
1394         } else {        /* if (qmfbid == 0) */
1395                 w2 = opj_dwt_getnorm_real(level, orient);
1396         }
1397
1398         wmsedec = w1 * w2 * stepsize * (1 << bpno);
1399         wmsedec *= wmsedec * nmsedec / 8192.0;
1400
1401         return wmsedec;
1402 }
1403
1404 static OPJ_BOOL opj_t1_allocate_buffers(
1405                 opj_t1_t *t1,
1406                 OPJ_UINT32 w,
1407                 OPJ_UINT32 h)
1408 {
1409         OPJ_UINT32 datasize=w * h;
1410         OPJ_UINT32 flagssize;
1411
1412         /* encoder uses tile buffer, so no need to allocate */
1413         if (!t1->encoder) {
1414                 if(datasize > t1->datasize){
1415                         opj_aligned_free(t1->data);
1416                         t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1417                         if(!t1->data){
1418                                 /* FIXME event manager error callback */
1419                                 return OPJ_FALSE;
1420                         }
1421                         t1->datasize=datasize;
1422                 }
1423                 /* memset first arg is declared to never be null by gcc */
1424                 if (t1->data != NULL) {
1425                         memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1426                 }
1427         }
1428         t1->flags_stride=w+2;
1429         flagssize=t1->flags_stride * (h+2);
1430
1431         if(flagssize > t1->flagssize){
1432                 opj_aligned_free(t1->flags);
1433                 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1434                 if(!t1->flags){
1435                         /* FIXME event manager error callback */
1436                         return OPJ_FALSE;
1437                 }
1438                 t1->flagssize=flagssize;
1439         }
1440         memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1441         
1442         if (!t1->encoder) {
1443                 OPJ_UINT32 colflags_size=t1->flags_stride * ((h+3) / 4 + 2);
1444
1445                 if(colflags_size > t1->colflags_size){
1446                         opj_aligned_free(t1->colflags);
1447                         t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(opj_colflag_t));
1448                         if(!t1->colflags){
1449                                 /* FIXME event manager error callback */
1450                                 return OPJ_FALSE;
1451                         }
1452                         t1->colflags_size=colflags_size;
1453                 }
1454                 memset(t1->colflags,0,colflags_size * sizeof(opj_colflag_t));
1455         }
1456
1457         t1->w=w;
1458         t1->h=h;
1459
1460         return OPJ_TRUE;
1461 }
1462
1463 /* ----------------------------------------------------------------------- */
1464
1465 /* ----------------------------------------------------------------------- */
1466 /**
1467  * Creates a new Tier 1 handle
1468  * and initializes the look-up tables of the Tier-1 coder/decoder
1469  * @return a new T1 handle if successful, returns NULL otherwise
1470 */
1471 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1472 {
1473         opj_t1_t *l_t1 = 00;
1474
1475         l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1476         if (!l_t1) {
1477                 return 00;
1478         }
1479
1480         /* create MQC and RAW handles */
1481         l_t1->mqc = opj_mqc_create();
1482         if (! l_t1->mqc) {
1483                 opj_t1_destroy(l_t1);
1484                 return 00;
1485         }
1486
1487         l_t1->raw = opj_raw_create();
1488         if (! l_t1->raw) {
1489                 opj_t1_destroy(l_t1);
1490                 return 00;
1491         }
1492         l_t1->encoder = isEncoder;
1493
1494         return l_t1;
1495 }
1496
1497
1498 /**
1499  * Destroys a previously created T1 handle
1500  *
1501  * @param p_t1 Tier 1 handle to destroy
1502 */
1503 void opj_t1_destroy(opj_t1_t *p_t1)
1504 {
1505         if (! p_t1) {
1506                 return;
1507         }
1508
1509         /* destroy MQC and RAW handles */
1510         opj_mqc_destroy(p_t1->mqc);
1511         p_t1->mqc = 00;
1512         opj_raw_destroy(p_t1->raw);
1513         p_t1->raw = 00;
1514         
1515         /* encoder uses tile buffer, so no need to free */
1516         if (!p_t1->encoder && p_t1->data) {
1517                 opj_aligned_free(p_t1->data);
1518                 p_t1->data = 00;
1519         }
1520
1521         if (p_t1->flags) {
1522                 opj_aligned_free(p_t1->flags);
1523                 p_t1->flags = 00;
1524         }
1525
1526         if (p_t1->colflags) {
1527                 opj_aligned_free(p_t1->colflags);
1528                 p_t1->colflags = 00;
1529         }
1530         opj_free(p_t1);
1531 }
1532
1533 typedef struct
1534 {
1535     OPJ_UINT32 resno;
1536     opj_tcd_cblk_dec_t* cblk;
1537     opj_tcd_band_t* band;
1538     opj_tcd_tilecomp_t* tilec;
1539     opj_tccp_t* tccp;
1540     volatile OPJ_BOOL* pret;
1541 } opj_t1_cblk_decode_processing_job_t;
1542
1543 static void opj_t1_destroy_wrapper(void* t1)
1544 {
1545     opj_t1_destroy( (opj_t1_t*) t1 );
1546 }
1547
1548 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1549 {
1550     opj_tcd_cblk_dec_t* cblk;
1551     opj_tcd_band_t* band;
1552     opj_tcd_tilecomp_t* tilec;
1553     opj_tccp_t* tccp;
1554     OPJ_INT32* OPJ_RESTRICT datap;
1555     OPJ_UINT32 cblk_w, cblk_h;
1556     OPJ_INT32 x, y;
1557     OPJ_UINT32 i, j;
1558     opj_t1_cblk_decode_processing_job_t* job;
1559     opj_t1_t* t1;
1560     OPJ_UINT32 resno;
1561     OPJ_UINT32 tile_w;
1562
1563     job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1564     resno = job->resno;
1565     cblk = job->cblk;
1566     band = job->band;
1567     tilec = job->tilec;
1568     tccp = job->tccp;
1569     tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1570
1571     if( !*(job->pret) )
1572     {
1573         opj_free(job);
1574         return;
1575     }
1576
1577     t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1578     if( t1 == NULL )
1579     {
1580         t1 = opj_t1_create( OPJ_FALSE );
1581         opj_tls_set( tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper );
1582     }
1583
1584     if (OPJ_FALSE == opj_t1_decode_cblk(
1585                             t1,
1586                             cblk,
1587                             band->bandno,
1588                             (OPJ_UINT32)tccp->roishift,
1589                             tccp->cblksty)) {
1590             *(job->pret) = OPJ_FALSE;
1591             opj_free(job);
1592             return;
1593     }
1594
1595     x = cblk->x0 - band->x0;
1596     y = cblk->y0 - band->y0;
1597     if (band->bandno & 1) {
1598         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1599         x += pres->x1 - pres->x0;
1600     }
1601     if (band->bandno & 2) {
1602         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1603         y += pres->y1 - pres->y0;
1604     }
1605
1606     datap=t1->data;
1607     cblk_w = t1->w;
1608     cblk_h = t1->h;
1609
1610     if (tccp->roishift) {
1611         OPJ_INT32 thresh = 1 << tccp->roishift;
1612         for (j = 0; j < cblk_h; ++j) {
1613             for (i = 0; i < cblk_w; ++i) {
1614                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1615                 OPJ_INT32 mag = abs(val);
1616                 if (mag >= thresh) {
1617                     mag >>= tccp->roishift;
1618                     datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1619                 }
1620             }
1621         }
1622     }
1623     if (tccp->qmfbid == 1) {
1624         OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1625         for (j = 0; j < cblk_h; ++j) {
1626             i = 0;
1627             for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1628                 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1629                 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1630                 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1631                 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1632                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1633                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1634                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1635                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1636             }
1637             for (; i < cblk_w; ++i) {
1638                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1639                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1640             }
1641         }
1642     } else {        /* if (tccp->qmfbid == 0) */
1643         OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1644         for (j = 0; j < cblk_h; ++j) {
1645             OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1646             for (i = 0; i < cblk_w; ++i) {
1647                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1648                 *tiledp2 = tmp;
1649                 datap++;
1650                 tiledp2++;
1651             }
1652             tiledp += tile_w;
1653         }
1654     }
1655
1656     opj_free(job);
1657 }
1658
1659
1660 void opj_t1_decode_cblks( opj_thread_pool_t* tp,
1661                           volatile OPJ_BOOL* pret,
1662                           opj_tcd_tilecomp_t* tilec,
1663                           opj_tccp_t* tccp
1664                          )
1665 {
1666         OPJ_UINT32 resno, bandno, precno, cblkno;
1667
1668         for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1669                 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1670
1671                 for (bandno = 0; bandno < res->numbands; ++bandno) {
1672                         opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1673
1674                         for (precno = 0; precno < res->pw * res->ph; ++precno) {
1675                                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1676
1677                                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1678                                         opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1679                     opj_t1_cblk_decode_processing_job_t* job;
1680
1681                     job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1, sizeof(opj_t1_cblk_decode_processing_job_t));
1682                     if( !job )
1683                     {
1684                         *pret = OPJ_FALSE;
1685                         return;
1686                     }
1687                     job->resno = resno;
1688                     job->cblk = cblk;
1689                     job->band = band;
1690                     job->tilec = tilec;
1691                     job->tccp = tccp;
1692                     job->pret = pret;
1693                     opj_thread_pool_submit_job( tp, opj_t1_clbl_decode_processor, job );
1694                     if( !(*pret) )
1695                         return;
1696                                 } /* cblkno */
1697                         } /* precno */
1698                 } /* bandno */
1699         } /* resno */
1700
1701     return;
1702 }
1703
1704
1705 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1706                             opj_tcd_cblk_dec_t* cblk,
1707                             OPJ_UINT32 orient,
1708                             OPJ_UINT32 roishift,
1709                             OPJ_UINT32 cblksty)
1710 {
1711         opj_raw_t *raw = t1->raw;       /* RAW component */
1712         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1713         
1714         OPJ_INT32 bpno_plus_one;
1715         OPJ_UINT32 passtype;
1716         OPJ_UINT32 segno, passno;
1717         OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1718
1719         mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1720
1721         if(!opj_t1_allocate_buffers(
1722                                 t1,
1723                                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1724                                 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1725         {
1726                 return OPJ_FALSE;
1727         }
1728
1729         bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1730         passtype = 2;
1731
1732         opj_mqc_resetstates(mqc);
1733         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1734         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1735         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1736
1737         for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1738                 opj_tcd_seg_t *seg = &cblk->segs[segno];
1739
1740                 /* BYPASS mode */
1741                 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1742                 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1743                 if(seg->data == 00){
1744                         continue;
1745                 }
1746                 if (type == T1_TYPE_RAW) {
1747                         opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1748                 } else {
1749             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1750                     return OPJ_FALSE;
1751             }
1752                 }
1753
1754                 if( t1->w == 64 && t1->h == 64 )
1755                 {
1756                   for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1757                           switch (passtype) {
1758                                   case 0:
1759                                           if (type == T1_TYPE_RAW) {
1760                                                   opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1761                                           } else {
1762                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1763                                                           opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1764                                                   } else {
1765                                                           opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1766                                                   }
1767                                           }
1768                                           break;
1769                                   case 1:
1770                                           if (type == T1_TYPE_RAW) {
1771                                                           opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1772                                           } else {
1773                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1774                                                           opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1775                                                   } else {
1776                                                           opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1777                                                   }
1778                                           }
1779                                           break;
1780                                   case 2:
1781                                           opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1782                                           break;
1783                           }
1784
1785                           if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1786                                   opj_mqc_resetstates(mqc);
1787                                   opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1788                                   opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1789                                   opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1790                           }
1791                           if (++passtype == 3) {
1792                                   passtype = 0;
1793                                   bpno_plus_one--;
1794                           }
1795                   }
1796                 }
1797                 else
1798                 {
1799                   for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1800                           switch (passtype) {
1801                                   case 0:
1802                                           if (type == T1_TYPE_RAW) {
1803                                                   opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1804                                           } else {
1805                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1806                                                           opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1807                                                   } else {
1808                                                           opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1809                                                   }
1810                                           }
1811                                           break;
1812                                   case 1:
1813                                           if (type == T1_TYPE_RAW) {
1814                                                           opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1815                                           } else {
1816                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1817                                                           opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1818                                                   } else {
1819                                                           opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1820                                                   }
1821                                           }
1822                                           break;
1823                                   case 2:
1824                                           opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1825                                           break;
1826                           }
1827
1828                           if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1829                                   opj_mqc_resetstates(mqc);
1830                                   opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1831                                   opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1832                                   opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1833                           }
1834                           if (++passtype == 3) {
1835                                   passtype = 0;
1836                                   bpno_plus_one--;
1837                           }
1838                   }
1839                 }
1840         }
1841     return OPJ_TRUE;
1842 }
1843
1844
1845
1846
1847 OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
1848                                 opj_tcd_tile_t *tile,
1849                                 opj_tcp_t *tcp,
1850                                 const OPJ_FLOAT64 * mct_norms,
1851                                 OPJ_UINT32 mct_numcomps
1852                                 )
1853 {
1854         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1855
1856         tile->distotile = 0;            /* fixed_quality */
1857
1858         for (compno = 0; compno < tile->numcomps; ++compno) {
1859                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1860                 opj_tccp_t* tccp = &tcp->tccps[compno];
1861                 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1862
1863                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1864                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1865
1866                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1867                                 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1868                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1869
1870                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1871                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1872
1873                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1874                                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1875                                                 OPJ_INT32* OPJ_RESTRICT tiledp;
1876                                                 OPJ_UINT32 cblk_w;
1877                                                 OPJ_UINT32 cblk_h;
1878                                                 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1879
1880                                                 OPJ_INT32 x = cblk->x0 - band->x0;
1881                                                 OPJ_INT32 y = cblk->y0 - band->y0;
1882                                                 if (band->bandno & 1) {
1883                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1884                                                         x += pres->x1 - pres->x0;
1885                                                 }
1886                                                 if (band->bandno & 2) {
1887                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1888                                                         y += pres->y1 - pres->y0;
1889                                                 }
1890
1891                                                 if(!opj_t1_allocate_buffers(
1892                                                                         t1,
1893                                                                         (OPJ_UINT32)(cblk->x1 - cblk->x0),
1894                                                                         (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1895                                                 {
1896                                                         return OPJ_FALSE;
1897                                                 }
1898
1899                                                 cblk_w = t1->w;
1900                                                 cblk_h = t1->h;
1901                                                 tileLineAdvance = tile_w - cblk_w;
1902
1903                                                 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1904                                                 t1->data = tiledp;
1905                                                 t1->data_stride = tile_w;
1906                                                 if (tccp->qmfbid == 1) {
1907                                                         for (j = 0; j < cblk_h; ++j) {
1908                                                                 for (i = 0; i < cblk_w; ++i) {
1909                                                                         tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1910                                                                         tileIndex++;
1911                                                                 }
1912                                                                 tileIndex += tileLineAdvance;
1913                                                         }
1914                                                 } else {                /* if (tccp->qmfbid == 0) */
1915                                                         for (j = 0; j < cblk_h; ++j) {
1916                                                                 for (i = 0; i < cblk_w; ++i) {
1917                                                                         OPJ_INT32 tmp = tiledp[tileIndex];
1918                                                                         tiledp[tileIndex] =
1919                                                                                 opj_int_fix_mul_t1(
1920                                                                                 tmp,
1921                                                                                 bandconst);
1922                                                                         tileIndex++;
1923                                                                 }
1924                                                                 tileIndex += tileLineAdvance;
1925                                                         }
1926                                                 }
1927
1928                                                 opj_t1_encode_cblk(
1929                                                                 t1,
1930                                                                 cblk,
1931                                                                 band->bandno,
1932                                                                 compno,
1933                                                                 tilec->numresolutions - 1 - resno,
1934                                                                 tccp->qmfbid,
1935                                                                 band->stepsize,
1936                                                                 tccp->cblksty,
1937                                                                 tile->numcomps,
1938                                                                 tile,
1939                                                                 mct_norms,
1940                                                                 mct_numcomps);
1941
1942                                         } /* cblkno */
1943                                 } /* precno */
1944                         } /* bandno */
1945                 } /* resno  */
1946         } /* compno  */
1947         return OPJ_TRUE;
1948 }
1949
1950 /** mod fixed_quality */
1951 static void opj_t1_encode_cblk(opj_t1_t *t1,
1952                         opj_tcd_cblk_enc_t* cblk,
1953                         OPJ_UINT32 orient,
1954                         OPJ_UINT32 compno,
1955                         OPJ_UINT32 level,
1956                         OPJ_UINT32 qmfbid,
1957                         OPJ_FLOAT64 stepsize,
1958                         OPJ_UINT32 cblksty,
1959                         OPJ_UINT32 numcomps,
1960                         opj_tcd_tile_t * tile,
1961                         const OPJ_FLOAT64 * mct_norms,
1962                         OPJ_UINT32 mct_numcomps)
1963 {
1964         OPJ_FLOAT64 cumwmsedec = 0.0;
1965
1966         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1967
1968         OPJ_UINT32 passno;
1969         OPJ_INT32 bpno;
1970         OPJ_UINT32 passtype;
1971         OPJ_INT32 nmsedec = 0;
1972         OPJ_INT32 max;
1973         OPJ_UINT32 i, j;
1974         OPJ_BYTE type = T1_TYPE_MQ;
1975         OPJ_FLOAT64 tempwmsedec;
1976
1977         mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1978
1979         max = 0;
1980         for (i = 0; i < t1->w; ++i) {
1981                 for (j = 0; j < t1->h; ++j) {
1982                         OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1983                         max = opj_int_max(max, tmp);
1984                 }
1985         }
1986
1987         cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1988
1989         bpno = (OPJ_INT32)(cblk->numbps - 1);
1990         passtype = 2;
1991
1992         opj_mqc_resetstates(mqc);
1993         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1994         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1995         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1996         opj_mqc_init_enc(mqc, cblk->data);
1997
1998         for (passno = 0; bpno >= 0; ++passno) {
1999                 opj_tcd_pass_t *pass = &cblk->passes[passno];
2000                 OPJ_UINT32 correction = 3;
2001                 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2002
2003                 switch (passtype) {
2004                         case 0:
2005                                 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2006                                 break;
2007                         case 1:
2008                                 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2009                                 break;
2010                         case 2:
2011                                 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2012                                 /* code switch SEGMARK (i.e. SEGSYM) */
2013                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
2014                                         opj_mqc_segmark_enc(mqc);
2015                                 break;
2016                 }
2017
2018                 /* fixed_quality */
2019                 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
2020                 cumwmsedec += tempwmsedec;
2021                 tile->distotile += tempwmsedec;
2022
2023                 /* Code switch "RESTART" (i.e. TERMALL) */
2024                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
2025                         if (type == T1_TYPE_RAW) {
2026                                 opj_mqc_flush(mqc);
2027                                 correction = 1;
2028                                 /* correction = mqc_bypass_flush_enc(); */
2029                         } else {                        /* correction = mqc_restart_enc(); */
2030                                 opj_mqc_flush(mqc);
2031                                 correction = 1;
2032                         }
2033                         pass->term = 1;
2034                 } else {
2035                         if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
2036                                 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2037                                 if (type == T1_TYPE_RAW) {
2038                                         opj_mqc_flush(mqc);
2039                                         correction = 1;
2040                                         /* correction = mqc_bypass_flush_enc(); */
2041                                 } else {                /* correction = mqc_restart_enc(); */
2042                                         opj_mqc_flush(mqc);
2043                                         correction = 1;
2044                                 }
2045                                 pass->term = 1;
2046                         } else {
2047                                 pass->term = 0;
2048                         }
2049                 }
2050
2051                 if (++passtype == 3) {
2052                         passtype = 0;
2053                         bpno--;
2054                 }
2055
2056                 if (pass->term && bpno > 0) {
2057                         type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2058                         if (type == T1_TYPE_RAW)
2059                                 opj_mqc_bypass_init_enc(mqc);
2060                         else
2061                                 opj_mqc_restart_init_enc(mqc);
2062                 }
2063
2064                 pass->distortiondec = cumwmsedec;
2065                 pass->rate = opj_mqc_numbytes(mqc) + correction;        /* FIXME */
2066
2067                 /* Code-switch "RESET" */
2068                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2069                         opj_mqc_reset_enc(mqc);
2070         }
2071
2072         /* Code switch "ERTERM" (i.e. PTERM) */
2073         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2074                 opj_mqc_erterm_enc(mqc);
2075         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2076                 opj_mqc_flush(mqc);
2077
2078         cblk->totalpasses = passno;
2079
2080         for (passno = 0; passno<cblk->totalpasses; passno++) {
2081                 opj_tcd_pass_t *pass = &cblk->passes[passno];
2082                 if (pass->rate > opj_mqc_numbytes(mqc))
2083                         pass->rate = opj_mqc_numbytes(mqc);
2084                 /*Preventing generation of FF as last data byte of a pass*/
2085                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2086                         pass->rate--;
2087                 }
2088                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2089         }
2090 }
2091
2092 #if 0
2093 static void opj_t1_dec_refpass_step(   opj_t1_t *t1,
2094                                 opj_flag_t *flagsp,
2095                                 OPJ_INT32 *datap,
2096                                 OPJ_INT32 poshalf,
2097                                 OPJ_INT32 neghalf,
2098                                 OPJ_BYTE type,
2099                                 OPJ_UINT32 vsc)
2100 {
2101         OPJ_INT32  t;
2102         OPJ_UINT32 v,flag;
2103
2104         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
2105         opj_raw_t *raw = t1->raw;       /* RAW component */
2106
2107         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2108         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2109                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
2110                 if (type == T1_TYPE_RAW) {
2111                         v = opj_raw_decode(raw);
2112                 } else {
2113                         v = opj_mqc_decode(mqc);
2114                 }
2115                 t = v ? poshalf : neghalf;
2116                 *datap += *datap < 0 ? -t : t;
2117                 *flagsp |= T1_REFINE;
2118         }
2119 }                               /* VSC and  BYPASS by Antonin  */
2120 #endif
2121
2122
2123
2124 #if 0
2125 static void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
2126                                 opj_flag_t *flagsp,
2127                                 OPJ_INT32 *datap,
2128                                 OPJ_UINT32 orient,
2129                                 OPJ_INT32 oneplushalf,
2130                                 OPJ_BYTE type,
2131                                 OPJ_UINT32 vsc)
2132 {
2133         OPJ_UINT32 v, flag;
2134
2135         opj_raw_t *raw = t1->raw;       /* RAW component */
2136         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
2137
2138         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2139         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2140                 if (type == T1_TYPE_RAW) {
2141                         if (opj_raw_decode(raw)) {
2142                                 v = opj_raw_decode(raw);        /* ESSAI */
2143                                 *datap = v ? -oneplushalf : oneplushalf;
2144                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2145                         }
2146                 } else {
2147                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2148                         if (opj_mqc_decode(mqc)) {
2149                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2150                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2151                                 *datap = v ? -oneplushalf : oneplushalf;
2152                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2153                         }
2154                 }
2155                 *flagsp |= T1_VISIT;
2156         }
2157 }                               /* VSC and  BYPASS by Antonin */
2158 #endif