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