[trunk] rename t1_getwmsedec_v2 to opj_t1_getwmsedec and remove t1_getwmsedec
[openjpeg.git] / src / lib / openjp2 / t1.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "opj_includes.h"
34 #include "t1_luts.h"
35
36 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
37 /*@{*/
38
39 /** @name Local static functions */
40 /*@{*/
41
42 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
43 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
44 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
45 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
46 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
47 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
48 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
49 /**
50 Encode significant pass
51 */
52 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
53                                     opj_flag_t *flagsp,
54                                     OPJ_INT32 *datap,
55                                     OPJ_UINT32 orient,
56                                     OPJ_INT32 bpno,
57                                     OPJ_INT32 one,
58                                     OPJ_INT32 *nmsedec,
59                                     OPJ_BYTE type,
60                                     OPJ_UINT32 vsc);
61
62 /**
63 Decode significant pass
64 */
65 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
66                                     opj_flag_t *flagsp,
67                                     OPJ_INT32 *datap,
68                                     OPJ_UINT32 orient,
69                                     OPJ_INT32 oneplushalf,
70                                     OPJ_BYTE type,
71                                     OPJ_UINT32 vsc);
72
73 static INLINE void opj_t1_dec_sigpass_step_raw(
74                 opj_t1_t *t1,
75                 opj_flag_t *flagsp,
76                 int *datap,
77                 int orient,
78                 int oneplushalf,
79                 int vsc);
80 static INLINE void opj_t1_dec_sigpass_step_mqc(
81                 opj_t1_t *t1,
82                 opj_flag_t *flagsp,
83                 int *datap,
84                 int orient,
85                 int oneplushalf);
86 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
87                 opj_t1_t *t1,
88                 opj_flag_t *flagsp,
89                 int *datap,
90                 int orient,
91                 int oneplushalf,
92                 int vsc);
93
94
95 /**
96 Encode significant pass
97 */
98 static void opj_t1_enc_sigpass( opj_t1_t *t1,
99                                 OPJ_INT32 bpno,
100                                 OPJ_UINT32 orient,
101                                 OPJ_INT32 *nmsedec,
102                                 OPJ_BYTE type,
103                                 OPJ_UINT32 cblksty);
104
105 /**
106 Decode significant pass
107 */
108 static void opj_t1_dec_sigpass( opj_t1_t *t1,
109                                 OPJ_INT32 bpno,
110                                 OPJ_UINT32 orient,
111                                 OPJ_BYTE type,
112                                 OPJ_UINT32 cblksty);
113
114 static void opj_t1_dec_sigpass_raw(
115                 opj_t1_t *t1,
116                 int bpno,
117                 int orient,
118                 int cblksty);
119 static void opj_t1_dec_sigpass_mqc(
120                 opj_t1_t *t1,
121                 int bpno,
122                 int orient);
123 static void opj_t1_dec_sigpass_mqc_vsc(
124                 opj_t1_t *t1,
125                 int bpno,
126                 int orient);
127
128
129
130 /**
131 Encode refinement pass
132 */
133 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
134                                     opj_flag_t *flagsp,
135                                     OPJ_INT32 *datap,
136                                     OPJ_INT32 bpno,
137                                     OPJ_INT32 one,
138                                     OPJ_INT32 *nmsedec,
139                                     OPJ_BYTE type,
140                                     OPJ_UINT32 vsc);
141
142
143 /**
144 Encode refinement pass
145 */
146 static void opj_t1_enc_refpass( opj_t1_t *t1,
147                                 OPJ_INT32 bpno,
148                                 OPJ_INT32 *nmsedec,
149                                 OPJ_BYTE type,
150                                 OPJ_UINT32 cblksty);
151
152 /**
153 Decode refinement pass
154 */
155 static void opj_t1_dec_refpass( opj_t1_t *t1,
156                                 OPJ_INT32 bpno,
157                                 OPJ_BYTE type,
158                                 OPJ_UINT32 cblksty);
159 static void opj_t1_dec_refpass_raw(
160                 opj_t1_t *t1,
161                 int bpno,
162                 int cblksty);
163 static void opj_t1_dec_refpass_mqc(
164                 opj_t1_t *t1,
165                 int bpno);
166 static void opj_t1_dec_refpass_mqc_vsc(
167                 opj_t1_t *t1,
168                 int bpno);
169
170
171 /**
172 Decode refinement pass
173 */
174 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
175                                     opj_flag_t *flagsp,
176                                     OPJ_INT32 *datap,
177                                     OPJ_INT32 poshalf,
178                                     OPJ_INT32 neghalf,
179                                     OPJ_BYTE type,
180                                     OPJ_UINT32 vsc);
181
182 static void INLINE opj_t1_dec_refpass_step_raw(
183                 opj_t1_t *t1,
184                 opj_flag_t *flagsp,
185                 int *datap,
186                 int poshalf,
187                 int neghalf,
188                 int vsc);
189 static void INLINE opj_t1_dec_refpass_step_mqc(
190                 opj_t1_t *t1,
191                 opj_flag_t *flagsp,
192                 int *datap,
193                 int poshalf,
194                 int neghalf);
195 static void INLINE opj_t1_dec_refpass_step_mqc_vsc(
196                 opj_t1_t *t1,
197                 opj_flag_t *flagsp,
198                 int *datap,
199                 int poshalf,
200                 int neghalf,
201                 int 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                 int *datap,
225                 int orient,
226                 int oneplushalf);
227 static void opj_t1_dec_clnpass_step(
228                 opj_t1_t *t1,
229                 opj_flag_t *flagsp,
230                 int *datap,
231                 int orient,
232                 int oneplushalf);
233 static void opj_t1_dec_clnpass_step_vsc(
234                 opj_t1_t *t1,
235                 opj_flag_t *flagsp,
236                 int *datap,
237                 int orient,
238                 int oneplushalf,
239                 int partial,
240                 int 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                 int bpno,
256                 int orient,
257                 int 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
270 static void opj_t1_encode_cblk( opj_t1_t *t1,
271                                 opj_tcd_cblk_enc_t* cblk,
272                                 OPJ_UINT32 orient,
273                                 OPJ_UINT32 compno,
274                                 OPJ_UINT32 level,
275                                 OPJ_UINT32 qmfbid,
276                                 OPJ_FLOAT64 stepsize,
277                                 OPJ_UINT32 cblksty,
278                                 OPJ_UINT32 numcomps,
279                                 opj_tcd_tile_t * tile,
280                                 const OPJ_FLOAT64 * mct_norms);
281
282 /**
283 Decode 1 code-block
284 @param t1 T1 handle
285 @param cblk Code-block coding parameters
286 @param orient
287 @param roishift Region of interest shifting value
288 @param cblksty Code-block style
289 */
290 static opj_bool opj_t1_decode_cblk( opj_t1_t *t1,
291                                     opj_tcd_cblk_dec_t* cblk,
292                                     OPJ_UINT32 orient,
293                                     OPJ_UINT32 roishift,
294                                     OPJ_UINT32 cblksty);
295
296 opj_bool opj_t1_allocate_buffers(   opj_t1_t *t1,
297                                     OPJ_UINT32 w,
298                                     OPJ_UINT32 h);
299
300 /*@}*/
301
302 /*@}*/
303
304 /* ----------------------------------------------------------------------- */
305
306 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
307         return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
308 }
309
310 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
311         return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
312 }
313
314 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
315         OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
316         OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
317         return (tmp2);
318 }
319
320 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
321         return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
322 }
323
324 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
325         if (bitpos > T1_NMSEDEC_FRACBITS) {
326                 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
327         }
328         
329         return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
330 }
331
332 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
333         if (bitpos > T1_NMSEDEC_FRACBITS) {
334                 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
335         }
336
337     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
338 }
339
340 void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
341         opj_flag_t *np = flagsp - stride;
342         opj_flag_t *sp = flagsp + stride;
343
344         static const opj_flag_t mod[] = {
345                 T1_SIG_S, T1_SIG_S|T1_SGN_S,
346                 T1_SIG_E, T1_SIG_E|T1_SGN_E,
347                 T1_SIG_W, T1_SIG_W|T1_SGN_W,
348                 T1_SIG_N, T1_SIG_N|T1_SGN_N
349         };
350
351         np[-1] |= T1_SIG_SE;
352         np[0]  |= mod[s];
353         np[1]  |= T1_SIG_SW;
354
355         flagsp[-1] |= mod[s+2];
356         flagsp[0]  |= T1_SIG;
357         flagsp[1]  |= mod[s+4];
358
359         sp[-1] |= T1_SIG_NE;
360         sp[0]  |= mod[s+6];
361         sp[1]  |= T1_SIG_NW;
362 }
363
364 void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
365                                 opj_flag_t *flagsp,
366                                 OPJ_INT32 *datap,
367                                 OPJ_UINT32 orient,
368                                 OPJ_INT32 bpno,
369                                 OPJ_INT32 one,
370                                 OPJ_INT32 *nmsedec,
371                                 OPJ_BYTE type,
372                                 OPJ_UINT32 vsc
373                                 )
374 {
375         OPJ_INT32 v;
376     OPJ_UINT32 flag;
377         
378         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
379         
380         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
381         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
382                 v = opj_int_abs(*datap) & one ? 1 : 0;
383                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));       /* ESSAI */
384                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
385                         opj_mqc_bypass_enc(mqc, v);
386                 } else {
387                         opj_mqc_encode(mqc, v);
388                 }
389                 if (v) {
390                         v = *datap < 0 ? 1 : 0;
391                         *nmsedec +=     opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
392                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));       /* ESSAI */
393                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
394                                 opj_mqc_bypass_enc(mqc, v);
395                         } else {
396                                 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
397                         }
398                         opj_t1_updateflags(flagsp, v, t1->flags_stride);
399                 }
400                 *flagsp |= T1_VISIT;
401         }
402 }
403
404
405 static INLINE void opj_t1_dec_sigpass_step_raw(
406                 opj_t1_t *t1,
407                 opj_flag_t *flagsp,
408                 int *datap,
409                 int orient,
410                 int oneplushalf,
411                 int vsc)
412 {
413         int v, flag;
414        
415         opj_raw_t *raw = t1->raw;       /* RAW component */
416        
417         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
418         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
419                         if (opj_raw_decode(raw)) {
420                                 v = opj_raw_decode(raw);    /* ESSAI */
421                                 *datap = v ? -oneplushalf : oneplushalf;
422                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
423                         }
424                 *flagsp |= T1_VISIT;
425         }
426 }      
427
428 INLINE void opj_t1_dec_sigpass_step_mqc(
429                 opj_t1_t *t1,
430                 opj_flag_t *flagsp,
431                 int *datap,
432                 int orient,
433                 int oneplushalf)
434 {
435         int v, flag;
436        
437         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
438        
439         flag = *flagsp;
440         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
441                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
442                         if (opj_mqc_decode(mqc)) {
443                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
444                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
445                                 *datap = v ? -oneplushalf : oneplushalf;
446                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
447                         }
448                 *flagsp |= T1_VISIT;
449         }
450 }                               /* VSC and  BYPASS by Antonin */
451
452 INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
453                 opj_t1_t *t1,
454                 opj_flag_t *flagsp,
455                 int *datap,
456                 int orient,
457                 int oneplushalf,
458                 int vsc)
459 {
460         int v, flag;
461        
462         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
463        
464         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
465         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
466                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
467                 if (opj_mqc_decode(mqc)) {
468                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
469                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
470                         *datap = v ? -oneplushalf : oneplushalf;
471                         opj_t1_updateflags(flagsp, v, t1->flags_stride);
472                 }
473                 *flagsp |= T1_VISIT;
474         }
475 }                               /* VSC and  BYPASS by Antonin */
476
477
478
479 void opj_t1_enc_sigpass(opj_t1_t *t1,
480                         OPJ_INT32 bpno,
481                         OPJ_UINT32 orient,
482                         OPJ_INT32 *nmsedec,
483                         OPJ_BYTE type,
484                         OPJ_UINT32 cblksty
485                         )
486 {
487         OPJ_UINT32 i, j, k, vsc; 
488     OPJ_INT32 one;
489
490         *nmsedec = 0;
491         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
492         for (k = 0; k < t1->h; k += 4) {
493                 for (i = 0; i < t1->w; ++i) {
494                         for (j = k; j < k + 4 && j < t1->h; ++j) {
495                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
496                                 opj_t1_enc_sigpass_step(
497                                                 t1,
498                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
499                                                 &t1->data[(j * t1->w) + i],
500                                                 orient,
501                                                 bpno,
502                                                 one,
503                                                 nmsedec,
504                                                 type,
505                                                 vsc);
506                         }
507                 }
508         }
509 }
510
511 void opj_t1_dec_sigpass_raw(
512                 opj_t1_t *t1,
513                 int bpno,
514                 int orient,
515                 int cblksty)
516 {
517         int one, half, oneplushalf, vsc;
518         OPJ_UINT32 i, j, k; 
519         one = 1 << bpno;
520         half = one >> 1;
521         oneplushalf = one | half;
522         for (k = 0; k < t1->h; k += 4) {
523                 for (i = 0; i < t1->w; ++i) {
524                         for (j = k; j < k + 4 && j < t1->h; ++j) {
525                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
526                                 opj_t1_dec_sigpass_step_raw(
527                                                 t1,
528                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
529                                                 &t1->data[(j * t1->w) + i],
530                                                 orient,
531                                                 oneplushalf,
532                                                 vsc);
533                         }
534                 }
535         }
536 }                               /* VSC and  BYPASS by Antonin */
537
538 void opj_t1_dec_sigpass_mqc(
539                 opj_t1_t *t1,
540                 int bpno,
541                 int orient)
542 {
543         int one, half, oneplushalf;
544         OPJ_UINT32 i, j, k;
545         int *data1 = t1->data;
546         opj_flag_t *flags1 = &t1->flags[1];
547         one = 1 << bpno;
548         half = one >> 1;
549         oneplushalf = one | half;
550         for (k = 0; k < (t1->h & ~3); k += 4) {
551                 for (i = 0; i < t1->w; ++i) {
552                         int *data2 = data1 + i;
553                         opj_flag_t *flags2 = flags1 + i;
554                         flags2 += t1->flags_stride;
555                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
556                         data2 += t1->w;
557                         flags2 += t1->flags_stride;
558                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
559                         data2 += t1->w;
560                         flags2 += t1->flags_stride;
561                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
562                         data2 += t1->w;
563                         flags2 += t1->flags_stride;
564                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
565                         data2 += t1->w;
566                 }
567                 data1 += t1->w << 2;
568                 flags1 += t1->flags_stride << 2;
569         }
570         for (i = 0; i < t1->w; ++i) {
571                 int *data2 = data1 + i;
572                 opj_flag_t *flags2 = flags1 + i;
573                 for (j = k; j < t1->h; ++j) {
574                         flags2 += t1->flags_stride;
575                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
576                         data2 += t1->w;
577                 }
578         }
579 }                               /* VSC and  BYPASS by Antonin */
580
581 void opj_t1_dec_sigpass_mqc_vsc(
582                 opj_t1_t *t1,
583                 int bpno,
584                 int orient)
585 {
586         int one, half, oneplushalf, vsc;
587         OPJ_UINT32 i, j, k;
588         one = 1 << bpno;
589         half = one >> 1;
590         oneplushalf = one | half;
591         for (k = 0; k < t1->h; k += 4) {
592                 for (i = 0; i < t1->w; ++i) {
593                         for (j = k; j < k + 4 && j < t1->h; ++j) {
594                                 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
595                                 opj_t1_dec_sigpass_step_mqc_vsc(
596                                                 t1,
597                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
598                                                 &t1->data[(j * t1->w) + i],
599                                                 orient,
600                                                 oneplushalf,
601                                                 vsc);
602                         }
603                 }
604         }
605 }                               /* VSC and  BYPASS by Antonin */
606
607
608
609 void opj_t1_enc_refpass_step(   opj_t1_t *t1,
610                                 opj_flag_t *flagsp,
611                                 OPJ_INT32 *datap,
612                                 OPJ_INT32 bpno,
613                                 OPJ_INT32 one,
614                                 OPJ_INT32 *nmsedec,
615                                 OPJ_BYTE type,
616                                 OPJ_UINT32 vsc)
617 {
618         OPJ_INT32 v;
619         OPJ_UINT32 flag;
620         
621         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
622         
623         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
624         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
625                 *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
626                 v = opj_int_abs(*datap) & one ? 1 : 0;
627                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
628                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
629                         opj_mqc_bypass_enc(mqc, v);
630                 } else {
631                         opj_mqc_encode(mqc, v);
632                 }
633                 *flagsp |= T1_REFINE;
634         }
635 }
636
637 INLINE void opj_t1_dec_refpass_step_raw(
638                 opj_t1_t *t1,
639                 opj_flag_t *flagsp,
640                 int *datap,
641                 int poshalf,
642                 int neghalf,
643                 int vsc)
644 {
645         int v, t, flag;
646        
647         opj_raw_t *raw = t1->raw;       /* RAW component */
648        
649         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
650         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
651                         v = opj_raw_decode(raw);
652                 t = v ? poshalf : neghalf;
653                 *datap += *datap < 0 ? -t : t;
654                 *flagsp |= T1_REFINE;
655         }
656 }                               /* VSC and  BYPASS by Antonin  */
657
658 INLINE void opj_t1_dec_refpass_step_mqc(
659                 opj_t1_t *t1,
660                 opj_flag_t *flagsp,
661                 int *datap,
662                 int poshalf,
663                 int neghalf)
664 {
665         int v, t, flag;
666        
667         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
668        
669         flag = *flagsp;
670         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
671                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
672                         v = opj_mqc_decode(mqc);
673                 t = v ? poshalf : neghalf;
674                 *datap += *datap < 0 ? -t : t;
675                 *flagsp |= T1_REFINE;
676                 }
677 }                               /* VSC and  BYPASS by Antonin  */
678
679 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
680                 opj_t1_t *t1,
681                 opj_flag_t *flagsp,
682                 int *datap,
683                 int poshalf,
684                 int neghalf,
685                 int vsc)
686 {
687         int v, t, flag;
688        
689         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
690        
691         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
692         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
693                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
694                 v = opj_mqc_decode(mqc);
695                 t = v ? poshalf : neghalf;
696                 *datap += *datap < 0 ? -t : t;
697                 *flagsp |= T1_REFINE;
698         }
699 }                               /* VSC and  BYPASS by Antonin  */
700
701
702 void opj_t1_enc_refpass(
703                 opj_t1_t *t1,
704                 OPJ_INT32 bpno,
705                 OPJ_INT32 *nmsedec,
706                 OPJ_BYTE type,
707                 OPJ_UINT32 cblksty)
708 {
709         OPJ_UINT32 i, j, k, vsc;
710     OPJ_INT32 one;
711
712         *nmsedec = 0;
713         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
714         for (k = 0; k < t1->h; k += 4) {
715                 for (i = 0; i < t1->w; ++i) {
716                         for (j = k; j < k + 4 && j < t1->h; ++j) {
717                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
718                                 opj_t1_enc_refpass_step(
719                                                 t1,
720                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
721                                                 &t1->data[(j * t1->w) + i],
722                                                 bpno,
723                                                 one,
724                                                 nmsedec,
725                                                 type,
726                                                 vsc);
727                         }
728                 }
729         }
730 }
731
732 void opj_t1_dec_refpass_raw(
733                 opj_t1_t *t1,
734                 int bpno,
735                 int cblksty)
736 {
737         int one, poshalf, neghalf;
738         OPJ_UINT32 i, j, k;
739         int vsc;
740         one = 1 << bpno;
741         poshalf = one >> 1;
742         neghalf = bpno > 0 ? -poshalf : -1;
743         for (k = 0; k < t1->h; k += 4) {
744                 for (i = 0; i < t1->w; ++i) {
745                         for (j = k; j < k + 4 && j < t1->h; ++j) {
746                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
747                                 opj_t1_dec_refpass_step_raw(
748                                                 t1,
749                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
750                                                 &t1->data[(j * t1->w) + i],
751                                                 poshalf,
752                                                 neghalf,
753                                                 vsc);
754                         }
755                 }
756         }
757 }                               /* VSC and  BYPASS by Antonin */
758
759 void opj_t1_dec_refpass_mqc(
760                 opj_t1_t *t1,
761                 int bpno)
762 {
763         int one, poshalf, neghalf;
764         OPJ_UINT32 i, j, k;
765         int *data1 = t1->data;
766         opj_flag_t *flags1 = &t1->flags[1];
767         one = 1 << bpno;
768         poshalf = one >> 1;
769         neghalf = bpno > 0 ? -poshalf : -1;
770         for (k = 0; k < (t1->h & ~3); k += 4) {
771                 for (i = 0; i < t1->w; ++i) {
772                         int *data2 = data1 + i;
773                         opj_flag_t *flags2 = flags1 + i;
774                         flags2 += t1->flags_stride;
775                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
776                         data2 += t1->w;
777                         flags2 += t1->flags_stride;
778                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
779                         data2 += t1->w;
780                         flags2 += t1->flags_stride;
781                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
782                         data2 += t1->w;
783                         flags2 += t1->flags_stride;
784                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
785                         data2 += t1->w;
786                 }
787                 data1 += t1->w << 2;
788                 flags1 += t1->flags_stride << 2;
789         }
790         for (i = 0; i < t1->w; ++i) {
791                 int *data2 = data1 + i;
792                 opj_flag_t *flags2 = flags1 + i;
793                 for (j = k; j < t1->h; ++j) {
794                         flags2 += t1->flags_stride;
795                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
796                         data2 += t1->w;
797                 }
798         }
799 }                               /* VSC and  BYPASS by Antonin */
800
801 void opj_t1_dec_refpass_mqc_vsc(
802                 opj_t1_t *t1,
803                 int bpno)
804 {
805         int one, poshalf, neghalf;
806         OPJ_UINT32 i, j, k;
807         int vsc;
808         one = 1 << bpno;
809         poshalf = one >> 1;
810         neghalf = bpno > 0 ? -poshalf : -1;
811         for (k = 0; k < t1->h; k += 4) {
812                 for (i = 0; i < t1->w; ++i) {
813                         for (j = k; j < k + 4 && j < t1->h; ++j) {
814                                 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
815                                 opj_t1_dec_refpass_step_mqc_vsc(
816                                                 t1,
817                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
818                                                 &t1->data[(j * t1->w) + i],
819                                                 poshalf,
820                                                 neghalf,
821                                                 vsc);
822                         }
823                 }
824         }
825 }                               /* VSC and  BYPASS by Antonin */
826
827
828 void opj_t1_enc_clnpass_step(
829                 opj_t1_t *t1,
830                 opj_flag_t *flagsp,
831                 OPJ_INT32 *datap,
832                 OPJ_UINT32 orient,
833                 OPJ_INT32 bpno,
834                 OPJ_INT32 one,
835                 OPJ_INT32 *nmsedec,
836                 OPJ_UINT32 partial,
837                 OPJ_UINT32 vsc)
838 {
839         OPJ_INT32 v;
840         OPJ_UINT32 flag;
841         
842         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
843         
844         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
845         if (partial) {
846                 goto LABEL_PARTIAL;
847         }
848         if (!(*flagsp & (T1_SIG | T1_VISIT))) {
849                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
850                 v = opj_int_abs(*datap) & one ? 1 : 0;
851                 opj_mqc_encode(mqc, v);
852                 if (v) {
853 LABEL_PARTIAL:
854                         *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
855                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
856                         v = *datap < 0 ? 1 : 0;
857                         opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
858                         opj_t1_updateflags(flagsp, v, t1->flags_stride);
859                 }
860         }
861         *flagsp &= ~T1_VISIT;
862 }
863
864 static void opj_t1_dec_clnpass_step_partial(
865                 opj_t1_t *t1,
866                 opj_flag_t *flagsp,
867                 int *datap,
868                 int orient,
869                 int oneplushalf)
870 {
871         int v, flag;
872         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
873         
874         OPJ_ARG_NOT_USED(orient);
875         
876         flag = *flagsp;
877         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
878         v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
879         *datap = v ? -oneplushalf : oneplushalf;
880         opj_t1_updateflags(flagsp, v, t1->flags_stride);
881         *flagsp &= ~T1_VISIT;
882 }                               /* VSC and  BYPASS by Antonin */
883
884 static void opj_t1_dec_clnpass_step(
885                 opj_t1_t *t1,
886                 opj_flag_t *flagsp,
887                 int *datap,
888                 int orient,
889                 int oneplushalf)
890 {
891         int v, flag;
892         
893         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
894         
895         flag = *flagsp;
896         if (!(flag & (T1_SIG | T1_VISIT))) {
897                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
898                 if (opj_mqc_decode(mqc)) {
899                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
900                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
901                         *datap = v ? -oneplushalf : oneplushalf;
902                         opj_t1_updateflags(flagsp, v, t1->flags_stride);
903                 }
904         }
905         *flagsp &= ~T1_VISIT;
906 }                               /* VSC and  BYPASS by Antonin */
907
908 static void opj_t1_dec_clnpass_step_vsc(
909                 opj_t1_t *t1,
910                 opj_flag_t *flagsp,
911                 int *datap,
912                 int orient,
913                 int oneplushalf,
914                 int partial,
915                 int vsc)
916 {
917         int v, flag;
918         
919         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
920         
921         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
922         if (partial) {
923                 goto LABEL_PARTIAL;
924         }
925         if (!(flag & (T1_SIG | T1_VISIT))) {
926                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
927                 if (opj_mqc_decode(mqc)) {
928 LABEL_PARTIAL:
929                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
930                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
931                         *datap = v ? -oneplushalf : oneplushalf;
932                         opj_t1_updateflags(flagsp, v, t1->flags_stride);
933                 }
934         }
935         *flagsp &= ~T1_VISIT;
936 }
937
938 void opj_t1_enc_clnpass(
939                 opj_t1_t *t1,
940                 OPJ_INT32 bpno,
941                 OPJ_UINT32 orient,
942                 OPJ_INT32 *nmsedec,
943                 OPJ_UINT32 cblksty)
944 {
945         OPJ_UINT32 i, j, k;
946         OPJ_INT32 one;
947         OPJ_UINT32 agg, runlen, vsc;
948         
949         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
950         
951         *nmsedec = 0;
952         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
953         for (k = 0; k < t1->h; k += 4) {
954                 for (i = 0; i < t1->w; ++i) {
955                         if (k + 3 < t1->h) {
956                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
957                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
958                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
959                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
961                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
962                                 } else {
963                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
964                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
965                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
966                                                 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
967                                 }
968                         } else {
969                                 agg = 0;
970                         }
971                         if (agg) {
972                                 for (runlen = 0; runlen < 4; ++runlen) {
973                                         if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
974                                                 break;
975                                 }
976                                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
977                                 opj_mqc_encode(mqc, runlen != 4);
978                                 if (runlen == 4) {
979                                         continue;
980                                 }
981                                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
982                                 opj_mqc_encode(mqc, runlen >> 1);
983                                 opj_mqc_encode(mqc, runlen & 1);
984                         } else {
985                                 runlen = 0;
986                         }
987                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
988                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
989                                 opj_t1_enc_clnpass_step(
990                                                 t1,
991                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
992                                                 &t1->data[(j * t1->w) + i],
993                                                 orient,
994                                                 bpno,
995                                                 one,
996                                                 nmsedec,
997                                                 agg && (j == k + runlen),
998                                                 vsc);
999                         }
1000                 }
1001         }
1002 }
1003
1004 static void opj_t1_dec_clnpass(
1005                 opj_t1_t *t1,
1006                 int bpno,
1007                 int orient,
1008                 int cblksty)
1009 {
1010         int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
1011         int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1012         
1013         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1014         
1015         one = 1 << bpno;
1016         half = one >> 1;
1017         oneplushalf = one | half;
1018         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1019         for (k = 0; k < t1->h; k += 4) {
1020                 for (i = 0; i < t1->w; ++i) {
1021                         if (k + 3 < t1->h) {
1022                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1023                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1024                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1025                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
1026                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1027                                 } else {
1028                                 agg = 0;
1029                         }
1030                         if (agg) {
1031                                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1032                                 if (!opj_mqc_decode(mqc)) {
1033                                         continue;
1034                                 }
1035                                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1036                                 runlen = opj_mqc_decode(mqc);
1037                                 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1038                         } else {
1039                                 runlen = 0;
1040                         }
1041                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1042                                         vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1043                                         opj_t1_dec_clnpass_step_vsc(
1044                                                 t1,
1045                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1046                                                 &t1->data[(j * t1->w) + i],
1047                                                 orient,
1048                                                 oneplushalf,
1049                                                 agg && (j == k + runlen),
1050                                                 vsc);
1051                         }
1052                 }
1053         }
1054         } else {
1055                 int *data1 = t1->data;
1056                 opj_flag_t *flags1 = &t1->flags[1];
1057                 for (k = 0; k < (t1->h & ~3); k += 4) {
1058                         for (i = 0; i < t1->w; ++i) {
1059                                 int *data2 = data1 + i;
1060                                 opj_flag_t *flags2 = flags1 + i;
1061                                 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1062                                         || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1063                                         || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1064                                         || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1065                                 if (agg) {
1066                                         opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1067                                         if (!opj_mqc_decode(mqc)) {
1068                                                 continue;
1069                                         }
1070                                         opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1071                                         runlen = opj_mqc_decode(mqc);
1072                                         runlen = (runlen << 1) | opj_mqc_decode(mqc);
1073                                         flags2 += runlen * t1->flags_stride;
1074                                         data2 += runlen * t1->w;
1075                                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1076                                                 flags2 += t1->flags_stride;
1077                                                 if (agg && (j == k + runlen)) {
1078                                                         opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1079                                                 } else {
1080                                                         opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1081                                                 }
1082                                                 data2 += t1->w;
1083                                         }
1084                                 } else {
1085                                         flags2 += t1->flags_stride;
1086                                         opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1087                                         data2 += t1->w;
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                                 }
1098                         }
1099                         data1 += t1->w << 2;
1100                         flags1 += t1->flags_stride << 2;
1101                 }
1102                 for (i = 0; i < t1->w; ++i) {
1103                         int *data2 = data1 + i;
1104                         opj_flag_t *flags2 = flags1 + i;
1105                         for (j = k; j < t1->h; ++j) {
1106                                 flags2 += t1->flags_stride;
1107                                 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1108                                 data2 += t1->w;
1109                         }
1110                 }
1111         }
1112
1113         if (segsym) {
1114                 int v = 0;
1115                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1116                 v = opj_mqc_decode(mqc);
1117                 v = (v << 1) | opj_mqc_decode(mqc);
1118                 v = (v << 1) | opj_mqc_decode(mqc);
1119                 v = (v << 1) | opj_mqc_decode(mqc);
1120                 /*
1121                 if (v!=0xa) {
1122                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1123                 } 
1124                 */
1125         }
1126 }                               /* VSC and  BYPASS by Antonin */
1127
1128
1129 /** mod fixed_quality */
1130 static OPJ_FLOAT64 opj_t1_getwmsedec(
1131                 OPJ_INT32 nmsedec,
1132                 OPJ_UINT32 compno,
1133                 OPJ_UINT32 level,
1134                 OPJ_UINT32 orient,
1135                 OPJ_INT32 bpno,
1136                 OPJ_UINT32 qmfbid,
1137                 OPJ_FLOAT64 stepsize,
1138                 OPJ_UINT32 numcomps,
1139                 const OPJ_FLOAT64 * mct_norms)
1140 {
1141         OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1142
1143         if (mct_norms) {
1144                 w1 = mct_norms[compno];
1145         }
1146
1147         if (qmfbid == 1) {
1148                 w2 = opj_dwt_getnorm(level, orient);
1149         } else {        /* if (qmfbid == 0) */
1150                 w2 = opj_dwt_getnorm_real(level, orient);
1151         }
1152
1153         wmsedec = w1 * w2 * stepsize * (1 << bpno);
1154         wmsedec *= wmsedec * nmsedec / 8192.0;
1155
1156         return wmsedec;
1157 }
1158
1159 opj_bool opj_t1_allocate_buffers(
1160                 opj_t1_t *t1,
1161                 OPJ_UINT32 w,
1162                 OPJ_UINT32 h)
1163 {
1164         OPJ_UINT32 datasize=w * h;
1165         OPJ_UINT32 flagssize;
1166
1167         if(datasize > t1->datasize){
1168                 opj_aligned_free(t1->data);
1169                 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1170                 if(!t1->data){
1171                         return OPJ_FALSE;
1172                 }
1173                 t1->datasize=datasize;
1174         }
1175         memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1176
1177         t1->flags_stride=w+2;
1178         flagssize=t1->flags_stride * (h+2);
1179
1180         if(flagssize > t1->flagssize){
1181                 opj_aligned_free(t1->flags);
1182                 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1183                 if(!t1->flags){
1184                         return OPJ_FALSE;
1185                 }
1186                 t1->flagssize=flagssize;
1187         }
1188         memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1189
1190         t1->w=w;
1191         t1->h=h;
1192
1193         return OPJ_TRUE;
1194 }
1195
1196 /* ----------------------------------------------------------------------- */
1197
1198 /* ----------------------------------------------------------------------- */
1199 /**
1200  * Creates a new Tier 1 handle
1201  * and initializes the look-up tables of the Tier-1 coder/decoder
1202  * @return a new T1 handle if successful, returns NULL otherwise
1203 */
1204 opj_t1_t* opj_t1_create()
1205 {
1206         opj_t1_t *l_t1 = 00;
1207
1208         l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1209         if (!l_t1) {
1210                 return 00;
1211         }
1212         memset(l_t1,0,sizeof(opj_t1_t));
1213
1214         /* create MQC and RAW handles */
1215         l_t1->mqc = opj_mqc_create();
1216         if (! l_t1->mqc) {
1217                 opj_t1_destroy(l_t1);
1218                 return 00;
1219         }
1220
1221         l_t1->raw = opj_raw_create();
1222         if (! l_t1->raw) {
1223                 opj_t1_destroy(l_t1);
1224                 return 00;
1225         }
1226
1227         return l_t1;
1228 }
1229
1230
1231 /**
1232  * Destroys a previously created T1 handle
1233  *
1234  * @param p_t1 Tier 1 handle to destroy
1235 */
1236 void opj_t1_destroy(opj_t1_t *p_t1)
1237 {
1238         if (! p_t1) {
1239                 return;
1240         }
1241
1242         /* destroy MQC and RAW handles */
1243         opj_mqc_destroy(p_t1->mqc);
1244         p_t1->mqc = 00;
1245         opj_raw_destroy(p_t1->raw);
1246         p_t1->raw = 00;
1247         
1248     if (p_t1->data) {
1249                 opj_aligned_free(p_t1->data);
1250                 p_t1->data = 00;
1251         }
1252
1253         if (p_t1->flags) {
1254                 opj_aligned_free(p_t1->flags);
1255                 p_t1->flags = 00;
1256         }
1257
1258         opj_free(p_t1);
1259 }
1260
1261 opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
1262                             opj_tcd_tilecomp_t* tilec,
1263                             opj_tccp_t* tccp
1264                             )
1265 {
1266         OPJ_UINT32 resno, bandno, precno, cblkno;
1267         OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1268
1269         for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1270                 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1271
1272                 for (bandno = 0; bandno < res->numbands; ++bandno) {
1273                         opj_tcd_band_t* restrict band = &res->bands[bandno];
1274
1275                         for (precno = 0; precno < res->pw * res->ph; ++precno) {
1276                                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1277
1278                                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1279                                         opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1280                                         OPJ_INT32* restrict datap;
1281                                         /*void* restrict tiledp;*/
1282                                         OPJ_UINT32 cblk_w, cblk_h;
1283                                         OPJ_INT32 x, y;
1284                                         OPJ_UINT32 i, j;
1285
1286                     if (OPJ_FALSE == opj_t1_decode_cblk(
1287                                             t1,
1288                                             cblk,
1289                                             band->bandno,
1290                                             tccp->roishift,
1291                                             tccp->cblksty)) {
1292                             return OPJ_FALSE;
1293                     }
1294
1295                                         x = cblk->x0 - band->x0;
1296                                         y = cblk->y0 - band->y0;
1297                                         if (band->bandno & 1) {
1298                                                 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1299                                                 x += pres->x1 - pres->x0;
1300                                         }
1301                                         if (band->bandno & 2) {
1302                                                 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1303                                                 y += pres->y1 - pres->y0;
1304                                         }
1305
1306                                         datap=t1->data;
1307                                         cblk_w = t1->w;
1308                                         cblk_h = t1->h;
1309
1310                                         if (tccp->roishift) {
1311                                                 OPJ_INT32 thresh = 1 << tccp->roishift;
1312                                                 for (j = 0; j < cblk_h; ++j) {
1313                                                         for (i = 0; i < cblk_w; ++i) {
1314                                                                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1315                                                                 OPJ_INT32 mag = abs(val);
1316                                                                 if (mag >= thresh) {
1317                                                                         mag >>= tccp->roishift;
1318                                                                         datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1319                                                                 }
1320                                                         }
1321                                                 }
1322                                         }
1323
1324                                         /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1325                                         if (tccp->qmfbid == 1) {
1326                         int* restrict tiledp = &tilec->data[(y * tile_w) + x];
1327                                                 for (j = 0; j < cblk_h; ++j) {
1328                                                         for (i = 0; i < cblk_w; ++i) {
1329                                                                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1330                                                                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1331                                                         }
1332                                                 }
1333                                         } else {                /* if (tccp->qmfbid == 0) */
1334                         float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
1335                                                 for (j = 0; j < cblk_h; ++j) {
1336                             float* restrict tiledp2 = tiledp;
1337                                                         for (i = 0; i < cblk_w; ++i) {
1338                                 float tmp = *datap * band->stepsize;
1339                                 *tiledp2 = tmp;
1340                                 datap++;
1341                                 tiledp2++;
1342                                                                 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1343                                                                 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1344
1345                                                         }
1346                             tiledp += tile_w;
1347                                                 }
1348                                         }
1349                     //opj_free(cblk->data);
1350                                         //opj_free(cblk->segs);
1351                                         /*cblk->segs = 00;*/
1352                                 } /* cblkno */
1353                 //opj_free(precinct->cblks.dec);
1354                         } /* precno */
1355                 } /* bandno */
1356         } /* resno */
1357         return OPJ_TRUE;
1358 }
1359
1360
1361 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;
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                                 cblk->x1 - cblk->x0,
1378                                 cblk->y1 - cblk->y0))
1379         {
1380                 return OPJ_FALSE;
1381         }
1382
1383         bpno = roishift + cblk->numbps - 1;
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 <= ((OPJ_INT32) (cblk->numbps) - 1) - 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; ++passno) {
1409 /*                      switch (passtype) {
1410                                 case 0:
1411                                         opj_t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1412                                         break;
1413                                 case 1:
1414                                         opj_t1_dec_refpass(t1, bpno+1, type, cblksty);
1415                                         break;
1416                                 case 2:
1417                                         opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1418                                         break;
1419                         }*/
1420             switch (passtype) {
1421                 case 0:
1422                     if (type == T1_TYPE_RAW) {
1423                         opj_t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1424                     } else {
1425                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1426                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1427                         } else {
1428                             opj_t1_dec_sigpass_mqc(t1, bpno+1, orient);
1429                         }
1430                     }
1431                     break;
1432                 case 1:
1433                     if (type == T1_TYPE_RAW) {
1434                             opj_t1_dec_refpass_raw(t1, bpno+1, cblksty);
1435                     } else {
1436                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1437                             opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1438                         } else {
1439                             opj_t1_dec_refpass_mqc(t1, bpno+1);
1440                         }
1441                     }
1442                     break;
1443                 case 2:
1444                     opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1445                     break;
1446             }
1447
1448                         if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1449                                 opj_mqc_resetstates(mqc);
1450                                 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1451                                 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1452                                 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1453                         }
1454                         if (++passtype == 3) {
1455                                 passtype = 0;
1456                                 bpno--;
1457                         }
1458                 }
1459         }
1460         return OPJ_TRUE;
1461 }
1462
1463
1464
1465
1466 opj_bool opj_t1_encode_cblks(   opj_t1_t *t1,
1467                                 opj_tcd_tile_t *tile,
1468                                 opj_tcp_t *tcp,
1469                                 const OPJ_FLOAT64 * mct_norms
1470                                 )
1471 {
1472         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1473
1474         tile->distotile = 0;            /* fixed_quality */
1475
1476         for (compno = 0; compno < tile->numcomps; ++compno) {
1477                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1478                 opj_tccp_t* tccp = &tcp->tccps[compno];
1479                 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1480
1481                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1482                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1483
1484                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1485                                 opj_tcd_band_t* restrict band = &res->bands[bandno];
1486                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1487
1488                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1489                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1490
1491                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1492                                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1493                                                 OPJ_INT32 * restrict datap;
1494                                                 OPJ_INT32* restrict tiledp;
1495                                                 OPJ_UINT32 cblk_w;
1496                                                 OPJ_UINT32 cblk_h;
1497                                                 OPJ_UINT32 i, j;
1498
1499                                                 OPJ_INT32 x = cblk->x0 - band->x0;
1500                                                 OPJ_INT32 y = cblk->y0 - band->y0;
1501                                                 if (band->bandno & 1) {
1502                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1503                                                         x += pres->x1 - pres->x0;
1504                                                 }
1505                                                 if (band->bandno & 2) {
1506                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1507                                                         y += pres->y1 - pres->y0;
1508                                                 }
1509
1510                                                 if(!opj_t1_allocate_buffers(
1511                                                                         t1,
1512                                                                         cblk->x1 - cblk->x0,
1513                                                                         cblk->y1 - cblk->y0))
1514                                                 {
1515                                                         return OPJ_FALSE;
1516                                                 }
1517
1518                                                 datap=t1->data;
1519                                                 cblk_w = t1->w;
1520                                                 cblk_h = t1->h;
1521
1522                                                 tiledp=&tilec->data[(y * tile_w) + x];
1523                                                 if (tccp->qmfbid == 1) {
1524                                                         for (j = 0; j < cblk_h; ++j) {
1525                                                                 for (i = 0; i < cblk_w; ++i) {
1526                                                                         OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1527                                                                         datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1528                                                                 }
1529                                                         }
1530                                                 } else {                /* if (tccp->qmfbid == 0) */
1531                                                         for (j = 0; j < cblk_h; ++j) {
1532                                                                 for (i = 0; i < cblk_w; ++i) {
1533                                                                         OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1534                                                                         datap[(j * cblk_w) + i] =
1535                                                                                 opj_int_fix_mul(
1536                                                                                 tmp,
1537                                                                                 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1538                                                                 }
1539                                                         }
1540                                                 }
1541
1542                                                 opj_t1_encode_cblk(
1543                                                                 t1,
1544                                                                 cblk,
1545                                                                 band->bandno,
1546                                                                 compno,
1547                                                                 tilec->numresolutions - 1 - resno,
1548                                                                 tccp->qmfbid,
1549                                                                 band->stepsize,
1550                                                                 tccp->cblksty,
1551                                                                 tile->numcomps,
1552                                                                 tile,
1553                                                                 mct_norms);
1554
1555                                         } /* cblkno */
1556                                 } /* precno */
1557                         } /* bandno */
1558                 } /* resno  */
1559         } /* compno  */
1560         return OPJ_TRUE;
1561 }
1562
1563 /** mod fixed_quality */
1564 void opj_t1_encode_cblk(opj_t1_t *t1,
1565                         opj_tcd_cblk_enc_t* cblk,
1566                         OPJ_UINT32 orient,
1567                         OPJ_UINT32 compno,
1568                         OPJ_UINT32 level,
1569                         OPJ_UINT32 qmfbid,
1570                         OPJ_FLOAT64 stepsize,
1571                         OPJ_UINT32 cblksty,
1572                         OPJ_UINT32 numcomps,
1573                         opj_tcd_tile_t * tile,
1574                         const OPJ_FLOAT64 * mct_norms)
1575 {
1576         OPJ_FLOAT64 cumwmsedec = 0.0;
1577
1578         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1579
1580         OPJ_UINT32 passno;
1581         OPJ_INT32 bpno;
1582         OPJ_UINT32 passtype;
1583         OPJ_INT32 nmsedec = 0;
1584         OPJ_INT32 max;
1585         OPJ_UINT32 i;
1586         OPJ_BYTE type = T1_TYPE_MQ;
1587         OPJ_FLOAT64 tempwmsedec;
1588
1589         max = 0;
1590         for (i = 0; i < t1->w * t1->h; ++i) {
1591                 OPJ_INT32 tmp = abs(t1->data[i]);
1592                 max = opj_int_max(max, tmp);
1593         }
1594
1595         cblk->numbps = max ? (opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1596
1597         bpno = 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) ;
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 == (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 void opj_t1_dec_refpass(opj_t1_t *t1,
1701                         OPJ_INT32 bpno,
1702                         OPJ_BYTE type,
1703                         OPJ_UINT32 cblksty)
1704 {
1705         OPJ_UINT32 i, j, k;
1706         OPJ_INT32 one, poshalf, neghalf;
1707         OPJ_UINT32 vsc;
1708         one = 1 << bpno;
1709         poshalf = one >> 1;
1710         neghalf = bpno > 0 ? -poshalf : -1;
1711         for (k = 0; k < t1->h; k += 4) {
1712                 for (i = 0; i < t1->w; ++i) {
1713                         for (j = k; j < k + 4 && j < t1->h; ++j) {
1714                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1715                                 opj_t1_dec_refpass_step(
1716                                                 t1,
1717                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1718                                                 &t1->data[(j * t1->w) + i],
1719                                                 poshalf,
1720                                                 neghalf,
1721                                                 type,
1722                                                 vsc);
1723                         }
1724                 }
1725         }
1726 }                               /* VSC and  BYPASS by Antonin */
1727
1728
1729 void opj_t1_dec_refpass_step(   opj_t1_t *t1,
1730                                 opj_flag_t *flagsp,
1731                                 OPJ_INT32 *datap,
1732                                 OPJ_INT32 poshalf,
1733                                 OPJ_INT32 neghalf,
1734                                 OPJ_BYTE type,
1735                                 OPJ_UINT32 vsc)
1736 {
1737         OPJ_INT32  t;
1738         OPJ_UINT32 v,flag;
1739
1740         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1741         opj_raw_t *raw = t1->raw;       /* RAW component */
1742
1743         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1744         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1745                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
1746                 if (type == T1_TYPE_RAW) {
1747                         v = opj_raw_decode(raw);
1748                 } else {
1749                         v = opj_mqc_decode(mqc);
1750                 }
1751                 t = v ? poshalf : neghalf;
1752                 *datap += *datap < 0 ? -t : t;
1753                 *flagsp |= T1_REFINE;
1754         }
1755 }                               /* VSC and  BYPASS by Antonin  */
1756
1757 void opj_t1_dec_sigpass(opj_t1_t *t1,
1758                         OPJ_INT32 bpno,
1759                         OPJ_UINT32 orient,
1760                         OPJ_BYTE type,
1761                         OPJ_UINT32 cblksty)
1762 {
1763         OPJ_UINT32 i, j, k, vsc;
1764         OPJ_INT32 one, half, oneplushalf;
1765         one = 1 << bpno;
1766         half = one >> 1;
1767         oneplushalf = one | half;
1768         for (k = 0; k < t1->h; k += 4) {
1769                 for (i = 0; i < t1->w; ++i) {
1770                         for (j = k; j < k + 4 && j < t1->h; ++j) {
1771                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1772                                 opj_t1_dec_sigpass_step(
1773                                                 t1,
1774                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1775                                                 &t1->data[(j * t1->w) + i],
1776                                                 orient,
1777                                                 oneplushalf,
1778                                                 type,
1779                                                 vsc);
1780                         }
1781                 }
1782         }
1783 }                               /* VSC and  BYPASS by Antonin */
1784
1785 void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
1786                                 opj_flag_t *flagsp,
1787                                 OPJ_INT32 *datap,
1788                                 OPJ_UINT32 orient,
1789                                 OPJ_INT32 oneplushalf,
1790                                 OPJ_BYTE type,
1791                                 OPJ_UINT32 vsc)
1792 {
1793         OPJ_UINT32 v, flag;
1794
1795         opj_raw_t *raw = t1->raw;       /* RAW component */
1796         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1797
1798         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1799         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1800                 if (type == T1_TYPE_RAW) {
1801                         if (opj_raw_decode(raw)) {
1802                                 v = opj_raw_decode(raw);        /* ESSAI */
1803                                 *datap = v ? -oneplushalf : oneplushalf;
1804                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1805                         }
1806                 } else {
1807                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1808                         if (opj_mqc_decode(mqc)) {
1809                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1810                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1811                                 *datap = v ? -oneplushalf : oneplushalf;
1812                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1813                         }
1814                 }
1815                 *flagsp |= T1_VISIT;
1816         }
1817 }                               /* VSC and  BYPASS by Antonin */
1818