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