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