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