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