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