Update the README.cmake file : an instruction was missing to run the tests correctly.
[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 Decode significant pass
64 */
65 static INLINE void t1_dec_sigpass_step_raw(
66                 opj_t1_t *t1,
67                 flag_t *flagsp,
68                 int *datap,
69                 int orient,
70                 int oneplushalf,
71                 int vsc);
72 static INLINE void t1_dec_sigpass_step_mqc(
73                 opj_t1_t *t1,
74                 flag_t *flagsp,
75                 int *datap,
76                 int orient,
77                 int oneplushalf);
78 static INLINE void t1_dec_sigpass_step_mqc_vsc(
79                 opj_t1_t *t1,
80                 flag_t *flagsp,
81                 int *datap,
82                 int orient,
83                 int oneplushalf,
84                 int vsc);
85 /**
86 Encode significant pass
87 */
88 static void t1_enc_sigpass(
89                 opj_t1_t *t1,
90                 int bpno,
91                 int orient,
92                 int *nmsedec,
93                 char type,
94                 int cblksty);
95 /**
96 Decode significant pass
97 */
98 static void t1_dec_sigpass_raw(
99                 opj_t1_t *t1,
100                 int bpno,
101                 int orient,
102                 int cblksty);
103 static void t1_dec_sigpass_mqc(
104                 opj_t1_t *t1,
105                 int bpno,
106                 int orient);
107 static void t1_dec_sigpass_mqc_vsc(
108                 opj_t1_t *t1,
109                 int bpno,
110                 int orient);
111 /**
112 Encode refinement pass
113 */
114 static void t1_enc_refpass_step(
115                 opj_t1_t *t1,
116                 flag_t *flagsp,
117                 int *datap,
118                 int bpno,
119                 int one,
120                 int *nmsedec,
121                 char type,
122                 int vsc);
123 /**
124 Decode refinement pass
125 */
126 static void INLINE t1_dec_refpass_step_raw(
127                 opj_t1_t *t1,
128                 flag_t *flagsp,
129                 int *datap,
130                 int poshalf,
131                 int neghalf,
132                 int vsc);
133 static void INLINE t1_dec_refpass_step_mqc(
134                 opj_t1_t *t1,
135                 flag_t *flagsp,
136                 int *datap,
137                 int poshalf,
138                 int neghalf);
139 static void INLINE t1_dec_refpass_step_mqc_vsc(
140                 opj_t1_t *t1,
141                 flag_t *flagsp,
142                 int *datap,
143                 int poshalf,
144                 int neghalf,
145                 int vsc);
146
147 /**
148 Encode refinement pass
149 */
150 static void t1_enc_refpass(
151                 opj_t1_t *t1,
152                 int bpno,
153                 int *nmsedec,
154                 char type,
155                 int cblksty);
156 /**
157 Decode refinement pass
158 */
159 static void t1_dec_refpass_raw(
160                 opj_t1_t *t1,
161                 int bpno,
162                 int cblksty);
163 static void t1_dec_refpass_mqc(
164                 opj_t1_t *t1,
165                 int bpno);
166 static void t1_dec_refpass_mqc_vsc(
167                 opj_t1_t *t1,
168                 int bpno);
169 /**
170 Encode clean-up pass
171 */
172 static void t1_enc_clnpass_step(
173                 opj_t1_t *t1,
174                 flag_t *flagsp,
175                 int *datap,
176                 int orient,
177                 int bpno,
178                 int one,
179                 int *nmsedec,
180                 int partial,
181                 int vsc);
182 /**
183 Decode clean-up pass
184 */
185 static void t1_dec_clnpass_step_partial(
186                 opj_t1_t *t1,
187                 flag_t *flagsp,
188                 int *datap,
189                 int orient,
190                 int oneplushalf);
191 static void t1_dec_clnpass_step(
192                 opj_t1_t *t1,
193                 flag_t *flagsp,
194                 int *datap,
195                 int orient,
196                 int oneplushalf);
197 static void t1_dec_clnpass_step_vsc(
198                 opj_t1_t *t1,
199                 flag_t *flagsp,
200                 int *datap,
201                 int orient,
202                 int oneplushalf,
203                 int partial,
204                 int vsc);
205 /**
206 Encode clean-up pass
207 */
208 static void t1_enc_clnpass(
209                 opj_t1_t *t1,
210                 int bpno,
211                 int orient,
212                 int *nmsedec,
213                 int cblksty);
214 /**
215 Decode clean-up pass
216 */
217 static void t1_dec_clnpass(
218                 opj_t1_t *t1,
219                 int bpno,
220                 int orient,
221                 int cblksty);
222 static double t1_getwmsedec(
223                 int nmsedec,
224                 int compno,
225                 int level,
226                 int orient,
227                 int bpno,
228                 int qmfbid,
229                 double stepsize,
230                 int numcomps,
231                 int mct);
232 /**
233 Encode 1 code-block
234 @param t1 T1 handle
235 @param cblk Code-block coding parameters
236 @param orient
237 @param compno Component number
238 @param level
239 @param qmfbid
240 @param stepsize
241 @param cblksty Code-block style
242 @param numcomps
243 @param tile
244 */
245 static void t1_encode_cblk(
246                 opj_t1_t *t1,
247                 opj_tcd_cblk_enc_t* cblk,
248                 int orient,
249                 int compno,
250                 int level,
251                 int qmfbid,
252                 double stepsize,
253                 int cblksty,
254                 int numcomps,
255                 int mct,
256                 opj_tcd_tile_t * tile);
257 /**
258 Decode 1 code-block
259 @param t1 T1 handle
260 @param cblk Code-block coding parameters
261 @param orient
262 @param roishift Region of interest shifting value
263 @param cblksty Code-block style
264 */
265 static void t1_decode_cblk(
266                 opj_t1_t *t1,
267                 opj_tcd_cblk_dec_t* cblk,
268                 int orient,
269                 int roishift,
270                 int cblksty);
271
272 /*@}*/
273
274 /*@}*/
275
276 /* ----------------------------------------------------------------------- */
277
278 static char t1_getctxno_zc(int f, int orient) {
279         return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
280 }
281
282 static char t1_getctxno_sc(int f) {
283         return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
284 }
285
286 static int t1_getctxno_mag(int f) {
287         int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
288         int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
289         return (tmp2);
290 }
291
292 static char t1_getspb(int f) {
293         return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
294 }
295
296 static short t1_getnmsedec_sig(int x, int bitpos) {
297         if (bitpos > T1_NMSEDEC_FRACBITS) {
298                 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
299         }
300         
301         return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
302 }
303
304 static short t1_getnmsedec_ref(int x, int bitpos) {
305         if (bitpos > T1_NMSEDEC_FRACBITS) {
306                 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
307         }
308
309     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
310 }
311
312 static void t1_updateflags(flag_t *flagsp, int s, int stride) {
313         flag_t *np = flagsp - stride;
314         flag_t *sp = flagsp + stride;
315
316         static const flag_t mod[] = {
317                 T1_SIG_S, T1_SIG_S|T1_SGN_S,
318                 T1_SIG_E, T1_SIG_E|T1_SGN_E,
319                 T1_SIG_W, T1_SIG_W|T1_SGN_W,
320                 T1_SIG_N, T1_SIG_N|T1_SGN_N
321         };
322
323         np[-1] |= T1_SIG_SE;
324         np[0]  |= mod[s];
325         np[1]  |= T1_SIG_SW;
326
327         flagsp[-1] |= mod[s+2];
328         flagsp[0]  |= T1_SIG;
329         flagsp[1]  |= mod[s+4];
330
331         sp[-1] |= T1_SIG_NE;
332         sp[0]  |= mod[s+6];
333         sp[1]  |= T1_SIG_NW;
334 }
335
336 static void t1_enc_sigpass_step(
337                 opj_t1_t *t1,
338                 flag_t *flagsp,
339                 int *datap,
340                 int orient,
341                 int bpno,
342                 int one,
343                 int *nmsedec,
344                 char type,
345                 int vsc)
346 {
347         int v, flag;
348         
349         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
350         
351         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
352         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
353                 v = int_abs(*datap) & one ? 1 : 0;
354                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));       /* ESSAI */
355                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
356                         mqc_bypass_enc(mqc, v);
357                 } else {
358                         mqc_encode(mqc, v);
359                 }
360                 if (v) {
361                         v = *datap < 0 ? 1 : 0;
362                         *nmsedec +=     t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
363                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));       /* ESSAI */
364                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
365                                 mqc_bypass_enc(mqc, v);
366                         } else {
367                                 mqc_encode(mqc, v ^ t1_getspb(flag));
368                         }
369                         t1_updateflags(flagsp, v, t1->flags_stride);
370                 }
371                 *flagsp |= T1_VISIT;
372         }
373 }
374
375 static INLINE void t1_dec_sigpass_step_raw(
376                 opj_t1_t *t1,
377                 flag_t *flagsp,
378                 int *datap,
379                 int orient,
380                 int oneplushalf,
381                 int vsc)
382 {
383         int v, flag;
384         
385         opj_raw_t *raw = t1->raw;       /* RAW component */
386         
387         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
388         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
389                         if (raw_decode(raw)) {
390                                 v = raw_decode(raw);    /* ESSAI */
391                                 *datap = v ? -oneplushalf : oneplushalf;
392                                 t1_updateflags(flagsp, v, t1->flags_stride);
393                         }
394                 *flagsp |= T1_VISIT;
395         }
396 }                               /* VSC and  BYPASS by Antonin */
397
398 static INLINE void t1_dec_sigpass_step_mqc(
399                 opj_t1_t *t1,
400                 flag_t *flagsp,
401                 int *datap,
402                 int orient,
403                 int oneplushalf)
404 {
405         int v, flag;
406         
407         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
408         
409         flag = *flagsp;
410         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
411                         mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
412                         if (mqc_decode(mqc)) {
413                                 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
414                                 v = mqc_decode(mqc) ^ t1_getspb(flag);
415                                 *datap = v ? -oneplushalf : oneplushalf;
416                                 t1_updateflags(flagsp, v, t1->flags_stride);
417                         }
418                 *flagsp |= T1_VISIT;
419         }
420 }                               /* VSC and  BYPASS by Antonin */
421
422 static INLINE void t1_dec_sigpass_step_mqc_vsc(
423                 opj_t1_t *t1,
424                 flag_t *flagsp,
425                 int *datap,
426                 int orient,
427                 int oneplushalf,
428                 int vsc)
429 {
430         int v, flag;
431         
432         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
433         
434         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
435         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
436                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
437                 if (mqc_decode(mqc)) {
438                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
439                         v = mqc_decode(mqc) ^ t1_getspb(flag);
440                         *datap = v ? -oneplushalf : oneplushalf;
441                         t1_updateflags(flagsp, v, t1->flags_stride);
442                 }
443                 *flagsp |= T1_VISIT;
444         }
445 }                               /* VSC and  BYPASS by Antonin */
446
447 static void t1_enc_sigpass(
448                 opj_t1_t *t1,
449                 int bpno,
450                 int orient,
451                 int *nmsedec,
452                 char type,
453                 int cblksty)
454 {
455         int i, j, k, one, vsc;
456         *nmsedec = 0;
457         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
458         for (k = 0; k < t1->h; k += 4) {
459                 for (i = 0; i < t1->w; ++i) {
460                         for (j = k; j < k + 4 && j < t1->h; ++j) {
461                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
462                                 t1_enc_sigpass_step(
463                                                 t1,
464                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
465                                                 &t1->data[(j * t1->w) + i],
466                                                 orient,
467                                                 bpno,
468                                                 one,
469                                                 nmsedec,
470                                                 type,
471                                                 vsc);
472                         }
473                 }
474         }
475 }
476
477 static void t1_dec_sigpass_raw(
478                 opj_t1_t *t1,
479                 int bpno,
480                 int orient,
481                 int cblksty)
482 {
483         int i, j, k, one, half, oneplushalf, vsc;
484         one = 1 << bpno;
485         half = one >> 1;
486         oneplushalf = one | half;
487         for (k = 0; k < t1->h; k += 4) {
488                 for (i = 0; i < t1->w; ++i) {
489                         for (j = k; j < k + 4 && j < t1->h; ++j) {
490                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
491                                 t1_dec_sigpass_step_raw(
492                                                 t1,
493                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
494                                                 &t1->data[(j * t1->w) + i],
495                                                 orient,
496                                                 oneplushalf,
497                                                 vsc);
498                         }
499                 }
500         }
501 }                               /* VSC and  BYPASS by Antonin */
502
503 static void t1_dec_sigpass_mqc(
504                 opj_t1_t *t1,
505                 int bpno,
506                 int orient)
507 {
508         int i, j, k, one, half, oneplushalf;
509         int *data1 = t1->data;
510         flag_t *flags1 = &t1->flags[1];
511         one = 1 << bpno;
512         half = one >> 1;
513         oneplushalf = one | half;
514         for (k = 0; k < (t1->h & ~3); k += 4) {
515                 for (i = 0; i < t1->w; ++i) {
516                         int *data2 = data1 + i;
517                         flag_t *flags2 = flags1 + i;
518                         flags2 += t1->flags_stride;
519                         t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
520                         data2 += t1->w;
521                         flags2 += t1->flags_stride;
522                         t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
523                         data2 += t1->w;
524                         flags2 += t1->flags_stride;
525                         t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
526                         data2 += t1->w;
527                         flags2 += t1->flags_stride;
528                         t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
529                         data2 += t1->w;
530                 }
531                 data1 += t1->w << 2;
532                 flags1 += t1->flags_stride << 2;
533         }
534         for (i = 0; i < t1->w; ++i) {
535                 int *data2 = data1 + i;
536                 flag_t *flags2 = flags1 + i;
537                 for (j = k; j < t1->h; ++j) {
538                         flags2 += t1->flags_stride;
539                         t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
540                         data2 += t1->w;
541                 }
542         }
543 }                               /* VSC and  BYPASS by Antonin */
544
545 static void t1_dec_sigpass_mqc_vsc(
546                 opj_t1_t *t1,
547                 int bpno,
548                 int orient)
549 {
550         int i, j, k, one, half, oneplushalf, vsc;
551         one = 1 << bpno;
552         half = one >> 1;
553         oneplushalf = one | half;
554         for (k = 0; k < t1->h; k += 4) {
555                 for (i = 0; i < t1->w; ++i) {
556                         for (j = k; j < k + 4 && j < t1->h; ++j) {
557                                 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
558                                 t1_dec_sigpass_step_mqc_vsc(
559                                                 t1,
560                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
561                                                 &t1->data[(j * t1->w) + i],
562                                                 orient,
563                                                 oneplushalf,
564                                                 vsc);
565                         }
566                 }
567         }
568 }                               /* VSC and  BYPASS by Antonin */
569
570 static void t1_enc_refpass_step(
571                 opj_t1_t *t1,
572                 flag_t *flagsp,
573                 int *datap,
574                 int bpno,
575                 int one,
576                 int *nmsedec,
577                 char type,
578                 int vsc)
579 {
580         int v, flag;
581         
582         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
583         
584         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
585         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
586                 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
587                 v = int_abs(*datap) & one ? 1 : 0;
588                 mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
589                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
590                         mqc_bypass_enc(mqc, v);
591                 } else {
592                         mqc_encode(mqc, v);
593                 }
594                 *flagsp |= T1_REFINE;
595         }
596 }
597
598 static INLINE void t1_dec_refpass_step_raw(
599                 opj_t1_t *t1,
600                 flag_t *flagsp,
601                 int *datap,
602                 int poshalf,
603                 int neghalf,
604                 int vsc)
605 {
606         int v, t, flag;
607         
608         opj_raw_t *raw = t1->raw;       /* RAW component */
609         
610         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
611         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
612                         v = raw_decode(raw);
613                 t = v ? poshalf : neghalf;
614                 *datap += *datap < 0 ? -t : t;
615                 *flagsp |= T1_REFINE;
616         }
617 }                               /* VSC and  BYPASS by Antonin  */
618
619 static INLINE void t1_dec_refpass_step_mqc(
620                 opj_t1_t *t1,
621                 flag_t *flagsp,
622                 int *datap,
623                 int poshalf,
624                 int neghalf)
625 {
626         int v, t, flag;
627         
628         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
629         
630         flag = *flagsp;
631         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
632                 mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
633                         v = mqc_decode(mqc);
634                 t = v ? poshalf : neghalf;
635                 *datap += *datap < 0 ? -t : t;
636                 *flagsp |= T1_REFINE;
637                 }
638 }                               /* VSC and  BYPASS by Antonin  */
639
640 static INLINE void t1_dec_refpass_step_mqc_vsc(
641                 opj_t1_t *t1,
642                 flag_t *flagsp,
643                 int *datap,
644                 int poshalf,
645                 int neghalf,
646                 int vsc)
647 {
648         int v, t, flag;
649         
650         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
651         
652         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
653         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
654                 mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
655                 v = mqc_decode(mqc);
656                 t = v ? poshalf : neghalf;
657                 *datap += *datap < 0 ? -t : t;
658                 *flagsp |= T1_REFINE;
659         }
660 }                               /* VSC and  BYPASS by Antonin  */
661
662 static void t1_enc_refpass(
663                 opj_t1_t *t1,
664                 int bpno,
665                 int *nmsedec,
666                 char type,
667                 int cblksty)
668 {
669         int i, j, k, one, vsc;
670         *nmsedec = 0;
671         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
672         for (k = 0; k < t1->h; k += 4) {
673                 for (i = 0; i < t1->w; ++i) {
674                         for (j = k; j < k + 4 && j < t1->h; ++j) {
675                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
676                                 t1_enc_refpass_step(
677                                                 t1,
678                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
679                                                 &t1->data[(j * t1->w) + i],
680                                                 bpno,
681                                                 one,
682                                                 nmsedec,
683                                                 type,
684                                                 vsc);
685                         }
686                 }
687         }
688 }
689
690 static void t1_dec_refpass_raw(
691                 opj_t1_t *t1,
692                 int bpno,
693                 int cblksty)
694 {
695         int i, j, k, one, poshalf, neghalf;
696         int vsc;
697         one = 1 << bpno;
698         poshalf = one >> 1;
699         neghalf = bpno > 0 ? -poshalf : -1;
700         for (k = 0; k < t1->h; k += 4) {
701                 for (i = 0; i < t1->w; ++i) {
702                         for (j = k; j < k + 4 && j < t1->h; ++j) {
703                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
704                                 t1_dec_refpass_step_raw(
705                                                 t1,
706                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
707                                                 &t1->data[(j * t1->w) + i],
708                                                 poshalf,
709                                                 neghalf,
710                                                 vsc);
711                         }
712                 }
713         }
714 }                               /* VSC and  BYPASS by Antonin */
715
716 static void t1_dec_refpass_mqc(
717                 opj_t1_t *t1,
718                 int bpno)
719 {
720         int i, j, k, one, poshalf, neghalf;
721         int *data1 = t1->data;
722         flag_t *flags1 = &t1->flags[1];
723         one = 1 << bpno;
724         poshalf = one >> 1;
725         neghalf = bpno > 0 ? -poshalf : -1;
726         for (k = 0; k < (t1->h & ~3); k += 4) {
727                 for (i = 0; i < t1->w; ++i) {
728                         int *data2 = data1 + i;
729                         flag_t *flags2 = flags1 + i;
730                         flags2 += t1->flags_stride;
731                         t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
732                         data2 += t1->w;
733                         flags2 += t1->flags_stride;
734                         t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
735                         data2 += t1->w;
736                         flags2 += t1->flags_stride;
737                         t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
738                         data2 += t1->w;
739                         flags2 += t1->flags_stride;
740                         t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
741                         data2 += t1->w;
742                 }
743                 data1 += t1->w << 2;
744                 flags1 += t1->flags_stride << 2;
745         }
746         for (i = 0; i < t1->w; ++i) {
747                 int *data2 = data1 + i;
748                 flag_t *flags2 = flags1 + i;
749                 for (j = k; j < t1->h; ++j) {
750                         flags2 += t1->flags_stride;
751                         t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
752                         data2 += t1->w;
753                 }
754         }
755 }                               /* VSC and  BYPASS by Antonin */
756
757 static void t1_dec_refpass_mqc_vsc(
758                 opj_t1_t *t1,
759                 int bpno)
760 {
761         int i, j, k, one, poshalf, neghalf;
762         int vsc;
763         one = 1 << bpno;
764         poshalf = one >> 1;
765         neghalf = bpno > 0 ? -poshalf : -1;
766         for (k = 0; k < t1->h; k += 4) {
767                 for (i = 0; i < t1->w; ++i) {
768                         for (j = k; j < k + 4 && j < t1->h; ++j) {
769                                 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
770                                 t1_dec_refpass_step_mqc_vsc(
771                                                 t1,
772                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
773                                                 &t1->data[(j * t1->w) + i],
774                                                 poshalf,
775                                                 neghalf,
776                                                 vsc);
777                         }
778                 }
779         }
780 }                               /* VSC and  BYPASS by Antonin */
781
782 static void t1_enc_clnpass_step(
783                 opj_t1_t *t1,
784                 flag_t *flagsp,
785                 int *datap,
786                 int orient,
787                 int bpno,
788                 int one,
789                 int *nmsedec,
790                 int partial,
791                 int vsc)
792 {
793         int v, flag;
794         
795         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
796         
797         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
798         if (partial) {
799                 goto LABEL_PARTIAL;
800         }
801         if (!(*flagsp & (T1_SIG | T1_VISIT))) {
802                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
803                 v = int_abs(*datap) & one ? 1 : 0;
804                 mqc_encode(mqc, v);
805                 if (v) {
806 LABEL_PARTIAL:
807                         *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
808                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
809                         v = *datap < 0 ? 1 : 0;
810                         mqc_encode(mqc, v ^ t1_getspb(flag));
811                         t1_updateflags(flagsp, v, t1->flags_stride);
812                 }
813         }
814         *flagsp &= ~T1_VISIT;
815 }
816
817 static void t1_dec_clnpass_step_partial(
818                 opj_t1_t *t1,
819                 flag_t *flagsp,
820                 int *datap,
821                 int orient,
822                 int oneplushalf)
823 {
824         int v, flag;
825         
826         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
827         
828         flag = *flagsp;
829         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
830         v = mqc_decode(mqc) ^ t1_getspb(flag);
831         *datap = v ? -oneplushalf : oneplushalf;
832         t1_updateflags(flagsp, v, t1->flags_stride);
833         *flagsp &= ~T1_VISIT;
834 }                               /* VSC and  BYPASS by Antonin */
835
836 static void t1_dec_clnpass_step(
837                 opj_t1_t *t1,
838                 flag_t *flagsp,
839                 int *datap,
840                 int orient,
841                 int oneplushalf)
842 {
843         int v, flag;
844         
845         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
846         
847         flag = *flagsp;
848         if (!(flag & (T1_SIG | T1_VISIT))) {
849                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
850                 if (mqc_decode(mqc)) {
851                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
852                         v = mqc_decode(mqc) ^ t1_getspb(flag);
853                         *datap = v ? -oneplushalf : oneplushalf;
854                         t1_updateflags(flagsp, v, t1->flags_stride);
855                 }
856         }
857         *flagsp &= ~T1_VISIT;
858 }                               /* VSC and  BYPASS by Antonin */
859
860 static void t1_dec_clnpass_step_vsc(
861                 opj_t1_t *t1,
862                 flag_t *flagsp,
863                 int *datap,
864                 int orient,
865                 int oneplushalf,
866                 int partial,
867                 int vsc)
868 {
869         int v, flag;
870         
871         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
872         
873         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
874         if (partial) {
875                 goto LABEL_PARTIAL;
876         }
877         if (!(flag & (T1_SIG | T1_VISIT))) {
878                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
879                 if (mqc_decode(mqc)) {
880 LABEL_PARTIAL:
881                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
882                         v = mqc_decode(mqc) ^ t1_getspb(flag);
883                         *datap = v ? -oneplushalf : oneplushalf;
884                         t1_updateflags(flagsp, v, t1->flags_stride);
885                 }
886         }
887         *flagsp &= ~T1_VISIT;
888 }
889
890 static void t1_enc_clnpass(
891                 opj_t1_t *t1,
892                 int bpno,
893                 int orient,
894                 int *nmsedec,
895                 int cblksty)
896 {
897         int i, j, k, one, agg, runlen, vsc;
898         
899         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
900         
901         *nmsedec = 0;
902         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
903         for (k = 0; k < t1->h; k += 4) {
904                 for (i = 0; i < t1->w; ++i) {
905                         if (k + 3 < t1->h) {
906                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
907                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
908                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
909                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
910                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
911                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
912                                 } else {
913                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
914                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
915                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
916                                                 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
917                                 }
918                         } else {
919                                 agg = 0;
920                         }
921                         if (agg) {
922                                 for (runlen = 0; runlen < 4; ++runlen) {
923                                         if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
924                                                 break;
925                                 }
926                                 mqc_setcurctx(mqc, T1_CTXNO_AGG);
927                                 mqc_encode(mqc, runlen != 4);
928                                 if (runlen == 4) {
929                                         continue;
930                                 }
931                                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
932                                 mqc_encode(mqc, runlen >> 1);
933                                 mqc_encode(mqc, runlen & 1);
934                         } else {
935                                 runlen = 0;
936                         }
937                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
938                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
939                                 t1_enc_clnpass_step(
940                                                 t1,
941                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
942                                                 &t1->data[(j * t1->w) + i],
943                                                 orient,
944                                                 bpno,
945                                                 one,
946                                                 nmsedec,
947                                                 agg && (j == k + runlen),
948                                                 vsc);
949                         }
950                 }
951         }
952 }
953
954 static void t1_dec_clnpass(
955                 opj_t1_t *t1,
956                 int bpno,
957                 int orient,
958                 int cblksty)
959 {
960         int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
961         int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
962         
963         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
964         
965         one = 1 << bpno;
966         half = one >> 1;
967         oneplushalf = one | half;
968         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
969         for (k = 0; k < t1->h; k += 4) {
970                 for (i = 0; i < t1->w; ++i) {
971                         if (k + 3 < t1->h) {
972                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
973                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
974                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
975                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
976                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
977                                 } else {
978                                 agg = 0;
979                         }
980                         if (agg) {
981                                 mqc_setcurctx(mqc, T1_CTXNO_AGG);
982                                 if (!mqc_decode(mqc)) {
983                                         continue;
984                                 }
985                                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
986                                 runlen = mqc_decode(mqc);
987                                 runlen = (runlen << 1) | mqc_decode(mqc);
988                         } else {
989                                 runlen = 0;
990                         }
991                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
992                                         vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
993                                         t1_dec_clnpass_step_vsc(
994                                                 t1,
995                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
996                                                 &t1->data[(j * t1->w) + i],
997                                                 orient,
998                                                 oneplushalf,
999                                                 agg && (j == k + runlen),
1000                                                 vsc);
1001                         }
1002                 }
1003         }
1004         } else {
1005                 int *data1 = t1->data;
1006                 flag_t *flags1 = &t1->flags[1];
1007                 for (k = 0; k < (t1->h & ~3); k += 4) {
1008                         for (i = 0; i < t1->w; ++i) {
1009                                 int *data2 = data1 + i;
1010                                 flag_t *flags2 = flags1 + i;
1011                                 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1012                                         || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1013                                         || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1014                                         || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1015                                 if (agg) {
1016                                         mqc_setcurctx(mqc, T1_CTXNO_AGG);
1017                                         if (!mqc_decode(mqc)) {
1018                                                 continue;
1019                                         }
1020                                         mqc_setcurctx(mqc, T1_CTXNO_UNI);
1021                                         runlen = mqc_decode(mqc);
1022                                         runlen = (runlen << 1) | mqc_decode(mqc);
1023                                         flags2 += runlen * t1->flags_stride;
1024                                         data2 += runlen * t1->w;
1025                                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1026                                                 flags2 += t1->flags_stride;
1027                                                 if (agg && (j == k + runlen)) {
1028                                                         t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1029                                                 } else {
1030                                                         t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1031                                                 }
1032                                                 data2 += t1->w;
1033                                         }
1034                                 } else {
1035                                         flags2 += t1->flags_stride;
1036                                         t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1037                                         data2 += t1->w;
1038                                         flags2 += t1->flags_stride;
1039                                         t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1040                                         data2 += t1->w;
1041                                         flags2 += t1->flags_stride;
1042                                         t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1043                                         data2 += t1->w;
1044                                         flags2 += t1->flags_stride;
1045                                         t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1046                                         data2 += t1->w;
1047                                 }
1048                         }
1049                         data1 += t1->w << 2;
1050                         flags1 += t1->flags_stride << 2;
1051                 }
1052                 for (i = 0; i < t1->w; ++i) {
1053                         int *data2 = data1 + i;
1054                         flag_t *flags2 = flags1 + i;
1055                         for (j = k; j < t1->h; ++j) {
1056                                 flags2 += t1->flags_stride;
1057                                 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1058                                 data2 += t1->w;
1059                         }
1060                 }
1061         }
1062
1063         if (segsym) {
1064                 int v = 0;
1065                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1066                 v = mqc_decode(mqc);
1067                 v = (v << 1) | mqc_decode(mqc);
1068                 v = (v << 1) | mqc_decode(mqc);
1069                 v = (v << 1) | mqc_decode(mqc);
1070                 /*
1071                 if (v!=0xa) {
1072                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1073                 } 
1074                 */
1075         }
1076 }                               /* VSC and  BYPASS by Antonin */
1077
1078
1079 /** mod fixed_quality */
1080 static double t1_getwmsedec(
1081                 int nmsedec,
1082                 int compno,
1083                 int level,
1084                 int orient,
1085                 int bpno,
1086                 int qmfbid,
1087                 double stepsize,
1088                 int numcomps,
1089                 int mct)
1090 {
1091         double w1, w2, wmsedec;
1092         if (qmfbid == 1) {
1093                 w1 = (mct && numcomps==3) ? mct_getnorm(compno) : 1.0;
1094                 w2 = dwt_getnorm(level, orient);
1095         } else {                        /* if (qmfbid == 0) */
1096                 w1 = (mct && numcomps==3) ? mct_getnorm_real(compno) : 1.0;
1097                 w2 = dwt_getnorm_real(level, orient);
1098         }
1099         wmsedec = w1 * w2 * stepsize * (1 << bpno);
1100         wmsedec *= wmsedec * nmsedec / 8192.0;
1101         
1102         return wmsedec;
1103 }
1104
1105 static bool allocate_buffers(
1106                 opj_t1_t *t1,
1107                 int w,
1108                 int h)
1109 {
1110         int datasize=w * h;
1111         int flagssize;
1112
1113         if(datasize > t1->datasize){
1114                 opj_aligned_free(t1->data);
1115                 t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int));
1116                 if(!t1->data){
1117                         return false;
1118                 }
1119                 t1->datasize=datasize;
1120         }
1121         memset(t1->data,0,datasize * sizeof(int));
1122
1123         t1->flags_stride=w+2;
1124         flagssize=t1->flags_stride * (h+2);
1125
1126         if(flagssize > t1->flagssize){
1127                 opj_aligned_free(t1->flags);
1128                 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
1129                 if(!t1->flags){
1130                         return false;
1131                 }
1132                 t1->flagssize=flagssize;
1133         }
1134         memset(t1->flags,0,flagssize * sizeof(flag_t));
1135
1136         t1->w=w;
1137         t1->h=h;
1138
1139         return true;
1140 }
1141
1142 /** mod fixed_quality */
1143 static void t1_encode_cblk(
1144                 opj_t1_t *t1,
1145                 opj_tcd_cblk_enc_t* cblk,
1146                 int orient,
1147                 int compno,
1148                 int level,
1149                 int qmfbid,
1150                 double stepsize,
1151                 int cblksty,
1152                 int numcomps,
1153                 int mct,
1154                 opj_tcd_tile_t * tile)
1155 {
1156         double cumwmsedec = 0.0;
1157
1158         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1159
1160         int passno, bpno, passtype;
1161         int nmsedec = 0;
1162         int i, max;
1163         char type = T1_TYPE_MQ;
1164         double tempwmsedec;
1165
1166         max = 0;
1167         for (i = 0; i < t1->w * t1->h; ++i) {
1168                 int tmp = abs(t1->data[i]);
1169                 max = int_max(max, tmp);
1170         }
1171
1172         cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1173         
1174         bpno = cblk->numbps - 1;
1175         passtype = 2;
1176         
1177         mqc_resetstates(mqc);
1178         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1179         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1180         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1181         mqc_init_enc(mqc, cblk->data);
1182         
1183         for (passno = 0; bpno >= 0; ++passno) {
1184                 opj_tcd_pass_t *pass = &cblk->passes[passno];
1185                 int correction = 3;
1186                 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1187                 
1188                 switch (passtype) {
1189                         case 0:
1190                                 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1191                                 break;
1192                         case 1:
1193                                 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1194                                 break;
1195                         case 2:
1196                                 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1197                                 /* code switch SEGMARK (i.e. SEGSYM) */
1198                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1199                                         mqc_segmark_enc(mqc);
1200                                 break;
1201                 }
1202                 
1203                 /* fixed_quality */
1204                 tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps, mct);
1205                 cumwmsedec += tempwmsedec;
1206                 tile->distotile += tempwmsedec;
1207                 
1208                 /* Code switch "RESTART" (i.e. TERMALL) */
1209                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1210                         if (type == T1_TYPE_RAW) {
1211                                 mqc_flush(mqc);
1212                                 correction = 1;
1213                                 /* correction = mqc_bypass_flush_enc(); */
1214                         } else {                        /* correction = mqc_restart_enc(); */
1215                                 mqc_flush(mqc);
1216                                 correction = 1;
1217                         }
1218                         pass->term = 1;
1219                 } else {
1220                         if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
1221                                 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1222                                 if (type == T1_TYPE_RAW) {
1223                                         mqc_flush(mqc);
1224                                         correction = 1;
1225                                         /* correction = mqc_bypass_flush_enc(); */
1226                                 } else {                /* correction = mqc_restart_enc(); */
1227                                         mqc_flush(mqc);
1228                                         correction = 1;
1229                                 }
1230                                 pass->term = 1;
1231                         } else {
1232                                 pass->term = 0;
1233                         }
1234                 }
1235                 
1236                 if (++passtype == 3) {
1237                         passtype = 0;
1238                         bpno--;
1239                 }
1240                 
1241                 if (pass->term && bpno > 0) {
1242                         type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1243                         if (type == T1_TYPE_RAW)
1244                                 mqc_bypass_init_enc(mqc);
1245                         else
1246                                 mqc_restart_init_enc(mqc);
1247                 }
1248                 
1249                 pass->distortiondec = cumwmsedec;
1250                 pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */
1251                 
1252                 /* Code-switch "RESET" */
1253                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1254                         mqc_reset_enc(mqc);
1255         }
1256         
1257         /* Code switch "ERTERM" (i.e. PTERM) */
1258         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1259                 mqc_erterm_enc(mqc);
1260         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1261                 mqc_flush(mqc);
1262         
1263         cblk->totalpasses = passno;
1264
1265         for (passno = 0; passno<cblk->totalpasses; passno++) {
1266                 opj_tcd_pass_t *pass = &cblk->passes[passno];
1267                 if (pass->rate > mqc_numbytes(mqc))
1268                         pass->rate = mqc_numbytes(mqc);
1269                 /*Preventing generation of FF as last data byte of a pass*/
1270                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1271                         pass->rate--;
1272                 }
1273                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);             
1274         }
1275 }
1276
1277 static void t1_decode_cblk(
1278                 opj_t1_t *t1,
1279                 opj_tcd_cblk_dec_t* cblk,
1280                 int orient,
1281                 int roishift,
1282                 int cblksty)
1283 {
1284         opj_raw_t *raw = t1->raw;       /* RAW component */
1285         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1286
1287         int bpno, passtype;
1288         int segno, passno;
1289         char type = T1_TYPE_MQ; /* BYPASS mode */
1290
1291         if(!allocate_buffers(
1292                                 t1,
1293                                 cblk->x1 - cblk->x0,
1294                                 cblk->y1 - cblk->y0))
1295         {
1296                 return;
1297         }
1298
1299         bpno = roishift + cblk->numbps - 1;
1300         passtype = 2;
1301         
1302         mqc_resetstates(mqc);
1303         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1304         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1305         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1306         
1307         for (segno = 0; segno < cblk->numsegs; ++segno) {
1308                 opj_tcd_seg_t *seg = &cblk->segs[segno];
1309                 
1310                 /* BYPASS mode */
1311                 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1312                 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1313                 if(seg->data == NULL){
1314                         continue;
1315                 }
1316                 if (type == T1_TYPE_RAW) {
1317                         raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1318                 } else {
1319                         mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len);
1320                 }
1321                 
1322                 for (passno = 0; passno < seg->numpasses; ++passno) {
1323                         switch (passtype) {
1324                                 case 0:
1325                                         if (type == T1_TYPE_RAW) {
1326                                                 t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1327                                         } else {
1328                                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1329                                                         t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1330                                                 } else {
1331                                                         t1_dec_sigpass_mqc(t1, bpno+1, orient);
1332                                                 }
1333                                         }
1334                                         break;
1335                                 case 1:
1336                                         if (type == T1_TYPE_RAW) {
1337                                                 t1_dec_refpass_raw(t1, bpno+1, cblksty);
1338                                         } else {
1339                                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1340                                                         t1_dec_refpass_mqc_vsc(t1, bpno+1);
1341                                                 } else {
1342                                                         t1_dec_refpass_mqc(t1, bpno+1);
1343                                                 }
1344                                         }
1345                                         break;
1346                                 case 2:
1347                                         t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1348                                         break;
1349                         }
1350                         
1351                         if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1352                                 mqc_resetstates(mqc);
1353                                 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);                         
1354                                 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1355                                 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1356                         }
1357                         if (++passtype == 3) {
1358                                 passtype = 0;
1359                                 bpno--;
1360                         }
1361                 }
1362         }
1363 }
1364
1365 /* ----------------------------------------------------------------------- */
1366
1367 opj_t1_t* t1_create(opj_common_ptr cinfo) {
1368         opj_t1_t *t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1369         if(!t1)
1370                 return NULL;
1371
1372         t1->cinfo = cinfo;
1373         /* create MQC and RAW handles */
1374         t1->mqc = mqc_create();
1375         t1->raw = raw_create();
1376
1377         t1->data=NULL;
1378         t1->flags=NULL;
1379         t1->datasize=0;
1380         t1->flagssize=0;
1381
1382         return t1;
1383 }
1384
1385 void t1_destroy(opj_t1_t *t1) {
1386         if(t1) {
1387                 /* destroy MQC and RAW handles */
1388                 mqc_destroy(t1->mqc);
1389                 raw_destroy(t1->raw);
1390                 opj_aligned_free(t1->data);
1391                 opj_aligned_free(t1->flags);
1392                 opj_free(t1);
1393         }
1394 }
1395
1396 void t1_encode_cblks(
1397                 opj_t1_t *t1,
1398                 opj_tcd_tile_t *tile,
1399                 opj_tcp_t *tcp)
1400 {
1401         int compno, resno, bandno, precno, cblkno;
1402
1403         tile->distotile = 0;            /* fixed_quality */
1404
1405         for (compno = 0; compno < tile->numcomps; ++compno) {
1406                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1407                 opj_tccp_t* tccp = &tcp->tccps[compno];
1408                 int tile_w = tilec->x1 - tilec->x0;
1409
1410                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1411                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1412
1413                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1414                                 opj_tcd_band_t* restrict band = &res->bands[bandno];
1415
1416                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1417                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1418
1419                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1420                                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1421                                                 int* restrict datap;
1422                                                 int* restrict tiledp;
1423                                                 int cblk_w;
1424                                                 int cblk_h;
1425                                                 int i, j;
1426
1427                                                 int x = cblk->x0 - band->x0;
1428                                                 int y = cblk->y0 - band->y0;
1429                                                 if (band->bandno & 1) {
1430                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1431                                                         x += pres->x1 - pres->x0;
1432                                                 }
1433                                                 if (band->bandno & 2) {
1434                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1435                                                         y += pres->y1 - pres->y0;
1436                                                 }
1437
1438                                                 if(!allocate_buffers(
1439                                                                         t1,
1440                                                                         cblk->x1 - cblk->x0,
1441                                                                         cblk->y1 - cblk->y0))
1442                                                 {
1443                                                         return;
1444                                                 }
1445
1446                                                 datap=t1->data;
1447                                                 cblk_w = t1->w;
1448                                                 cblk_h = t1->h;
1449
1450                                                 tiledp=&tilec->data[(y * tile_w) + x];
1451                                                 if (tccp->qmfbid == 1) {
1452                                                         for (j = 0; j < cblk_h; ++j) {
1453                                                                 for (i = 0; i < cblk_w; ++i) {
1454                                                                         int tmp = tiledp[(j * tile_w) + i];
1455                                                                         datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1456                                                                 }
1457                                                         }
1458                                                 } else {                /* if (tccp->qmfbid == 0) */
1459                                                         for (j = 0; j < cblk_h; ++j) {
1460                                                                 for (i = 0; i < cblk_w; ++i) {
1461                                                                         int tmp = tiledp[(j * tile_w) + i];
1462                                                                         datap[(j * cblk_w) + i] =
1463                                                                                 fix_mul(
1464                                                                                 tmp,
1465                                                                                 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS);
1466                                                                 }
1467                                                         }
1468                                                 }
1469
1470                                                 t1_encode_cblk(
1471                                                                 t1,
1472                                                                 cblk,
1473                                                                 band->bandno,
1474                                                                 compno,
1475                                                                 tilec->numresolutions - 1 - resno,
1476                                                                 tccp->qmfbid,
1477                                                                 band->stepsize,
1478                                                                 tccp->cblksty,
1479                                                                 tile->numcomps,
1480                                                                 tcp->mct,
1481                                                                 tile);
1482
1483                                         } /* cblkno */
1484                                 } /* precno */
1485                         } /* bandno */
1486                 } /* resno  */
1487         } /* compno  */
1488 }
1489
1490 void t1_decode_cblks(
1491                 opj_t1_t* t1,
1492                 opj_tcd_tilecomp_t* tilec,
1493                 opj_tccp_t* tccp)
1494 {
1495         int resno, bandno, precno, cblkno;
1496
1497         int tile_w = tilec->x1 - tilec->x0;
1498
1499         for (resno = 0; resno < tilec->numresolutions; ++resno) {
1500                 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1501
1502                 for (bandno = 0; bandno < res->numbands; ++bandno) {
1503                         opj_tcd_band_t* restrict band = &res->bands[bandno];
1504
1505                         for (precno = 0; precno < res->pw * res->ph; ++precno) {
1506                                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1507
1508                                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1509                                         opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1510                                         int* restrict datap;
1511                                         int cblk_w, cblk_h;
1512                                         int x, y;
1513                                         int i, j;
1514
1515                                         t1_decode_cblk(
1516                                                         t1,
1517                                                         cblk,
1518                                                         band->bandno,
1519                                                         tccp->roishift,
1520                                                         tccp->cblksty);
1521
1522                                         x = cblk->x0 - band->x0;
1523                                         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                                         datap=t1->data;
1534                                         cblk_w = t1->w;
1535                                         cblk_h = t1->h;
1536
1537                                         if (tccp->roishift) {
1538                                                 int thresh = 1 << tccp->roishift;
1539                                                 for (j = 0; j < cblk_h; ++j) {
1540                                                         for (i = 0; i < cblk_w; ++i) {
1541                                                                 int val = datap[(j * cblk_w) + i];
1542                                                                 int mag = abs(val);
1543                                                                 if (mag >= thresh) {
1544                                                                         mag >>= tccp->roishift;
1545                                                                         datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1546                                                                 }
1547                                                         }
1548                                                 }
1549                                         }
1550
1551                                         if (tccp->qmfbid == 1) {
1552                                                 int* restrict tiledp = &tilec->data[(y * tile_w) + x];
1553                                                 for (j = 0; j < cblk_h; ++j) {
1554                                                         for (i = 0; i < cblk_w; ++i) {
1555                                                                 int tmp = datap[(j * cblk_w) + i];
1556                                                                 ((int*)tiledp)[(j * tile_w) + i] = tmp / 2;
1557                                                         }
1558                                                 }
1559                                         } else {                /* if (tccp->qmfbid == 0) */
1560                                                 float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
1561                                                 for (j = 0; j < cblk_h; ++j) {
1562                                                         float* restrict tiledp2 = tiledp;
1563                                                         for (i = 0; i < cblk_w; ++i) {
1564                                                                 float tmp = *datap * band->stepsize;
1565                                                                 *tiledp2 = tmp;
1566                                                                 datap++;
1567                                                                 tiledp2++;
1568                                                         }
1569                                                         tiledp += tile_w;
1570                                                 }
1571                                         }
1572                                         opj_free(cblk->data);
1573                                         opj_free(cblk->segs);
1574                                 } /* cblkno */
1575                                 opj_free(precinct->cblks.dec);
1576                         } /* precno */
1577                 } /* bandno */
1578         } /* resno */
1579 }
1580