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