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