[trunk] Remove deprecated functions (not called anywhere in the code base).
[openjpeg.git] / src / lib / openjp2 / t1.c
index 2a8a67323ab7ada15936bfa7c3995ac5922dea3c..6e2b557f4ebc2ee804ec64ec6ef0338a141e17b1 100644 (file)
@@ -45,12 +45,12 @@ static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
-static void opj_t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
+static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
 /**
 Encode significant pass
 */
 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
-                                    flag_t *flagsp,
+                                    opj_flag_t *flagsp,
                                     OPJ_INT32 *datap,
                                     OPJ_UINT32 orient,
                                     OPJ_INT32 bpno,
@@ -63,13 +63,35 @@ static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
 Decode significant pass
 */
 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
-                                    flag_t *flagsp,
+                                    opj_flag_t *flagsp,
                                     OPJ_INT32 *datap,
                                     OPJ_UINT32 orient,
                                     OPJ_INT32 oneplushalf,
                                     OPJ_BYTE type,
                                     OPJ_UINT32 vsc);
 
+static INLINE void opj_t1_dec_sigpass_step_raw(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 orient,
+                OPJ_INT32 oneplushalf,
+                OPJ_INT32 vsc);
+static INLINE void opj_t1_dec_sigpass_step_mqc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 orient,
+                OPJ_INT32 oneplushalf);
+static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 orient,
+                OPJ_INT32 oneplushalf,
+                OPJ_INT32 vsc);
+
+
 /**
 Encode significant pass
 */
@@ -83,18 +105,27 @@ static void opj_t1_enc_sigpass( opj_t1_t *t1,
 /**
 Decode significant pass
 */
-static void opj_t1_dec_sigpass( opj_t1_t *t1,
-                                OPJ_INT32 bpno,
-                                OPJ_UINT32 orient,
-                                OPJ_BYTE type,
-                                OPJ_UINT32 cblksty);
+static void opj_t1_dec_sigpass_raw(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 orient,
+                OPJ_INT32 cblksty);
+static void opj_t1_dec_sigpass_mqc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 orient);
+static void opj_t1_dec_sigpass_mqc_vsc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 orient);
+
 
 
 /**
 Encode refinement pass
 */
 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
-                                    flag_t *flagsp,
+                                    opj_flag_t *flagsp,
                                     OPJ_INT32 *datap,
                                     OPJ_INT32 bpno,
                                     OPJ_INT32 one,
@@ -115,28 +146,58 @@ static void opj_t1_enc_refpass( opj_t1_t *t1,
 /**
 Decode refinement pass
 */
-static void opj_t1_dec_refpass( opj_t1_t *t1,
-                                OPJ_INT32 bpno,
-                                OPJ_BYTE type,
-                                OPJ_UINT32 cblksty);
+static void opj_t1_dec_refpass_raw(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 cblksty);
+static void opj_t1_dec_refpass_mqc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno);
+static void opj_t1_dec_refpass_mqc_vsc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno);
+
 
 /**
 Decode refinement pass
 */
 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
-                                    flag_t *flagsp,
+                                    opj_flag_t *flagsp,
                                     OPJ_INT32 *datap,
                                     OPJ_INT32 poshalf,
                                     OPJ_INT32 neghalf,
                                     OPJ_BYTE type,
                                     OPJ_UINT32 vsc);
 
+static INLINE void  opj_t1_dec_refpass_step_raw(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 poshalf,
+                OPJ_INT32 neghalf,
+                OPJ_INT32 vsc);
+static INLINE void opj_t1_dec_refpass_step_mqc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 poshalf,
+                OPJ_INT32 neghalf);
+static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 poshalf,
+                OPJ_INT32 neghalf,
+                OPJ_INT32 vsc);
+
+
+
 /**
 Encode clean-up pass
 */
 static void opj_t1_enc_clnpass_step(
                opj_t1_t *t1,
-               flag_t *flagsp,
+               opj_flag_t *flagsp,
                OPJ_INT32 *datap,
                OPJ_UINT32 orient,
                OPJ_INT32 bpno,
@@ -147,26 +208,26 @@ static void opj_t1_enc_clnpass_step(
 /**
 Decode clean-up pass
 */
-static void t1_dec_clnpass_step_partial(
+static void opj_t1_dec_clnpass_step_partial(
                opj_t1_t *t1,
-               flag_t *flagsp,
-               int *datap,
-               int orient,
-               int oneplushalf);
-static void t1_dec_clnpass_step(
+               opj_flag_t *flagsp,
+               OPJ_INT32 *datap,
+               OPJ_INT32 orient,
+               OPJ_INT32 oneplushalf);
+static void opj_t1_dec_clnpass_step(
                opj_t1_t *t1,
-               flag_t *flagsp,
-               int *datap,
-               int orient,
-               int oneplushalf);
-static void t1_dec_clnpass_step_vsc(
+               opj_flag_t *flagsp,
+               OPJ_INT32 *datap,
+               OPJ_INT32 orient,
+               OPJ_INT32 oneplushalf);
+static void opj_t1_dec_clnpass_step_vsc(
                opj_t1_t *t1,
-               flag_t *flagsp,
-               int *datap,
-               int orient,
-               int oneplushalf,
-               int partial,
-               int vsc);
+               opj_flag_t *flagsp,
+               OPJ_INT32 *datap,
+               OPJ_INT32 orient,
+               OPJ_INT32 oneplushalf,
+               OPJ_INT32 partial,
+               OPJ_INT32 vsc);
 /**
 Encode clean-up pass
 */
@@ -179,23 +240,13 @@ static void opj_t1_enc_clnpass(
 /**
 Decode clean-up pass
 */
-static void t1_dec_clnpass(
+static void opj_t1_dec_clnpass(
                opj_t1_t *t1,
-               int bpno,
-               int orient,
-               int cblksty);
-static double t1_getwmsedec(
-               int nmsedec,
-               int compno,
-               int level,
-               int orient,
-               int bpno,
-               int qmfbid,
-               double stepsize,
-               int numcomps,
-               int mct);
-
-static OPJ_FLOAT64 t1_getwmsedec_v2(
+               OPJ_INT32 bpno,
+               OPJ_INT32 orient,
+               OPJ_INT32 cblksty);
+
+static OPJ_FLOAT64 opj_t1_getwmsedec(
                OPJ_INT32 nmsedec,
                OPJ_UINT32 compno,
                OPJ_UINT32 level,
@@ -207,7 +258,7 @@ static OPJ_FLOAT64 t1_getwmsedec_v2(
                const OPJ_FLOAT64 * mct_norms);
 
 static void opj_t1_encode_cblk( opj_t1_t *t1,
-                                opj_tcd_cblk_enc_v2_t* cblk,
+                                opj_tcd_cblk_enc_t* cblk,
                                 OPJ_UINT32 orient,
                                 OPJ_UINT32 compno,
                                 OPJ_UINT32 level,
@@ -215,7 +266,7 @@ static void opj_t1_encode_cblk( opj_t1_t *t1,
                                 OPJ_FLOAT64 stepsize,
                                 OPJ_UINT32 cblksty,
                                 OPJ_UINT32 numcomps,
-                                opj_tcd_tile_v2_t * tile,
+                                opj_tcd_tile_t * tile,
                                 const OPJ_FLOAT64 * mct_norms);
 
 /**
@@ -226,13 +277,13 @@ Decode 1 code-block
 @param roishift Region of interest shifting value
 @param cblksty Code-block style
 */
-static opj_bool opj_t1_decode_cblk( opj_t1_t *t1,
-                                    opj_tcd_cblk_dec_v2_t* cblk,
+static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
+                                    opj_tcd_cblk_dec_t* cblk,
                                     OPJ_UINT32 orient,
                                     OPJ_UINT32 roishift,
                                     OPJ_UINT32 cblksty);
 
-opj_bool opj_t1_allocate_buffers(   opj_t1_t *t1,
+OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
                                     OPJ_UINT32 w,
                                     OPJ_UINT32 h);
 
@@ -276,11 +327,11 @@ OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
 }
 
-void opj_t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
-       flag_t *np = flagsp - stride;
-       flag_t *sp = flagsp + stride;
+void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
+       opj_flag_t *np = flagsp - stride;
+       opj_flag_t *sp = flagsp + stride;
 
-       static const flag_t mod[] = {
+       static const opj_flag_t mod[] = {
                T1_SIG_S, T1_SIG_S|T1_SGN_S,
                T1_SIG_E, T1_SIG_E|T1_SGN_E,
                T1_SIG_W, T1_SIG_W|T1_SGN_W,
@@ -301,7 +352,7 @@ void opj_t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
 }
 
 void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
-                                flag_t *flagsp,
+                                opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_UINT32 orient,
                                 OPJ_INT32 bpno,
@@ -318,7 +369,7 @@ void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
        
        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
-               v = int_abs(*datap) & one ? 1 : 0;
+               v = opj_int_abs(*datap) & one ? 1 : 0;
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));       /* ESSAI */
                if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
                        opj_mqc_bypass_enc(mqc, v);
@@ -327,7 +378,7 @@ void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
                }
                if (v) {
                        v = *datap < 0 ? 1 : 0;
-                       *nmsedec +=     opj_t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
+                       *nmsedec +=     opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
                        opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));       /* ESSAI */
                        if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
                                opj_mqc_bypass_enc(mqc, v);
@@ -339,6 +390,82 @@ void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
                *flagsp |= T1_VISIT;
        }
 }
+
+
+static INLINE void opj_t1_dec_sigpass_step_raw(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 orient,
+                OPJ_INT32 oneplushalf,
+                OPJ_INT32 vsc)
+{
+        OPJ_INT32 v, flag;
+        opj_raw_t *raw = t1->raw;       /* RAW component */
+        OPJ_ARG_NOT_USED(orient);
+       
+        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+                        if (opj_raw_decode(raw)) {
+                                v = opj_raw_decode(raw);    /* ESSAI */
+                                *datap = v ? -oneplushalf : oneplushalf;
+                                opj_t1_updateflags(flagsp, v, t1->flags_stride);
+                        }
+                *flagsp |= T1_VISIT;
+        }
+}      
+
+INLINE void opj_t1_dec_sigpass_step_mqc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 orient,
+                OPJ_INT32 oneplushalf)
+{
+        OPJ_INT32 v, flag;
+       
+        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+        flag = *flagsp;
+        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+                        opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
+                        if (opj_mqc_decode(mqc)) {
+                                opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
+                                v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
+                                *datap = v ? -oneplushalf : oneplushalf;
+                                opj_t1_updateflags(flagsp, v, t1->flags_stride);
+                        }
+                *flagsp |= T1_VISIT;
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 orient,
+                OPJ_INT32 oneplushalf,
+                OPJ_INT32 vsc)
+{
+        OPJ_INT32 v, flag;
+       
+        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
+                opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
+                if (opj_mqc_decode(mqc)) {
+                        opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
+                        v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
+                        *datap = v ? -oneplushalf : oneplushalf;
+                        opj_t1_updateflags(flagsp, v, t1->flags_stride);
+                }
+                *flagsp |= T1_VISIT;
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+
+
 void opj_t1_enc_sigpass(opj_t1_t *t1,
                         OPJ_INT32 bpno,
                         OPJ_UINT32 orient,
@@ -371,9 +498,106 @@ void opj_t1_enc_sigpass(opj_t1_t *t1,
        }
 }
 
+void opj_t1_dec_sigpass_raw(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 orient,
+                OPJ_INT32 cblksty)
+{
+        OPJ_INT32 one, half, oneplushalf, vsc;
+        OPJ_UINT32 i, j, k; 
+        one = 1 << bpno;
+        half = one >> 1;
+        oneplushalf = one | half;
+        for (k = 0; k < t1->h; k += 4) {
+                for (i = 0; i < t1->w; ++i) {
+                        for (j = k; j < k + 4 && j < t1->h; ++j) {
+                                vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+                                opj_t1_dec_sigpass_step_raw(
+                                                t1,
+                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+                                                &t1->data[(j * t1->w) + i],
+                                                orient,
+                                                oneplushalf,
+                                                vsc);
+                        }
+                }
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+void opj_t1_dec_sigpass_mqc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 orient)
+{
+        OPJ_INT32 one, half, oneplushalf;
+        OPJ_UINT32 i, j, k;
+        OPJ_INT32 *data1 = t1->data;
+        opj_flag_t *flags1 = &t1->flags[1];
+        one = 1 << bpno;
+        half = one >> 1;
+        oneplushalf = one | half;
+        for (k = 0; k < (t1->h & ~3); k += 4) {
+                for (i = 0; i < t1->w; ++i) {
+                        OPJ_INT32 *data2 = data1 + i;
+                        opj_flag_t *flags2 = flags1 + i;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+                        data2 += t1->w;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+                        data2 += t1->w;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+                        data2 += t1->w;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+                        data2 += t1->w;
+                }
+                data1 += t1->w << 2;
+                flags1 += t1->flags_stride << 2;
+        }
+        for (i = 0; i < t1->w; ++i) {
+                OPJ_INT32 *data2 = data1 + i;
+                opj_flag_t *flags2 = flags1 + i;
+                for (j = k; j < t1->h; ++j) {
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
+                        data2 += t1->w;
+                }
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+void opj_t1_dec_sigpass_mqc_vsc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 orient)
+{
+        OPJ_INT32 one, half, oneplushalf, vsc;
+        OPJ_UINT32 i, j, k;
+        one = 1 << bpno;
+        half = one >> 1;
+        oneplushalf = one | half;
+        for (k = 0; k < t1->h; k += 4) {
+                for (i = 0; i < t1->w; ++i) {
+                        for (j = k; j < k + 4 && j < t1->h; ++j) {
+                                vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
+                                opj_t1_dec_sigpass_step_mqc_vsc(
+                                                t1,
+                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+                                                &t1->data[(j * t1->w) + i],
+                                                orient,
+                                                oneplushalf,
+                                                vsc);
+                        }
+                }
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+
 
 void opj_t1_enc_refpass_step(   opj_t1_t *t1,
-                                flag_t *flagsp,
+                                opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_INT32 bpno,
                                 OPJ_INT32 one,
@@ -388,8 +612,8 @@ void opj_t1_enc_refpass_step(   opj_t1_t *t1,
        
        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
-               *nmsedec += opj_t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
-               v = int_abs(*datap) & one ? 1 : 0;
+               *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
+               v = opj_int_abs(*datap) & one ? 1 : 0;
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
                if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
                        opj_mqc_bypass_enc(mqc, v);
@@ -400,6 +624,70 @@ void opj_t1_enc_refpass_step(   opj_t1_t *t1,
        }
 }
 
+INLINE void opj_t1_dec_refpass_step_raw(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 poshalf,
+                OPJ_INT32 neghalf,
+                OPJ_INT32 vsc)
+{
+        OPJ_INT32 v, t, flag;
+       
+        opj_raw_t *raw = t1->raw;       /* RAW component */
+       
+        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+                        v = opj_raw_decode(raw);
+                t = v ? poshalf : neghalf;
+                *datap += *datap < 0 ? -t : t;
+                *flagsp |= T1_REFINE;
+        }
+}                               /* VSC and  BYPASS by Antonin  */
+
+INLINE void opj_t1_dec_refpass_step_mqc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 poshalf,
+                OPJ_INT32 neghalf)
+{
+        OPJ_INT32 v, t, flag;
+       
+        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+        flag = *flagsp;
+        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
+                        v = opj_mqc_decode(mqc);
+                t = v ? poshalf : neghalf;
+                *datap += *datap < 0 ? -t : t;
+                *flagsp |= T1_REFINE;
+                }
+}                               /* VSC and  BYPASS by Antonin  */
+
+INLINE void opj_t1_dec_refpass_step_mqc_vsc(
+                opj_t1_t *t1,
+                opj_flag_t *flagsp,
+                OPJ_INT32 *datap,
+                OPJ_INT32 poshalf,
+                OPJ_INT32 neghalf,
+                OPJ_INT32 vsc)
+{
+        OPJ_INT32 v, t, flag;
+       
+        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
+       
+        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
+        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
+                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
+                v = opj_mqc_decode(mqc);
+                t = v ? poshalf : neghalf;
+                *datap += *datap < 0 ? -t : t;
+                *flagsp |= T1_REFINE;
+        }
+}                               /* VSC and  BYPASS by Antonin  */
+
 
 void opj_t1_enc_refpass(
                opj_t1_t *t1,
@@ -431,10 +719,105 @@ void opj_t1_enc_refpass(
        }
 }
 
+void opj_t1_dec_refpass_raw(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno,
+                OPJ_INT32 cblksty)
+{
+        OPJ_INT32 one, poshalf, neghalf;
+        OPJ_UINT32 i, j, k;
+        OPJ_INT32 vsc;
+        one = 1 << bpno;
+        poshalf = one >> 1;
+        neghalf = bpno > 0 ? -poshalf : -1;
+        for (k = 0; k < t1->h; k += 4) {
+                for (i = 0; i < t1->w; ++i) {
+                        for (j = k; j < k + 4 && j < t1->h; ++j) {
+                                vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+                                opj_t1_dec_refpass_step_raw(
+                                                t1,
+                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+                                                &t1->data[(j * t1->w) + i],
+                                                poshalf,
+                                                neghalf,
+                                                vsc);
+                        }
+                }
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+void opj_t1_dec_refpass_mqc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno)
+{
+        OPJ_INT32 one, poshalf, neghalf;
+        OPJ_UINT32 i, j, k;
+        OPJ_INT32 *data1 = t1->data;
+        opj_flag_t *flags1 = &t1->flags[1];
+        one = 1 << bpno;
+        poshalf = one >> 1;
+        neghalf = bpno > 0 ? -poshalf : -1;
+        for (k = 0; k < (t1->h & ~3); k += 4) {
+                for (i = 0; i < t1->w; ++i) {
+                        OPJ_INT32 *data2 = data1 + i;
+                        opj_flag_t *flags2 = flags1 + i;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+                        data2 += t1->w;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+                        data2 += t1->w;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+                        data2 += t1->w;
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+                        data2 += t1->w;
+                }
+                data1 += t1->w << 2;
+                flags1 += t1->flags_stride << 2;
+        }
+        for (i = 0; i < t1->w; ++i) {
+                OPJ_INT32 *data2 = data1 + i;
+                opj_flag_t *flags2 = flags1 + i;
+                for (j = k; j < t1->h; ++j) {
+                        flags2 += t1->flags_stride;
+                        opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
+                        data2 += t1->w;
+                }
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
+void opj_t1_dec_refpass_mqc_vsc(
+                opj_t1_t *t1,
+                OPJ_INT32 bpno)
+{
+        OPJ_INT32 one, poshalf, neghalf;
+        OPJ_UINT32 i, j, k;
+        OPJ_INT32 vsc;
+        one = 1 << bpno;
+        poshalf = one >> 1;
+        neghalf = bpno > 0 ? -poshalf : -1;
+        for (k = 0; k < t1->h; k += 4) {
+                for (i = 0; i < t1->w; ++i) {
+                        for (j = k; j < k + 4 && j < t1->h; ++j) {
+                                vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
+                                opj_t1_dec_refpass_step_mqc_vsc(
+                                                t1,
+                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
+                                                &t1->data[(j * t1->w) + i],
+                                                poshalf,
+                                                neghalf,
+                                                vsc);
+                        }
+                }
+        }
+}                               /* VSC and  BYPASS by Antonin */
+
 
 void opj_t1_enc_clnpass_step(
                opj_t1_t *t1,
-               flag_t *flagsp,
+               opj_flag_t *flagsp,
                OPJ_INT32 *datap,
                OPJ_UINT32 orient,
                OPJ_INT32 bpno,
@@ -454,11 +837,11 @@ void opj_t1_enc_clnpass_step(
        }
        if (!(*flagsp & (T1_SIG | T1_VISIT))) {
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
-               v = int_abs(*datap) & one ? 1 : 0;
+               v = opj_int_abs(*datap) & one ? 1 : 0;
                opj_mqc_encode(mqc, v);
                if (v) {
 LABEL_PARTIAL:
-                       *nmsedec += opj_t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
+                       *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
                        opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
                        v = *datap < 0 ? 1 : 0;
                        opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
@@ -468,14 +851,14 @@ LABEL_PARTIAL:
        *flagsp &= ~T1_VISIT;
 }
 
-static void t1_dec_clnpass_step_partial(
+static void opj_t1_dec_clnpass_step_partial(
                opj_t1_t *t1,
-               flag_t *flagsp,
-               int *datap,
-               int orient,
-               int oneplushalf)
+               opj_flag_t *flagsp,
+               OPJ_INT32 *datap,
+               OPJ_INT32 orient,
+               OPJ_INT32 oneplushalf)
 {
-       int v, flag;
+       OPJ_INT32 v, flag;
        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
        
        OPJ_ARG_NOT_USED(orient);
@@ -488,14 +871,14 @@ static void t1_dec_clnpass_step_partial(
        *flagsp &= ~T1_VISIT;
 }                              /* VSC and  BYPASS by Antonin */
 
-static void t1_dec_clnpass_step(
+static void opj_t1_dec_clnpass_step(
                opj_t1_t *t1,
-               flag_t *flagsp,
-               int *datap,
-               int orient,
-               int oneplushalf)
+               opj_flag_t *flagsp,
+               OPJ_INT32 *datap,
+               OPJ_INT32 orient,
+               OPJ_INT32 oneplushalf)
 {
-       int v, flag;
+       OPJ_INT32 v, flag;
        
        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
        
@@ -512,16 +895,16 @@ static void t1_dec_clnpass_step(
        *flagsp &= ~T1_VISIT;
 }                              /* VSC and  BYPASS by Antonin */
 
-static void t1_dec_clnpass_step_vsc(
+static void opj_t1_dec_clnpass_step_vsc(
                opj_t1_t *t1,
-               flag_t *flagsp,
-               int *datap,
-               int orient,
-               int oneplushalf,
-               int partial,
-               int vsc)
+               opj_flag_t *flagsp,
+               OPJ_INT32 *datap,
+               OPJ_INT32 orient,
+               OPJ_INT32 oneplushalf,
+               OPJ_INT32 partial,
+               OPJ_INT32 vsc)
 {
-       int v, flag;
+       OPJ_INT32 v, flag;
        
        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
        
@@ -577,7 +960,7 @@ void opj_t1_enc_clnpass(
                        }
                        if (agg) {
                                for (runlen = 0; runlen < 4; ++runlen) {
-                                       if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
+                                       if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
                                                break;
                                }
                                opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
@@ -608,14 +991,15 @@ void opj_t1_enc_clnpass(
        }
 }
 
-static void t1_dec_clnpass(
+static void opj_t1_dec_clnpass(
                opj_t1_t *t1,
-               int bpno,
-               int orient,
-               int cblksty)
+               OPJ_INT32 bpno,
+               OPJ_INT32 orient,
+               OPJ_INT32 cblksty)
 {
-       int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
-       int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
+       OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
+    OPJ_UINT32 i, j, k;
+       OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
        
        opj_mqc_t *mqc = t1->mqc;       /* MQC component */
        
@@ -647,7 +1031,7 @@ static void t1_dec_clnpass(
                        }
                        for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
                                        vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
-                                       t1_dec_clnpass_step_vsc(
+                                       opj_t1_dec_clnpass_step_vsc(
                                                t1,
                                                &t1->flags[((j+1) * t1->flags_stride) + i + 1],
                                                &t1->data[(j * t1->w) + i],
@@ -659,12 +1043,12 @@ static void t1_dec_clnpass(
                }
        }
        } else {
-               int *data1 = t1->data;
-               flag_t *flags1 = &t1->flags[1];
+               OPJ_INT32 *data1 = t1->data;
+               opj_flag_t *flags1 = &t1->flags[1];
                for (k = 0; k < (t1->h & ~3); k += 4) {
                        for (i = 0; i < t1->w; ++i) {
-                               int *data2 = data1 + i;
-                               flag_t *flags2 = flags1 + i;
+                               OPJ_INT32 *data2 = data1 + i;
+                               opj_flag_t *flags2 = flags1 + i;
                                agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
                                        || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
                                        || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
@@ -682,24 +1066,24 @@ static void t1_dec_clnpass(
                                        for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
                                                flags2 += t1->flags_stride;
                                                if (agg && (j == k + runlen)) {
-                                                       t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
+                                                       opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
                                                } else {
-                                                       t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+                                                       opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
                                                }
                                                data2 += t1->w;
                                        }
                                } else {
                                        flags2 += t1->flags_stride;
-                                       t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+                                       opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
                                        data2 += t1->w;
                                        flags2 += t1->flags_stride;
-                                       t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+                                       opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
                                        data2 += t1->w;
                                        flags2 += t1->flags_stride;
-                                       t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+                                       opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
                                        data2 += t1->w;
                                        flags2 += t1->flags_stride;
-                                       t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+                                       opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
                                        data2 += t1->w;
                                }
                        }
@@ -707,18 +1091,18 @@ static void t1_dec_clnpass(
                        flags1 += t1->flags_stride << 2;
                }
                for (i = 0; i < t1->w; ++i) {
-                       int *data2 = data1 + i;
-                       flag_t *flags2 = flags1 + i;
+                       OPJ_INT32 *data2 = data1 + i;
+                       opj_flag_t *flags2 = flags1 + i;
                        for (j = k; j < t1->h; ++j) {
                                flags2 += t1->flags_stride;
-                               t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
+                               opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
                                data2 += t1->w;
                        }
                }
        }
 
        if (segsym) {
-               int v = 0;
+               OPJ_INT32 v = 0;
                opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
                v = opj_mqc_decode(mqc);
                v = (v << 1) | opj_mqc_decode(mqc);
@@ -734,33 +1118,7 @@ static void t1_dec_clnpass(
 
 
 /** mod fixed_quality */
-static double t1_getwmsedec(
-               int nmsedec,
-               int compno,
-               int level,
-               int orient,
-               int bpno,
-               int qmfbid,
-               double stepsize,
-               int numcomps,
-               int mct)
-{
-       double w1, w2, wmsedec;
-       if (qmfbid == 1) {
-               w1 = (mct && numcomps==3) ? opj_mct_getnorm(compno) : 1.0;
-               w2 = dwt_getnorm(level, orient);
-       } else {                        /* if (qmfbid == 0) */
-               w1 = (mct && numcomps==3) ? opj_mct_getnorm_real(compno) : 1.0;
-               w2 = dwt_getnorm_real(level, orient);
-       }
-       wmsedec = w1 * w2 * stepsize * (1 << bpno);
-       wmsedec *= wmsedec * nmsedec / 8192.0;
-       
-       return wmsedec;
-}
-
-/** mod fixed_quality */
-static OPJ_FLOAT64 t1_getwmsedec_v2(
+static OPJ_FLOAT64 opj_t1_getwmsedec(
                OPJ_INT32 nmsedec,
                OPJ_UINT32 compno,
                OPJ_UINT32 level,
@@ -772,6 +1130,7 @@ static OPJ_FLOAT64 t1_getwmsedec_v2(
                const OPJ_FLOAT64 * mct_norms)
 {
        OPJ_FLOAT64 w1 = 1, w2, wmsedec;
+    OPJ_ARG_NOT_USED(numcomps);
 
        if (mct_norms) {
                w1 = mct_norms[compno];
@@ -789,7 +1148,7 @@ static OPJ_FLOAT64 t1_getwmsedec_v2(
        return wmsedec;
 }
 
-opj_bool opj_t1_allocate_buffers(
+OPJ_BOOL opj_t1_allocate_buffers(
                opj_t1_t *t1,
                OPJ_UINT32 w,
                OPJ_UINT32 h)
@@ -812,13 +1171,13 @@ opj_bool opj_t1_allocate_buffers(
 
        if(flagssize > t1->flagssize){
                opj_aligned_free(t1->flags);
-               t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
+               t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
                if(!t1->flags){
                        return OPJ_FALSE;
                }
                t1->flagssize=flagssize;
        }
-       memset(t1->flags,0,flagssize * sizeof(flag_t));
+       memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
 
        t1->w=w;
        t1->h=h;
@@ -851,7 +1210,7 @@ opj_t1_t* opj_t1_create()
                return 00;
        }
 
-       l_t1->raw = raw_create();
+       l_t1->raw = opj_raw_create();
        if (! l_t1->raw) {
                opj_t1_destroy(l_t1);
                return 00;
@@ -875,7 +1234,7 @@ void opj_t1_destroy(opj_t1_t *p_t1)
        /* destroy MQC and RAW handles */
        opj_mqc_destroy(p_t1->mqc);
        p_t1->mqc = 00;
-       raw_destroy(p_t1->raw);
+       opj_raw_destroy(p_t1->raw);
        p_t1->raw = 00;
        
     if (p_t1->data) {
@@ -891,8 +1250,8 @@ void opj_t1_destroy(opj_t1_t *p_t1)
        opj_free(p_t1);
 }
 
-opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
-                            opj_tcd_tilecomp_v2_t* tilec,
+OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
+                            opj_tcd_tilecomp_t* tilec,
                             opj_tccp_t* tccp
                             )
 {
@@ -900,18 +1259,18 @@ opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
        OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
 
        for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
-               opj_tcd_resolution_v2_t* res = &tilec->resolutions[resno];
+               opj_tcd_resolution_t* res = &tilec->resolutions[resno];
 
                for (bandno = 0; bandno < res->numbands; ++bandno) {
-                       opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
+                       opj_tcd_band_t* restrict band = &res->bands[bandno];
 
                        for (precno = 0; precno < res->pw * res->ph; ++precno) {
-                               opj_tcd_precinct_v2_t* precinct = &band->precincts[precno];
+                               opj_tcd_precinct_t* precinct = &band->precincts[precno];
 
                                for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
-                                       opj_tcd_cblk_dec_v2_t* cblk = &precinct->cblks.dec[cblkno];
+                                       opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
                                        OPJ_INT32* restrict datap;
-                                       void* restrict tiledp;
+                                       /*void* restrict tiledp;*/
                                        OPJ_UINT32 cblk_w, cblk_h;
                                        OPJ_INT32 x, y;
                                        OPJ_UINT32 i, j;
@@ -928,11 +1287,11 @@ opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
                                        x = cblk->x0 - band->x0;
                                        y = cblk->y0 - band->y0;
                                        if (band->bandno & 1) {
-                                               opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
+                                               opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
                                                x += pres->x1 - pres->x0;
                                        }
                                        if (band->bandno & 2) {
-                                               opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
+                                               opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
                                                y += pres->y1 - pres->y0;
                                        }
 
@@ -954,8 +1313,9 @@ opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
                                                }
                                        }
 
-                                       tiledp=(void*)&tilec->data[(y * tile_w) + x];
+                                       /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
                                        if (tccp->qmfbid == 1) {
+                        OPJ_INT32* restrict tiledp = &tilec->data[(y * tile_w) + x];
                                                for (j = 0; j < cblk_h; ++j) {
                                                        for (i = 0; i < cblk_w; ++i) {
                                                                OPJ_INT32 tmp = datap[(j * cblk_w) + i];
@@ -963,16 +1323,26 @@ opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
                                                        }
                                                }
                                        } else {                /* if (tccp->qmfbid == 0) */
+                        OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(y * tile_w) + x];
                                                for (j = 0; j < cblk_h; ++j) {
+                            OPJ_FLOAT32* restrict tiledp2 = tiledp;
                                                        for (i = 0; i < cblk_w; ++i) {
-                                                               float tmp = datap[(j * cblk_w) + i] * band->stepsize;
-                                                               ((float*)tiledp)[(j * tile_w) + i] = tmp;
+                                OPJ_FLOAT32 tmp = *datap * band->stepsize;
+                                *tiledp2 = tmp;
+                                datap++;
+                                tiledp2++;
+                                                               /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
+                                                               ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
+
                                                        }
+                            tiledp += tile_w;
                                                }
                                        }
-                                       /*opj_free(cblk->segs);*/
+                    /*opj_free(cblk->data);
+                                       opj_free(cblk->segs);*/
                                        /*cblk->segs = 00;*/
                                } /* cblkno */
+                /*opj_free(precinct->cblks.dec);*/
                        } /* precno */
                } /* bandno */
        } /* resno */
@@ -980,8 +1350,8 @@ opj_bool opj_t1_decode_cblks(   opj_t1_t* t1,
 }
 
 
-opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
-                            opj_tcd_cblk_dec_v2_t* cblk,
+OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
+                            opj_tcd_cblk_dec_t* cblk,
                             OPJ_UINT32 orient,
                             OPJ_UINT32 roishift,
                             OPJ_UINT32 cblksty)
@@ -1020,7 +1390,7 @@ opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
                        continue;
                }
                if (type == T1_TYPE_RAW) {
-                       raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
+                       opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
                } else {
             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
                     return OPJ_FALSE;
@@ -1028,17 +1398,33 @@ opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
                }
 
                for (passno = 0; passno < seg->real_num_passes; ++passno) {
-                       switch (passtype) {
-                               case 0:
-                                       opj_t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
-                                       break;
-                               case 1:
-                                       opj_t1_dec_refpass(t1, bpno+1, type, cblksty);
-                                       break;
-                               case 2:
-                                       t1_dec_clnpass(t1, bpno+1, orient, cblksty);
-                                       break;
-                       }
+            switch (passtype) {
+                case 0:
+                    if (type == T1_TYPE_RAW) {
+                        opj_t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
+                    } else {
+                        if (cblksty & J2K_CCP_CBLKSTY_VSC) {
+                            opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
+                        } else {
+                            opj_t1_dec_sigpass_mqc(t1, bpno+1, orient);
+                        }
+                    }
+                    break;
+                case 1:
+                    if (type == T1_TYPE_RAW) {
+                            opj_t1_dec_refpass_raw(t1, bpno+1, cblksty);
+                    } else {
+                        if (cblksty & J2K_CCP_CBLKSTY_VSC) {
+                            opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
+                        } else {
+                            opj_t1_dec_refpass_mqc(t1, bpno+1);
+                        }
+                    }
+                    break;
+                case 2:
+                    opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
+                    break;
+            }
 
                        if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
                                opj_mqc_resetstates(mqc);
@@ -1055,9 +1441,12 @@ opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
         return OPJ_TRUE;
 }
 
-opj_bool opj_t1_encode_cblks(   opj_t1_t *t1,
-                                opj_tcd_tile_v2_t *tile,
-                                opj_tcp_v2_t *tcp,
+
+
+
+OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
+                                opj_tcd_tile_t *tile,
+                                opj_tcp_t *tcp,
                                 const OPJ_FLOAT64 * mct_norms
                                 )
 {
@@ -1066,22 +1455,22 @@ opj_bool opj_t1_encode_cblks(   opj_t1_t *t1,
        tile->distotile = 0;            /* fixed_quality */
 
        for (compno = 0; compno < tile->numcomps; ++compno) {
-               opj_tcd_tilecomp_v2_t* tilec = &tile->comps[compno];
+               opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
                opj_tccp_t* tccp = &tcp->tccps[compno];
                OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
 
                for (resno = 0; resno < tilec->numresolutions; ++resno) {
-                       opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
+                       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
 
                        for (bandno = 0; bandno < res->numbands; ++bandno) {
-                               opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
+                               opj_tcd_band_t* restrict band = &res->bands[bandno];
                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
 
                                for (precno = 0; precno < res->pw * res->ph; ++precno) {
-                                       opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
+                                       opj_tcd_precinct_t *prc = &band->precincts[precno];
 
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
-                                               opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno];
+                                               opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
                                                OPJ_INT32 * restrict datap;
                                                OPJ_INT32* restrict tiledp;
                                                OPJ_UINT32 cblk_w;
@@ -1091,11 +1480,11 @@ opj_bool opj_t1_encode_cblks(   opj_t1_t *t1,
                                                OPJ_INT32 x = cblk->x0 - band->x0;
                                                OPJ_INT32 y = cblk->y0 - band->y0;
                                                if (band->bandno & 1) {
-                                                       opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
                                                        x += pres->x1 - pres->x0;
                                                }
                                                if (band->bandno & 2) {
-                                                       opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
+                                                       opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
                                                        y += pres->y1 - pres->y0;
                                                }
 
@@ -1124,7 +1513,7 @@ opj_bool opj_t1_encode_cblks(   opj_t1_t *t1,
                                                                for (i = 0; i < cblk_w; ++i) {
                                                                        OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
                                                                        datap[(j * cblk_w) + i] =
-                                                                               fix_mul(
+                                                                               opj_int_fix_mul(
                                                                                tmp,
                                                                                bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
                                                                }
@@ -1154,7 +1543,7 @@ opj_bool opj_t1_encode_cblks(   opj_t1_t *t1,
 
 /** mod fixed_quality */
 void opj_t1_encode_cblk(opj_t1_t *t1,
-                        opj_tcd_cblk_enc_v2_t* cblk,
+                        opj_tcd_cblk_enc_t* cblk,
                         OPJ_UINT32 orient,
                         OPJ_UINT32 compno,
                         OPJ_UINT32 level,
@@ -1162,7 +1551,7 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
                         OPJ_FLOAT64 stepsize,
                         OPJ_UINT32 cblksty,
                         OPJ_UINT32 numcomps,
-                        opj_tcd_tile_v2_t * tile,
+                        opj_tcd_tile_t * tile,
                         const OPJ_FLOAT64 * mct_norms)
 {
        OPJ_FLOAT64 cumwmsedec = 0.0;
@@ -1181,10 +1570,10 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
        max = 0;
        for (i = 0; i < t1->w * t1->h; ++i) {
                OPJ_INT32 tmp = abs(t1->data[i]);
-               max = int_max(max, tmp);
+               max = opj_int_max(max, tmp);
        }
 
-       cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
+       cblk->numbps = max ? (opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
 
        bpno = cblk->numbps - 1;
        passtype = 2;
@@ -1196,7 +1585,7 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
        opj_mqc_init_enc(mqc, cblk->data);
 
        for (passno = 0; bpno >= 0; ++passno) {
-               opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
+               opj_tcd_pass_t *pass = &cblk->passes[passno];
                OPJ_UINT32 correction = 3;
                type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
 
@@ -1216,7 +1605,7 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
                }
 
                /* fixed_quality */
-               tempwmsedec = t1_getwmsedec_v2(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
+               tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
                cumwmsedec += tempwmsedec;
                tile->distotile += tempwmsedec;
 
@@ -1233,7 +1622,7 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
                        pass->term = 1;
                } else {
                        if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
-                               || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
+                               || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
                                if (type == T1_TYPE_RAW) {
                                        opj_mqc_flush(mqc);
                                        correction = 1;
@@ -1278,7 +1667,7 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
        cblk->totalpasses = passno;
 
        for (passno = 0; passno<cblk->totalpasses; passno++) {
-               opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
+               opj_tcd_pass_t *pass = &cblk->passes[passno];
                if (pass->rate > opj_mqc_numbytes(mqc))
                        pass->rate = opj_mqc_numbytes(mqc);
                /*Preventing generation of FF as last data byte of a pass*/
@@ -1289,37 +1678,8 @@ void opj_t1_encode_cblk(opj_t1_t *t1,
        }
 }
 
-void opj_t1_dec_refpass(opj_t1_t *t1,
-                        OPJ_INT32 bpno,
-                        OPJ_BYTE type,
-                        OPJ_UINT32 cblksty)
-{
-       OPJ_UINT32 i, j, k;
-       OPJ_INT32 one, poshalf, neghalf;
-       OPJ_UINT32 vsc;
-       one = 1 << bpno;
-       poshalf = one >> 1;
-       neghalf = bpno > 0 ? -poshalf : -1;
-       for (k = 0; k < t1->h; k += 4) {
-               for (i = 0; i < t1->w; ++i) {
-                       for (j = k; j < k + 4 && j < t1->h; ++j) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
-                               opj_t1_dec_refpass_step(
-                                               t1,
-                                               &t1->flags[((j+1) * t1->flags_stride) + i + 1],
-                                               &t1->data[(j * t1->w) + i],
-                                               poshalf,
-                                               neghalf,
-                                               type,
-                                               vsc);
-                       }
-               }
-       }
-}                              /* VSC and  BYPASS by Antonin */
-
-
 void opj_t1_dec_refpass_step(   opj_t1_t *t1,
-                                flag_t *flagsp,
+                                opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_INT32 poshalf,
                                 OPJ_INT32 neghalf,
@@ -1336,7 +1696,7 @@ void opj_t1_dec_refpass_step(   opj_t1_t *t1,
        if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
                opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
                if (type == T1_TYPE_RAW) {
-                       v = raw_decode(raw);
+                       v = opj_raw_decode(raw);
                } else {
                        v = opj_mqc_decode(mqc);
                }
@@ -1346,36 +1706,10 @@ void opj_t1_dec_refpass_step(   opj_t1_t *t1,
        }
 }                              /* VSC and  BYPASS by Antonin  */
 
-void opj_t1_dec_sigpass(opj_t1_t *t1,
-                        OPJ_INT32 bpno,
-                        OPJ_UINT32 orient,
-                        OPJ_BYTE type,
-                        OPJ_UINT32 cblksty)
-{
-       OPJ_UINT32 i, j, k, vsc;
-       OPJ_INT32 one, half, oneplushalf;
-       one = 1 << bpno;
-       half = one >> 1;
-       oneplushalf = one | half;
-       for (k = 0; k < t1->h; k += 4) {
-               for (i = 0; i < t1->w; ++i) {
-                       for (j = k; j < k + 4 && j < t1->h; ++j) {
-                               vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
-                               opj_t1_dec_sigpass_step(
-                                               t1,
-                                               &t1->flags[((j+1) * t1->flags_stride) + i + 1],
-                                               &t1->data[(j * t1->w) + i],
-                                               orient,
-                                               oneplushalf,
-                                               type,
-                                               vsc);
-                       }
-               }
-       }
-}                              /* VSC and  BYPASS by Antonin */
+
 
 void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
-                                flag_t *flagsp,
+                                opj_flag_t *flagsp,
                                 OPJ_INT32 *datap,
                                 OPJ_UINT32 orient,
                                 OPJ_INT32 oneplushalf,
@@ -1390,8 +1724,8 @@ void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
        flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
        if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
                if (type == T1_TYPE_RAW) {
-                       if (raw_decode(raw)) {
-                               v = raw_decode(raw);    /* ESSAI */
+                       if (opj_raw_decode(raw)) {
+                               v = opj_raw_decode(raw);        /* ESSAI */
                                *datap = v ? -oneplushalf : oneplushalf;
                                opj_t1_updateflags(flagsp, v, t1->flags_stride);
                        }