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