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