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