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