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