Force inlining of mqc decoding and pass steps through heavy use of macros, so as...
[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  * Copyright (c) 2012, Carl Hetherington
16  * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #include "opj_includes.h"
42 #include "t1_luts.h"
43
44 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
45 /*@{*/
46
47 #define T1_FLAGS(x, y) (t1->flags[x + 1 + ((y / 4) + 1) * (t1->w+2)])
48
49 #define opj_t1_setcurctx(curctx, ctxno)  curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
50
51 /** @name Local static functions */
52 /*@{*/
53
54 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
55 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
56 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
57 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
58 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
59                                        OPJ_UINT32 s, OPJ_UINT32 stride);
60
61
62 /**
63 Decode significant pass
64 */
65
66 static INLINE void opj_t1_dec_sigpass_step_raw(
67     opj_t1_t *t1,
68     opj_flag_t *flagsp,
69     OPJ_INT32 *datap,
70     OPJ_INT32 oneplushalf,
71     OPJ_INT32 vsc,
72     OPJ_UINT32 row);
73 static INLINE void opj_t1_dec_sigpass_step_mqc(
74     opj_t1_t *t1,
75     opj_flag_t *flagsp,
76     OPJ_INT32 *datap,
77     OPJ_INT32 oneplushalf,
78     OPJ_UINT32 row,
79     OPJ_UINT32 flags_stride);
80 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
81     opj_t1_t *t1,
82     opj_flag_t *flagsp,
83     OPJ_INT32 *datap,
84     OPJ_INT32 oneplushalf,
85     OPJ_INT32 vsc,
86     OPJ_UINT32 row);
87
88
89 /**
90 Encode significant pass
91 */
92 static void opj_t1_enc_sigpass(opj_t1_t *t1,
93                                OPJ_INT32 bpno,
94                                OPJ_INT32 *nmsedec,
95                                OPJ_BYTE type,
96                                OPJ_UINT32 cblksty);
97
98 /**
99 Decode significant pass
100 */
101 static void opj_t1_dec_sigpass_raw(
102     opj_t1_t *t1,
103     OPJ_INT32 bpno,
104     OPJ_INT32 cblksty);
105 static void opj_t1_dec_sigpass_mqc_vsc(
106     opj_t1_t *t1,
107     OPJ_INT32 bpno);
108
109
110
111 /**
112 Encode refinement pass
113 */
114 static void opj_t1_enc_refpass(opj_t1_t *t1,
115                                OPJ_INT32 bpno,
116                                OPJ_INT32 *nmsedec,
117                                OPJ_BYTE type,
118                                OPJ_UINT32 cblksty);
119
120 /**
121 Decode refinement pass
122 */
123 static void opj_t1_dec_refpass_raw(
124     opj_t1_t *t1,
125     OPJ_INT32 bpno);
126 static void opj_t1_dec_refpass_mqc_vsc(
127     opj_t1_t *t1,
128     OPJ_INT32 bpno);
129
130
131 /**
132 Decode refinement pass
133 */
134
135 static INLINE void  opj_t1_dec_refpass_step_raw(
136     opj_t1_t *t1,
137     opj_flag_t *flagsp,
138     OPJ_INT32 *datap,
139     OPJ_INT32 poshalf,
140     OPJ_UINT32 row);
141 static INLINE void opj_t1_dec_refpass_step_mqc(
142     opj_t1_t *t1,
143     opj_flag_t *flagsp,
144     OPJ_INT32 *datap,
145     OPJ_INT32 poshalf,
146     OPJ_UINT32 row);
147 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
148     opj_t1_t *t1,
149     opj_flag_t *flagsp,
150     OPJ_INT32 *datap,
151     OPJ_INT32 poshalf,
152     OPJ_INT32 vsc,
153     OPJ_UINT32 row);
154
155
156 /**
157 Decode clean-up pass
158 */
159
160 static void opj_t1_dec_clnpass_step(
161     opj_t1_t *t1,
162     opj_flag_t *flagsp,
163     OPJ_INT32 *datap,
164     OPJ_INT32 oneplushalf,
165     OPJ_UINT32 row);
166 static void opj_t1_dec_clnpass_step_vsc(
167     opj_t1_t *t1,
168     opj_flag_t *flagsp,
169     OPJ_INT32 *datap,
170     OPJ_INT32 oneplushalf,
171     OPJ_INT32 partial,
172     OPJ_INT32 vsc,
173     OPJ_UINT32 row);
174 /**
175 Encode clean-up pass
176 */
177 static void opj_t1_enc_clnpass(
178     opj_t1_t *t1,
179     OPJ_INT32 bpno,
180     OPJ_INT32 *nmsedec,
181     OPJ_UINT32 cblksty);
182
183 static OPJ_FLOAT64 opj_t1_getwmsedec(
184     OPJ_INT32 nmsedec,
185     OPJ_UINT32 compno,
186     OPJ_UINT32 level,
187     OPJ_UINT32 orient,
188     OPJ_INT32 bpno,
189     OPJ_UINT32 qmfbid,
190     OPJ_FLOAT64 stepsize,
191     OPJ_UINT32 numcomps,
192     const OPJ_FLOAT64 * mct_norms,
193     OPJ_UINT32 mct_numcomps);
194
195 static void opj_t1_encode_cblk(opj_t1_t *t1,
196                                opj_tcd_cblk_enc_t* cblk,
197                                OPJ_UINT32 orient,
198                                OPJ_UINT32 compno,
199                                OPJ_UINT32 level,
200                                OPJ_UINT32 qmfbid,
201                                OPJ_FLOAT64 stepsize,
202                                OPJ_UINT32 cblksty,
203                                OPJ_UINT32 numcomps,
204                                opj_tcd_tile_t * tile,
205                                const OPJ_FLOAT64 * mct_norms,
206                                OPJ_UINT32 mct_numcomps);
207
208 /**
209 Decode 1 code-block
210 @param t1 T1 handle
211 @param cblk Code-block coding parameters
212 @param orient
213 @param roishift Region of interest shifting value
214 @param cblksty Code-block style
215 */
216 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
217                                    opj_tcd_cblk_dec_t* cblk,
218                                    OPJ_UINT32 orient,
219                                    OPJ_UINT32 roishift,
220                                    OPJ_UINT32 cblksty);
221
222 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
223                                         OPJ_UINT32 w,
224                                         OPJ_UINT32 h);
225
226 /*@}*/
227
228 /*@}*/
229
230 /* ----------------------------------------------------------------------- */
231
232 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
233 {
234     return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
235 }
236
237 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
238         OPJ_UINT32 pfX,
239         OPJ_UINT32 nfX,
240         OPJ_UINT32 ci)
241 {
242     /*
243       0 pfX T1_CHI_THIS           T1_LUT_SGN_W
244       1 tfX T1_SIGMA_1            T1_LUT_SIG_N
245       2 nfX T1_CHI_THIS           T1_LUT_SGN_E
246       3 tfX T1_SIGMA_3            T1_LUT_SIG_W
247       4  fX T1_CHI_(THIS - 1)     T1_LUT_SGN_N
248       5 tfX T1_SIGMA_5            T1_LUT_SIG_E
249       6  fX T1_CHI_(THIS + 1)     T1_LUT_SGN_S
250       7 tfX T1_SIGMA_7            T1_LUT_SIG_S
251     */
252
253     OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
254                                          T1_SIGMA_7);
255
256     lu |= (pfX >> (T1_CHI_THIS_I      + (ci * 3U))) & (1U << 0);
257     lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
258     if (ci == 0U) {
259         lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
260     } else {
261         lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
262     }
263     lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
264     return lu;
265 }
266
267 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
268 {
269     return lut_ctxno_sc[lu];
270 }
271
272 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
273 {
274     OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
275     OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
276     return tmp2;
277 }
278
279 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
280 {
281     return lut_spb[lu];
282 }
283
284 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
285 {
286     if (bitpos > 0) {
287         return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
288     }
289
290     return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
291 }
292
293 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
294 {
295     if (bitpos > 0) {
296         return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
297     }
298
299     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
300 }
301
302 #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride ) \
303 { \
304     /* east */ \
305     flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
306  \
307     /* mark target as significant */ \
308     flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
309  \
310     /* west */ \
311     flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
312  \
313     /* north-west, north, north-east */ \
314     if (ci == 0U) { \
315         opj_flag_t* north = flagsp - (stride); \
316         *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
317         north[-1] |= T1_SIGMA_17; \
318         north[1] |= T1_SIGMA_15; \
319     } \
320  \
321     /* south-west, south, south-east */ \
322     if (ci == 3U) { \
323         opj_flag_t* south = flagsp + (stride); \
324         *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
325         south[-1] |= T1_SIGMA_2; \
326         south[1] |= T1_SIGMA_0; \
327     } \
328 }
329
330
331 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
332                                        OPJ_UINT32 s, OPJ_UINT32 stride)
333 {
334     opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride);
335 }
336
337 /**
338 Encode significant pass
339 */
340 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
341         opj_flag_t *flagsp,
342         OPJ_INT32 *datap,
343         OPJ_INT32 bpno,
344         OPJ_INT32 one,
345         OPJ_INT32 *nmsedec,
346         OPJ_BYTE type,
347         OPJ_UINT32 ci,
348         OPJ_UINT32 vsc
349                                           )
350 {
351     OPJ_UINT32 v;
352
353     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
354
355     OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
356                                    T1_CHI_S) << (ci * 3U)) : ~0U;
357     OPJ_UINT32 const flags = *flagsp & vsc_mask;
358
359     if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
360             (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
361         OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
362         v = opj_int_abs(*datap) & one ? 1 : 0;
363 #ifdef DEBUG_ENC_SIG
364         fprintf(stderr, "   ctxt1=%d\n", ctxt1);
365 #endif
366         opj_mqc_setcurctx(mqc, ctxt1);
367         if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
368             opj_mqc_bypass_enc(mqc, v);
369         } else {
370             opj_mqc_encode(mqc, v);
371         }
372         if (v) {
373             /* Note: using flags instead of *flagsp & vsc_mask result */
374             /* in slow down. Probably because of register pressure */
375             OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
376                                 *flagsp & vsc_mask,
377                                 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
378                                 ci);
379             OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
380             v = *datap < 0 ? 1 : 0;
381             *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
382                                               (OPJ_UINT32)bpno);
383 #ifdef DEBUG_ENC_SIG
384             fprintf(stderr, "   ctxt2=%d\n", ctxt2);
385 #endif
386             opj_mqc_setcurctx(mqc, ctxt2);
387             if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
388                 opj_mqc_bypass_enc(mqc, v);
389             } else {
390                 OPJ_UINT32 spb = opj_t1_getspb(lu);
391 #ifdef DEBUG_ENC_SIG
392                 fprintf(stderr, "   spb=%d\n", spb);
393 #endif
394                 opj_mqc_encode(mqc, v ^ spb);
395             }
396             opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
397         }
398         *flagsp |= T1_PI_THIS << (ci * 3U);
399     }
400 }
401
402 static INLINE void opj_t1_dec_sigpass_step_raw(
403     opj_t1_t *t1,
404     opj_flag_t *flagsp,
405     OPJ_INT32 *datap,
406     OPJ_INT32 oneplushalf,
407     OPJ_INT32 vsc,
408     OPJ_UINT32 ci)
409 {
410     OPJ_UINT32 v;
411     opj_raw_t *raw = &(t1->raw);       /* RAW component */
412
413     OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
414                                    T1_CHI_S) << (ci * 3U)) : ~0U;
415     OPJ_UINT32 const flags = *flagsp & vsc_mask;
416
417     if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
418             (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
419         if (opj_raw_decode(raw)) {
420             v = opj_raw_decode(raw);
421             *datap = v ? -oneplushalf : oneplushalf;
422             opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
423         }
424         *flagsp |= T1_PI_THIS << (ci * 3U);
425     }
426 }
427
428 #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
429                                           data_stride, ci, mqc, curctx, \
430                                           v, a, c, ct, oneplushalf) \
431 { \
432     if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
433         (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
434         OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
435         opj_t1_setcurctx(curctx, ctxt1); \
436         opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
437         if (v) { \
438             OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
439                                 flags, \
440                                 flagsp[-1], flagsp[1], \
441                                 ci); \
442             OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
443             OPJ_UINT32 spb = opj_t1_getspb(lu); \
444             opj_t1_setcurctx(curctx, ctxt2); \
445             opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
446             v = v ^ spb; \
447             data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
448             opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride); \
449         } \
450         flags |= T1_PI_THIS << (ci * 3U); \
451     } \
452 }
453
454 static INLINE void opj_t1_dec_sigpass_step_mqc(
455     opj_t1_t *t1,
456     opj_flag_t *flagsp,
457     OPJ_INT32 *datap,
458     OPJ_INT32 oneplushalf,
459     OPJ_UINT32 ci,
460     OPJ_UINT32 flags_stride)
461 {
462     OPJ_UINT32 v;
463
464     opj_mqc_t *mqc = &(t1->mqc);       /* MQC component */
465     opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
466                                       0, ci, mqc, mqc->curctx,
467                                       v, mqc->a, mqc->c, mqc->ct, oneplushalf);
468 }
469
470 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
471     opj_t1_t *t1,
472     opj_flag_t *flagsp,
473     OPJ_INT32 *datap,
474     OPJ_INT32 oneplushalf,
475     OPJ_INT32 vsc,
476     OPJ_UINT32 ci)
477 {
478     OPJ_UINT32 v;
479
480     opj_mqc_t *mqc = &(t1->mqc);       /* MQC component */
481
482     OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
483                                    T1_CHI_S) << (ci * 3U)) : ~0U;
484     OPJ_UINT32 const flags = *flagsp & vsc_mask;
485
486     if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
487             (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
488         OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
489                             *flagsp & vsc_mask,
490                             flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
491                             ci);
492         OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
493         opj_mqc_setcurctx(mqc, ctxt1);
494         opj_mqc_decode(v, mqc);
495         if (v) {
496             OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
497             OPJ_UINT32 spb = opj_t1_getspb(lu);
498             opj_mqc_setcurctx(mqc, ctxt2);
499             opj_mqc_decode(v, mqc);
500             v = v ^ spb;
501             *datap = v ? -oneplushalf : oneplushalf;
502             opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
503         }
504         *flagsp |= T1_PI_THIS << (ci * 3U);
505     }
506 }
507
508
509 static void opj_t1_enc_sigpass(opj_t1_t *t1,
510                                OPJ_INT32 bpno,
511                                OPJ_INT32 *nmsedec,
512                                OPJ_BYTE type,
513                                OPJ_UINT32 cblksty
514                               )
515 {
516     OPJ_UINT32 i, k;
517     OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
518     opj_flag_t* f = &T1_FLAGS(0, 0);
519     OPJ_UINT32 const extra = 2;
520
521     *nmsedec = 0;
522 #ifdef DEBUG_ENC_SIG
523     fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
524 #endif
525     for (k = 0; k < (t1->h & ~3U); k += 4) {
526 #ifdef DEBUG_ENC_SIG
527         fprintf(stderr, " k=%d\n", k);
528 #endif
529         for (i = 0; i < t1->w; ++i) {
530 #ifdef DEBUG_ENC_SIG
531             fprintf(stderr, " i=%d\n", i);
532 #endif
533             if (*f == 0U) {
534                 /* Nothing to do for any of the 4 data points */
535                 f++;
536                 continue;
537             }
538             opj_t1_enc_sigpass_step(
539                 t1,
540                 f,
541                 &t1->data[((k + 0) * t1->data_stride) + i],
542                 bpno,
543                 one,
544                 nmsedec,
545                 type,
546                 0, 0);
547             opj_t1_enc_sigpass_step(
548                 t1,
549                 f,
550                 &t1->data[((k + 1) * t1->data_stride) + i],
551                 bpno,
552                 one,
553                 nmsedec,
554                 type,
555                 1, 0);
556             opj_t1_enc_sigpass_step(
557                 t1,
558                 f,
559                 &t1->data[((k + 2) * t1->data_stride) + i],
560                 bpno,
561                 one,
562                 nmsedec,
563                 type,
564                 2, 0);
565             opj_t1_enc_sigpass_step(
566                 t1,
567                 f,
568                 &t1->data[((k + 3) * t1->data_stride) + i],
569                 bpno,
570                 one,
571                 nmsedec,
572                 type,
573                 3, cblksty & J2K_CCP_CBLKSTY_VSC);
574             ++f;
575         }
576         f += extra;
577     }
578
579     if (k < t1->h) {
580         OPJ_UINT32 j;
581 #ifdef DEBUG_ENC_SIG
582         fprintf(stderr, " k=%d\n", k);
583 #endif
584         for (i = 0; i < t1->w; ++i) {
585 #ifdef DEBUG_ENC_SIG
586             fprintf(stderr, " i=%d\n", i);
587 #endif
588             if (*f == 0U) {
589                 /* Nothing to do for any of the 4 data points */
590                 f++;
591                 continue;
592             }
593             for (j = k; j < t1->h; ++j) {
594                 opj_t1_enc_sigpass_step(
595                     t1,
596                     f,
597                     &t1->data[(j * t1->data_stride) + i],
598                     bpno,
599                     one,
600                     nmsedec,
601                     type,
602                     j - k,
603                     (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
604             }
605             ++f;
606         }
607     }
608 }
609
610 static void opj_t1_dec_sigpass_raw(
611     opj_t1_t *t1,
612     OPJ_INT32 bpno,
613     OPJ_INT32 cblksty)
614 {
615     OPJ_INT32 one, half, oneplushalf;
616     OPJ_UINT32 i, j, k;
617     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
618     one = 1 << bpno;
619     half = one >> 1;
620     oneplushalf = one | half;
621     if ((cblksty & J2K_CCP_CBLKSTY_VSC)) {
622         OPJ_INT32 *data1 = t1->data;
623         for (k = 0; k < t1->h; k += 4) {
624             for (i = 0; i < t1->w; ++i) {
625                 OPJ_INT32* data2 = data1 + i;
626                 for (j = k; j < k + 4 && j < t1->h; ++j) {
627                     OPJ_INT32 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
628                     opj_t1_dec_sigpass_step_raw(
629                         t1,
630                         flagsp,
631                         data2,
632                         oneplushalf,
633                         vsc,
634                         j - k);
635                     data2 += t1->w;
636                 }
637                 flagsp ++;
638             }
639             data1 += t1->w << 2;
640             flagsp += 2;
641         }
642     } else {
643         OPJ_INT32 *data1 = t1->data;
644         for (k = 0; k < (t1->h & ~3U); k += 4) {
645             for (i = 0; i < t1->w; ++i) {
646                 OPJ_INT32* data2 = data1 + i;
647                 opj_t1_dec_sigpass_step_raw(
648                     t1,
649                     flagsp,
650                     data2,
651                     oneplushalf,
652                     0, /* vsc */
653                     0U);
654                 data2 += t1->w;
655                 opj_t1_dec_sigpass_step_raw(
656                     t1,
657                     flagsp,
658                     data2,
659                     oneplushalf,
660                     0, /* vsc */
661                     1U);
662                 data2 += t1->w;
663                 opj_t1_dec_sigpass_step_raw(
664                     t1,
665                     flagsp,
666                     data2,
667                     oneplushalf,
668                     0, /* vsc */
669                     2U);
670                 data2 += t1->w;
671                 opj_t1_dec_sigpass_step_raw(
672                     t1,
673                     flagsp,
674                     data2,
675                     oneplushalf,
676                     0, /* vsc */
677                     3U);
678                 data2 += t1->w;
679                 flagsp ++;
680             }
681             data1 += t1->w << 2;
682             flagsp += 2;
683         }
684         if (k < t1->h) {
685             for (i = 0; i < t1->w; ++i) {
686                 OPJ_INT32* data2 = data1 + i;
687                 for (j = 0; j < t1->h - k; ++j) {
688                     opj_t1_dec_sigpass_step_raw(
689                         t1,
690                         flagsp,
691                         data2,
692                         oneplushalf,
693                         0, /* vsc */
694                         j);
695                     data2 += t1->w;
696                 }
697                 flagsp ++;
698             }
699         }
700     }
701 }
702
703 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
704 { \
705         OPJ_INT32 one, half, oneplushalf; \
706         OPJ_UINT32 i, j, k; \
707         register OPJ_INT32 *data = t1->data; \
708         register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
709         const OPJ_UINT32 l_w = w; \
710         opj_mqc_t* mqc = &(t1->mqc); \
711         DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
712         register OPJ_UINT32 v; \
713         one = 1 << bpno; \
714         half = one >> 1; \
715         oneplushalf = one | half; \
716         for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
717                 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
718                         opj_flag_t flags = *flagsp; \
719                         if( flags != 0 ) { \
720                             opj_t1_dec_sigpass_step_mqc_macro( \
721                                 flags, flagsp, flags_stride, data, \
722                                 l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf); \
723                             opj_t1_dec_sigpass_step_mqc_macro( \
724                                 flags, flagsp, flags_stride, data, \
725                                 l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf); \
726                             opj_t1_dec_sigpass_step_mqc_macro( \
727                                 flags, flagsp, flags_stride, data, \
728                                 l_w, 2, mqc, curctx, v, a, c, ct, oneplushalf); \
729                             opj_t1_dec_sigpass_step_mqc_macro( \
730                                 flags, flagsp, flags_stride, data, \
731                                 l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf); \
732                             *flagsp = flags; \
733                         } \
734                 } \
735         } \
736         UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
737         if( k < h ) { \
738             for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
739                 for (j = 0; j < h - k; ++j) { \
740                         opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
741                             data + j * l_w, oneplushalf, j, flags_stride); \
742                 } \
743             } \
744         } \
745 }
746
747 static void opj_t1_dec_sigpass_mqc_64x64(
748     opj_t1_t *t1,
749     OPJ_INT32 bpno)
750 {
751     opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
752 }
753
754 static void opj_t1_dec_sigpass_mqc_generic(
755     opj_t1_t *t1,
756     OPJ_INT32 bpno)
757 {
758     opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2);
759 }
760
761 static void opj_t1_dec_sigpass_mqc_vsc(
762     opj_t1_t *t1,
763     OPJ_INT32 bpno)
764 {
765     OPJ_INT32 one, half, oneplushalf, vsc;
766     OPJ_UINT32 i, j, k;
767     OPJ_INT32 *data1 = t1->data;
768     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
769     one = 1 << bpno;
770     half = one >> 1;
771     oneplushalf = one | half;
772     for (k = 0; k < (t1->h & ~3U); k += 4U) {
773         for (i = 0; i < t1->w; ++i) {
774             OPJ_INT32 *data2 = data1 + i;
775             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 0U);
776             data2 += t1->w;
777             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 1U);
778             data2 += t1->w;
779             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 2U);
780             data2 += t1->w;
781             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 1, 3U);
782             data2 += t1->w;
783             flagsp ++;
784         }
785         data1 += t1->w << 2;
786         flagsp += 2;
787     }
788     for (i = 0; i < t1->w; ++i) {
789         OPJ_INT32 *data2 = data1 + i;
790         for (j = k; j < t1->h; ++j) {
791             vsc = (j == t1->h - 1) ? 1 : 0;
792             opj_t1_dec_sigpass_step_mqc_vsc(
793                 t1,
794                 flagsp,
795                 data2,
796                 oneplushalf,
797                 vsc,
798                 j - k);
799             data2 += t1->w;
800         }
801         flagsp ++;
802     }
803 }
804
805
806 /**
807 Encode refinement pass step
808 */
809 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
810         opj_flag_t *flagsp,
811         OPJ_INT32 *datap,
812         OPJ_INT32 bpno,
813         OPJ_INT32 one,
814         OPJ_INT32 *nmsedec,
815         OPJ_BYTE type,
816         OPJ_UINT32 ci,
817         OPJ_UINT32 vsc)
818 {
819     OPJ_UINT32 v;
820
821     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
822
823     OPJ_UINT32 const shift_flags =
824         vsc ?
825         ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
826         :
827         (*flagsp >> (ci * 3U));
828
829     if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
830         OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
831         *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
832                                           (OPJ_UINT32)bpno);
833         v = opj_int_abs(*datap) & one ? 1 : 0;
834 #ifdef DEBUG_ENC_REF
835         fprintf(stderr, "  ctxt=%d\n", ctxt);
836 #endif
837         opj_mqc_setcurctx(mqc, ctxt);
838         if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
839             opj_mqc_bypass_enc(mqc, v);
840         } else {
841             opj_mqc_encode(mqc, v);
842         }
843         *flagsp |= T1_MU_THIS << (ci * 3U);
844     }
845 }
846
847
848 static INLINE void opj_t1_dec_refpass_step_raw(
849     opj_t1_t *t1,
850     opj_flag_t *flagsp,
851     OPJ_INT32 *datap,
852     OPJ_INT32 poshalf,
853     OPJ_UINT32 ci)
854 {
855     OPJ_UINT32 v;
856
857     opj_raw_t *raw = &(t1->raw);       /* RAW component */
858
859     if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
860             (T1_SIGMA_THIS << (ci * 3U))) {
861         v = opj_raw_decode(raw);
862         *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
863         *flagsp |= T1_MU_THIS << (ci * 3U);
864     }
865 }
866
867 #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
868                                           mqc, curctx, v, a, c, ct, poshalf) \
869 { \
870     if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
871             (T1_SIGMA_THIS << (ci * 3U))) { \
872         OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
873         opj_t1_setcurctx(curctx, ctxt); \
874         opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
875         data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
876         flags |= T1_MU_THIS << (ci * 3U); \
877     } \
878 }
879
880 static INLINE void opj_t1_dec_refpass_step_mqc(
881     opj_t1_t *t1,
882     opj_flag_t *flagsp,
883     OPJ_INT32 *datap,
884     OPJ_INT32 poshalf,
885     OPJ_UINT32 ci)
886 {
887     OPJ_UINT32 v;
888
889     opj_mqc_t *mqc = &(t1->mqc);       /* MQC component */
890     opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
891                                       mqc, mqc->curctx, v, mqc->a, mqc->c,
892                                       mqc->ct, poshalf);
893 }
894
895 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
896     opj_t1_t *t1,
897     opj_flag_t *flagsp,
898     OPJ_INT32 *datap,
899     OPJ_INT32 poshalf,
900     OPJ_INT32 vsc,
901     OPJ_UINT32 ci)
902 {
903     OPJ_UINT32 v;
904
905     opj_mqc_t *mqc = &(t1->mqc);       /* MQC component */
906
907     OPJ_UINT32 const shift_flags =
908         vsc ?
909         ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
910         :
911         (*flagsp >> (ci * 3U));
912
913
914     if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
915         OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
916         opj_mqc_setcurctx(mqc, ctxt);
917         opj_mqc_decode(v, mqc);
918         *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
919         *flagsp |= T1_MU_THIS << (ci * 3U);
920     }
921 }
922
923 static void opj_t1_enc_refpass(
924     opj_t1_t *t1,
925     OPJ_INT32 bpno,
926     OPJ_INT32 *nmsedec,
927     OPJ_BYTE type,
928     OPJ_UINT32 cblksty)
929 {
930     OPJ_UINT32 i, k;
931     const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
932     opj_flag_t* f = &T1_FLAGS(0, 0);
933     const OPJ_UINT32 extra = 2U;
934
935     *nmsedec = 0;
936 #ifdef DEBUG_ENC_REF
937     fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
938 #endif
939     for (k = 0; k < (t1->h & ~3U); k += 4) {
940 #ifdef DEBUG_ENC_REF
941         fprintf(stderr, " k=%d\n", k);
942 #endif
943         for (i = 0; i < t1->w; ++i) {
944 #ifdef DEBUG_ENC_REF
945             fprintf(stderr, " i=%d\n", i);
946 #endif
947             if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
948                 /* none significant */
949                 f++;
950                 continue;
951             }
952             if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
953                     (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
954                 /* all processed by sigpass */
955                 f++;
956                 continue;
957             }
958
959             opj_t1_enc_refpass_step(
960                 t1,
961                 f,
962                 &t1->data[((k + 0) * t1->data_stride) + i],
963                 bpno,
964                 one,
965                 nmsedec,
966                 type,
967                 0, 0);
968             opj_t1_enc_refpass_step(
969                 t1,
970                 f,
971                 &t1->data[((k + 1) * t1->data_stride) + i],
972                 bpno,
973                 one,
974                 nmsedec,
975                 type,
976                 1, 0);
977             opj_t1_enc_refpass_step(
978                 t1,
979                 f,
980                 &t1->data[((k + 2) * t1->data_stride) + i],
981                 bpno,
982                 one,
983                 nmsedec,
984                 type,
985                 2, 0);
986             opj_t1_enc_refpass_step(
987                 t1,
988                 f,
989                 &t1->data[((k + 3) * t1->data_stride) + i],
990                 bpno,
991                 one,
992                 nmsedec,
993                 type,
994                 3, cblksty & J2K_CCP_CBLKSTY_VSC);
995             ++f;
996         }
997         f += extra;
998     }
999
1000     if (k < t1->h) {
1001         OPJ_UINT32 j;
1002 #ifdef DEBUG_ENC_REF
1003         fprintf(stderr, " k=%d\n", k);
1004 #endif
1005         for (i = 0; i < t1->w; ++i) {
1006 #ifdef DEBUG_ENC_REF
1007             fprintf(stderr, " i=%d\n", i);
1008 #endif
1009             if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1010                 /* none significant */
1011                 f++;
1012                 continue;
1013             }
1014             for (j = k; j < t1->h; ++j) {
1015                 opj_t1_enc_refpass_step(
1016                     t1,
1017                     f,
1018                     &t1->data[(j * t1->data_stride) + i],
1019                     bpno,
1020                     one,
1021                     nmsedec,
1022                     type,
1023                     j - k,
1024                     (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1025             }
1026             ++f;
1027         }
1028     }
1029 }
1030
1031
1032 static void opj_t1_dec_refpass_raw(
1033     opj_t1_t *t1,
1034     OPJ_INT32 bpno)
1035 {
1036     OPJ_INT32 one, poshalf;
1037     OPJ_UINT32 i, j, k;
1038     OPJ_INT32 *data1 = t1->data;
1039     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1040     one = 1 << bpno;
1041     poshalf = one >> 1;
1042     for (k = 0; k < (t1->h & ~3U); k += 4) {
1043         for (i = 0; i < t1->w; ++i) {
1044             OPJ_INT32 *data2 = data1 + i;
1045             opj_t1_dec_refpass_step_raw(
1046                 t1,
1047                 flagsp,
1048                 data2,
1049                 poshalf,
1050                 0U);
1051             data2 += t1->w;
1052             opj_t1_dec_refpass_step_raw(
1053                 t1,
1054                 flagsp,
1055                 data2,
1056                 poshalf,
1057                 1U);
1058             data2 += t1->w;
1059             opj_t1_dec_refpass_step_raw(
1060                 t1,
1061                 flagsp,
1062                 data2,
1063                 poshalf,
1064                 2U);
1065             data2 += t1->w;
1066             opj_t1_dec_refpass_step_raw(
1067                 t1,
1068                 flagsp,
1069                 data2,
1070                 poshalf,
1071                 3U);
1072             data2 += t1->w;
1073             flagsp ++;
1074         }
1075         data1 += t1->w << 2;
1076         flagsp += 2;
1077     }
1078     if (k < t1->h) {
1079         for (i = 0; i < t1->w; ++i) {
1080             OPJ_INT32 *data2 = data1 + i;
1081             for (j = k; j < t1->h; ++j) {
1082                 opj_t1_dec_refpass_step_raw(
1083                     t1,
1084                     flagsp,
1085                     data2,
1086                     poshalf,
1087                     j - k);
1088                 data2 += t1->w;
1089             }
1090             flagsp ++;
1091         }
1092     }
1093 }
1094
1095 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1096 { \
1097         OPJ_INT32 one, poshalf; \
1098         OPJ_UINT32 i, j, k; \
1099         register OPJ_INT32 *data = t1->data; \
1100         register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1101         const OPJ_UINT32 l_w = w; \
1102         opj_mqc_t* mqc = &(t1->mqc); \
1103         DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1104         register OPJ_UINT32 v; \
1105         one = 1 << bpno; \
1106         poshalf = one >> 1; \
1107         for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1108                 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1109                         opj_flag_t flags = *flagsp; \
1110                         if( flags != 0 ) { \
1111                             opj_t1_dec_refpass_step_mqc_macro( \
1112                                 flags, data, l_w, 0, \
1113                                 mqc, curctx, v, a, c, ct, poshalf); \
1114                             opj_t1_dec_refpass_step_mqc_macro( \
1115                                 flags, data, l_w, 1, \
1116                                 mqc, curctx, v, a, c, ct, poshalf); \
1117                             opj_t1_dec_refpass_step_mqc_macro( \
1118                                 flags, data, l_w, 2, \
1119                                 mqc, curctx, v, a, c, ct, poshalf); \
1120                             opj_t1_dec_refpass_step_mqc_macro( \
1121                                 flags, data, l_w, 3, \
1122                                 mqc, curctx, v, a, c, ct, poshalf); \
1123                             *flagsp = flags; \
1124                         } \
1125                 } \
1126         } \
1127         UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1128         if( k < h ) { \
1129             for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1130                 for (j = 0; j < h - k; ++j) { \
1131                         opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
1132                 } \
1133             } \
1134         } \
1135 }
1136
1137 static void opj_t1_dec_refpass_mqc_64x64(
1138     opj_t1_t *t1,
1139     OPJ_INT32 bpno)
1140 {
1141     opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1142 }
1143
1144 static void opj_t1_dec_refpass_mqc_generic(
1145     opj_t1_t *t1,
1146     OPJ_INT32 bpno)
1147 {
1148     opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1149 }
1150
1151 static void opj_t1_dec_refpass_mqc_vsc(
1152     opj_t1_t *t1,
1153     OPJ_INT32 bpno)
1154 {
1155     OPJ_INT32 one, poshalf;
1156     OPJ_UINT32 i, j, k;
1157     OPJ_INT32 vsc;
1158     OPJ_INT32 *data1 = t1->data;
1159     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1160     one = 1 << bpno;
1161     poshalf = one >> 1;
1162     for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1163         for (i = 0; i < t1->w; ++i) {
1164             OPJ_INT32 *data2 = data1 + i;
1165             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1166                                             0, 0U);
1167             data2 += t1->w;
1168             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1169                                             0, 1U);
1170             data2 += t1->w;
1171             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1172                                             0, 2U);
1173             data2 += t1->w;
1174             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1175                                             1, 3U);
1176             data2 += t1->w;
1177             flagsp ++;
1178         }
1179         data1 += t1->w << 2;
1180         flagsp += 2U;
1181     }
1182     for (i = 0; i < t1->w; ++i) {
1183         OPJ_INT32 *data2 = data1 + i;
1184         for (j = k; j < t1->h; ++j) {
1185             vsc = (j == t1->h - 1) ? 1 : 0;
1186             opj_t1_dec_refpass_step_mqc_vsc(
1187                 t1,
1188                 flagsp,
1189                 data2,
1190                 poshalf,
1191                 vsc,
1192                 j - k);
1193             data2 += t1->w;
1194         }
1195         flagsp ++;
1196     }
1197 }
1198
1199 /**
1200 Encode clean-up pass step
1201 */
1202 static void opj_t1_enc_clnpass_step(
1203     opj_t1_t *t1,
1204     opj_flag_t *flagsp,
1205     OPJ_INT32 *datap,
1206     OPJ_INT32 bpno,
1207     OPJ_INT32 one,
1208     OPJ_INT32 *nmsedec,
1209     OPJ_UINT32 agg,
1210     OPJ_UINT32 runlen,
1211     OPJ_UINT32 lim,
1212     OPJ_UINT32 cblksty)
1213 {
1214     OPJ_UINT32 v;
1215     OPJ_UINT32 ci;
1216     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1217
1218     const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1219                               T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1220
1221     if ((*flagsp & check) == check) {
1222         if (runlen == 0) {
1223             *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1224         } else if (runlen == 1) {
1225             *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1226         } else if (runlen == 2) {
1227             *flagsp &= ~(T1_PI_2 | T1_PI_3);
1228         } else if (runlen == 3) {
1229             *flagsp &= ~(T1_PI_3);
1230         }
1231         return;
1232     }
1233
1234     for (ci = runlen; ci < lim; ++ci) {
1235         OPJ_UINT32 vsc;
1236         OPJ_UINT32 vsc_mask;
1237         opj_flag_t flags;
1238
1239         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1240         vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1241                            (ci * 3U)) : ~0U;
1242         flags = *flagsp & vsc_mask;
1243
1244         if ((agg != 0) && (ci == runlen)) {
1245             goto LABEL_PARTIAL;
1246         }
1247
1248         if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1249             OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1250 #ifdef DEBUG_ENC_CLN
1251             printf("   ctxt1=%d\n", ctxt1);
1252 #endif
1253             opj_mqc_setcurctx(mqc, ctxt1);
1254             v = opj_int_abs(*datap) & one ? 1 : 0;
1255             opj_mqc_encode(mqc, v);
1256             if (v) {
1257                 OPJ_UINT32 ctxt2, spb;
1258                 OPJ_UINT32 lu;
1259 LABEL_PARTIAL:
1260                 /* Note: using flags instead of *flagsp & vsc_mask result */
1261                 /* in slow down. Probably because of register pressure */
1262                 lu = opj_t1_getctxtno_sc_or_spb_index(
1263                          *flagsp & vsc_mask,
1264                          flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1265                          ci);
1266                 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1267                                                   (OPJ_UINT32)bpno);
1268                 ctxt2 = opj_t1_getctxno_sc(lu);
1269 #ifdef DEBUG_ENC_CLN
1270                 printf("   ctxt2=%d\n", ctxt2);
1271 #endif
1272                 opj_mqc_setcurctx(mqc, ctxt2);
1273
1274                 v = *datap < 0 ? 1 : 0;
1275                 spb = opj_t1_getspb(lu);
1276 #ifdef DEBUG_ENC_CLN
1277                 printf("   spb=%d\n", spb);
1278 #endif
1279                 opj_mqc_encode(mqc, v ^ spb);
1280                 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1281             }
1282         }
1283         *flagsp &= ~(T1_PI_THIS << (3U * ci));
1284         datap += t1->data_stride;
1285     }
1286 }
1287
1288 #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
1289                                       flags, flagsp, flags_stride, data, \
1290                                       data_stride, ci, mqc, curctx, \
1291                                       v, a, c, ct, oneplushalf) \
1292 { \
1293     if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
1294         do { \
1295             if( !partial ) { \
1296                 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
1297                 opj_t1_setcurctx(curctx, ctxt1); \
1298                 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1299                 if( !v ) \
1300                     break; \
1301             } \
1302             { \
1303                 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
1304                                     flags, flagsp[-1], flagsp[1], \
1305                                     ci); \
1306                 opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
1307                 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1308                 v = v ^ opj_t1_getspb(lu); \
1309                 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
1310                 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride); \
1311             } \
1312         } while(0); \
1313     } \
1314 }
1315
1316 static void opj_t1_dec_clnpass_step(
1317     opj_t1_t *t1,
1318     opj_flag_t *flagsp,
1319     OPJ_INT32 *datap,
1320     OPJ_INT32 oneplushalf,
1321     OPJ_UINT32 ci)
1322 {
1323     OPJ_UINT32 v;
1324
1325     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1326     opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
1327                                   *flagsp, flagsp, t1->w + 2U, datap,
1328                                   0, ci, mqc, mqc->curctx,
1329                                   v, mqc->a, mqc->c, mqc->ct, oneplushalf);
1330 }
1331
1332 static void opj_t1_dec_clnpass_step_vsc(
1333     opj_t1_t *t1,
1334     opj_flag_t *flagsp,
1335     OPJ_INT32 *datap,
1336     OPJ_INT32 oneplushalf,
1337     OPJ_INT32 partial,
1338     OPJ_INT32 vsc,
1339     OPJ_UINT32 ci)
1340 {
1341     OPJ_UINT32 v;
1342
1343     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1344
1345     OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1346                                    T1_CHI_S) <<
1347                                   (ci * 3U)) : ~0U;
1348     OPJ_UINT32 flags = *flagsp & vsc_mask;
1349     if (partial) {
1350         goto LABEL_PARTIAL;
1351     }
1352     if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1353         OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1354         opj_mqc_setcurctx(mqc, ctxt1);
1355         opj_mqc_decode(v, mqc);
1356         if (v) {
1357             OPJ_UINT32 lu;
1358 LABEL_PARTIAL:
1359             lu = opj_t1_getctxtno_sc_or_spb_index(
1360                      *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1361                      flagsp[1] & vsc_mask,
1362                      ci);
1363             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1364             opj_mqc_decode(v, mqc);
1365             v = v ^ opj_t1_getspb(lu);
1366             *datap = v ? -oneplushalf : oneplushalf;
1367             opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1368         }
1369     }
1370     /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1371 }
1372
1373 static void opj_t1_enc_clnpass(
1374     opj_t1_t *t1,
1375     OPJ_INT32 bpno,
1376     OPJ_INT32 *nmsedec,
1377     OPJ_UINT32 cblksty)
1378 {
1379     OPJ_UINT32 i, k;
1380     const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1381     OPJ_UINT32 agg, runlen;
1382
1383     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1384
1385     const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1386                                 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1387                                 ~0U;
1388
1389     *nmsedec = 0;
1390 #ifdef DEBUG_ENC_CLN
1391     printf("enc_clnpass: bpno=%d\n", bpno);
1392 #endif
1393     for (k = 0; k < (t1->h & ~3U); k += 4) {
1394 #ifdef DEBUG_ENC_CLN
1395         printf(" k=%d\n", k);
1396 #endif
1397         for (i = 0; i < t1->w; ++i) {
1398 #ifdef DEBUG_ENC_CLN
1399             printf("  i=%d\n", i);
1400 #endif
1401             agg = !(T1_FLAGS(i, k) & agg_mask);
1402 #ifdef DEBUG_ENC_CLN
1403             printf("   agg=%d\n", agg);
1404 #endif
1405             if (agg) {
1406                 for (runlen = 0; runlen < 4; ++runlen) {
1407                     if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1408                         break;
1409                     }
1410                 }
1411                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1412                 opj_mqc_encode(mqc, runlen != 4);
1413                 if (runlen == 4) {
1414                     continue;
1415                 }
1416                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1417                 opj_mqc_encode(mqc, runlen >> 1);
1418                 opj_mqc_encode(mqc, runlen & 1);
1419             } else {
1420                 runlen = 0;
1421             }
1422             opj_t1_enc_clnpass_step(
1423                 t1,
1424                 &T1_FLAGS(i, k),
1425                 &t1->data[((k + runlen) * t1->data_stride) + i],
1426                 bpno,
1427                 one,
1428                 nmsedec,
1429                 agg,
1430                 runlen,
1431                 4U,
1432                 cblksty);
1433         }
1434     }
1435     if (k < t1->h) {
1436         agg = 0;
1437         runlen = 0;
1438 #ifdef DEBUG_ENC_CLN
1439         printf(" k=%d\n", k);
1440 #endif
1441         for (i = 0; i < t1->w; ++i) {
1442 #ifdef DEBUG_ENC_CLN
1443             printf("  i=%d\n", i);
1444             printf("   agg=%d\n", agg);
1445 #endif
1446             opj_t1_enc_clnpass_step(
1447                 t1,
1448                 &T1_FLAGS(i, k),
1449                 &t1->data[((k + runlen) * t1->data_stride) + i],
1450                 bpno,
1451                 one,
1452                 nmsedec,
1453                 agg,
1454                 runlen,
1455                 t1->h - k,
1456                 cblksty);
1457         }
1458     }
1459 }
1460
1461 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1462 { \
1463     OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1464     OPJ_UINT32 runlen, v; \
1465     OPJ_UINT32 i, j, k; \
1466     OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1467     const OPJ_UINT32 l_w = w; \
1468     opj_mqc_t* mqc = &(t1->mqc); \
1469     one = 1 << bpno; \
1470     half = one >> 1; \
1471     oneplushalf = one | half; \
1472     if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1473     opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1474     const OPJ_UINT32 agg_mask =  ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1475     for (k = 0; k < h; k += 4) { \
1476         for (i = 0; i < w; ++i) { \
1477             opj_flag_t *flagsp2 = flagsp1 + i; \
1478             if (k + 3 < h) { \
1479                     agg = !(*flagsp2 & agg_mask); \
1480                 } else { \
1481                 agg = 0; \
1482             } \
1483             if (agg) { \
1484                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1485                 opj_mqc_decode(v, mqc); \
1486                 if (!v) { \
1487                     continue; \
1488                 } \
1489                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1490                 opj_mqc_decode(runlen, mqc); \
1491                 opj_mqc_decode(v, mqc); \
1492                 runlen = (runlen << 1) | v; \
1493             } else { \
1494                 runlen = 0; \
1495             } \
1496             for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1497                     vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1498                     opj_t1_dec_clnpass_step_vsc( \
1499                         t1, \
1500                         flagsp2, \
1501                         &t1->data[(j * w) + i], \
1502                         oneplushalf, \
1503                         agg && (j == k + runlen), \
1504                         vsc, j - k); \
1505             } \
1506             *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1507         } \
1508         flagsp1 += flags_stride; \
1509     } \
1510     } else { \
1511         DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1512         register OPJ_UINT32 v; \
1513         register OPJ_INT32 *data = t1->data; \
1514         register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1515         for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1516             for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1517                 opj_flag_t flags = *flagsp; \
1518                 if (flags == 0) { \
1519                     OPJ_UINT32 partial = OPJ_TRUE; \
1520                     opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
1521                     opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1522                     if (!v) { \
1523                         continue; \
1524                     } \
1525                     opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
1526                     opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
1527                     opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1528                     runlen = (runlen << 1) | v; \
1529                     switch(runlen) { \
1530                         case 0: \
1531                             opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
1532                                                 flags, flagsp, flags_stride, data, \
1533                                                 l_w, 0, mqc, curctx, \
1534                                                 v, a, c, ct, oneplushalf); \
1535                             partial = OPJ_FALSE; \
1536                             /* falltrough */ \
1537                         case 1: \
1538                             opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1539                                                 flags, flagsp, flags_stride, data, \
1540                                                 l_w, 1, mqc, curctx, \
1541                                                 v, a, c, ct, oneplushalf); \
1542                             partial = OPJ_FALSE; \
1543                             /* falltrough */ \
1544                         case 2: \
1545                             opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1546                                                 flags, flagsp, flags_stride, data, \
1547                                                 l_w, 2, mqc, curctx, \
1548                                                 v, a, c, ct, oneplushalf); \
1549                             partial = OPJ_FALSE; \
1550                             /* falltrough */ \
1551                         case 3: \
1552                             opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1553                                                 flags, flagsp, flags_stride, data, \
1554                                                 l_w, 3, mqc, curctx, \
1555                                                 v, a, c, ct, oneplushalf); \
1556                             break; \
1557                     } \
1558                 } else { \
1559                     opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1560                                         flags, flagsp, flags_stride, data, \
1561                                         l_w, 0, mqc, curctx, \
1562                                         v, a, c, ct, oneplushalf); \
1563                     opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1564                                         flags, flagsp, flags_stride, data, \
1565                                         l_w, 1, mqc, curctx, \
1566                                         v, a, c, ct, oneplushalf); \
1567                     opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1568                                         flags, flagsp, flags_stride, data, \
1569                                         l_w, 2, mqc, curctx, \
1570                                         v, a, c, ct, oneplushalf); \
1571                     opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1572                                         flags, flagsp, flags_stride, data, \
1573                                         l_w, 3, mqc, curctx, \
1574                                         v, a, c, ct, oneplushalf); \
1575                 } \
1576                 *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1577             } \
1578         } \
1579         UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1580         if( k < h ) { \
1581             for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
1582                 for (j = 0; j < h - k; ++j) { \
1583                     opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j); \
1584                 } \
1585                 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1586             } \
1587         } \
1588     } \
1589  \
1590     if (segsym) { \
1591         OPJ_UINT32 v, v2; \
1592         opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1593         opj_mqc_decode(v, mqc); \
1594         opj_mqc_decode(v2, mqc); \
1595         v = (v << 1) | v2; \
1596         opj_mqc_decode(v2, mqc); \
1597         v = (v << 1) | v2; \
1598         opj_mqc_decode(v2, mqc); \
1599         v = (v << 1) | v2; \
1600         /* \
1601         if (v!=0xa) { \
1602             opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1603         } \
1604         */ \
1605     } \
1606 }
1607
1608 static void opj_t1_dec_clnpass_64x64(
1609     opj_t1_t *t1,
1610     OPJ_INT32 bpno,
1611     OPJ_INT32 cblksty)
1612 {
1613     opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1614 }
1615
1616 static void opj_t1_dec_clnpass_generic(
1617     opj_t1_t *t1,
1618     OPJ_INT32 bpno,
1619     OPJ_INT32 cblksty)
1620 {
1621     opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1622                                 t1->w + 2U);
1623 }
1624
1625
1626 /** mod fixed_quality */
1627 static OPJ_FLOAT64 opj_t1_getwmsedec(
1628     OPJ_INT32 nmsedec,
1629     OPJ_UINT32 compno,
1630     OPJ_UINT32 level,
1631     OPJ_UINT32 orient,
1632     OPJ_INT32 bpno,
1633     OPJ_UINT32 qmfbid,
1634     OPJ_FLOAT64 stepsize,
1635     OPJ_UINT32 numcomps,
1636     const OPJ_FLOAT64 * mct_norms,
1637     OPJ_UINT32 mct_numcomps)
1638 {
1639     OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1640     OPJ_ARG_NOT_USED(numcomps);
1641
1642     if (mct_norms && (compno < mct_numcomps)) {
1643         w1 = mct_norms[compno];
1644     }
1645
1646     if (qmfbid == 1) {
1647         w2 = opj_dwt_getnorm(level, orient);
1648     } else {    /* if (qmfbid == 0) */
1649         w2 = opj_dwt_getnorm_real(level, orient);
1650     }
1651
1652     wmsedec = w1 * w2 * stepsize * (1 << bpno);
1653     wmsedec *= wmsedec * nmsedec / 8192.0;
1654
1655     return wmsedec;
1656 }
1657
1658 static OPJ_BOOL opj_t1_allocate_buffers(
1659     opj_t1_t *t1,
1660     OPJ_UINT32 w,
1661     OPJ_UINT32 h)
1662 {
1663     size_t flagssize;
1664     OPJ_UINT32 flags_stride;
1665
1666     /* encoder uses tile buffer, so no need to allocate */
1667     if (!t1->encoder) {
1668         size_t datasize;
1669
1670 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1671         /* Overflow check */
1672         if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1673             /* FIXME event manager error callback */
1674             return OPJ_FALSE;
1675         }
1676 #endif
1677         datasize = (size_t)w * h;
1678
1679         /* Overflow check */
1680         if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1681             /* FIXME event manager error callback */
1682             return OPJ_FALSE;
1683         }
1684
1685         if (datasize > (size_t)t1->datasize) {
1686             opj_aligned_free(t1->data);
1687             t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1688             if (!t1->data) {
1689                 /* FIXME event manager error callback */
1690                 return OPJ_FALSE;
1691             }
1692 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1693             /* TODO remove this if t1->datasize type changes to size_t */
1694             /* Overflow check */
1695             if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1696                 /* FIXME event manager error callback */
1697                 return OPJ_FALSE;
1698             }
1699 #endif
1700             t1->datasize = (OPJ_UINT32)datasize;
1701         }
1702         /* memset first arg is declared to never be null by gcc */
1703         if (t1->data != NULL) {
1704             memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1705         }
1706     }
1707
1708     /* Overflow check */
1709     if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1710         /* FIXME event manager error callback */
1711         return OPJ_FALSE;
1712     }
1713     flags_stride = w + 2U; /* can't be 0U */
1714
1715 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1716     /* Overflow check */
1717     if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1718         /* FIXME event manager error callback */
1719         return OPJ_FALSE;
1720     }
1721 #endif
1722     flagssize = (h + 3U) / 4U + 2U;
1723
1724     /* Overflow check */
1725     if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1726         /* FIXME event manager error callback */
1727         return OPJ_FALSE;
1728     }
1729     flagssize *= (size_t)flags_stride;
1730     {
1731         /* BIG FAT XXX */
1732         opj_flag_t* p;
1733         OPJ_UINT32 x;
1734         OPJ_UINT32 flags_height = (h + 3U) / 4U;
1735
1736         if (flagssize > (size_t)t1->flagssize) {
1737             /* Overflow check */
1738             if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1739                 /* FIXME event manager error callback */
1740                 return OPJ_FALSE;
1741             }
1742             opj_aligned_free(t1->flags);
1743             t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1744                             opj_flag_t));
1745             if (!t1->flags) {
1746                 /* FIXME event manager error callback */
1747                 return OPJ_FALSE;
1748             }
1749 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1750             /* TODO remove this if t1->flagssize type changes to size_t */
1751             /* Overflow check */
1752             if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1753                 /* FIXME event manager error callback */
1754                 return OPJ_FALSE;
1755             }
1756 #endif
1757         }
1758         t1->flagssize = (OPJ_UINT32)flagssize;
1759
1760         memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1761
1762         p = &t1->flags[0];
1763         for (x = 0; x < flags_stride; ++x) {
1764             /* magic value to hopefully stop any passes being interested in this entry */
1765             *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1766         }
1767
1768         p = &t1->flags[((flags_height + 1) * flags_stride)];
1769         for (x = 0; x < flags_stride; ++x) {
1770             /* magic value to hopefully stop any passes being interested in this entry */
1771             *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1772         }
1773
1774         if (h % 4) {
1775             OPJ_UINT32 v = 0;
1776             p = &t1->flags[((flags_height) * flags_stride)];
1777             if (h % 4 == 1) {
1778                 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1779             } else if (h % 4 == 2) {
1780                 v |= T1_PI_2 | T1_PI_3;
1781             } else if (h % 4 == 3) {
1782                 v |= T1_PI_3;
1783             }
1784             for (x = 0; x < flags_stride; ++x) {
1785                 *p++ = v;
1786             }
1787         }
1788     }
1789
1790     t1->w = w;
1791     t1->h = h;
1792
1793     return OPJ_TRUE;
1794 }
1795
1796 /* ----------------------------------------------------------------------- */
1797
1798 /* ----------------------------------------------------------------------- */
1799 /**
1800  * Creates a new Tier 1 handle
1801  * and initializes the look-up tables of the Tier-1 coder/decoder
1802  * @return a new T1 handle if successful, returns NULL otherwise
1803 */
1804 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1805 {
1806     opj_t1_t *l_t1 = 00;
1807
1808     l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1809     if (!l_t1) {
1810         return 00;
1811     }
1812
1813     l_t1->encoder = isEncoder;
1814
1815     return l_t1;
1816 }
1817
1818
1819 /**
1820  * Destroys a previously created T1 handle
1821  *
1822  * @param p_t1 Tier 1 handle to destroy
1823 */
1824 void opj_t1_destroy(opj_t1_t *p_t1)
1825 {
1826     if (! p_t1) {
1827         return;
1828     }
1829
1830     /* encoder uses tile buffer, so no need to free */
1831     if (!p_t1->encoder && p_t1->data) {
1832         opj_aligned_free(p_t1->data);
1833         p_t1->data = 00;
1834     }
1835
1836     if (p_t1->flags) {
1837         opj_aligned_free(p_t1->flags);
1838         p_t1->flags = 00;
1839     }
1840
1841     opj_free(p_t1);
1842 }
1843
1844 typedef struct {
1845     OPJ_UINT32 resno;
1846     opj_tcd_cblk_dec_t* cblk;
1847     opj_tcd_band_t* band;
1848     opj_tcd_tilecomp_t* tilec;
1849     opj_tccp_t* tccp;
1850     volatile OPJ_BOOL* pret;
1851 } opj_t1_cblk_decode_processing_job_t;
1852
1853 static void opj_t1_destroy_wrapper(void* t1)
1854 {
1855     opj_t1_destroy((opj_t1_t*) t1);
1856 }
1857
1858 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1859 {
1860     opj_tcd_cblk_dec_t* cblk;
1861     opj_tcd_band_t* band;
1862     opj_tcd_tilecomp_t* tilec;
1863     opj_tccp_t* tccp;
1864     OPJ_INT32* OPJ_RESTRICT datap;
1865     OPJ_UINT32 cblk_w, cblk_h;
1866     OPJ_INT32 x, y;
1867     OPJ_UINT32 i, j;
1868     opj_t1_cblk_decode_processing_job_t* job;
1869     opj_t1_t* t1;
1870     OPJ_UINT32 resno;
1871     OPJ_UINT32 tile_w;
1872
1873     job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1874     resno = job->resno;
1875     cblk = job->cblk;
1876     band = job->band;
1877     tilec = job->tilec;
1878     tccp = job->tccp;
1879     tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1880
1881     if (!*(job->pret)) {
1882         opj_free(job);
1883         return;
1884     }
1885
1886     t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1887     if (t1 == NULL) {
1888         t1 = opj_t1_create(OPJ_FALSE);
1889         opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1890     }
1891
1892     if (OPJ_FALSE == opj_t1_decode_cblk(
1893                 t1,
1894                 cblk,
1895                 band->bandno,
1896                 (OPJ_UINT32)tccp->roishift,
1897                 tccp->cblksty)) {
1898         *(job->pret) = OPJ_FALSE;
1899         opj_free(job);
1900         return;
1901     }
1902
1903     x = cblk->x0 - band->x0;
1904     y = cblk->y0 - band->y0;
1905     if (band->bandno & 1) {
1906         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1907         x += pres->x1 - pres->x0;
1908     }
1909     if (band->bandno & 2) {
1910         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1911         y += pres->y1 - pres->y0;
1912     }
1913
1914     datap = t1->data;
1915     cblk_w = t1->w;
1916     cblk_h = t1->h;
1917
1918     if (tccp->roishift) {
1919         OPJ_INT32 thresh = 1 << tccp->roishift;
1920         for (j = 0; j < cblk_h; ++j) {
1921             for (i = 0; i < cblk_w; ++i) {
1922                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1923                 OPJ_INT32 mag = abs(val);
1924                 if (mag >= thresh) {
1925                     mag >>= tccp->roishift;
1926                     datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1927                 }
1928             }
1929         }
1930     }
1931     if (tccp->qmfbid == 1) {
1932         OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1933                                                        (OPJ_UINT32)x];
1934         for (j = 0; j < cblk_h; ++j) {
1935             i = 0;
1936             for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1937                 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1938                 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1939                 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1940                 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1941                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1942                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1943                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1944                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1945             }
1946             for (; i < cblk_w; ++i) {
1947                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1948                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1949             }
1950         }
1951     } else {        /* if (tccp->qmfbid == 0) */
1952         OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1953                                                          tile_w + (OPJ_UINT32)x];
1954         for (j = 0; j < cblk_h; ++j) {
1955             OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1956             for (i = 0; i < cblk_w; ++i) {
1957                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1958                 *tiledp2 = tmp;
1959                 datap++;
1960                 tiledp2++;
1961             }
1962             tiledp += tile_w;
1963         }
1964     }
1965
1966     opj_free(job);
1967 }
1968
1969
1970 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1971                          volatile OPJ_BOOL* pret,
1972                          opj_tcd_tilecomp_t* tilec,
1973                          opj_tccp_t* tccp
1974                         )
1975 {
1976     OPJ_UINT32 resno, bandno, precno, cblkno;
1977
1978     for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1979         opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1980
1981         for (bandno = 0; bandno < res->numbands; ++bandno) {
1982             opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1983
1984             for (precno = 0; precno < res->pw * res->ph; ++precno) {
1985                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1986
1987                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1988                     opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1989                     opj_t1_cblk_decode_processing_job_t* job;
1990
1991                     job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1992                             sizeof(opj_t1_cblk_decode_processing_job_t));
1993                     if (!job) {
1994                         *pret = OPJ_FALSE;
1995                         return;
1996                     }
1997                     job->resno = resno;
1998                     job->cblk = cblk;
1999                     job->band = band;
2000                     job->tilec = tilec;
2001                     job->tccp = tccp;
2002                     job->pret = pret;
2003                     opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2004                     if (!(*pret)) {
2005                         return;
2006                     }
2007                 } /* cblkno */
2008             } /* precno */
2009         } /* bandno */
2010     } /* resno */
2011
2012     return;
2013 }
2014
2015
2016 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2017                                    opj_tcd_cblk_dec_t* cblk,
2018                                    OPJ_UINT32 orient,
2019                                    OPJ_UINT32 roishift,
2020                                    OPJ_UINT32 cblksty)
2021 {
2022     opj_raw_t *raw = &(t1->raw);   /* RAW component */
2023     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
2024
2025     OPJ_INT32 bpno_plus_one;
2026     OPJ_UINT32 passtype;
2027     OPJ_UINT32 segno, passno;
2028     OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2029
2030     mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2031
2032     if (!opj_t1_allocate_buffers(
2033                 t1,
2034                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2035                 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2036         return OPJ_FALSE;
2037     }
2038
2039     bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2040     passtype = 2;
2041
2042     opj_mqc_resetstates(mqc);
2043     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2044     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2045     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2046
2047     for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2048         opj_tcd_seg_t *seg = &cblk->segs[segno];
2049
2050         /* BYPASS mode */
2051         type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2052                 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2053         /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2054         if (seg->data == 00) {
2055             continue;
2056         }
2057         if (type == T1_TYPE_RAW) {
2058             opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2059         } else {
2060             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2061                                               seg->len)) {
2062                 return OPJ_FALSE;
2063             }
2064         }
2065
2066         if (t1->w == 64 && t1->h == 64) {
2067             for (passno = 0; (passno < seg->real_num_passes) &&
2068                     (bpno_plus_one >= 1); ++passno) {
2069                 switch (passtype) {
2070                 case 0:
2071                     if (type == T1_TYPE_RAW) {
2072                         opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2073                     } else {
2074                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2075                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2076                         } else {
2077                             opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2078                         }
2079                     }
2080                     break;
2081                 case 1:
2082                     if (type == T1_TYPE_RAW) {
2083                         opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2084                     } else {
2085                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2086                             opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2087                         } else {
2088                             opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2089                         }
2090                     }
2091                     break;
2092                 case 2:
2093                     opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2094                     break;
2095                 }
2096
2097                 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2098                     opj_mqc_resetstates(mqc);
2099                     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2100                     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2101                     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2102                 }
2103                 if (++passtype == 3) {
2104                     passtype = 0;
2105                     bpno_plus_one--;
2106                 }
2107             }
2108         } else {
2109             for (passno = 0; (passno < seg->real_num_passes) &&
2110                     (bpno_plus_one >= 1); ++passno) {
2111                 switch (passtype) {
2112                 case 0:
2113                     if (type == T1_TYPE_RAW) {
2114                         opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2115                     } else {
2116                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2117                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2118                         } else {
2119                             opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2120                         }
2121                     }
2122                     break;
2123                 case 1:
2124                     if (type == T1_TYPE_RAW) {
2125                         opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2126                     } else {
2127                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2128                             opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2129                         } else {
2130                             opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2131                         }
2132                     }
2133                     break;
2134                 case 2:
2135                     opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2136                     break;
2137                 }
2138
2139                 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2140                     opj_mqc_resetstates(mqc);
2141                     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2142                     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2143                     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2144                 }
2145                 if (++passtype == 3) {
2146                     passtype = 0;
2147                     bpno_plus_one--;
2148                 }
2149             }
2150         }
2151     }
2152     return OPJ_TRUE;
2153 }
2154
2155
2156
2157
2158 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2159                              opj_tcd_tile_t *tile,
2160                              opj_tcp_t *tcp,
2161                              const OPJ_FLOAT64 * mct_norms,
2162                              OPJ_UINT32 mct_numcomps
2163                             )
2164 {
2165     OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2166
2167     tile->distotile = 0;        /* fixed_quality */
2168
2169     for (compno = 0; compno < tile->numcomps; ++compno) {
2170         opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2171         opj_tccp_t* tccp = &tcp->tccps[compno];
2172         OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2173
2174         for (resno = 0; resno < tilec->numresolutions; ++resno) {
2175             opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2176
2177             for (bandno = 0; bandno < res->numbands; ++bandno) {
2178                 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2179                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2180
2181                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2182                     opj_tcd_precinct_t *prc = &band->precincts[precno];
2183
2184                     for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2185                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2186                         OPJ_INT32* OPJ_RESTRICT tiledp;
2187                         OPJ_UINT32 cblk_w;
2188                         OPJ_UINT32 cblk_h;
2189                         OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2190
2191                         OPJ_INT32 x = cblk->x0 - band->x0;
2192                         OPJ_INT32 y = cblk->y0 - band->y0;
2193                         if (band->bandno & 1) {
2194                             opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2195                             x += pres->x1 - pres->x0;
2196                         }
2197                         if (band->bandno & 2) {
2198                             opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2199                             y += pres->y1 - pres->y0;
2200                         }
2201
2202                         if (!opj_t1_allocate_buffers(
2203                                     t1,
2204                                     (OPJ_UINT32)(cblk->x1 - cblk->x0),
2205                                     (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2206                             return OPJ_FALSE;
2207                         }
2208
2209                         cblk_w = t1->w;
2210                         cblk_h = t1->h;
2211                         tileLineAdvance = tile_w - cblk_w;
2212
2213                         tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2214                         t1->data = tiledp;
2215                         t1->data_stride = tile_w;
2216                         if (tccp->qmfbid == 1) {
2217                             for (j = 0; j < cblk_h; ++j) {
2218                                 for (i = 0; i < cblk_w; ++i) {
2219                                     tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2220                                     tileIndex++;
2221                                 }
2222                                 tileIndex += tileLineAdvance;
2223                             }
2224                         } else {        /* if (tccp->qmfbid == 0) */
2225                             for (j = 0; j < cblk_h; ++j) {
2226                                 for (i = 0; i < cblk_w; ++i) {
2227                                     OPJ_INT32 tmp = tiledp[tileIndex];
2228                                     tiledp[tileIndex] =
2229                                         opj_int_fix_mul_t1(
2230                                             tmp,
2231                                             bandconst);
2232                                     tileIndex++;
2233                                 }
2234                                 tileIndex += tileLineAdvance;
2235                             }
2236                         }
2237
2238                         opj_t1_encode_cblk(
2239                             t1,
2240                             cblk,
2241                             band->bandno,
2242                             compno,
2243                             tilec->numresolutions - 1 - resno,
2244                             tccp->qmfbid,
2245                             band->stepsize,
2246                             tccp->cblksty,
2247                             tile->numcomps,
2248                             tile,
2249                             mct_norms,
2250                             mct_numcomps);
2251
2252                     } /* cblkno */
2253                 } /* precno */
2254             } /* bandno */
2255         } /* resno  */
2256     } /* compno  */
2257     return OPJ_TRUE;
2258 }
2259
2260 /** mod fixed_quality */
2261 static void opj_t1_encode_cblk(opj_t1_t *t1,
2262                                opj_tcd_cblk_enc_t* cblk,
2263                                OPJ_UINT32 orient,
2264                                OPJ_UINT32 compno,
2265                                OPJ_UINT32 level,
2266                                OPJ_UINT32 qmfbid,
2267                                OPJ_FLOAT64 stepsize,
2268                                OPJ_UINT32 cblksty,
2269                                OPJ_UINT32 numcomps,
2270                                opj_tcd_tile_t * tile,
2271                                const OPJ_FLOAT64 * mct_norms,
2272                                OPJ_UINT32 mct_numcomps)
2273 {
2274     OPJ_FLOAT64 cumwmsedec = 0.0;
2275
2276     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
2277
2278     OPJ_UINT32 passno;
2279     OPJ_INT32 bpno;
2280     OPJ_UINT32 passtype;
2281     OPJ_INT32 nmsedec = 0;
2282     OPJ_INT32 max;
2283     OPJ_UINT32 i, j;
2284     OPJ_BYTE type = T1_TYPE_MQ;
2285     OPJ_FLOAT64 tempwmsedec;
2286
2287     mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2288
2289     max = 0;
2290     for (i = 0; i < t1->w; ++i) {
2291         for (j = 0; j < t1->h; ++j) {
2292             OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2293             max = opj_int_max(max, tmp);
2294         }
2295     }
2296
2297     cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2298                                       T1_NMSEDEC_FRACBITS) : 0;
2299
2300     bpno = (OPJ_INT32)(cblk->numbps - 1);
2301     passtype = 2;
2302
2303     opj_mqc_resetstates(mqc);
2304     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2305     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2306     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2307     opj_mqc_init_enc(mqc, cblk->data);
2308
2309     for (passno = 0; bpno >= 0; ++passno) {
2310         opj_tcd_pass_t *pass = &cblk->passes[passno];
2311         OPJ_UINT32 correction = 3;
2312         type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2313                 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2314
2315         switch (passtype) {
2316         case 0:
2317             opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2318             break;
2319         case 1:
2320             opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2321             break;
2322         case 2:
2323             opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2324             /* code switch SEGMARK (i.e. SEGSYM) */
2325             if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2326                 opj_mqc_segmark_enc(mqc);
2327             }
2328             break;
2329         }
2330
2331         /* fixed_quality */
2332         tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2333                                         stepsize, numcomps, mct_norms, mct_numcomps) ;
2334         cumwmsedec += tempwmsedec;
2335         tile->distotile += tempwmsedec;
2336
2337         /* Code switch "RESTART" (i.e. TERMALL) */
2338         if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2339                 (bpno - 1 < 0))) {
2340             if (type == T1_TYPE_RAW) {
2341                 opj_mqc_flush(mqc);
2342                 correction = 1;
2343                 /* correction = mqc_bypass_flush_enc(); */
2344             } else {            /* correction = mqc_restart_enc(); */
2345                 opj_mqc_flush(mqc);
2346                 correction = 1;
2347             }
2348             pass->term = 1;
2349         } else {
2350             if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2351                     || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2352                     (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2353                 if (type == T1_TYPE_RAW) {
2354                     opj_mqc_flush(mqc);
2355                     correction = 1;
2356                     /* correction = mqc_bypass_flush_enc(); */
2357                 } else {        /* correction = mqc_restart_enc(); */
2358                     opj_mqc_flush(mqc);
2359                     correction = 1;
2360                 }
2361                 pass->term = 1;
2362             } else {
2363                 pass->term = 0;
2364             }
2365         }
2366
2367         if (++passtype == 3) {
2368             passtype = 0;
2369             bpno--;
2370         }
2371
2372         if (pass->term && bpno > 0) {
2373             type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2374                     (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2375             if (type == T1_TYPE_RAW) {
2376                 opj_mqc_bypass_init_enc(mqc);
2377             } else {
2378                 opj_mqc_restart_init_enc(mqc);
2379             }
2380         }
2381
2382         pass->distortiondec = cumwmsedec;
2383         pass->rate = opj_mqc_numbytes(mqc) + correction;    /* FIXME */
2384
2385         /* Code-switch "RESET" */
2386         if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2387             opj_mqc_reset_enc(mqc);
2388         }
2389     }
2390
2391     /* Code switch "ERTERM" (i.e. PTERM) */
2392     if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2393         opj_mqc_erterm_enc(mqc);
2394     } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2395         opj_mqc_flush(mqc);
2396     }
2397
2398     cblk->totalpasses = passno;
2399
2400     for (passno = 0; passno < cblk->totalpasses; passno++) {
2401         opj_tcd_pass_t *pass = &cblk->passes[passno];
2402         if (pass->rate > opj_mqc_numbytes(mqc)) {
2403             pass->rate = opj_mqc_numbytes(mqc);
2404         }
2405         /*Preventing generation of FF as last data byte of a pass*/
2406         if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2407             pass->rate--;
2408         }
2409         pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2410     }
2411 }