T1: fix BYPASS/LAZY, TERMALL/RESTART and PTERM/ERTERM encoding modes. (#674)
[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)(sp[0] | 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)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
476                                      (T1_COLFLAG_RBS * row)));
477     }
478 }
479
480 static INLINE void opj_t1_dec_sigpass_step_mqc(
481     opj_t1_t *t1,
482     opj_flag_t *flagsp,
483     opj_colflag_t* colflagsp,
484     OPJ_INT32 *datap,
485     OPJ_INT32 oneplushalf,
486     OPJ_UINT32 row,
487     OPJ_UINT32 flags_stride)
488 {
489     OPJ_INT32 v, flag;
490
491     opj_mqc_t *mqc = t1->mqc;       /* MQC component */
492 #ifdef CONSISTENCY_CHECK
493     assert(((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
494            ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
495                             T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
496             (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))));
497 #endif
498     if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
499                         T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
500             (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) {
501         flag = *flagsp;
502         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
503         if (opj_mqc_decode(mqc)) {
504             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
505             v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
506             *datap = v ? -oneplushalf : oneplushalf;
507             opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
508         }
509 #ifdef CONSISTENCY_CHECK
510         *flagsp |= T1_VISIT;
511 #endif
512         *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
513                                      (T1_COLFLAG_RBS * row)));
514     }
515 }                               /* VSC and  BYPASS by Antonin */
516
517 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
518     opj_t1_t *t1,
519     opj_flag_t *flagsp,
520     opj_colflag_t* colflagsp,
521     OPJ_INT32 *datap,
522     OPJ_INT32 oneplushalf,
523     OPJ_INT32 vsc,
524     OPJ_UINT32 row)
525 {
526     OPJ_UINT32 v, flag;
527
528     opj_mqc_t *mqc = t1->mqc;       /* MQC component */
529
530     flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
531            (*flagsp);
532     if ((flag & T1_SIG_OTH) &&
533             !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
534                             (T1_COLFLAG_RBS * row)))) {
535         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
536         if (opj_mqc_decode(mqc)) {
537             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
538             v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
539             *datap = v ? -oneplushalf : oneplushalf;
540             opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
541         }
542 #ifdef CONSISTENCY_CHECK
543         *flagsp |= T1_VISIT;
544 #endif
545         *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
546                                      (T1_COLFLAG_RBS * row)));
547     }
548 }                               /* VSC and  BYPASS by Antonin */
549
550
551
552 static void opj_t1_enc_sigpass(opj_t1_t *t1,
553                                OPJ_INT32 bpno,
554                                OPJ_INT32 *nmsedec,
555                                OPJ_BYTE type,
556                                OPJ_UINT32 cblksty
557                               )
558 {
559     OPJ_UINT32 i, j, k, vsc;
560     OPJ_INT32 one;
561
562     *nmsedec = 0;
563     one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
564     for (k = 0; k < t1->h; k += 4) {
565         for (i = 0; i < t1->w; ++i) {
566             for (j = k; j < k + 4 && j < t1->h; ++j) {
567                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
568                         j == t1->h - 1)) ? 1 : 0;
569                 opj_t1_enc_sigpass_step(
570                     t1,
571                     &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
572                     &t1->data[(j * t1->data_stride) + i],
573                     bpno,
574                     one,
575                     nmsedec,
576                     type,
577                     vsc);
578             }
579         }
580     }
581 }
582
583 static void opj_t1_dec_sigpass_raw(
584     opj_t1_t *t1,
585     OPJ_INT32 bpno,
586     OPJ_INT32 cblksty)
587 {
588     OPJ_INT32 one, half, oneplushalf, vsc;
589     OPJ_UINT32 i, j, k;
590     opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
591     one = 1 << bpno;
592     half = one >> 1;
593     oneplushalf = one | half;
594     for (k = 0; k < t1->h; k += 4) {
595         for (i = 0; i < t1->w; ++i) {
596             opj_colflag_t *colflags2 = colflags1 + i;
597             for (j = k; j < k + 4 && j < t1->h; ++j) {
598                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
599                         j == t1->h - 1)) ? 1 : 0;
600                 opj_t1_dec_sigpass_step_raw(
601                     t1,
602                     &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
603                     colflags2,
604                     &t1->data[(j * t1->w) + i],
605                     oneplushalf,
606                     vsc,
607                     j - k);
608             }
609         }
610         colflags1 += t1->flags_stride;
611     }
612 }                               /* VSC and  BYPASS by Antonin */
613
614 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
615 { \
616         OPJ_INT32 one, half, oneplushalf; \
617         OPJ_UINT32 i, j, k; \
618         OPJ_INT32 *data1 = t1->data; \
619         opj_flag_t *flags1 = &t1->flags[1]; \
620         opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
621         one = 1 << bpno; \
622         half = one >> 1; \
623         oneplushalf = one | half; \
624         for (k = 0; k < (h & ~3u); k += 4) { \
625                 for (i = 0; i < w; ++i) { \
626                         OPJ_INT32 *data2 = data1 + i; \
627                         opj_flag_t *flags2 = flags1 + i; \
628                         opj_colflag_t *colflags2 = colflags1 + i; \
629                         if( *colflags2 == 0 ) continue; \
630                         flags2 += flags_stride; \
631                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
632                         data2 += w; \
633                         flags2 += flags_stride; \
634                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
635                         data2 += w; \
636                         flags2 += flags_stride; \
637                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
638                         data2 += w; \
639                         flags2 += flags_stride; \
640                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
641                         data2 += w; \
642                 } \
643                 data1 += w << 2; \
644                 flags1 += flags_stride << 2; \
645                 colflags1 += flags_stride; \
646         } \
647         for (i = 0; i < w; ++i) { \
648                 OPJ_INT32 *data2 = data1 + i; \
649                 opj_flag_t *flags2 = flags1 + i; \
650                 opj_colflag_t *colflags2 = colflags1 + i; \
651                 for (j = k; j < h; ++j) { \
652                         flags2 += flags_stride; \
653                         opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
654                         data2 += w; \
655                 } \
656         } \
657 }
658
659 static void opj_t1_dec_sigpass_mqc_64x64(
660     opj_t1_t *t1,
661     OPJ_INT32 bpno)
662 {
663     opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
664 }
665
666 static void opj_t1_dec_sigpass_mqc_generic(
667     opj_t1_t *t1,
668     OPJ_INT32 bpno)
669 {
670     opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
671 }
672
673 /* VSC and  BYPASS by Antonin */
674 static void opj_t1_dec_sigpass_mqc_vsc(
675     opj_t1_t *t1,
676     OPJ_INT32 bpno)
677 {
678     OPJ_INT32 one, half, oneplushalf, vsc;
679     OPJ_UINT32 i, j, k;
680     OPJ_INT32 *data1 = t1->data;
681     opj_flag_t *flags1 = &t1->flags[1];
682     opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
683     one = 1 << bpno;
684     half = one >> 1;
685     oneplushalf = one | half;
686     for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
687         for (i = 0; i < t1->w; ++i) {
688             OPJ_INT32 *data2 = data1 + i;
689             opj_flag_t *flags2 = flags1 + i;
690             opj_colflag_t *colflags2 = colflags1 + i;
691             flags2 += t1->flags_stride;
692             opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
693                                             0U);
694             data2 += t1->w;
695             flags2 += t1->flags_stride;
696             opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
697                                             1U);
698             data2 += t1->w;
699             flags2 += t1->flags_stride;
700             opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
701                                             2U);
702             data2 += t1->w;
703             flags2 += t1->flags_stride;
704             opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1,
705                                             3U);
706             data2 += t1->w;
707         }
708         data1 += t1->w << 2;
709         flags1 += t1->flags_stride << 2;
710         colflags1 += t1->flags_stride;
711     }
712     for (i = 0; i < t1->w; ++i) {
713         opj_colflag_t *colflags2 = colflags1 + i;
714         for (j = k; j < t1->h; ++j) {
715             vsc = (j == t1->h - 1) ? 1 : 0;
716             opj_t1_dec_sigpass_step_mqc_vsc(
717                 t1,
718                 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
719                 colflags2,
720                 &t1->data[(j * t1->w) + i],
721                 oneplushalf,
722                 vsc,
723                 j - k);
724         }
725     }
726 }                               /* VSC and  BYPASS by Antonin */
727
728
729
730 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
731                                     opj_flag_t *flagsp,
732                                     OPJ_INT32 *datap,
733                                     OPJ_INT32 bpno,
734                                     OPJ_INT32 one,
735                                     OPJ_INT32 *nmsedec,
736                                     OPJ_BYTE type,
737                                     OPJ_UINT32 vsc)
738 {
739     OPJ_INT32 v;
740     OPJ_UINT32 flag;
741
742     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
743
744     flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
745                                            T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
746     if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
747         *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
748                                           (OPJ_UINT32)(bpno));
749         v = (opj_int_abs(*datap) & one) ? 1 : 0;
750         opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));  /* ESSAI */
751         if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
752             opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
753         } else {
754             opj_mqc_encode(mqc, (OPJ_UINT32)v);
755         }
756         *flagsp |= T1_REFINE;
757     }
758 }
759
760 static INLINE void opj_t1_dec_refpass_step_raw(
761     opj_t1_t *t1,
762     opj_colflag_t *colflagsp,
763     OPJ_INT32 *datap,
764     OPJ_INT32 poshalf,
765     OPJ_INT32 neghalf,
766     OPJ_UINT32 row)
767 {
768     OPJ_INT32 v, t;
769
770     opj_raw_t *raw = t1->raw;       /* RAW component */
771
772     if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
773                        (T1_COLFLAG_RBS * row))) ==
774             ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
775         v = (OPJ_INT32)opj_raw_decode(raw);
776         t = v ? poshalf : neghalf;
777         *datap += *datap < 0 ? -t : t;
778         *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
779                                      (T1_COLFLAG_RBS * row)));
780     }
781 }                               /* VSC and  BYPASS by Antonin  */
782
783 static INLINE void opj_t1_dec_refpass_step_mqc(
784     opj_t1_t *t1,
785 #ifdef CONSISTENCY_CHECK
786     opj_flag_t *flagsp,
787 #else
788     opj_flag_t *flagsp_unused,
789 #endif
790     opj_colflag_t *colflagsp,
791     OPJ_INT32 *datap,
792     OPJ_INT32 poshalf,
793     OPJ_INT32 neghalf,
794     OPJ_UINT32 row)
795 {
796     OPJ_INT32 v, t;
797
798     opj_mqc_t *mqc = t1->mqc;       /* MQC component */
799 #ifdef CONSISTENCY_CHECK
800     assert(((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
801            ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
802                            (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS *
803                                    row))));
804 #else
805     (void)flagsp_unused;
806 #endif
807     if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
808                        (T1_COLFLAG_RBS * row))) ==
809             ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
810         OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 <<
811                                          (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
812         OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
813                                          (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
814         opj_mqc_setcurctx(mqc, tmp2);      /* ESSAI */
815         v = opj_mqc_decode(mqc);
816         t = v ? poshalf : neghalf;
817         *datap += *datap < 0 ? -t : t;
818         *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
819                                      (T1_COLFLAG_RBS * row)));
820     }
821 }                               /* VSC and  BYPASS by Antonin  */
822
823 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
824     opj_t1_t *t1,
825     opj_flag_t *flagsp,
826     opj_colflag_t *colflagsp,
827     OPJ_INT32 *datap,
828     OPJ_INT32 poshalf,
829     OPJ_INT32 neghalf,
830     OPJ_INT32 vsc,
831     OPJ_UINT32 row)
832 {
833     OPJ_UINT32 v;
834     OPJ_INT32 t;
835
836     opj_mqc_t *mqc = t1->mqc;       /* MQC component */
837
838     if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
839                        (T1_COLFLAG_RBS * row))) ==
840             ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
841         OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
842                                                 T1_SGN_S))) : (*flagsp);
843         OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
844         OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
845                                          (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
846         opj_mqc_setcurctx(mqc, tmp2);      /* ESSAI */
847         v = (OPJ_UINT32)opj_mqc_decode(mqc);
848         t = v ? poshalf : neghalf;
849         *datap += *datap < 0 ? -t : t;
850         *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
851                                      (T1_COLFLAG_RBS * row)));
852     }
853 }                               /* VSC and  BYPASS by Antonin  */
854
855
856 static void opj_t1_enc_refpass(
857     opj_t1_t *t1,
858     OPJ_INT32 bpno,
859     OPJ_INT32 *nmsedec,
860     OPJ_BYTE type,
861     OPJ_UINT32 cblksty)
862 {
863     OPJ_UINT32 i, j, k, vsc;
864     OPJ_INT32 one;
865
866     *nmsedec = 0;
867     one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
868     for (k = 0; k < t1->h; k += 4) {
869         for (i = 0; i < t1->w; ++i) {
870             for (j = k; j < k + 4 && j < t1->h; ++j) {
871                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
872                         j == t1->h - 1)) ? 1 : 0;
873                 opj_t1_enc_refpass_step(
874                     t1,
875                     &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
876                     &t1->data[(j * t1->data_stride) + i],
877                     bpno,
878                     one,
879                     nmsedec,
880                     type,
881                     vsc);
882             }
883         }
884     }
885 }
886
887 static void opj_t1_dec_refpass_raw(
888     opj_t1_t *t1,
889     OPJ_INT32 bpno)
890 {
891     OPJ_INT32 one, poshalf, neghalf;
892     OPJ_UINT32 i, j, k;
893     opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
894     one = 1 << bpno;
895     poshalf = one >> 1;
896     neghalf = bpno > 0 ? -poshalf : -1;
897     for (k = 0; k < t1->h; k += 4) {
898         for (i = 0; i < t1->w; ++i) {
899             opj_colflag_t *colflags2 = colflags1 + i;
900             for (j = k; j < k + 4 && j < t1->h; ++j) {
901                 opj_t1_dec_refpass_step_raw(
902                     t1,
903                     colflags2,
904                     &t1->data[(j * t1->w) + i],
905                     poshalf,
906                     neghalf, j - k);
907             }
908         }
909         colflags1 += t1->flags_stride;
910     }
911 }                               /* VSC and  BYPASS by Antonin */
912
913 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
914 { \
915         OPJ_INT32 one, poshalf, neghalf; \
916         OPJ_UINT32 i, j, k; \
917         OPJ_INT32 *data1 = t1->data; \
918         opj_flag_t *flags1 = &t1->flags[1]; \
919         opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
920         one = 1 << bpno; \
921         poshalf = one >> 1; \
922         neghalf = bpno > 0 ? -poshalf : -1; \
923         for (k = 0; k < (h & ~3u); k += 4) { \
924                 for (i = 0; i < w; ++i) { \
925                         OPJ_INT32 *data2 = data1 + i; \
926                         opj_flag_t *flags2 = flags1 + i; \
927                         opj_colflag_t *colflags2 = colflags1 + i; \
928                         if( *colflags2 == 0 ) continue; \
929                         flags2 += flags_stride; \
930                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
931                         data2 += w; \
932                         flags2 += flags_stride; \
933                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
934                         data2 += w; \
935                         flags2 += flags_stride; \
936                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
937                         data2 += w; \
938                         flags2 += flags_stride; \
939                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
940                         data2 += w; \
941                 } \
942                 data1 += w << 2; \
943                 flags1 += flags_stride << 2; \
944                 colflags1 += flags_stride; \
945         } \
946         for (i = 0; i < w; ++i) { \
947                 OPJ_INT32 *data2 = data1 + i; \
948                 opj_flag_t *flags2 = flags1 + i; \
949                 opj_colflag_t *colflags2 = colflags1 + i; \
950                 for (j = k; j < h; ++j) { \
951                         flags2 += flags_stride; \
952                         opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
953                         data2 += w; \
954                 } \
955         } \
956 }
957
958 static void opj_t1_dec_refpass_mqc_64x64(
959     opj_t1_t *t1,
960     OPJ_INT32 bpno)
961 {
962     opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
963 }
964
965 static void opj_t1_dec_refpass_mqc_generic(
966     opj_t1_t *t1,
967     OPJ_INT32 bpno)
968 {
969     opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
970 }
971
972 /* VSC and  BYPASS by Antonin */
973 static void opj_t1_dec_refpass_mqc_vsc(
974     opj_t1_t *t1,
975     OPJ_INT32 bpno)
976 {
977     OPJ_INT32 one, poshalf, neghalf;
978     OPJ_UINT32 i, j, k;
979     OPJ_INT32 vsc;
980     OPJ_INT32 *data1 = t1->data;
981     opj_flag_t *flags1 = &t1->flags[1];
982     opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
983     one = 1 << bpno;
984     poshalf = one >> 1;
985     neghalf = bpno > 0 ? -poshalf : -1;
986     for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
987         for (i = 0; i < t1->w; ++i) {
988             OPJ_INT32 *data2 = data1 + i;
989             opj_flag_t *flags2 = flags1 + i;
990             opj_colflag_t *colflags2 = colflags1 + i;
991             flags2 += t1->flags_stride;
992             opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
993                                             0, 0U);
994             data2 += t1->w;
995             flags2 += t1->flags_stride;
996             opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
997                                             0, 1U);
998             data2 += t1->w;
999             flags2 += t1->flags_stride;
1000             opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1001                                             0, 2U);
1002             data2 += t1->w;
1003             flags2 += t1->flags_stride;
1004             opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1005                                             1, 3U);
1006             data2 += t1->w;
1007         }
1008         data1 += t1->w << 2;
1009         flags1 += t1->flags_stride << 2;
1010         colflags1 += t1->flags_stride;
1011     }
1012     for (i = 0; i < t1->w; ++i) {
1013         opj_colflag_t *colflags2 = colflags1 + i;
1014         for (j = k; j < t1->h; ++j) {
1015             vsc = (j == t1->h - 1) ? 1 : 0;
1016             opj_t1_dec_refpass_step_mqc_vsc(
1017                 t1,
1018                 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1019                 colflags2,
1020                 &t1->data[(j * t1->w) + i],
1021                 poshalf, neghalf,
1022                 vsc,
1023                 j - k);
1024         }
1025     }
1026 }                               /* VSC and  BYPASS by Antonin */
1027
1028
1029 static void opj_t1_enc_clnpass_step(
1030     opj_t1_t *t1,
1031     opj_flag_t *flagsp,
1032     OPJ_INT32 *datap,
1033     OPJ_INT32 bpno,
1034     OPJ_INT32 one,
1035     OPJ_INT32 *nmsedec,
1036     OPJ_UINT32 partial,
1037     OPJ_UINT32 vsc)
1038 {
1039     OPJ_INT32 v;
1040     OPJ_UINT32 flag;
1041
1042     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1043
1044     flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
1045                                            T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
1046     if (partial) {
1047         goto LABEL_PARTIAL;
1048     }
1049     if (!(*flagsp & (T1_SIG | T1_VISIT))) {
1050         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1051         v = (opj_int_abs(*datap) & one) ? 1 : 0;
1052         opj_mqc_encode(mqc, (OPJ_UINT32)v);
1053         if (v) {
1054 LABEL_PARTIAL:
1055             *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1056                                               (OPJ_UINT32)(bpno));
1057             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1058             v = *datap < 0 ? 1 : 0;
1059             opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
1060             opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
1061         }
1062     }
1063     *flagsp &= (opj_flag_t)~T1_VISIT;
1064 }
1065
1066 static void opj_t1_dec_clnpass_step_partial(
1067     opj_t1_t *t1,
1068     opj_flag_t *flagsp,
1069     opj_colflag_t *colflagsp,
1070     OPJ_INT32 *datap,
1071     OPJ_INT32 oneplushalf,
1072     OPJ_UINT32 row)
1073 {
1074     OPJ_INT32 v, flag;
1075     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1076
1077     flag = *flagsp;
1078     opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1079     v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1080     *datap = v ? -oneplushalf : oneplushalf;
1081     opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1082                                row);
1083 #ifdef CONSISTENCY_CHECK
1084     *flagsp &= ~T1_VISIT;
1085 #endif
1086 }               /* VSC and  BYPASS by Antonin */
1087
1088 static void opj_t1_dec_clnpass_step(
1089     opj_t1_t *t1,
1090     opj_flag_t *flagsp,
1091     opj_colflag_t *colflagsp,
1092     OPJ_INT32 *datap,
1093     OPJ_INT32 oneplushalf,
1094     OPJ_UINT32 row)
1095 {
1096     OPJ_INT32 v, flag;
1097
1098     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1099 #ifdef CONSISTENCY_CHECK
1100     assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1101                 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1102 #endif
1103     if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1104                         (4 * row)))) {
1105         flag = *flagsp;
1106         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1107         if (opj_mqc_decode(mqc)) {
1108             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1109             v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1110             *datap = v ? -oneplushalf : oneplushalf;
1111             opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1112                                        row);
1113         }
1114     }
1115 #ifdef CONSISTENCY_CHECK
1116     *flagsp &= ~T1_VISIT;
1117 #endif
1118 }               /* VSC and  BYPASS by Antonin */
1119
1120 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1121     opj_t1_t *t1,
1122     opj_flag_t *flagsp,
1123     opj_colflag_t *colflagsp,
1124     OPJ_INT32 *datap,
1125     OPJ_INT32 oneplushalf,
1126     OPJ_UINT32 row,
1127     OPJ_UINT32 flags_stride)
1128 {
1129     OPJ_INT32 v;
1130     OPJ_INT32 flag;
1131
1132     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1133
1134     flag = *flagsp;
1135     /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1136     {
1137         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1138         if (opj_mqc_decode(mqc)) {
1139             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1140             v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1141             *datap = v ? -oneplushalf : oneplushalf;
1142             opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1143         }
1144     }
1145     /*flagsp &= ~T1_VISIT;*/
1146 }
1147
1148 static void opj_t1_dec_clnpass_step_vsc(
1149     opj_t1_t *t1,
1150     opj_flag_t *flagsp,
1151     opj_colflag_t *colflagsp,
1152     OPJ_INT32 *datap,
1153     OPJ_INT32 oneplushalf,
1154     OPJ_INT32 partial,
1155     OPJ_INT32 vsc,
1156     OPJ_UINT32 row)
1157 {
1158     OPJ_UINT32 v, flag;
1159
1160     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1161
1162     flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1163            (*flagsp);
1164     if (partial) {
1165         goto LABEL_PARTIAL;
1166     }
1167     if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1168                         (T1_COLFLAG_RBS * row)))) {
1169         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1170         if (opj_mqc_decode(mqc)) {
1171 LABEL_PARTIAL:
1172             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1173             v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1174             *datap = v ? -oneplushalf : oneplushalf;
1175             opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1176         }
1177     }
1178 #ifdef CONSISTENCY_CHECK
1179     *flagsp &= ~T1_VISIT;
1180 #endif
1181 }
1182
1183 static void opj_t1_enc_clnpass(
1184     opj_t1_t *t1,
1185     OPJ_INT32 bpno,
1186     OPJ_INT32 *nmsedec,
1187     OPJ_UINT32 cblksty)
1188 {
1189     OPJ_UINT32 i, j, k;
1190     OPJ_INT32 one;
1191     OPJ_UINT32 agg, runlen, vsc;
1192
1193     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1194
1195     *nmsedec = 0;
1196     one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1197     for (k = 0; k < t1->h; k += 4) {
1198         for (i = 0; i < t1->w; ++i) {
1199             if (k + 3 < t1->h) {
1200                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1201                     agg = !(MACRO_t1_flags(1 + k, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1202                             || MACRO_t1_flags(1 + k + 1, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1203                             || MACRO_t1_flags(1 + k + 2, 1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1204                             || (MACRO_t1_flags(1 + k + 3, 1 + i)
1205                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT |
1206                                         T1_SIG_OTH));
1207                 } else {
1208                     agg = !((MACRO_t1_flags(1 + k, 1 + i) |
1209                              MACRO_t1_flags(1 + k + 1, 1 + i) |
1210                              MACRO_t1_flags(1 + k + 2, 1 + i) |
1211                              MACRO_t1_flags(1 + k + 3, 1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1212                 }
1213             } else {
1214                 agg = 0;
1215             }
1216             if (agg) {
1217                 for (runlen = 0; runlen < 4; ++runlen) {
1218                     if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1219                         break;
1220                     }
1221                 }
1222                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1223                 opj_mqc_encode(mqc, runlen != 4);
1224                 if (runlen == 4) {
1225                     continue;
1226                 }
1227                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1228                 opj_mqc_encode(mqc, runlen >> 1);
1229                 opj_mqc_encode(mqc, runlen & 1);
1230             } else {
1231                 runlen = 0;
1232             }
1233             for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1234                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
1235                         j == t1->h - 1)) ? 1 : 0;
1236                 opj_t1_enc_clnpass_step(
1237                     t1,
1238                     &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1239                     &t1->data[(j * t1->data_stride) + i],
1240                     bpno,
1241                     one,
1242                     nmsedec,
1243                     agg && (j == k + runlen),
1244                     vsc);
1245             }
1246         }
1247     }
1248 }
1249
1250 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1251
1252 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1253 { \
1254     OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1255     OPJ_UINT32 i, j, k; \
1256     OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1257      \
1258     opj_mqc_t *mqc = t1->mqc;   /* MQC component */ \
1259      \
1260     one = 1 << bpno; \
1261     half = one >> 1; \
1262     oneplushalf = one | half; \
1263     if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1264     opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1265     for (k = 0; k < h; k += 4) { \
1266         for (i = 0; i < w; ++i) { \
1267             opj_colflag_t *colflags2 = colflags1 + i; \
1268             if (k + 3 < h) { \
1269                     agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1270                                            T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1271                                            T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1272                                            T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1273                           ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1274                            & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1275                 } else { \
1276                 agg = 0; \
1277             } \
1278             if (agg) { \
1279                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1280                 if (!opj_mqc_decode(mqc)) { \
1281                     continue; \
1282                 } \
1283                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1284                 runlen = opj_mqc_decode(mqc); \
1285                 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1286             } else { \
1287                 runlen = 0; \
1288             } \
1289             for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1290                     vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1291                     opj_t1_dec_clnpass_step_vsc( \
1292                         t1, \
1293                         &t1->flags[((j+1) * flags_stride) + i + 1], \
1294                         colflags2, \
1295                         &t1->data[(j * w) + i], \
1296                         oneplushalf, \
1297                         agg && (j == k + (OPJ_UINT32)runlen), \
1298                         vsc, j - k); \
1299             } \
1300             *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1301         } \
1302         colflags1 += flags_stride; \
1303     } \
1304     } else { \
1305         OPJ_INT32 *data1 = t1->data; \
1306         opj_flag_t *flags1 = &t1->flags[1]; \
1307         opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1308         for (k = 0; k < (h & ~3u); k += 4) { \
1309             for (i = 0; i < w; ++i) { \
1310                 OPJ_INT32 *data2 = data1 + i; \
1311                 opj_flag_t *flags2 = flags1 + i; \
1312                 opj_colflag_t *colflags2 = colflags1 + i; \
1313                 opj_colflag_t colflags = *colflags2; \
1314                 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1315                                      T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1316                                      T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1317                                      T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1318                 if( consistency_check ) { \
1319                     assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1320                                       MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1321                                       MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1322                                       MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1323                 } \
1324                 if (agg) { \
1325                     opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1326                     if (!opj_mqc_decode(mqc)) { \
1327                         continue; \
1328                     } \
1329                     opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1330                     runlen = opj_mqc_decode(mqc); \
1331                     runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1332                     flags2 += (OPJ_UINT32)runlen * flags_stride; \
1333                     data2 += (OPJ_UINT32)runlen * w; \
1334                     for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1335                         flags2 += flags_stride; \
1336                         if (j == (OPJ_UINT32)runlen) { \
1337                             opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1338                         } else { \
1339                             opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1340                         } \
1341                         data2 += w; \
1342                     } \
1343                 } else { \
1344                     flags2 += flags_stride; \
1345                     if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1346                     if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1347                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1348                     } \
1349                     if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1350                     data2 += w; \
1351                     flags2 += flags_stride; \
1352                     if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1353                     if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1354                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1355                     } \
1356                     if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1357                     data2 += w; \
1358                     flags2 += flags_stride; \
1359                     if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1360                     if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1361                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1362                     } \
1363                     if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1364                     data2 += w; \
1365                     flags2 += flags_stride; \
1366                     if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1367                     if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1368                         opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1369                     } \
1370                     if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1371                     data2 += w; \
1372                 } \
1373                 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1374             } \
1375             data1 += w << 2; \
1376             flags1 += flags_stride << 2; \
1377             colflags1 += flags_stride; \
1378         } \
1379         for (i = 0; i < w; ++i) { \
1380             OPJ_INT32 *data2 = data1 + i; \
1381             opj_flag_t *flags2 = flags1 + i; \
1382             opj_colflag_t *colflags2 = colflags1 + i; \
1383             for (j = k; j < h; ++j) { \
1384                 flags2 += flags_stride; \
1385                 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1386                 data2 += w; \
1387             } \
1388             *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1389         } \
1390     } \
1391  \
1392     if (segsym) { \
1393         OPJ_INT32 v = 0; \
1394         opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1395         v = opj_mqc_decode(mqc); \
1396         v = (v << 1) | opj_mqc_decode(mqc); \
1397         v = (v << 1) | opj_mqc_decode(mqc); \
1398         v = (v << 1) | opj_mqc_decode(mqc); \
1399         /* \
1400         if (v!=0xa) { \
1401             opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1402         } \
1403         */ \
1404     } \
1405 }               /* VSC and  BYPASS by Antonin */
1406
1407 static void opj_t1_dec_clnpass_64x64(
1408     opj_t1_t *t1,
1409     OPJ_INT32 bpno,
1410     OPJ_INT32 cblksty)
1411 {
1412 #ifdef CONSISTENCY_CHECK
1413     opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1414 #else
1415     opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1416 #endif
1417 }
1418
1419 static void opj_t1_dec_clnpass_generic(
1420     opj_t1_t *t1,
1421     OPJ_INT32 bpno,
1422     OPJ_INT32 cblksty)
1423 {
1424 #ifdef CONSISTENCY_CHECK
1425     opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1426                                 t1->flags_stride);
1427 #else
1428     opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1429                                 t1->flags_stride);
1430 #endif
1431 }
1432
1433
1434 /** mod fixed_quality */
1435 static OPJ_FLOAT64 opj_t1_getwmsedec(
1436     OPJ_INT32 nmsedec,
1437     OPJ_UINT32 compno,
1438     OPJ_UINT32 level,
1439     OPJ_UINT32 orient,
1440     OPJ_INT32 bpno,
1441     OPJ_UINT32 qmfbid,
1442     OPJ_FLOAT64 stepsize,
1443     OPJ_UINT32 numcomps,
1444     const OPJ_FLOAT64 * mct_norms,
1445     OPJ_UINT32 mct_numcomps)
1446 {
1447     OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1448     OPJ_ARG_NOT_USED(numcomps);
1449
1450     if (mct_norms && (compno < mct_numcomps)) {
1451         w1 = mct_norms[compno];
1452     }
1453
1454     if (qmfbid == 1) {
1455         w2 = opj_dwt_getnorm(level, orient);
1456     } else {    /* if (qmfbid == 0) */
1457         w2 = opj_dwt_getnorm_real(level, orient);
1458     }
1459
1460     wmsedec = w1 * w2 * stepsize * (1 << bpno);
1461     wmsedec *= wmsedec * nmsedec / 8192.0;
1462
1463     return wmsedec;
1464 }
1465
1466 static OPJ_BOOL opj_t1_allocate_buffers(
1467     opj_t1_t *t1,
1468     OPJ_UINT32 w,
1469     OPJ_UINT32 h)
1470 {
1471     /* encoder uses tile buffer, so no need to allocate */
1472     if (!t1->encoder) {
1473         size_t datasize;
1474
1475 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1476         /* Overflow check */
1477         if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1478             /* FIXME event manager error callback */
1479             return OPJ_FALSE;
1480         }
1481 #endif
1482         datasize = (size_t)w * h;
1483
1484         /* Overflow check */
1485         if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1486             /* FIXME event manager error callback */
1487             return OPJ_FALSE;
1488         }
1489
1490         if (datasize > (size_t)t1->datasize) {
1491             opj_aligned_free(t1->data);
1492             t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1493             if (!t1->data) {
1494                 /* FIXME event manager error callback */
1495                 return OPJ_FALSE;
1496             }
1497 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1498             /* TODO remove this if t1->datasize type changes to size_t */
1499             /* Overflow check */
1500             if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1501                 /* FIXME event manager error callback */
1502                 return OPJ_FALSE;
1503             }
1504 #endif
1505             t1->datasize = (OPJ_UINT32)datasize;
1506         }
1507         /* memset first arg is declared to never be null by gcc */
1508         if (t1->data != NULL) {
1509             memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1510         }
1511     }
1512
1513     {
1514         size_t flagssize;
1515
1516         /* Overflow check */
1517         if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1518             /* FIXME event manager error callback */
1519             return OPJ_FALSE;
1520         }
1521         t1->flags_stride = w + 2U; /* can't be 0U */
1522
1523 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1524         /* Overflow check */
1525         if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1526             /* FIXME event manager error callback */
1527             return OPJ_FALSE;
1528         }
1529 #endif
1530         flagssize = (size_t)h + 3U;
1531
1532         /* Overflow check */
1533         if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1534             /* FIXME event manager error callback */
1535             return OPJ_FALSE;
1536         }
1537         flagssize *= (size_t)t1->flags_stride;
1538
1539         if (flagssize > (size_t)t1->flagssize) {
1540             /* Overflow check */
1541             if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1542                 /* FIXME event manager error callback */
1543                 return OPJ_FALSE;
1544             }
1545             opj_aligned_free(t1->flags);
1546             t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1547             if (!t1->flags) {
1548                 /* FIXME event manager error callback */
1549                 return OPJ_FALSE;
1550             }
1551 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1552             /* TODO remove this if t1->flagssize type changes to size_t */
1553             /* Overflow check */
1554             if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1555                 /* FIXME event manager error callback */
1556                 return OPJ_FALSE;
1557             }
1558 #endif
1559             t1->flagssize = (OPJ_UINT32)flagssize;
1560         }
1561         memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1562     }
1563     if (!t1->encoder) {
1564         size_t colflags_size = ((((size_t)h + 3U) / 4U) +
1565                                 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1566
1567         /* Overflow check */
1568         if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1569             /* FIXME event manager error callback */
1570             return OPJ_FALSE;
1571         }
1572         colflags_size *= (size_t)t1->flags_stride;
1573
1574         if (colflags_size > (size_t)t1->colflags_size) {
1575             /* Overflow check */
1576             if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1577                 /* FIXME event manager error callback */
1578                 return OPJ_FALSE;
1579             }
1580             opj_aligned_free(t1->colflags);
1581             t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1582                                opj_colflag_t));
1583             if (!t1->colflags) {
1584                 /* FIXME event manager error callback */
1585                 return OPJ_FALSE;
1586             }
1587 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1588             /* TODO remove this if t1->colflags_size type changes to size_t */
1589             /* Overflow check */
1590             if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1591                 /* FIXME event manager error callback */
1592                 return OPJ_FALSE;
1593             }
1594 #endif
1595             t1->colflags_size = (OPJ_UINT32)colflags_size;
1596         }
1597         memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1598     }
1599
1600     t1->w = w;
1601     t1->h = h;
1602
1603     return OPJ_TRUE;
1604 }
1605
1606 /* ----------------------------------------------------------------------- */
1607
1608 /* ----------------------------------------------------------------------- */
1609 /**
1610  * Creates a new Tier 1 handle
1611  * and initializes the look-up tables of the Tier-1 coder/decoder
1612  * @return a new T1 handle if successful, returns NULL otherwise
1613 */
1614 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1615 {
1616     opj_t1_t *l_t1 = 00;
1617
1618     l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1619     if (!l_t1) {
1620         return 00;
1621     }
1622
1623     /* create MQC and RAW handles */
1624     l_t1->mqc = opj_mqc_create();
1625     if (! l_t1->mqc) {
1626         opj_t1_destroy(l_t1);
1627         return 00;
1628     }
1629
1630     l_t1->raw = opj_raw_create();
1631     if (! l_t1->raw) {
1632         opj_t1_destroy(l_t1);
1633         return 00;
1634     }
1635     l_t1->encoder = isEncoder;
1636
1637     return l_t1;
1638 }
1639
1640
1641 /**
1642  * Destroys a previously created T1 handle
1643  *
1644  * @param p_t1 Tier 1 handle to destroy
1645 */
1646 void opj_t1_destroy(opj_t1_t *p_t1)
1647 {
1648     if (! p_t1) {
1649         return;
1650     }
1651
1652     /* destroy MQC and RAW handles */
1653     opj_mqc_destroy(p_t1->mqc);
1654     p_t1->mqc = 00;
1655     opj_raw_destroy(p_t1->raw);
1656     p_t1->raw = 00;
1657
1658     /* encoder uses tile buffer, so no need to free */
1659     if (!p_t1->encoder && p_t1->data) {
1660         opj_aligned_free(p_t1->data);
1661         p_t1->data = 00;
1662     }
1663
1664     if (p_t1->flags) {
1665         opj_aligned_free(p_t1->flags);
1666         p_t1->flags = 00;
1667     }
1668
1669     if (p_t1->colflags) {
1670         opj_aligned_free(p_t1->colflags);
1671         p_t1->colflags = 00;
1672     }
1673     opj_free(p_t1);
1674 }
1675
1676 typedef struct {
1677     OPJ_UINT32 resno;
1678     opj_tcd_cblk_dec_t* cblk;
1679     opj_tcd_band_t* band;
1680     opj_tcd_tilecomp_t* tilec;
1681     opj_tccp_t* tccp;
1682     volatile OPJ_BOOL* pret;
1683 } opj_t1_cblk_decode_processing_job_t;
1684
1685 static void opj_t1_destroy_wrapper(void* t1)
1686 {
1687     opj_t1_destroy((opj_t1_t*) t1);
1688 }
1689
1690 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1691 {
1692     opj_tcd_cblk_dec_t* cblk;
1693     opj_tcd_band_t* band;
1694     opj_tcd_tilecomp_t* tilec;
1695     opj_tccp_t* tccp;
1696     OPJ_INT32* OPJ_RESTRICT datap;
1697     OPJ_UINT32 cblk_w, cblk_h;
1698     OPJ_INT32 x, y;
1699     OPJ_UINT32 i, j;
1700     opj_t1_cblk_decode_processing_job_t* job;
1701     opj_t1_t* t1;
1702     OPJ_UINT32 resno;
1703     OPJ_UINT32 tile_w;
1704
1705     job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1706     resno = job->resno;
1707     cblk = job->cblk;
1708     band = job->band;
1709     tilec = job->tilec;
1710     tccp = job->tccp;
1711     tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1712
1713     if (!*(job->pret)) {
1714         opj_free(job);
1715         return;
1716     }
1717
1718     t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1719     if (t1 == NULL) {
1720         t1 = opj_t1_create(OPJ_FALSE);
1721         opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1722     }
1723
1724     if (OPJ_FALSE == opj_t1_decode_cblk(
1725                 t1,
1726                 cblk,
1727                 band->bandno,
1728                 (OPJ_UINT32)tccp->roishift,
1729                 tccp->cblksty)) {
1730         *(job->pret) = OPJ_FALSE;
1731         opj_free(job);
1732         return;
1733     }
1734
1735     x = cblk->x0 - band->x0;
1736     y = cblk->y0 - band->y0;
1737     if (band->bandno & 1) {
1738         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1739         x += pres->x1 - pres->x0;
1740     }
1741     if (band->bandno & 2) {
1742         opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1743         y += pres->y1 - pres->y0;
1744     }
1745
1746     datap = t1->data;
1747     cblk_w = t1->w;
1748     cblk_h = t1->h;
1749
1750     if (tccp->roishift) {
1751         OPJ_INT32 thresh = 1 << tccp->roishift;
1752         for (j = 0; j < cblk_h; ++j) {
1753             for (i = 0; i < cblk_w; ++i) {
1754                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1755                 OPJ_INT32 mag = abs(val);
1756                 if (mag >= thresh) {
1757                     mag >>= tccp->roishift;
1758                     datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1759                 }
1760             }
1761         }
1762     }
1763     if (tccp->qmfbid == 1) {
1764         OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1765                                                        (OPJ_UINT32)x];
1766         for (j = 0; j < cblk_h; ++j) {
1767             i = 0;
1768             for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1769                 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1770                 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1771                 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1772                 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1773                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1774                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1775                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1776                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1777             }
1778             for (; i < cblk_w; ++i) {
1779                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1780                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1781             }
1782         }
1783     } else {        /* if (tccp->qmfbid == 0) */
1784         OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1785                                                          tile_w + (OPJ_UINT32)x];
1786         for (j = 0; j < cblk_h; ++j) {
1787             OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1788             for (i = 0; i < cblk_w; ++i) {
1789                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1790                 *tiledp2 = tmp;
1791                 datap++;
1792                 tiledp2++;
1793             }
1794             tiledp += tile_w;
1795         }
1796     }
1797
1798     opj_free(job);
1799 }
1800
1801
1802 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1803                          volatile OPJ_BOOL* pret,
1804                          opj_tcd_tilecomp_t* tilec,
1805                          opj_tccp_t* tccp
1806                         )
1807 {
1808     OPJ_UINT32 resno, bandno, precno, cblkno;
1809
1810     for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1811         opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1812
1813         for (bandno = 0; bandno < res->numbands; ++bandno) {
1814             opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1815
1816             for (precno = 0; precno < res->pw * res->ph; ++precno) {
1817                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1818
1819                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1820                     opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1821                     opj_t1_cblk_decode_processing_job_t* job;
1822
1823                     job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1824                             sizeof(opj_t1_cblk_decode_processing_job_t));
1825                     if (!job) {
1826                         *pret = OPJ_FALSE;
1827                         return;
1828                     }
1829                     job->resno = resno;
1830                     job->cblk = cblk;
1831                     job->band = band;
1832                     job->tilec = tilec;
1833                     job->tccp = tccp;
1834                     job->pret = pret;
1835                     opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1836                     if (!(*pret)) {
1837                         return;
1838                     }
1839                 } /* cblkno */
1840             } /* precno */
1841         } /* bandno */
1842     } /* resno */
1843
1844     return;
1845 }
1846
1847
1848 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1849                                    opj_tcd_cblk_dec_t* cblk,
1850                                    OPJ_UINT32 orient,
1851                                    OPJ_UINT32 roishift,
1852                                    OPJ_UINT32 cblksty)
1853 {
1854     opj_raw_t *raw = t1->raw;   /* RAW component */
1855     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
1856
1857     OPJ_INT32 bpno_plus_one;
1858     OPJ_UINT32 passtype;
1859     OPJ_UINT32 segno, passno;
1860     OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1861
1862     mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
1863
1864     if (!opj_t1_allocate_buffers(
1865                 t1,
1866                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1867                 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1868         return OPJ_FALSE;
1869     }
1870
1871     bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1872     passtype = 2;
1873
1874     opj_mqc_resetstates(mqc);
1875     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1876     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1877     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1878
1879     for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1880         opj_tcd_seg_t *seg = &cblk->segs[segno];
1881
1882         /* BYPASS mode */
1883         type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1884                 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1885         /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1886         if (seg->data == 00) {
1887             continue;
1888         }
1889         if (type == T1_TYPE_RAW) {
1890             opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1891         } else {
1892             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
1893                                               seg->len)) {
1894                 return OPJ_FALSE;
1895             }
1896         }
1897
1898         if (t1->w == 64 && t1->h == 64) {
1899             for (passno = 0; (passno < seg->real_num_passes) &&
1900                     (bpno_plus_one >= 1); ++passno) {
1901                 switch (passtype) {
1902                 case 0:
1903                     if (type == T1_TYPE_RAW) {
1904                         opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1905                     } else {
1906                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1907                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1908                         } else {
1909                             opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
1910                         }
1911                     }
1912                     break;
1913                 case 1:
1914                     if (type == T1_TYPE_RAW) {
1915                         opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1916                     } else {
1917                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1918                             opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1919                         } else {
1920                             opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1921                         }
1922                     }
1923                     break;
1924                 case 2:
1925                     opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1926                     break;
1927                 }
1928
1929                 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1930                     opj_mqc_resetstates(mqc);
1931                     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1932                     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1933                     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1934                 }
1935                 if (++passtype == 3) {
1936                     passtype = 0;
1937                     bpno_plus_one--;
1938                 }
1939             }
1940         } else {
1941             for (passno = 0; (passno < seg->real_num_passes) &&
1942                     (bpno_plus_one >= 1); ++passno) {
1943                 switch (passtype) {
1944                 case 0:
1945                     if (type == T1_TYPE_RAW) {
1946                         opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1947                     } else {
1948                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1949                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1950                         } else {
1951                             opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
1952                         }
1953                     }
1954                     break;
1955                 case 1:
1956                     if (type == T1_TYPE_RAW) {
1957                         opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1958                     } else {
1959                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1960                             opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1961                         } else {
1962                             opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1963                         }
1964                     }
1965                     break;
1966                 case 2:
1967                     opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1968                     break;
1969                 }
1970
1971                 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1972                     opj_mqc_resetstates(mqc);
1973                     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1974                     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1975                     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1976                 }
1977                 if (++passtype == 3) {
1978                     passtype = 0;
1979                     bpno_plus_one--;
1980                 }
1981             }
1982         }
1983     }
1984     return OPJ_TRUE;
1985 }
1986
1987
1988
1989
1990 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
1991                              opj_tcd_tile_t *tile,
1992                              opj_tcp_t *tcp,
1993                              const OPJ_FLOAT64 * mct_norms,
1994                              OPJ_UINT32 mct_numcomps
1995                             )
1996 {
1997     OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1998
1999     tile->distotile = 0;        /* fixed_quality */
2000
2001     for (compno = 0; compno < tile->numcomps; ++compno) {
2002         opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2003         opj_tccp_t* tccp = &tcp->tccps[compno];
2004         OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2005
2006         for (resno = 0; resno < tilec->numresolutions; ++resno) {
2007             opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2008
2009             for (bandno = 0; bandno < res->numbands; ++bandno) {
2010                 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2011                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2012
2013                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2014                     opj_tcd_precinct_t *prc = &band->precincts[precno];
2015
2016                     for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2017                         opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2018                         OPJ_INT32* OPJ_RESTRICT tiledp;
2019                         OPJ_UINT32 cblk_w;
2020                         OPJ_UINT32 cblk_h;
2021                         OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2022
2023                         OPJ_INT32 x = cblk->x0 - band->x0;
2024                         OPJ_INT32 y = cblk->y0 - band->y0;
2025                         if (band->bandno & 1) {
2026                             opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2027                             x += pres->x1 - pres->x0;
2028                         }
2029                         if (band->bandno & 2) {
2030                             opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2031                             y += pres->y1 - pres->y0;
2032                         }
2033
2034                         if (!opj_t1_allocate_buffers(
2035                                     t1,
2036                                     (OPJ_UINT32)(cblk->x1 - cblk->x0),
2037                                     (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2038                             return OPJ_FALSE;
2039                         }
2040
2041                         cblk_w = t1->w;
2042                         cblk_h = t1->h;
2043                         tileLineAdvance = tile_w - cblk_w;
2044
2045                         tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2046                         t1->data = tiledp;
2047                         t1->data_stride = tile_w;
2048                         if (tccp->qmfbid == 1) {
2049                             for (j = 0; j < cblk_h; ++j) {
2050                                 for (i = 0; i < cblk_w; ++i) {
2051                                     tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2052                                     tileIndex++;
2053                                 }
2054                                 tileIndex += tileLineAdvance;
2055                             }
2056                         } else {        /* if (tccp->qmfbid == 0) */
2057                             for (j = 0; j < cblk_h; ++j) {
2058                                 for (i = 0; i < cblk_w; ++i) {
2059                                     OPJ_INT32 tmp = tiledp[tileIndex];
2060                                     tiledp[tileIndex] =
2061                                         opj_int_fix_mul_t1(
2062                                             tmp,
2063                                             bandconst);
2064                                     tileIndex++;
2065                                 }
2066                                 tileIndex += tileLineAdvance;
2067                             }
2068                         }
2069
2070                         opj_t1_encode_cblk(
2071                             t1,
2072                             cblk,
2073                             band->bandno,
2074                             compno,
2075                             tilec->numresolutions - 1 - resno,
2076                             tccp->qmfbid,
2077                             band->stepsize,
2078                             tccp->cblksty,
2079                             tile->numcomps,
2080                             tile,
2081                             mct_norms,
2082                             mct_numcomps);
2083
2084                     } /* cblkno */
2085                 } /* precno */
2086             } /* bandno */
2087         } /* resno  */
2088     } /* compno  */
2089     return OPJ_TRUE;
2090 }
2091
2092 /* Returns whether the pass (bpno, passtype) is terminated */
2093 static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
2094                                    OPJ_UINT32 cblksty,
2095                                    OPJ_INT32 bpno,
2096                                    OPJ_UINT32 passtype)
2097 {
2098     /* Is it the last cleanup pass ? */
2099     if (passtype == 2 && bpno == 0) {
2100         return OPJ_TRUE;
2101     }
2102
2103     if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
2104         return OPJ_TRUE;
2105     }
2106
2107     if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2108         /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
2109         if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
2110             return OPJ_TRUE;
2111         }
2112         /* and beyond terminate all the magnitude refinement passes (in raw) */
2113         /* and cleanup passes (in MQC) */
2114         if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
2115             return OPJ_TRUE;
2116         }
2117     }
2118
2119     return OPJ_FALSE;
2120 }
2121
2122
2123 /** mod fixed_quality */
2124 static void opj_t1_encode_cblk(opj_t1_t *t1,
2125                                opj_tcd_cblk_enc_t* cblk,
2126                                OPJ_UINT32 orient,
2127                                OPJ_UINT32 compno,
2128                                OPJ_UINT32 level,
2129                                OPJ_UINT32 qmfbid,
2130                                OPJ_FLOAT64 stepsize,
2131                                OPJ_UINT32 cblksty,
2132                                OPJ_UINT32 numcomps,
2133                                opj_tcd_tile_t * tile,
2134                                const OPJ_FLOAT64 * mct_norms,
2135                                OPJ_UINT32 mct_numcomps)
2136 {
2137     OPJ_FLOAT64 cumwmsedec = 0.0;
2138
2139     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
2140
2141     OPJ_UINT32 passno;
2142     OPJ_INT32 bpno;
2143     OPJ_UINT32 passtype;
2144     OPJ_INT32 nmsedec = 0;
2145     OPJ_INT32 max;
2146     OPJ_UINT32 i, j;
2147     OPJ_BYTE type = T1_TYPE_MQ;
2148     OPJ_FLOAT64 tempwmsedec;
2149
2150 #ifdef EXTRA_DEBUG
2151     printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
2152            cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
2153 #endif
2154
2155     mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2156
2157     max = 0;
2158     for (i = 0; i < t1->w; ++i) {
2159         for (j = 0; j < t1->h; ++j) {
2160             OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2161             max = opj_int_max(max, tmp);
2162         }
2163     }
2164
2165     cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2166                                       T1_NMSEDEC_FRACBITS) : 0;
2167
2168     bpno = (OPJ_INT32)(cblk->numbps - 1);
2169     passtype = 2;
2170
2171     opj_mqc_resetstates(mqc);
2172     opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2173     opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2174     opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2175     opj_mqc_init_enc(mqc, cblk->data);
2176
2177     for (passno = 0; bpno >= 0; ++passno) {
2178         opj_tcd_pass_t *pass = &cblk->passes[passno];
2179         type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2180                 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2181
2182         /* If the previous pass was terminating, we need to reset the encoder */
2183         if (passno > 0 && cblk->passes[passno - 1].term) {
2184             if (type == T1_TYPE_RAW) {
2185                 opj_mqc_bypass_init_enc(mqc);
2186             } else {
2187                 opj_mqc_restart_init_enc(mqc);
2188             }
2189         }
2190
2191         switch (passtype) {
2192         case 0:
2193             opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2194             break;
2195         case 1:
2196             opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2197             break;
2198         case 2:
2199             opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2200             /* code switch SEGMARK (i.e. SEGSYM) */
2201             if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2202                 opj_mqc_segmark_enc(mqc);
2203             }
2204             break;
2205         }
2206
2207         /* fixed_quality */
2208         tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2209                                         stepsize, numcomps, mct_norms, mct_numcomps) ;
2210         cumwmsedec += tempwmsedec;
2211         tile->distotile += tempwmsedec;
2212         pass->distortiondec = cumwmsedec;
2213
2214         if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
2215             /* If it is a terminated pass, terminate it */
2216             if (type == T1_TYPE_RAW) {
2217                 opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
2218             } else {
2219                 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2220                     opj_mqc_erterm_enc(mqc);
2221                 } else {
2222                     opj_mqc_flush(mqc);
2223                 }
2224             }
2225             pass->term = 1;
2226             pass->rate = opj_mqc_numbytes(mqc);
2227         } else {
2228             /* Non terminated pass */
2229             OPJ_UINT32 rate_extra_bytes;
2230             if (type == T1_TYPE_RAW) {
2231                 rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
2232                                        mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
2233             } else {
2234                 rate_extra_bytes = 3;
2235             }
2236             pass->term = 0;
2237             pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
2238         }
2239
2240         if (++passtype == 3) {
2241             passtype = 0;
2242             bpno--;
2243         }
2244
2245         /* Code-switch "RESET" */
2246         if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2247             opj_mqc_reset_enc(mqc);
2248         }
2249     }
2250
2251     cblk->totalpasses = passno;
2252
2253     if (cblk->totalpasses) {
2254         /* Make sure that pass rates are increasing */
2255         OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
2256         for (passno = cblk->totalpasses; passno > 0;) {
2257             opj_tcd_pass_t *pass = &cblk->passes[--passno];
2258             if (pass->rate > last_pass_rate) {
2259                 pass->rate = last_pass_rate;
2260             } else {
2261                 last_pass_rate = pass->rate;
2262             }
2263         }
2264     }
2265
2266     for (passno = 0; passno < cblk->totalpasses; passno++) {
2267         opj_tcd_pass_t *pass = &cblk->passes[passno];
2268
2269         /* Prevent generation of FF as last data byte of a pass*/
2270         /* For terminating passes, the flushing procedure ensured this already */
2271         assert(pass->rate > 0);
2272         if ((cblk->data[pass->rate - 1] == 0xFF)) {
2273             pass->rate--;
2274         }
2275         pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2276     }
2277
2278 #ifdef EXTRA_DEBUG
2279     printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
2280
2281     /* Check that there not 0xff >=0x90 sequences */
2282     if (cblk->totalpasses) {
2283         OPJ_UINT32 i;
2284         OPJ_UINT32 len = opj_mqc_numbytes(mqc);
2285         for (i = 1; i < len; ++i) {
2286             if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
2287                 printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);
2288                 abort();
2289             }
2290         }
2291     }
2292 #endif
2293 }
2294
2295 #if 0
2296 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
2297                                     opj_flag_t *flagsp,
2298                                     OPJ_INT32 *datap,
2299                                     OPJ_INT32 poshalf,
2300                                     OPJ_INT32 neghalf,
2301                                     OPJ_BYTE type,
2302                                     OPJ_UINT32 vsc)
2303 {
2304     OPJ_INT32  t;
2305     OPJ_UINT32 v, flag;
2306
2307     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
2308     opj_raw_t *raw = t1->raw;   /* RAW component */
2309
2310     flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2311            (*flagsp);
2312     if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2313         opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));  /* ESSAI */
2314         if (type == T1_TYPE_RAW) {
2315             v = opj_raw_decode(raw);
2316         } else {
2317             v = opj_mqc_decode(mqc);
2318         }
2319         t = v ? poshalf : neghalf;
2320         *datap += *datap < 0 ? -t : t;
2321         *flagsp |= T1_REFINE;
2322     }
2323 }               /* VSC and  BYPASS by Antonin  */
2324 #endif
2325
2326
2327
2328 #if 0
2329 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
2330                                     opj_flag_t *flagsp,
2331                                     OPJ_INT32 *datap,
2332                                     OPJ_UINT32 orient,
2333                                     OPJ_INT32 oneplushalf,
2334                                     OPJ_BYTE type,
2335                                     OPJ_UINT32 vsc)
2336 {
2337     OPJ_UINT32 v, flag;
2338
2339     opj_raw_t *raw = t1->raw;   /* RAW component */
2340     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
2341
2342     flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2343            (*flagsp);
2344     if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2345         if (type == T1_TYPE_RAW) {
2346             if (opj_raw_decode(raw)) {
2347                 v = opj_raw_decode(raw);    /* ESSAI */
2348                 *datap = v ? -oneplushalf : oneplushalf;
2349                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2350             }
2351         } else {
2352             opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2353             if (opj_mqc_decode(mqc)) {
2354                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2355                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2356                 *datap = v ? -oneplushalf : oneplushalf;
2357                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2358             }
2359         }
2360         *flagsp |= T1_VISIT;
2361     }
2362 }               /* VSC and  BYPASS by Antonin */
2363 #endif