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