Merge pull request #834 from trylab/issue833
[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         /* encoder uses tile buffer, so no need to allocate */
1410         if (!t1->encoder) {
1411                 size_t datasize;
1412                 
1413 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1414                 /* Overflow check */
1415                 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1416                         /* FIXME event manager error callback */
1417                         return OPJ_FALSE;
1418                 }
1419 #endif
1420                 datasize = (size_t)w * h;
1421
1422                 /* Overflow check */
1423                 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1424                         /* FIXME event manager error callback */
1425                         return OPJ_FALSE;
1426                 }
1427
1428                 if(datasize > (size_t)t1->datasize){
1429                         opj_aligned_free(t1->data);
1430                         t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1431                         if(!t1->data){
1432                                 /* FIXME event manager error callback */
1433                                 return OPJ_FALSE;
1434                         }
1435 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1436                         /* TODO remove this if t1->datasize type changes to size_t */
1437                         /* Overflow check */
1438                         if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1439                                 /* FIXME event manager error callback */
1440                                 return OPJ_FALSE;
1441                         }
1442 #endif
1443                         t1->datasize = (OPJ_UINT32)datasize;
1444                 }
1445                 /* memset first arg is declared to never be null by gcc */
1446                 if (t1->data != NULL) {
1447                         memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1448                 }
1449         }
1450
1451         {
1452                 size_t flagssize;
1453
1454                 /* Overflow check */
1455                 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1456                         /* FIXME event manager error callback */
1457                         return OPJ_FALSE;
1458                 }
1459                 t1->flags_stride = w + 2U; /* can't be 0U */
1460
1461 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1462                 /* Overflow check */
1463                 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1464                         /* FIXME event manager error callback */
1465                         return OPJ_FALSE;
1466                 }
1467 #endif
1468                 flagssize = (size_t)h + 3U;
1469                 
1470                 /* Overflow check */
1471                 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1472                         /* FIXME event manager error callback */
1473                         return OPJ_FALSE;
1474                 }
1475                 flagssize *= (size_t)t1->flags_stride;
1476
1477                 if(flagssize > (size_t)t1->flagssize){
1478                         /* Overflow check */
1479                         if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1480                                 /* FIXME event manager error callback */
1481                                 return OPJ_FALSE;
1482                         }
1483                         opj_aligned_free(t1->flags);
1484                         t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1485                         if(!t1->flags){
1486                                 /* FIXME event manager error callback */
1487                                 return OPJ_FALSE;
1488                         }
1489 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1490                         /* TODO remove this if t1->flagssize type changes to size_t */
1491                         /* Overflow check */
1492                         if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1493                                 /* FIXME event manager error callback */
1494                                 return OPJ_FALSE;
1495                         }
1496 #endif
1497                         t1->flagssize = (OPJ_UINT32)flagssize;
1498                 }
1499                 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1500         }
1501         if (!t1->encoder) {
1502                 size_t colflags_size = ((((size_t)h + 3U) / 4U) + 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1503                 
1504                 /* Overflow check */
1505                 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1506                         /* FIXME event manager error callback */
1507                         return OPJ_FALSE;
1508                 }
1509                 colflags_size *= (size_t)t1->flags_stride;
1510                 
1511                 if(colflags_size > (size_t)t1->colflags_size){
1512                         /* Overflow check */
1513                         if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1514                                 /* FIXME event manager error callback */
1515                                 return OPJ_FALSE;
1516                         }
1517                         opj_aligned_free(t1->colflags);
1518                         t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(opj_colflag_t));
1519                         if(!t1->colflags){
1520                                 /* FIXME event manager error callback */
1521                                 return OPJ_FALSE;
1522                         }
1523 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1524                         /* TODO remove this if t1->colflags_size type changes to size_t */
1525                         /* Overflow check */
1526                         if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1527                                 /* FIXME event manager error callback */
1528                                 return OPJ_FALSE;
1529                         }
1530 #endif
1531                         t1->colflags_size = (OPJ_UINT32)colflags_size;
1532                 }
1533                 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1534         }
1535
1536         t1->w = w;
1537         t1->h = h;
1538
1539         return OPJ_TRUE;
1540 }
1541
1542 /* ----------------------------------------------------------------------- */
1543
1544 /* ----------------------------------------------------------------------- */
1545 /**
1546  * Creates a new Tier 1 handle
1547  * and initializes the look-up tables of the Tier-1 coder/decoder
1548  * @return a new T1 handle if successful, returns NULL otherwise
1549 */
1550 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1551 {
1552         opj_t1_t *l_t1 = 00;
1553
1554         l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1555         if (!l_t1) {
1556                 return 00;
1557         }
1558
1559         /* create MQC and RAW handles */
1560         l_t1->mqc = opj_mqc_create();
1561         if (! l_t1->mqc) {
1562                 opj_t1_destroy(l_t1);
1563                 return 00;
1564         }
1565
1566         l_t1->raw = opj_raw_create();
1567         if (! l_t1->raw) {
1568                 opj_t1_destroy(l_t1);
1569                 return 00;
1570         }
1571         l_t1->encoder = isEncoder;
1572
1573         return l_t1;
1574 }
1575
1576
1577 /**
1578  * Destroys a previously created T1 handle
1579  *
1580  * @param p_t1 Tier 1 handle to destroy
1581 */
1582 void opj_t1_destroy(opj_t1_t *p_t1)
1583 {
1584         if (! p_t1) {
1585                 return;
1586         }
1587
1588         /* destroy MQC and RAW handles */
1589         opj_mqc_destroy(p_t1->mqc);
1590         p_t1->mqc = 00;
1591         opj_raw_destroy(p_t1->raw);
1592         p_t1->raw = 00;
1593         
1594         /* encoder uses tile buffer, so no need to free */
1595         if (!p_t1->encoder && p_t1->data) {
1596                 opj_aligned_free(p_t1->data);
1597                 p_t1->data = 00;
1598         }
1599
1600         if (p_t1->flags) {
1601                 opj_aligned_free(p_t1->flags);
1602                 p_t1->flags = 00;
1603         }
1604
1605         if (p_t1->colflags) {
1606                 opj_aligned_free(p_t1->colflags);
1607                 p_t1->colflags = 00;
1608         }
1609         opj_free(p_t1);
1610 }
1611
1612 typedef struct
1613 {
1614     OPJ_UINT32 resno;
1615     opj_tcd_cblk_dec_t* cblk;
1616     opj_tcd_band_t* band;
1617     opj_tcd_tilecomp_t* tilec;
1618     opj_tccp_t* tccp;
1619     volatile OPJ_BOOL* pret;
1620 } opj_t1_cblk_decode_processing_job_t;
1621
1622 static void opj_t1_destroy_wrapper(void* t1)
1623 {
1624     opj_t1_destroy( (opj_t1_t*) t1 );
1625 }
1626
1627 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1628 {
1629     opj_tcd_cblk_dec_t* cblk;
1630     opj_tcd_band_t* band;
1631     opj_tcd_tilecomp_t* tilec;
1632     opj_tccp_t* tccp;
1633     OPJ_INT32* OPJ_RESTRICT datap;
1634     OPJ_UINT32 cblk_w, cblk_h;
1635     OPJ_INT32 x, y;
1636     OPJ_UINT32 i, j;
1637     opj_t1_cblk_decode_processing_job_t* job;
1638     opj_t1_t* t1;
1639     OPJ_UINT32 resno;
1640     OPJ_UINT32 tile_w;
1641
1642     job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1643     resno = job->resno;
1644     cblk = job->cblk;
1645     band = job->band;
1646     tilec = job->tilec;
1647     tccp = job->tccp;
1648     tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1649
1650     if( !*(job->pret) )
1651     {
1652         opj_free(job);
1653         return;
1654     }
1655
1656     t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1657     if( t1 == NULL )
1658     {
1659         t1 = opj_t1_create( OPJ_FALSE );
1660         opj_tls_set( tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper );
1661     }
1662
1663     if (OPJ_FALSE == opj_t1_decode_cblk(
1664                             t1,
1665                             cblk,
1666                             band->bandno,
1667                             (OPJ_UINT32)tccp->roishift,
1668                             tccp->cblksty)) {
1669             *(job->pret) = OPJ_FALSE;
1670             opj_free(job);
1671             return;
1672     }
1673
1674     x = cblk->x0 - band->x0;
1675     y = cblk->y0 - band->y0;
1676     if (band->bandno & 1) {
1677         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1678         x += pres->x1 - pres->x0;
1679     }
1680     if (band->bandno & 2) {
1681         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1682         y += pres->y1 - pres->y0;
1683     }
1684
1685     datap=t1->data;
1686     cblk_w = t1->w;
1687     cblk_h = t1->h;
1688
1689     if (tccp->roishift) {
1690         OPJ_INT32 thresh = 1 << tccp->roishift;
1691         for (j = 0; j < cblk_h; ++j) {
1692             for (i = 0; i < cblk_w; ++i) {
1693                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1694                 OPJ_INT32 mag = abs(val);
1695                 if (mag >= thresh) {
1696                     mag >>= tccp->roishift;
1697                     datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1698                 }
1699             }
1700         }
1701     }
1702     if (tccp->qmfbid == 1) {
1703         OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1704         for (j = 0; j < cblk_h; ++j) {
1705             i = 0;
1706             for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1707                 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1708                 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1709                 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1710                 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1711                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1712                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1713                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1714                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1715             }
1716             for (; i < cblk_w; ++i) {
1717                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1718                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1719             }
1720         }
1721     } else {        /* if (tccp->qmfbid == 0) */
1722         OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1723         for (j = 0; j < cblk_h; ++j) {
1724             OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1725             for (i = 0; i < cblk_w; ++i) {
1726                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1727                 *tiledp2 = tmp;
1728                 datap++;
1729                 tiledp2++;
1730             }
1731             tiledp += tile_w;
1732         }
1733     }
1734
1735     opj_free(job);
1736 }
1737
1738
1739 void opj_t1_decode_cblks( opj_thread_pool_t* tp,
1740                           volatile OPJ_BOOL* pret,
1741                           opj_tcd_tilecomp_t* tilec,
1742                           opj_tccp_t* tccp
1743                          )
1744 {
1745         OPJ_UINT32 resno, bandno, precno, cblkno;
1746
1747         for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1748                 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1749
1750                 for (bandno = 0; bandno < res->numbands; ++bandno) {
1751                         opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1752
1753                         for (precno = 0; precno < res->pw * res->ph; ++precno) {
1754                                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1755
1756                                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1757                                         opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1758                     opj_t1_cblk_decode_processing_job_t* job;
1759
1760                     job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1, sizeof(opj_t1_cblk_decode_processing_job_t));
1761                     if( !job )
1762                     {
1763                         *pret = OPJ_FALSE;
1764                         return;
1765                     }
1766                     job->resno = resno;
1767                     job->cblk = cblk;
1768                     job->band = band;
1769                     job->tilec = tilec;
1770                     job->tccp = tccp;
1771                     job->pret = pret;
1772                     opj_thread_pool_submit_job( tp, opj_t1_clbl_decode_processor, job );
1773                     if( !(*pret) )
1774                         return;
1775                                 } /* cblkno */
1776                         } /* precno */
1777                 } /* bandno */
1778         } /* resno */
1779
1780     return;
1781 }
1782
1783
1784 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1785                             opj_tcd_cblk_dec_t* cblk,
1786                             OPJ_UINT32 orient,
1787                             OPJ_UINT32 roishift,
1788                             OPJ_UINT32 cblksty)
1789 {
1790         opj_raw_t *raw = t1->raw;       /* RAW component */
1791         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1792         
1793         OPJ_INT32 bpno_plus_one;
1794         OPJ_UINT32 passtype;
1795         OPJ_UINT32 segno, passno;
1796         OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1797
1798         mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1799
1800         if(!opj_t1_allocate_buffers(
1801                                 t1,
1802                                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1803                                 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1804         {
1805                 return OPJ_FALSE;
1806         }
1807
1808         bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1809         passtype = 2;
1810
1811         opj_mqc_resetstates(mqc);
1812         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1813         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1814         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1815
1816         for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1817                 opj_tcd_seg_t *seg = &cblk->segs[segno];
1818
1819                 /* BYPASS mode */
1820                 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1821                 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1822                 if(seg->data == 00){
1823                         continue;
1824                 }
1825                 if (type == T1_TYPE_RAW) {
1826                         opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1827                 } else {
1828             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1829                     return OPJ_FALSE;
1830             }
1831                 }
1832
1833                 if( t1->w == 64 && t1->h == 64 )
1834                 {
1835                   for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1836                           switch (passtype) {
1837                                   case 0:
1838                                           if (type == T1_TYPE_RAW) {
1839                                                   opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1840                                           } else {
1841                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1842                                                           opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1843                                                   } else {
1844                                                           opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1845                                                   }
1846                                           }
1847                                           break;
1848                                   case 1:
1849                                           if (type == T1_TYPE_RAW) {
1850                                                           opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1851                                           } else {
1852                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1853                                                           opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1854                                                   } else {
1855                                                           opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1856                                                   }
1857                                           }
1858                                           break;
1859                                   case 2:
1860                                           opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1861                                           break;
1862                           }
1863
1864                           if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1865                                   opj_mqc_resetstates(mqc);
1866                                   opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1867                                   opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1868                                   opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1869                           }
1870                           if (++passtype == 3) {
1871                                   passtype = 0;
1872                                   bpno_plus_one--;
1873                           }
1874                   }
1875                 }
1876                 else
1877                 {
1878                   for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1879                           switch (passtype) {
1880                                   case 0:
1881                                           if (type == T1_TYPE_RAW) {
1882                                                   opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1883                                           } else {
1884                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1885                                                           opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1886                                                   } else {
1887                                                           opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1888                                                   }
1889                                           }
1890                                           break;
1891                                   case 1:
1892                                           if (type == T1_TYPE_RAW) {
1893                                                           opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1894                                           } else {
1895                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1896                                                           opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1897                                                   } else {
1898                                                           opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1899                                                   }
1900                                           }
1901                                           break;
1902                                   case 2:
1903                                           opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1904                                           break;
1905                           }
1906
1907                           if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1908                                   opj_mqc_resetstates(mqc);
1909                                   opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1910                                   opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1911                                   opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1912                           }
1913                           if (++passtype == 3) {
1914                                   passtype = 0;
1915                                   bpno_plus_one--;
1916                           }
1917                   }
1918                 }
1919         }
1920     return OPJ_TRUE;
1921 }
1922
1923
1924
1925
1926 OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
1927                                 opj_tcd_tile_t *tile,
1928                                 opj_tcp_t *tcp,
1929                                 const OPJ_FLOAT64 * mct_norms,
1930                                 OPJ_UINT32 mct_numcomps
1931                                 )
1932 {
1933         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1934
1935         tile->distotile = 0;            /* fixed_quality */
1936
1937         for (compno = 0; compno < tile->numcomps; ++compno) {
1938                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1939                 opj_tccp_t* tccp = &tcp->tccps[compno];
1940                 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1941
1942                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1943                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1944
1945                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1946                                 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1947                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1948
1949                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1950                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1951
1952                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1953                                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1954                                                 OPJ_INT32* OPJ_RESTRICT tiledp;
1955                                                 OPJ_UINT32 cblk_w;
1956                                                 OPJ_UINT32 cblk_h;
1957                                                 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1958
1959                                                 OPJ_INT32 x = cblk->x0 - band->x0;
1960                                                 OPJ_INT32 y = cblk->y0 - band->y0;
1961                                                 if (band->bandno & 1) {
1962                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1963                                                         x += pres->x1 - pres->x0;
1964                                                 }
1965                                                 if (band->bandno & 2) {
1966                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1967                                                         y += pres->y1 - pres->y0;
1968                                                 }
1969
1970                                                 if(!opj_t1_allocate_buffers(
1971                                                                         t1,
1972                                                                         (OPJ_UINT32)(cblk->x1 - cblk->x0),
1973                                                                         (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1974                                                 {
1975                                                         return OPJ_FALSE;
1976                                                 }
1977
1978                                                 cblk_w = t1->w;
1979                                                 cblk_h = t1->h;
1980                                                 tileLineAdvance = tile_w - cblk_w;
1981
1982                                                 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1983                                                 t1->data = tiledp;
1984                                                 t1->data_stride = tile_w;
1985                                                 if (tccp->qmfbid == 1) {
1986                                                         for (j = 0; j < cblk_h; ++j) {
1987                                                                 for (i = 0; i < cblk_w; ++i) {
1988                                                                         tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1989                                                                         tileIndex++;
1990                                                                 }
1991                                                                 tileIndex += tileLineAdvance;
1992                                                         }
1993                                                 } else {                /* if (tccp->qmfbid == 0) */
1994                                                         for (j = 0; j < cblk_h; ++j) {
1995                                                                 for (i = 0; i < cblk_w; ++i) {
1996                                                                         OPJ_INT32 tmp = tiledp[tileIndex];
1997                                                                         tiledp[tileIndex] =
1998                                                                                 opj_int_fix_mul_t1(
1999                                                                                 tmp,
2000                                                                                 bandconst);
2001                                                                         tileIndex++;
2002                                                                 }
2003                                                                 tileIndex += tileLineAdvance;
2004                                                         }
2005                                                 }
2006
2007                                                 opj_t1_encode_cblk(
2008                                                                 t1,
2009                                                                 cblk,
2010                                                                 band->bandno,
2011                                                                 compno,
2012                                                                 tilec->numresolutions - 1 - resno,
2013                                                                 tccp->qmfbid,
2014                                                                 band->stepsize,
2015                                                                 tccp->cblksty,
2016                                                                 tile->numcomps,
2017                                                                 tile,
2018                                                                 mct_norms,
2019                                                                 mct_numcomps);
2020
2021                                         } /* cblkno */
2022                                 } /* precno */
2023                         } /* bandno */
2024                 } /* resno  */
2025         } /* compno  */
2026         return OPJ_TRUE;
2027 }
2028
2029 /** mod fixed_quality */
2030 static void opj_t1_encode_cblk(opj_t1_t *t1,
2031                         opj_tcd_cblk_enc_t* cblk,
2032                         OPJ_UINT32 orient,
2033                         OPJ_UINT32 compno,
2034                         OPJ_UINT32 level,
2035                         OPJ_UINT32 qmfbid,
2036                         OPJ_FLOAT64 stepsize,
2037                         OPJ_UINT32 cblksty,
2038                         OPJ_UINT32 numcomps,
2039                         opj_tcd_tile_t * tile,
2040                         const OPJ_FLOAT64 * mct_norms,
2041                         OPJ_UINT32 mct_numcomps)
2042 {
2043         OPJ_FLOAT64 cumwmsedec = 0.0;
2044
2045         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
2046
2047         OPJ_UINT32 passno;
2048         OPJ_INT32 bpno;
2049         OPJ_UINT32 passtype;
2050         OPJ_INT32 nmsedec = 0;
2051         OPJ_INT32 max;
2052         OPJ_UINT32 i, j;
2053         OPJ_BYTE type = T1_TYPE_MQ;
2054         OPJ_FLOAT64 tempwmsedec;
2055
2056         mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2057
2058         max = 0;
2059         for (i = 0; i < t1->w; ++i) {
2060                 for (j = 0; j < t1->h; ++j) {
2061                         OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
2062                         max = opj_int_max(max, tmp);
2063                 }
2064         }
2065
2066         cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
2067
2068         bpno = (OPJ_INT32)(cblk->numbps - 1);
2069         passtype = 2;
2070
2071         opj_mqc_resetstates(mqc);
2072         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2073         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2074         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2075         opj_mqc_init_enc(mqc, cblk->data);
2076
2077         for (passno = 0; bpno >= 0; ++passno) {
2078                 opj_tcd_pass_t *pass = &cblk->passes[passno];
2079                 OPJ_UINT32 correction = 3;
2080                 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2081
2082                 switch (passtype) {
2083                         case 0:
2084                                 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2085                                 break;
2086                         case 1:
2087                                 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2088                                 break;
2089                         case 2:
2090                                 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2091                                 /* code switch SEGMARK (i.e. SEGSYM) */
2092                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
2093                                         opj_mqc_segmark_enc(mqc);
2094                                 break;
2095                 }
2096
2097                 /* fixed_quality */
2098                 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
2099                 cumwmsedec += tempwmsedec;
2100                 tile->distotile += tempwmsedec;
2101
2102                 /* Code switch "RESTART" (i.e. TERMALL) */
2103                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
2104                         if (type == T1_TYPE_RAW) {
2105                                 opj_mqc_flush(mqc);
2106                                 correction = 1;
2107                                 /* correction = mqc_bypass_flush_enc(); */
2108                         } else {                        /* correction = mqc_restart_enc(); */
2109                                 opj_mqc_flush(mqc);
2110                                 correction = 1;
2111                         }
2112                         pass->term = 1;
2113                 } else {
2114                         if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
2115                                 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2116                                 if (type == T1_TYPE_RAW) {
2117                                         opj_mqc_flush(mqc);
2118                                         correction = 1;
2119                                         /* correction = mqc_bypass_flush_enc(); */
2120                                 } else {                /* correction = mqc_restart_enc(); */
2121                                         opj_mqc_flush(mqc);
2122                                         correction = 1;
2123                                 }
2124                                 pass->term = 1;
2125                         } else {
2126                                 pass->term = 0;
2127                         }
2128                 }
2129
2130                 if (++passtype == 3) {
2131                         passtype = 0;
2132                         bpno--;
2133                 }
2134
2135                 if (pass->term && bpno > 0) {
2136                         type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2137                         if (type == T1_TYPE_RAW)
2138                                 opj_mqc_bypass_init_enc(mqc);
2139                         else
2140                                 opj_mqc_restart_init_enc(mqc);
2141                 }
2142
2143                 pass->distortiondec = cumwmsedec;
2144                 pass->rate = opj_mqc_numbytes(mqc) + correction;        /* FIXME */
2145
2146                 /* Code-switch "RESET" */
2147                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2148                         opj_mqc_reset_enc(mqc);
2149         }
2150
2151         /* Code switch "ERTERM" (i.e. PTERM) */
2152         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2153                 opj_mqc_erterm_enc(mqc);
2154         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2155                 opj_mqc_flush(mqc);
2156
2157         cblk->totalpasses = passno;
2158
2159         for (passno = 0; passno<cblk->totalpasses; passno++) {
2160                 opj_tcd_pass_t *pass = &cblk->passes[passno];
2161                 if (pass->rate > opj_mqc_numbytes(mqc))
2162                         pass->rate = opj_mqc_numbytes(mqc);
2163                 /*Preventing generation of FF as last data byte of a pass*/
2164                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2165                         pass->rate--;
2166                 }
2167                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2168         }
2169 }
2170
2171 #if 0
2172 static void opj_t1_dec_refpass_step(   opj_t1_t *t1,
2173                                 opj_flag_t *flagsp,
2174                                 OPJ_INT32 *datap,
2175                                 OPJ_INT32 poshalf,
2176                                 OPJ_INT32 neghalf,
2177                                 OPJ_BYTE type,
2178                                 OPJ_UINT32 vsc)
2179 {
2180         OPJ_INT32  t;
2181         OPJ_UINT32 v,flag;
2182
2183         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
2184         opj_raw_t *raw = t1->raw;       /* RAW component */
2185
2186         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2187         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2188                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
2189                 if (type == T1_TYPE_RAW) {
2190                         v = opj_raw_decode(raw);
2191                 } else {
2192                         v = opj_mqc_decode(mqc);
2193                 }
2194                 t = v ? poshalf : neghalf;
2195                 *datap += *datap < 0 ? -t : t;
2196                 *flagsp |= T1_REFINE;
2197         }
2198 }                               /* VSC and  BYPASS by Antonin  */
2199 #endif
2200
2201
2202
2203 #if 0
2204 static void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
2205                                 opj_flag_t *flagsp,
2206                                 OPJ_INT32 *datap,
2207                                 OPJ_UINT32 orient,
2208                                 OPJ_INT32 oneplushalf,
2209                                 OPJ_BYTE type,
2210                                 OPJ_UINT32 vsc)
2211 {
2212         OPJ_UINT32 v, flag;
2213
2214         opj_raw_t *raw = t1->raw;       /* RAW component */
2215         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
2216
2217         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2218         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2219                 if (type == T1_TYPE_RAW) {
2220                         if (opj_raw_decode(raw)) {
2221                                 v = opj_raw_decode(raw);        /* ESSAI */
2222                                 *datap = v ? -oneplushalf : oneplushalf;
2223                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2224                         }
2225                 } else {
2226                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2227                         if (opj_mqc_decode(mqc)) {
2228                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2229                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2230                                 *datap = v ? -oneplushalf : oneplushalf;
2231                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2232                         }
2233                 }
2234                 *flagsp |= T1_VISIT;
2235         }
2236 }                               /* VSC and  BYPASS by Antonin */
2237 #endif