b86ecf239fa682296993a5a07162a9ddb4dc468c
[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 /** @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->w + 2);
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->w + 2);
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->w + 2);
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 = 2;
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;
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     if ((cblksty & J2K_CCP_CBLKSTY_VSC)) {
640         OPJ_INT32 *data1 = t1->data;
641         for (k = 0; k < t1->h; k += 4) {
642             for (i = 0; i < t1->w; ++i) {
643                 OPJ_INT32* data2 = data1 + i;
644                 for (j = k; j < k + 4 && j < t1->h; ++j) {
645                     OPJ_INT32 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
646                     opj_t1_dec_sigpass_step_raw(
647                         t1,
648                         flagsp,
649                         data2,
650                         oneplushalf,
651                         vsc,
652                         j - k);
653                     data2 += t1->w;
654                 }
655                 flagsp ++;
656             }
657             data1 += t1->w << 2;
658             flagsp += 2;
659         }
660     } else {
661         OPJ_INT32 *data1 = t1->data;
662         for (k = 0; k < (t1->h & ~3U); k += 4) {
663             for (i = 0; i < t1->w; ++i) {
664                 OPJ_INT32* data2 = data1 + i;
665                 opj_t1_dec_sigpass_step_raw(
666                     t1,
667                     flagsp,
668                     data2,
669                     oneplushalf,
670                     0, /* vsc */
671                     0U);
672                 data2 += t1->w;
673                 opj_t1_dec_sigpass_step_raw(
674                     t1,
675                     flagsp,
676                     data2,
677                     oneplushalf,
678                     0, /* vsc */
679                     1U);
680                 data2 += t1->w;
681                 opj_t1_dec_sigpass_step_raw(
682                     t1,
683                     flagsp,
684                     data2,
685                     oneplushalf,
686                     0, /* vsc */
687                     2U);
688                 data2 += t1->w;
689                 opj_t1_dec_sigpass_step_raw(
690                     t1,
691                     flagsp,
692                     data2,
693                     oneplushalf,
694                     0, /* vsc */
695                     3U);
696                 data2 += t1->w;
697                 flagsp ++;
698             }
699             data1 += t1->w << 2;
700             flagsp += 2;
701         }
702         if (k < t1->h) {
703             for (i = 0; i < t1->w; ++i) {
704                 OPJ_INT32* data2 = data1 + i;
705                 for (j = 0; j < t1->h - k; ++j) {
706                     opj_t1_dec_sigpass_step_raw(
707                         t1,
708                         flagsp,
709                         data2,
710                         oneplushalf,
711                         0, /* vsc */
712                         j);
713                     data2 += t1->w;
714                 }
715                 flagsp ++;
716             }
717         }
718     }
719 }
720
721 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
722 { \
723         OPJ_INT32 one, half, oneplushalf; \
724         OPJ_UINT32 i, j, k; \
725         OPJ_INT32 *data1 = t1->data; \
726         opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
727         one = 1 << bpno; \
728         half = one >> 1; \
729         oneplushalf = one | half; \
730         for (k = 0; k < (h & ~3u); k += 4) { \
731                 for (i = 0; i < w; ++i) { \
732                         OPJ_INT32 *data2 = data1 + i; \
733                         if( *flagsp != 0 ) { \
734                             opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 0U, flags_stride); \
735                             data2 += w; \
736                             opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 1U, flags_stride); \
737                             data2 += w; \
738                             opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 2U, flags_stride); \
739                             data2 += w; \
740                             opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 3U, flags_stride); \
741                             data2 += w; \
742                         } \
743                         flagsp ++; \
744                 } \
745                 data1 += w << 2; \
746                 flagsp += 2; \
747         } \
748         for (i = 0; i < w; ++i) { \
749                 OPJ_INT32 *data2 = data1 + i; \
750                 for (j = k; j < h; ++j) { \
751                         opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, j - k, flags_stride); \
752                         data2 += w; \
753                 } \
754                 flagsp ++; \
755         } \
756 }
757
758 static void opj_t1_dec_sigpass_mqc_64x64(
759     opj_t1_t *t1,
760     OPJ_INT32 bpno)
761 {
762     opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
763 }
764
765 static void opj_t1_dec_sigpass_mqc_generic(
766     opj_t1_t *t1,
767     OPJ_INT32 bpno)
768 {
769     opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2);
770 }
771
772 static void opj_t1_dec_sigpass_mqc_vsc(
773     opj_t1_t *t1,
774     OPJ_INT32 bpno)
775 {
776     OPJ_INT32 one, half, oneplushalf, vsc;
777     OPJ_UINT32 i, j, k;
778     OPJ_INT32 *data1 = t1->data;
779     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
780     one = 1 << bpno;
781     half = one >> 1;
782     oneplushalf = one | half;
783     for (k = 0; k < (t1->h & ~3U); k += 4U) {
784         for (i = 0; i < t1->w; ++i) {
785             OPJ_INT32 *data2 = data1 + i;
786             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 0U);
787             data2 += t1->w;
788             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 1U);
789             data2 += t1->w;
790             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 2U);
791             data2 += t1->w;
792             opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 1, 3U);
793             data2 += t1->w;
794             flagsp ++;
795         }
796         data1 += t1->w << 2;
797         flagsp += 2;
798     }
799     for (i = 0; i < t1->w; ++i) {
800         OPJ_INT32 *data2 = data1 + i;
801         for (j = k; j < t1->h; ++j) {
802             vsc = (j == t1->h - 1) ? 1 : 0;
803             opj_t1_dec_sigpass_step_mqc_vsc(
804                 t1,
805                 flagsp,
806                 data2,
807                 oneplushalf,
808                 vsc,
809                 j - k);
810             data2 += t1->w;
811         }
812         flagsp ++;
813     }
814 }
815
816
817 /**
818 Encode refinement pass step
819 */
820 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
821         opj_flag_t *flagsp,
822         OPJ_INT32 *datap,
823         OPJ_INT32 bpno,
824         OPJ_INT32 one,
825         OPJ_INT32 *nmsedec,
826         OPJ_BYTE type,
827         OPJ_UINT32 ci,
828         OPJ_UINT32 vsc)
829 {
830     OPJ_UINT32 v;
831
832     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
833
834     OPJ_UINT32 const shift_flags =
835         vsc ?
836         ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
837         :
838         (*flagsp >> (ci * 3U));
839
840     if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
841         OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
842         *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
843                                           (OPJ_UINT32)bpno);
844         v = opj_int_abs(*datap) & one ? 1 : 0;
845 #ifdef DEBUG_ENC_REF
846         fprintf(stderr, "  ctxt=%d\n", ctxt);
847 #endif
848         opj_mqc_setcurctx(mqc, ctxt);
849         if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
850             opj_mqc_bypass_enc(mqc, v);
851         } else {
852             opj_mqc_encode(mqc, v);
853         }
854         *flagsp |= T1_MU_THIS << (ci * 3U);
855     }
856 }
857
858
859 static INLINE void opj_t1_dec_refpass_step_raw(
860     opj_t1_t *t1,
861     opj_flag_t *flagsp,
862     OPJ_INT32 *datap,
863     OPJ_INT32 poshalf,
864     OPJ_INT32 neghalf,
865     OPJ_UINT32 ci)
866 {
867     OPJ_UINT32 v;
868     OPJ_INT32 t;
869
870     opj_raw_t *raw = &(t1->raw);       /* RAW component */
871
872     if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
873             (T1_SIGMA_THIS << (ci * 3U))) {
874         v = opj_raw_decode(raw);
875         t = v ? poshalf : neghalf;
876         *datap += *datap < 0 ? -t : t;
877         *flagsp |= T1_MU_THIS << (ci * 3U);
878     }
879 }
880
881 static INLINE void opj_t1_dec_refpass_step_mqc(
882     opj_t1_t *t1,
883     opj_flag_t *flagsp,
884     OPJ_INT32 *datap,
885     OPJ_INT32 poshalf,
886     OPJ_INT32 neghalf,
887     OPJ_UINT32 ci)
888 {
889     OPJ_UINT32 v;
890     OPJ_INT32 t;
891
892     opj_mqc_t *mqc = &(t1->mqc);       /* MQC component */
893
894     if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
895             (T1_SIGMA_THIS << (ci * 3U))) {
896         OPJ_UINT32 ctxt = opj_t1_getctxno_mag(*flagsp >> (ci * 3U));
897         opj_mqc_setcurctx(mqc, ctxt);
898         v = opj_mqc_decode(mqc);
899         t = v ? poshalf : neghalf;
900         *datap += *datap < 0 ? -t : t;
901         *flagsp |= T1_MU_THIS << (ci * 3U);
902     }
903 }
904
905 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
906     opj_t1_t *t1,
907     opj_flag_t *flagsp,
908     OPJ_INT32 *datap,
909     OPJ_INT32 poshalf,
910     OPJ_INT32 neghalf,
911     OPJ_INT32 vsc,
912     OPJ_UINT32 ci)
913 {
914     OPJ_UINT32 v;
915     OPJ_INT32 t;
916
917     opj_mqc_t *mqc = &(t1->mqc);       /* MQC component */
918
919     OPJ_UINT32 const shift_flags =
920         vsc ?
921         ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
922         :
923         (*flagsp >> (ci * 3U));
924
925
926     if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
927         OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
928         opj_mqc_setcurctx(mqc, ctxt);
929         v = opj_mqc_decode(mqc);
930         t = v ? poshalf : neghalf;
931         *datap += *datap < 0 ? -t : t;
932         *flagsp |= T1_MU_THIS << (ci * 3U);
933     }
934 }
935
936 static void opj_t1_enc_refpass(
937     opj_t1_t *t1,
938     OPJ_INT32 bpno,
939     OPJ_INT32 *nmsedec,
940     OPJ_BYTE type,
941     OPJ_UINT32 cblksty)
942 {
943     OPJ_UINT32 i, k;
944     const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
945     opj_flag_t* f = &T1_FLAGS(0, 0);
946     const OPJ_UINT32 extra = 2U;
947
948     *nmsedec = 0;
949 #ifdef DEBUG_ENC_REF
950     fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
951 #endif
952     for (k = 0; k < (t1->h & ~3U); k += 4) {
953 #ifdef DEBUG_ENC_REF
954         fprintf(stderr, " k=%d\n", k);
955 #endif
956         for (i = 0; i < t1->w; ++i) {
957 #ifdef DEBUG_ENC_REF
958             fprintf(stderr, " i=%d\n", i);
959 #endif
960             if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
961                 /* none significant */
962                 f++;
963                 continue;
964             }
965             if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
966                     (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
967                 /* all processed by sigpass */
968                 f++;
969                 continue;
970             }
971
972             opj_t1_enc_refpass_step(
973                 t1,
974                 f,
975                 &t1->data[((k + 0) * t1->data_stride) + i],
976                 bpno,
977                 one,
978                 nmsedec,
979                 type,
980                 0, 0);
981             opj_t1_enc_refpass_step(
982                 t1,
983                 f,
984                 &t1->data[((k + 1) * t1->data_stride) + i],
985                 bpno,
986                 one,
987                 nmsedec,
988                 type,
989                 1, 0);
990             opj_t1_enc_refpass_step(
991                 t1,
992                 f,
993                 &t1->data[((k + 2) * t1->data_stride) + i],
994                 bpno,
995                 one,
996                 nmsedec,
997                 type,
998                 2, 0);
999             opj_t1_enc_refpass_step(
1000                 t1,
1001                 f,
1002                 &t1->data[((k + 3) * t1->data_stride) + i],
1003                 bpno,
1004                 one,
1005                 nmsedec,
1006                 type,
1007                 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1008             ++f;
1009         }
1010         f += extra;
1011     }
1012
1013     if (k < t1->h) {
1014         OPJ_UINT32 j;
1015 #ifdef DEBUG_ENC_REF
1016         fprintf(stderr, " k=%d\n", k);
1017 #endif
1018         for (i = 0; i < t1->w; ++i) {
1019 #ifdef DEBUG_ENC_REF
1020             fprintf(stderr, " i=%d\n", i);
1021 #endif
1022             if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1023                 /* none significant */
1024                 f++;
1025                 continue;
1026             }
1027             for (j = k; j < t1->h; ++j) {
1028                 opj_t1_enc_refpass_step(
1029                     t1,
1030                     f,
1031                     &t1->data[(j * t1->data_stride) + i],
1032                     bpno,
1033                     one,
1034                     nmsedec,
1035                     type,
1036                     j - k,
1037                     (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1038             }
1039             ++f;
1040         }
1041     }
1042 }
1043
1044
1045 static void opj_t1_dec_refpass_raw(
1046     opj_t1_t *t1,
1047     OPJ_INT32 bpno)
1048 {
1049     OPJ_INT32 one, poshalf, neghalf;
1050     OPJ_UINT32 i, j, k;
1051     OPJ_INT32 *data1 = t1->data;
1052     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1053     one = 1 << bpno;
1054     poshalf = one >> 1;
1055     neghalf = bpno > 0 ? -poshalf : -1;
1056     for (k = 0; k < (t1->h & ~3U); k += 4) {
1057         for (i = 0; i < t1->w; ++i) {
1058             OPJ_INT32 *data2 = data1 + i;
1059             opj_t1_dec_refpass_step_raw(
1060                 t1,
1061                 flagsp,
1062                 data2,
1063                 poshalf,
1064                 neghalf, 0U);
1065             data2 += t1->w;
1066             opj_t1_dec_refpass_step_raw(
1067                 t1,
1068                 flagsp,
1069                 data2,
1070                 poshalf,
1071                 neghalf, 1U);
1072             data2 += t1->w;
1073             opj_t1_dec_refpass_step_raw(
1074                 t1,
1075                 flagsp,
1076                 data2,
1077                 poshalf,
1078                 neghalf, 2U);
1079             data2 += t1->w;
1080             opj_t1_dec_refpass_step_raw(
1081                 t1,
1082                 flagsp,
1083                 data2,
1084                 poshalf,
1085                 neghalf, 3U);
1086             data2 += t1->w;
1087             flagsp ++;
1088         }
1089         data1 += t1->w << 2;
1090         flagsp += 2;
1091     }
1092     if (k < t1->h) {
1093         for (i = 0; i < t1->w; ++i) {
1094             OPJ_INT32 *data2 = data1 + i;
1095             for (j = k; j < t1->h; ++j) {
1096                 opj_t1_dec_refpass_step_raw(
1097                     t1,
1098                     flagsp,
1099                     data2,
1100                     poshalf,
1101                     neghalf, j - k);
1102                 data2 += t1->w;
1103             }
1104             flagsp ++;
1105         }
1106     }
1107 }
1108
1109 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1110 { \
1111         OPJ_INT32 one, poshalf, neghalf; \
1112         OPJ_UINT32 i, j, k; \
1113         OPJ_INT32 *data1 = t1->data; \
1114         opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1115         one = 1 << bpno; \
1116         poshalf = one >> 1; \
1117         neghalf = bpno > 0 ? -poshalf : -1; \
1118         for (k = 0; k < (h & ~3u); k += 4) { \
1119                 for (i = 0; i < w; ++i) { \
1120                         OPJ_INT32 *data2 = data1 + i; \
1121                         if( *flagsp != 0 ) { \
1122                             opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 0U); \
1123                             data2 += w; \
1124                             opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 1U); \
1125                             data2 += w; \
1126                             opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 2U); \
1127                             data2 += w; \
1128                             opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 3U); \
1129                             data2 += w; \
1130                         } \
1131                         flagsp ++; \
1132                 } \
1133                 data1 += w << 2; \
1134                 flagsp += 2; \
1135         } \
1136         for (i = 0; i < w; ++i) { \
1137                 OPJ_INT32 *data2 = data1 + i; \
1138                 for (j = k; j < h; ++j) { \
1139                         opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, j - k); \
1140                         data2 += w; \
1141                 } \
1142                 flagsp ++; \
1143         } \
1144 }
1145
1146 static void opj_t1_dec_refpass_mqc_64x64(
1147     opj_t1_t *t1,
1148     OPJ_INT32 bpno)
1149 {
1150     opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1151 }
1152
1153 static void opj_t1_dec_refpass_mqc_generic(
1154     opj_t1_t *t1,
1155     OPJ_INT32 bpno)
1156 {
1157     opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1158 }
1159
1160 static void opj_t1_dec_refpass_mqc_vsc(
1161     opj_t1_t *t1,
1162     OPJ_INT32 bpno)
1163 {
1164     OPJ_INT32 one, poshalf, neghalf;
1165     OPJ_UINT32 i, j, k;
1166     OPJ_INT32 vsc;
1167     OPJ_INT32 *data1 = t1->data;
1168     opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1169     one = 1 << bpno;
1170     poshalf = one >> 1;
1171     neghalf = bpno > 0 ? -poshalf : -1;
1172     for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1173         for (i = 0; i < t1->w; ++i) {
1174             OPJ_INT32 *data2 = data1 + i;
1175             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1176                                             0, 0U);
1177             data2 += t1->w;
1178             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1179                                             0, 1U);
1180             data2 += t1->w;
1181             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1182                                             0, 2U);
1183             data2 += t1->w;
1184             opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1185                                             1, 3U);
1186             data2 += t1->w;
1187             flagsp ++;
1188         }
1189         data1 += t1->w << 2;
1190         flagsp += 2U;
1191     }
1192     for (i = 0; i < t1->w; ++i) {
1193         OPJ_INT32 *data2 = data1 + i;
1194         for (j = k; j < t1->h; ++j) {
1195             vsc = (j == t1->h - 1) ? 1 : 0;
1196             opj_t1_dec_refpass_step_mqc_vsc(
1197                 t1,
1198                 flagsp,
1199                 data2,
1200                 poshalf, neghalf,
1201                 vsc,
1202                 j - k);
1203             data2 += t1->w;
1204         }
1205         flagsp ++;
1206     }
1207 }
1208
1209 /**
1210 Encode clean-up pass step
1211 */
1212 static void opj_t1_enc_clnpass_step(
1213     opj_t1_t *t1,
1214     opj_flag_t *flagsp,
1215     OPJ_INT32 *datap,
1216     OPJ_INT32 bpno,
1217     OPJ_INT32 one,
1218     OPJ_INT32 *nmsedec,
1219     OPJ_UINT32 agg,
1220     OPJ_UINT32 runlen,
1221     OPJ_UINT32 lim,
1222     OPJ_UINT32 cblksty)
1223 {
1224     OPJ_UINT32 v;
1225     OPJ_UINT32 ci;
1226     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1227
1228     const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1229                               T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1230
1231     if ((*flagsp & check) == check) {
1232         if (runlen == 0) {
1233             *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1234         } else if (runlen == 1) {
1235             *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1236         } else if (runlen == 2) {
1237             *flagsp &= ~(T1_PI_2 | T1_PI_3);
1238         } else if (runlen == 3) {
1239             *flagsp &= ~(T1_PI_3);
1240         }
1241         return;
1242     }
1243
1244     for (ci = runlen; ci < lim; ++ci) {
1245         OPJ_UINT32 vsc;
1246         OPJ_UINT32 vsc_mask;
1247         opj_flag_t flags;
1248
1249         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1250         vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1251                            (ci * 3U)) : ~0U;
1252         flags = *flagsp & vsc_mask;
1253
1254         if ((agg != 0) && (ci == runlen)) {
1255             goto LABEL_PARTIAL;
1256         }
1257
1258         if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1259             OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1260 #ifdef DEBUG_ENC_CLN
1261             printf("   ctxt1=%d\n", ctxt1);
1262 #endif
1263             opj_mqc_setcurctx(mqc, ctxt1);
1264             v = opj_int_abs(*datap) & one ? 1 : 0;
1265             opj_mqc_encode(mqc, v);
1266             if (v) {
1267                 OPJ_UINT32 ctxt2, spb;
1268                 OPJ_UINT32 lu;
1269 LABEL_PARTIAL:
1270                 /* Note: using flags instead of *flagsp & vsc_mask result */
1271                 /* in slow down. Probably because of register pressure */
1272                 lu = opj_t1_getctxtno_sc_or_spb_index(
1273                          *flagsp & vsc_mask,
1274                          flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1275                          ci);
1276                 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1277                                                   (OPJ_UINT32)bpno);
1278                 ctxt2 = opj_t1_getctxno_sc(lu);
1279 #ifdef DEBUG_ENC_CLN
1280                 printf("   ctxt2=%d\n", ctxt2);
1281 #endif
1282                 opj_mqc_setcurctx(mqc, ctxt2);
1283
1284                 v = *datap < 0 ? 1 : 0;
1285                 spb = opj_t1_getspb(lu);
1286 #ifdef DEBUG_ENC_CLN
1287                 printf("   spb=%d\n", spb);
1288 #endif
1289                 opj_mqc_encode(mqc, v ^ spb);
1290                 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1291             }
1292         }
1293         *flagsp &= ~(T1_PI_THIS << (3U * ci));
1294         datap += t1->data_stride;
1295     }
1296 }
1297
1298
1299 static void opj_t1_dec_clnpass_step_partial(
1300     opj_t1_t *t1,
1301     opj_flag_t *flagsp,
1302     OPJ_INT32 *datap,
1303     OPJ_INT32 oneplushalf,
1304     OPJ_UINT32 ci)
1305 {
1306     OPJ_UINT32 v;
1307     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1308
1309     OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1310                         *flagsp, flagsp[-1], flagsp[1],
1311                         ci);
1312     opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1313     v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1314     *datap = v ? -oneplushalf : oneplushalf;
1315     opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1316     /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1317 }
1318
1319 static void opj_t1_dec_clnpass_step(
1320     opj_t1_t *t1,
1321     opj_flag_t *flagsp,
1322     OPJ_INT32 *datap,
1323     OPJ_INT32 oneplushalf,
1324     OPJ_UINT32 ci)
1325 {
1326     OPJ_UINT32 v;
1327
1328     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1329     if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1330         OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1331         opj_mqc_setcurctx(mqc, ctxt1);
1332         if (opj_mqc_decode(mqc)) {
1333             OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1334                                 *flagsp, flagsp[-1], flagsp[1],
1335                                 ci);
1336             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1337             v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1338             *datap = v ? -oneplushalf : oneplushalf;
1339             opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1340         }
1341     }
1342     /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1343 }
1344
1345 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1346     opj_t1_t *t1,
1347     opj_flag_t *flagsp,
1348     OPJ_INT32 *datap,
1349     OPJ_INT32 oneplushalf,
1350     OPJ_UINT32 ci,
1351     OPJ_UINT32 flags_stride)
1352 {
1353     OPJ_UINT32 v;
1354
1355     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1356
1357     OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1358     opj_mqc_setcurctx(mqc, ctxt1);
1359     if (opj_mqc_decode(mqc)) {
1360         OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1361                             *flagsp, flagsp[-1], flagsp[1],
1362                             ci);
1363         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1364         v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1365         *datap = v ? -oneplushalf : oneplushalf;
1366         opj_t1_update_flags(flagsp, ci, v, flags_stride);
1367     }
1368 }
1369
1370 static void opj_t1_dec_clnpass_step_vsc(
1371     opj_t1_t *t1,
1372     opj_flag_t *flagsp,
1373     OPJ_INT32 *datap,
1374     OPJ_INT32 oneplushalf,
1375     OPJ_INT32 partial,
1376     OPJ_INT32 vsc,
1377     OPJ_UINT32 ci)
1378 {
1379     OPJ_UINT32 v;
1380
1381     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1382
1383     OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1384                                    T1_CHI_S) <<
1385                                   (ci * 3U)) : ~0U;
1386     OPJ_UINT32 flags = *flagsp & vsc_mask;
1387     if (partial) {
1388         goto LABEL_PARTIAL;
1389     }
1390     if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1391         OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1392         opj_mqc_setcurctx(mqc, ctxt1);
1393         if (opj_mqc_decode(mqc)) {
1394             OPJ_UINT32 lu;
1395 LABEL_PARTIAL:
1396             lu = opj_t1_getctxtno_sc_or_spb_index(
1397                      *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1398                      flagsp[1] & vsc_mask,
1399                      ci);
1400             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1401             v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1402             *datap = v ? -oneplushalf : oneplushalf;
1403             opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1404         }
1405     }
1406     /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1407 }
1408
1409 static void opj_t1_enc_clnpass(
1410     opj_t1_t *t1,
1411     OPJ_INT32 bpno,
1412     OPJ_INT32 *nmsedec,
1413     OPJ_UINT32 cblksty)
1414 {
1415     OPJ_UINT32 i, k;
1416     const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1417     OPJ_UINT32 agg, runlen;
1418
1419     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
1420
1421     const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1422                                 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1423                                 ~0U;
1424
1425     *nmsedec = 0;
1426 #ifdef DEBUG_ENC_CLN
1427     printf("enc_clnpass: bpno=%d\n", bpno);
1428 #endif
1429     for (k = 0; k < (t1->h & ~3U); k += 4) {
1430 #ifdef DEBUG_ENC_CLN
1431         printf(" k=%d\n", k);
1432 #endif
1433         for (i = 0; i < t1->w; ++i) {
1434 #ifdef DEBUG_ENC_CLN
1435             printf("  i=%d\n", i);
1436 #endif
1437             agg = !(T1_FLAGS(i, k) & agg_mask);
1438 #ifdef DEBUG_ENC_CLN
1439             printf("   agg=%d\n", agg);
1440 #endif
1441             if (agg) {
1442                 for (runlen = 0; runlen < 4; ++runlen) {
1443                     if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1444                         break;
1445                     }
1446                 }
1447                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1448                 opj_mqc_encode(mqc, runlen != 4);
1449                 if (runlen == 4) {
1450                     continue;
1451                 }
1452                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1453                 opj_mqc_encode(mqc, runlen >> 1);
1454                 opj_mqc_encode(mqc, runlen & 1);
1455             } else {
1456                 runlen = 0;
1457             }
1458             opj_t1_enc_clnpass_step(
1459                 t1,
1460                 &T1_FLAGS(i, k),
1461                 &t1->data[((k + runlen) * t1->data_stride) + i],
1462                 bpno,
1463                 one,
1464                 nmsedec,
1465                 agg,
1466                 runlen,
1467                 4U,
1468                 cblksty);
1469         }
1470     }
1471     if (k < t1->h) {
1472         agg = 0;
1473         runlen = 0;
1474 #ifdef DEBUG_ENC_CLN
1475         printf(" k=%d\n", k);
1476 #endif
1477         for (i = 0; i < t1->w; ++i) {
1478 #ifdef DEBUG_ENC_CLN
1479             printf("  i=%d\n", i);
1480             printf("   agg=%d\n", agg);
1481 #endif
1482             opj_t1_enc_clnpass_step(
1483                 t1,
1484                 &T1_FLAGS(i, k),
1485                 &t1->data[((k + runlen) * t1->data_stride) + i],
1486                 bpno,
1487                 one,
1488                 nmsedec,
1489                 agg,
1490                 runlen,
1491                 t1->h - k,
1492                 cblksty);
1493         }
1494     }
1495 }
1496
1497 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1498 { \
1499     OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1500     OPJ_UINT32 runlen; \
1501     OPJ_UINT32 i, j, k; \
1502     OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1503      \
1504     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */ \
1505      \
1506     one = 1 << bpno; \
1507     half = one >> 1; \
1508     oneplushalf = one | half; \
1509     if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1510     opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1511     const OPJ_UINT32 agg_mask =  ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1512     for (k = 0; k < h; k += 4) { \
1513         for (i = 0; i < w; ++i) { \
1514             opj_flag_t *flagsp2 = flagsp1 + i; \
1515             if (k + 3 < h) { \
1516                     agg = !(*flagsp2 & agg_mask); \
1517                 } else { \
1518                 agg = 0; \
1519             } \
1520             if (agg) { \
1521                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1522                 if (!opj_mqc_decode(mqc)) { \
1523                     continue; \
1524                 } \
1525                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1526                 runlen = opj_mqc_decode(mqc); \
1527                 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1528             } else { \
1529                 runlen = 0; \
1530             } \
1531             for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1532                     vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1533                     opj_t1_dec_clnpass_step_vsc( \
1534                         t1, \
1535                         flagsp2, \
1536                         &t1->data[(j * w) + i], \
1537                         oneplushalf, \
1538                         agg && (j == k + runlen), \
1539                         vsc, j - k); \
1540             } \
1541             *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1542         } \
1543         flagsp1 += flags_stride; \
1544     } \
1545     } else { \
1546         OPJ_INT32 *data1 = t1->data; \
1547         opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1548         for (k = 0; k < (h & ~3u); k += 4) { \
1549             for (i = 0; i < w; ++i) { \
1550                 OPJ_INT32 *data2 = data1 + i; \
1551                 opj_flag_t *flagsp2 = flagsp1 + i; \
1552                 if (*flagsp2 == 0) { \
1553                     opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1554                     if (!opj_mqc_decode(mqc)) { \
1555                         continue; \
1556                     } \
1557                     opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1558                     runlen = opj_mqc_decode(mqc); \
1559                     runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1560                     data2 += runlen * w; \
1561                     for (j = runlen; j < 4; ++j) { \
1562                         if (j == runlen) { \
1563                             opj_t1_dec_clnpass_step_partial(t1, flagsp2, data2, oneplushalf, j); \
1564                         } else { \
1565                             opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j); \
1566                         } \
1567                         data2 += w; \
1568                     } \
1569                 } else { \
1570                     if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (0 * 3U)))) {\
1571                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 0U, flags_stride); \
1572                     } \
1573                     data2 += w; \
1574                     if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (1 * 3U)))) {\
1575                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 1U, flags_stride); \
1576                     } \
1577                     data2 += w; \
1578                     if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (2 * 3U)))) {\
1579                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 2U, flags_stride); \
1580                     } \
1581                     data2 += w; \
1582                     if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (3 * 3U)))) {\
1583                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 3U, flags_stride); \
1584                     } \
1585                     data2 += w; \
1586                 } \
1587                 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1588             } \
1589             data1 += w << 2; \
1590             flagsp1 += flags_stride; \
1591         } \
1592         for (i = 0; i < w; ++i) { \
1593             OPJ_INT32 *data2 = data1 + i; \
1594             opj_flag_t *flagsp2 = flagsp1 + i; \
1595             for (j = k; j < h; ++j) { \
1596                 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j - k); \
1597                 data2 += w; \
1598             } \
1599             *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1600         } \
1601     } \
1602  \
1603     if (segsym) { \
1604         OPJ_UINT32 v = 0; \
1605         opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1606         v = opj_mqc_decode(mqc); \
1607         v = (v << 1) | opj_mqc_decode(mqc); \
1608         v = (v << 1) | opj_mqc_decode(mqc); \
1609         v = (v << 1) | opj_mqc_decode(mqc); \
1610         /* \
1611         if (v!=0xa) { \
1612             opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1613         } \
1614         */ \
1615     } \
1616 }
1617
1618 static void opj_t1_dec_clnpass_64x64(
1619     opj_t1_t *t1,
1620     OPJ_INT32 bpno,
1621     OPJ_INT32 cblksty)
1622 {
1623     opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1624 }
1625
1626 static void opj_t1_dec_clnpass_generic(
1627     opj_t1_t *t1,
1628     OPJ_INT32 bpno,
1629     OPJ_INT32 cblksty)
1630 {
1631     opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1632                                 t1->w + 2U);
1633 }
1634
1635
1636 /** mod fixed_quality */
1637 static OPJ_FLOAT64 opj_t1_getwmsedec(
1638     OPJ_INT32 nmsedec,
1639     OPJ_UINT32 compno,
1640     OPJ_UINT32 level,
1641     OPJ_UINT32 orient,
1642     OPJ_INT32 bpno,
1643     OPJ_UINT32 qmfbid,
1644     OPJ_FLOAT64 stepsize,
1645     OPJ_UINT32 numcomps,
1646     const OPJ_FLOAT64 * mct_norms,
1647     OPJ_UINT32 mct_numcomps)
1648 {
1649     OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1650     OPJ_ARG_NOT_USED(numcomps);
1651
1652     if (mct_norms && (compno < mct_numcomps)) {
1653         w1 = mct_norms[compno];
1654     }
1655
1656     if (qmfbid == 1) {
1657         w2 = opj_dwt_getnorm(level, orient);
1658     } else {    /* if (qmfbid == 0) */
1659         w2 = opj_dwt_getnorm_real(level, orient);
1660     }
1661
1662     wmsedec = w1 * w2 * stepsize * (1 << bpno);
1663     wmsedec *= wmsedec * nmsedec / 8192.0;
1664
1665     return wmsedec;
1666 }
1667
1668 static OPJ_BOOL opj_t1_allocate_buffers(
1669     opj_t1_t *t1,
1670     OPJ_UINT32 w,
1671     OPJ_UINT32 h)
1672 {
1673     size_t flagssize;
1674     OPJ_UINT32 flags_stride;
1675
1676     /* encoder uses tile buffer, so no need to allocate */
1677     if (!t1->encoder) {
1678         size_t datasize;
1679
1680 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1681         /* Overflow check */
1682         if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1683             /* FIXME event manager error callback */
1684             return OPJ_FALSE;
1685         }
1686 #endif
1687         datasize = (size_t)w * h;
1688
1689         /* Overflow check */
1690         if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1691             /* FIXME event manager error callback */
1692             return OPJ_FALSE;
1693         }
1694
1695         if (datasize > (size_t)t1->datasize) {
1696             opj_aligned_free(t1->data);
1697             t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1698             if (!t1->data) {
1699                 /* FIXME event manager error callback */
1700                 return OPJ_FALSE;
1701             }
1702 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1703             /* TODO remove this if t1->datasize type changes to size_t */
1704             /* Overflow check */
1705             if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1706                 /* FIXME event manager error callback */
1707                 return OPJ_FALSE;
1708             }
1709 #endif
1710             t1->datasize = (OPJ_UINT32)datasize;
1711         }
1712         /* memset first arg is declared to never be null by gcc */
1713         if (t1->data != NULL) {
1714             memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1715         }
1716     }
1717
1718     /* Overflow check */
1719     if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1720         /* FIXME event manager error callback */
1721         return OPJ_FALSE;
1722     }
1723     flags_stride = w + 2U; /* can't be 0U */
1724
1725 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1726     /* Overflow check */
1727     if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1728         /* FIXME event manager error callback */
1729         return OPJ_FALSE;
1730     }
1731 #endif
1732     flagssize = (h + 3U) / 4U + 2U;
1733
1734     /* Overflow check */
1735     if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1736         /* FIXME event manager error callback */
1737         return OPJ_FALSE;
1738     }
1739     flagssize *= (size_t)flags_stride;
1740     {
1741         /* BIG FAT XXX */
1742         opj_flag_t* p;
1743         OPJ_UINT32 x;
1744         OPJ_UINT32 flags_height = (h + 3U) / 4U;
1745
1746         if (flagssize > (size_t)t1->flagssize) {
1747             /* Overflow check */
1748             if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1749                 /* FIXME event manager error callback */
1750                 return OPJ_FALSE;
1751             }
1752             opj_aligned_free(t1->flags);
1753             t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1754                             opj_flag_t));
1755             if (!t1->flags) {
1756                 /* FIXME event manager error callback */
1757                 return OPJ_FALSE;
1758             }
1759 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1760             /* TODO remove this if t1->flagssize type changes to size_t */
1761             /* Overflow check */
1762             if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1763                 /* FIXME event manager error callback */
1764                 return OPJ_FALSE;
1765             }
1766 #endif
1767         }
1768         t1->flagssize = (OPJ_UINT32)flagssize;
1769
1770         memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1771
1772         p = &t1->flags[0];
1773         for (x = 0; x < flags_stride; ++x) {
1774             /* magic value to hopefully stop any passes being interested in this entry */
1775             *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1776         }
1777
1778         p = &t1->flags[((flags_height + 1) * flags_stride)];
1779         for (x = 0; x < flags_stride; ++x) {
1780             /* magic value to hopefully stop any passes being interested in this entry */
1781             *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1782         }
1783
1784         if (h % 4) {
1785             OPJ_UINT32 v = 0;
1786             p = &t1->flags[((flags_height) * flags_stride)];
1787             if (h % 4 == 1) {
1788                 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1789             } else if (h % 4 == 2) {
1790                 v |= T1_PI_2 | T1_PI_3;
1791             } else if (h % 4 == 3) {
1792                 v |= T1_PI_3;
1793             }
1794             for (x = 0; x < flags_stride; ++x) {
1795                 *p++ = v;
1796             }
1797         }
1798     }
1799
1800     t1->w = w;
1801     t1->h = h;
1802
1803     return OPJ_TRUE;
1804 }
1805
1806 /* ----------------------------------------------------------------------- */
1807
1808 /* ----------------------------------------------------------------------- */
1809 /**
1810  * Creates a new Tier 1 handle
1811  * and initializes the look-up tables of the Tier-1 coder/decoder
1812  * @return a new T1 handle if successful, returns NULL otherwise
1813 */
1814 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1815 {
1816     opj_t1_t *l_t1 = 00;
1817
1818     l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1819     if (!l_t1) {
1820         return 00;
1821     }
1822
1823     l_t1->encoder = isEncoder;
1824
1825     return l_t1;
1826 }
1827
1828
1829 /**
1830  * Destroys a previously created T1 handle
1831  *
1832  * @param p_t1 Tier 1 handle to destroy
1833 */
1834 void opj_t1_destroy(opj_t1_t *p_t1)
1835 {
1836     if (! p_t1) {
1837         return;
1838     }
1839
1840     /* encoder uses tile buffer, so no need to free */
1841     if (!p_t1->encoder && p_t1->data) {
1842         opj_aligned_free(p_t1->data);
1843         p_t1->data = 00;
1844     }
1845
1846     if (p_t1->flags) {
1847         opj_aligned_free(p_t1->flags);
1848         p_t1->flags = 00;
1849     }
1850
1851     opj_free(p_t1);
1852 }
1853
1854 typedef struct {
1855     OPJ_UINT32 resno;
1856     opj_tcd_cblk_dec_t* cblk;
1857     opj_tcd_band_t* band;
1858     opj_tcd_tilecomp_t* tilec;
1859     opj_tccp_t* tccp;
1860     volatile OPJ_BOOL* pret;
1861 } opj_t1_cblk_decode_processing_job_t;
1862
1863 static void opj_t1_destroy_wrapper(void* t1)
1864 {
1865     opj_t1_destroy((opj_t1_t*) t1);
1866 }
1867
1868 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1869 {
1870     opj_tcd_cblk_dec_t* cblk;
1871     opj_tcd_band_t* band;
1872     opj_tcd_tilecomp_t* tilec;
1873     opj_tccp_t* tccp;
1874     OPJ_INT32* OPJ_RESTRICT datap;
1875     OPJ_UINT32 cblk_w, cblk_h;
1876     OPJ_INT32 x, y;
1877     OPJ_UINT32 i, j;
1878     opj_t1_cblk_decode_processing_job_t* job;
1879     opj_t1_t* t1;
1880     OPJ_UINT32 resno;
1881     OPJ_UINT32 tile_w;
1882
1883     job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1884     resno = job->resno;
1885     cblk = job->cblk;
1886     band = job->band;
1887     tilec = job->tilec;
1888     tccp = job->tccp;
1889     tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1890
1891     if (!*(job->pret)) {
1892         opj_free(job);
1893         return;
1894     }
1895
1896     t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1897     if (t1 == NULL) {
1898         t1 = opj_t1_create(OPJ_FALSE);
1899         opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1900     }
1901
1902     if (OPJ_FALSE == opj_t1_decode_cblk(
1903                 t1,
1904                 cblk,
1905                 band->bandno,
1906                 (OPJ_UINT32)tccp->roishift,
1907                 tccp->cblksty)) {
1908         *(job->pret) = OPJ_FALSE;
1909         opj_free(job);
1910         return;
1911     }
1912
1913     x = cblk->x0 - band->x0;
1914     y = cblk->y0 - band->y0;
1915     if (band->bandno & 1) {
1916         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1917         x += pres->x1 - pres->x0;
1918     }
1919     if (band->bandno & 2) {
1920         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1921         y += pres->y1 - pres->y0;
1922     }
1923
1924     datap = t1->data;
1925     cblk_w = t1->w;
1926     cblk_h = t1->h;
1927
1928     if (tccp->roishift) {
1929         OPJ_INT32 thresh = 1 << tccp->roishift;
1930         for (j = 0; j < cblk_h; ++j) {
1931             for (i = 0; i < cblk_w; ++i) {
1932                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1933                 OPJ_INT32 mag = abs(val);
1934                 if (mag >= thresh) {
1935                     mag >>= tccp->roishift;
1936                     datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1937                 }
1938             }
1939         }
1940     }
1941     if (tccp->qmfbid == 1) {
1942         OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1943                                                        (OPJ_UINT32)x];
1944         for (j = 0; j < cblk_h; ++j) {
1945             i = 0;
1946             for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1947                 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1948                 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1949                 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1950                 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1951                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1952                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1953                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1954                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1955             }
1956             for (; i < cblk_w; ++i) {
1957                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1958                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1959             }
1960         }
1961     } else {        /* if (tccp->qmfbid == 0) */
1962         OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1963                                                          tile_w + (OPJ_UINT32)x];
1964         for (j = 0; j < cblk_h; ++j) {
1965             OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1966             for (i = 0; i < cblk_w; ++i) {
1967                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1968                 *tiledp2 = tmp;
1969                 datap++;
1970                 tiledp2++;
1971             }
1972             tiledp += tile_w;
1973         }
1974     }
1975
1976     opj_free(job);
1977 }
1978
1979
1980 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1981                          volatile OPJ_BOOL* pret,
1982                          opj_tcd_tilecomp_t* tilec,
1983                          opj_tccp_t* tccp
1984                         )
1985 {
1986     OPJ_UINT32 resno, bandno, precno, cblkno;
1987
1988     for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1989         opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1990
1991         for (bandno = 0; bandno < res->numbands; ++bandno) {
1992             opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1993
1994             for (precno = 0; precno < res->pw * res->ph; ++precno) {
1995                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1996
1997                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1998                     opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1999                     opj_t1_cblk_decode_processing_job_t* job;
2000
2001                     job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
2002                             sizeof(opj_t1_cblk_decode_processing_job_t));
2003                     if (!job) {
2004                         *pret = OPJ_FALSE;
2005                         return;
2006                     }
2007                     job->resno = resno;
2008                     job->cblk = cblk;
2009                     job->band = band;
2010                     job->tilec = tilec;
2011                     job->tccp = tccp;
2012                     job->pret = pret;
2013                     opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2014                     if (!(*pret)) {
2015                         return;
2016                     }
2017                 } /* cblkno */
2018             } /* precno */
2019         } /* bandno */
2020     } /* resno */
2021
2022     return;
2023 }
2024
2025
2026 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2027                                    opj_tcd_cblk_dec_t* cblk,
2028                                    OPJ_UINT32 orient,
2029                                    OPJ_UINT32 roishift,
2030                                    OPJ_UINT32 cblksty)
2031 {
2032     opj_raw_t *raw = &(t1->raw);   /* RAW component */
2033     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
2034
2035     OPJ_INT32 bpno_plus_one;
2036     OPJ_UINT32 passtype;
2037     OPJ_UINT32 segno, passno;
2038     OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2039
2040     mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2041
2042     if (!opj_t1_allocate_buffers(
2043                 t1,
2044                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2045                 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2046         return OPJ_FALSE;
2047     }
2048
2049     bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2050     passtype = 2;
2051
2052     opj_mqc_resetstates(mqc);
2053     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2054     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2055     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2056
2057     for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2058         opj_tcd_seg_t *seg = &cblk->segs[segno];
2059
2060         /* BYPASS mode */
2061         type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2062                 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2063         /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2064         if (seg->data == 00) {
2065             continue;
2066         }
2067         if (type == T1_TYPE_RAW) {
2068             opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2069         } else {
2070             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2071                                               seg->len)) {
2072                 return OPJ_FALSE;
2073             }
2074         }
2075
2076         if (t1->w == 64 && t1->h == 64) {
2077             for (passno = 0; (passno < seg->real_num_passes) &&
2078                     (bpno_plus_one >= 1); ++passno) {
2079                 switch (passtype) {
2080                 case 0:
2081                     if (type == T1_TYPE_RAW) {
2082                         opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2083                     } else {
2084                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2085                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2086                         } else {
2087                             opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2088                         }
2089                     }
2090                     break;
2091                 case 1:
2092                     if (type == T1_TYPE_RAW) {
2093                         opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2094                     } else {
2095                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2096                             opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2097                         } else {
2098                             opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2099                         }
2100                     }
2101                     break;
2102                 case 2:
2103                     opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2104                     break;
2105                 }
2106
2107                 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2108                     opj_mqc_resetstates(mqc);
2109                     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2110                     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2111                     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2112                 }
2113                 if (++passtype == 3) {
2114                     passtype = 0;
2115                     bpno_plus_one--;
2116                 }
2117             }
2118         } else {
2119             for (passno = 0; (passno < seg->real_num_passes) &&
2120                     (bpno_plus_one >= 1); ++passno) {
2121                 switch (passtype) {
2122                 case 0:
2123                     if (type == T1_TYPE_RAW) {
2124                         opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2125                     } else {
2126                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2127                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2128                         } else {
2129                             opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2130                         }
2131                     }
2132                     break;
2133                 case 1:
2134                     if (type == T1_TYPE_RAW) {
2135                         opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2136                     } else {
2137                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2138                             opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2139                         } else {
2140                             opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2141                         }
2142                     }
2143                     break;
2144                 case 2:
2145                     opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2146                     break;
2147                 }
2148
2149                 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2150                     opj_mqc_resetstates(mqc);
2151                     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2152                     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2153                     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2154                 }
2155                 if (++passtype == 3) {
2156                     passtype = 0;
2157                     bpno_plus_one--;
2158                 }
2159             }
2160         }
2161     }
2162     return OPJ_TRUE;
2163 }
2164
2165
2166
2167
2168 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2169                              opj_tcd_tile_t *tile,
2170                              opj_tcp_t *tcp,
2171                              const OPJ_FLOAT64 * mct_norms,
2172                              OPJ_UINT32 mct_numcomps
2173                             )
2174 {
2175     OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2176
2177     tile->distotile = 0;        /* fixed_quality */
2178
2179     for (compno = 0; compno < tile->numcomps; ++compno) {
2180         opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2181         opj_tccp_t* tccp = &tcp->tccps[compno];
2182         OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2183
2184         for (resno = 0; resno < tilec->numresolutions; ++resno) {
2185             opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2186
2187             for (bandno = 0; bandno < res->numbands; ++bandno) {
2188                 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2189                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2190
2191                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2192                     opj_tcd_precinct_t *prc = &band->precincts[precno];
2193
2194                     for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2195                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2196                         OPJ_INT32* OPJ_RESTRICT tiledp;
2197                         OPJ_UINT32 cblk_w;
2198                         OPJ_UINT32 cblk_h;
2199                         OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2200
2201                         OPJ_INT32 x = cblk->x0 - band->x0;
2202                         OPJ_INT32 y = cblk->y0 - band->y0;
2203                         if (band->bandno & 1) {
2204                             opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2205                             x += pres->x1 - pres->x0;
2206                         }
2207                         if (band->bandno & 2) {
2208                             opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2209                             y += pres->y1 - pres->y0;
2210                         }
2211
2212                         if (!opj_t1_allocate_buffers(
2213                                     t1,
2214                                     (OPJ_UINT32)(cblk->x1 - cblk->x0),
2215                                     (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2216                             return OPJ_FALSE;
2217                         }
2218
2219                         cblk_w = t1->w;
2220                         cblk_h = t1->h;
2221                         tileLineAdvance = tile_w - cblk_w;
2222
2223                         tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2224                         t1->data = tiledp;
2225                         t1->data_stride = tile_w;
2226                         if (tccp->qmfbid == 1) {
2227                             for (j = 0; j < cblk_h; ++j) {
2228                                 for (i = 0; i < cblk_w; ++i) {
2229                                     tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2230                                     tileIndex++;
2231                                 }
2232                                 tileIndex += tileLineAdvance;
2233                             }
2234                         } else {        /* if (tccp->qmfbid == 0) */
2235                             for (j = 0; j < cblk_h; ++j) {
2236                                 for (i = 0; i < cblk_w; ++i) {
2237                                     OPJ_INT32 tmp = tiledp[tileIndex];
2238                                     tiledp[tileIndex] =
2239                                         opj_int_fix_mul_t1(
2240                                             tmp,
2241                                             bandconst);
2242                                     tileIndex++;
2243                                 }
2244                                 tileIndex += tileLineAdvance;
2245                             }
2246                         }
2247
2248                         opj_t1_encode_cblk(
2249                             t1,
2250                             cblk,
2251                             band->bandno,
2252                             compno,
2253                             tilec->numresolutions - 1 - resno,
2254                             tccp->qmfbid,
2255                             band->stepsize,
2256                             tccp->cblksty,
2257                             tile->numcomps,
2258                             tile,
2259                             mct_norms,
2260                             mct_numcomps);
2261
2262                     } /* cblkno */
2263                 } /* precno */
2264             } /* bandno */
2265         } /* resno  */
2266     } /* compno  */
2267     return OPJ_TRUE;
2268 }
2269
2270 /** mod fixed_quality */
2271 static void opj_t1_encode_cblk(opj_t1_t *t1,
2272                                opj_tcd_cblk_enc_t* cblk,
2273                                OPJ_UINT32 orient,
2274                                OPJ_UINT32 compno,
2275                                OPJ_UINT32 level,
2276                                OPJ_UINT32 qmfbid,
2277                                OPJ_FLOAT64 stepsize,
2278                                OPJ_UINT32 cblksty,
2279                                OPJ_UINT32 numcomps,
2280                                opj_tcd_tile_t * tile,
2281                                const OPJ_FLOAT64 * mct_norms,
2282                                OPJ_UINT32 mct_numcomps)
2283 {
2284     OPJ_FLOAT64 cumwmsedec = 0.0;
2285
2286     opj_mqc_t *mqc = &(t1->mqc);   /* MQC component */
2287
2288     OPJ_UINT32 passno;
2289     OPJ_INT32 bpno;
2290     OPJ_UINT32 passtype;
2291     OPJ_INT32 nmsedec = 0;
2292     OPJ_INT32 max;
2293     OPJ_UINT32 i, j;
2294     OPJ_BYTE type = T1_TYPE_MQ;
2295     OPJ_FLOAT64 tempwmsedec;
2296
2297     mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2298
2299     max = 0;
2300     for (i = 0; i < t1->w; ++i) {
2301         for (j = 0; j < t1->h; ++j) {
2302             OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2303             max = opj_int_max(max, tmp);
2304         }
2305     }
2306
2307     cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2308                                       T1_NMSEDEC_FRACBITS) : 0;
2309
2310     bpno = (OPJ_INT32)(cblk->numbps - 1);
2311     passtype = 2;
2312
2313     opj_mqc_resetstates(mqc);
2314     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2315     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2316     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2317     opj_mqc_init_enc(mqc, cblk->data);
2318
2319     for (passno = 0; bpno >= 0; ++passno) {
2320         opj_tcd_pass_t *pass = &cblk->passes[passno];
2321         OPJ_UINT32 correction = 3;
2322         type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2323                 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2324
2325         switch (passtype) {
2326         case 0:
2327             opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2328             break;
2329         case 1:
2330             opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2331             break;
2332         case 2:
2333             opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2334             /* code switch SEGMARK (i.e. SEGSYM) */
2335             if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2336                 opj_mqc_segmark_enc(mqc);
2337             }
2338             break;
2339         }
2340
2341         /* fixed_quality */
2342         tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2343                                         stepsize, numcomps, mct_norms, mct_numcomps) ;
2344         cumwmsedec += tempwmsedec;
2345         tile->distotile += tempwmsedec;
2346
2347         /* Code switch "RESTART" (i.e. TERMALL) */
2348         if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2349                 (bpno - 1 < 0))) {
2350             if (type == T1_TYPE_RAW) {
2351                 opj_mqc_flush(mqc);
2352                 correction = 1;
2353                 /* correction = mqc_bypass_flush_enc(); */
2354             } else {            /* correction = mqc_restart_enc(); */
2355                 opj_mqc_flush(mqc);
2356                 correction = 1;
2357             }
2358             pass->term = 1;
2359         } else {
2360             if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2361                     || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2362                     (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2363                 if (type == T1_TYPE_RAW) {
2364                     opj_mqc_flush(mqc);
2365                     correction = 1;
2366                     /* correction = mqc_bypass_flush_enc(); */
2367                 } else {        /* correction = mqc_restart_enc(); */
2368                     opj_mqc_flush(mqc);
2369                     correction = 1;
2370                 }
2371                 pass->term = 1;
2372             } else {
2373                 pass->term = 0;
2374             }
2375         }
2376
2377         if (++passtype == 3) {
2378             passtype = 0;
2379             bpno--;
2380         }
2381
2382         if (pass->term && bpno > 0) {
2383             type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2384                     (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2385             if (type == T1_TYPE_RAW) {
2386                 opj_mqc_bypass_init_enc(mqc);
2387             } else {
2388                 opj_mqc_restart_init_enc(mqc);
2389             }
2390         }
2391
2392         pass->distortiondec = cumwmsedec;
2393         pass->rate = opj_mqc_numbytes(mqc) + correction;    /* FIXME */
2394
2395         /* Code-switch "RESET" */
2396         if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2397             opj_mqc_reset_enc(mqc);
2398         }
2399     }
2400
2401     /* Code switch "ERTERM" (i.e. PTERM) */
2402     if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2403         opj_mqc_erterm_enc(mqc);
2404     } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2405         opj_mqc_flush(mqc);
2406     }
2407
2408     cblk->totalpasses = passno;
2409
2410     for (passno = 0; passno < cblk->totalpasses; passno++) {
2411         opj_tcd_pass_t *pass = &cblk->passes[passno];
2412         if (pass->rate > opj_mqc_numbytes(mqc)) {
2413             pass->rate = opj_mqc_numbytes(mqc);
2414         }
2415         /*Preventing generation of FF as last data byte of a pass*/
2416         if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2417             pass->rate--;
2418         }
2419         pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2420     }
2421 }