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