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