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