07e358a52b3ce5a0051c43530693d2d872e87919
[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 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
43 /*@{*/
44
45 /** @name Local static functions */
46 /*@{*/
47
48 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
49 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
50 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
52 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
53 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
54 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
55 /**
56 Encode significant pass
57 */
58 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
59                                     opj_flag_t *flagsp,
60                                     OPJ_INT32 *datap,
61                                     OPJ_UINT32 orient,
62                                     OPJ_INT32 bpno,
63                                     OPJ_INT32 one,
64                                     OPJ_INT32 *nmsedec,
65                                     OPJ_BYTE type,
66                                     OPJ_UINT32 vsc);
67
68 /**
69 Decode significant pass
70 */
71 #if 0
72 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
73                                     opj_flag_t *flagsp,
74                                     OPJ_INT32 *datap,
75                                     OPJ_UINT32 orient,
76                                     OPJ_INT32 oneplushalf,
77                                     OPJ_BYTE type,
78                                     OPJ_UINT32 vsc);
79 #endif
80
81 static INLINE void opj_t1_dec_sigpass_step_raw(
82                 opj_t1_t *t1,
83                 opj_flag_t *flagsp,
84                 OPJ_INT32 *datap,
85                 OPJ_INT32 orient,
86                 OPJ_INT32 oneplushalf,
87                 OPJ_INT32 vsc);
88 static INLINE void opj_t1_dec_sigpass_step_mqc(
89                 opj_t1_t *t1,
90                 opj_flag_t *flagsp,
91                 OPJ_INT32 *datap,
92                 OPJ_INT32 orient,
93                 OPJ_INT32 oneplushalf);
94 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
95                 opj_t1_t *t1,
96                 opj_flag_t *flagsp,
97                 OPJ_INT32 *datap,
98                 OPJ_INT32 orient,
99                 OPJ_INT32 oneplushalf,
100                 OPJ_INT32 vsc);
101
102
103 /**
104 Encode significant pass
105 */
106 static void opj_t1_enc_sigpass( opj_t1_t *t1,
107                                 OPJ_INT32 bpno,
108                                 OPJ_UINT32 orient,
109                                 OPJ_INT32 *nmsedec,
110                                 OPJ_BYTE type,
111                                 OPJ_UINT32 cblksty);
112
113 /**
114 Decode significant pass
115 */
116 static void opj_t1_dec_sigpass_raw(
117                 opj_t1_t *t1,
118                 OPJ_INT32 bpno,
119                 OPJ_INT32 orient,
120                 OPJ_INT32 cblksty);
121 static void opj_t1_dec_sigpass_mqc_vsc(
122                 opj_t1_t *t1,
123                 OPJ_INT32 bpno,
124                 OPJ_INT32 orient);
125
126
127
128 /**
129 Encode refinement pass
130 */
131 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
132                                     opj_flag_t *flagsp,
133                                     OPJ_INT32 *datap,
134                                     OPJ_INT32 bpno,
135                                     OPJ_INT32 one,
136                                     OPJ_INT32 *nmsedec,
137                                     OPJ_BYTE type,
138                                     OPJ_UINT32 vsc);
139
140
141 /**
142 Encode refinement pass
143 */
144 static void opj_t1_enc_refpass( opj_t1_t *t1,
145                                 OPJ_INT32 bpno,
146                                 OPJ_INT32 *nmsedec,
147                                 OPJ_BYTE type,
148                                 OPJ_UINT32 cblksty);
149
150 /**
151 Decode refinement pass
152 */
153 static void opj_t1_dec_refpass_raw(
154                 opj_t1_t *t1,
155                 OPJ_INT32 bpno,
156                 OPJ_INT32 cblksty);
157 static void opj_t1_dec_refpass_mqc_vsc(
158                 opj_t1_t *t1,
159                 OPJ_INT32 bpno);
160
161
162 /**
163 Decode refinement pass
164 */
165 #if 0
166 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
167                                     opj_flag_t *flagsp,
168                                     OPJ_INT32 *datap,
169                                     OPJ_INT32 poshalf,
170                                     OPJ_INT32 neghalf,
171                                     OPJ_BYTE type,
172                                     OPJ_UINT32 vsc);
173 #endif
174
175 static INLINE void  opj_t1_dec_refpass_step_raw(
176                 opj_t1_t *t1,
177                 opj_flag_t *flagsp,
178                 OPJ_INT32 *datap,
179                 OPJ_INT32 poshalf,
180                 OPJ_INT32 neghalf,
181                 OPJ_INT32 vsc);
182 static INLINE void opj_t1_dec_refpass_step_mqc(
183                 opj_t1_t *t1,
184                 opj_flag_t *flagsp,
185                 OPJ_INT32 *datap,
186                 OPJ_INT32 poshalf,
187                 OPJ_INT32 neghalf);
188 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
189                 opj_t1_t *t1,
190                 opj_flag_t *flagsp,
191                 OPJ_INT32 *datap,
192                 OPJ_INT32 poshalf,
193                 OPJ_INT32 neghalf,
194                 OPJ_INT32 vsc);
195
196
197
198 /**
199 Encode clean-up pass
200 */
201 static void opj_t1_enc_clnpass_step(
202                 opj_t1_t *t1,
203                 opj_flag_t *flagsp,
204                 OPJ_INT32 *datap,
205                 OPJ_UINT32 orient,
206                 OPJ_INT32 bpno,
207                 OPJ_INT32 one,
208                 OPJ_INT32 *nmsedec,
209                 OPJ_UINT32 partial,
210                 OPJ_UINT32 vsc);
211 /**
212 Decode clean-up pass
213 */
214 static void opj_t1_dec_clnpass_step_partial(
215                 opj_t1_t *t1,
216                 opj_flag_t *flagsp,
217                 OPJ_INT32 *datap,
218                 OPJ_INT32 orient,
219                 OPJ_INT32 oneplushalf);
220 static void opj_t1_dec_clnpass_step(
221                 opj_t1_t *t1,
222                 opj_flag_t *flagsp,
223                 OPJ_INT32 *datap,
224                 OPJ_INT32 orient,
225                 OPJ_INT32 oneplushalf);
226 static void opj_t1_dec_clnpass_step_vsc(
227                 opj_t1_t *t1,
228                 opj_flag_t *flagsp,
229                 OPJ_INT32 *datap,
230                 OPJ_INT32 orient,
231                 OPJ_INT32 oneplushalf,
232                 OPJ_INT32 partial,
233                 OPJ_INT32 vsc);
234 /**
235 Encode clean-up pass
236 */
237 static void opj_t1_enc_clnpass(
238                 opj_t1_t *t1,
239                 OPJ_INT32 bpno,
240                 OPJ_UINT32 orient,
241                 OPJ_INT32 *nmsedec,
242                 OPJ_UINT32 cblksty);
243 /**
244 Decode clean-up pass
245 */
246 static void opj_t1_dec_clnpass(
247                 opj_t1_t *t1,
248                 OPJ_INT32 bpno,
249                 OPJ_INT32 orient,
250                 OPJ_INT32 cblksty);
251
252 static OPJ_FLOAT64 opj_t1_getwmsedec(
253                 OPJ_INT32 nmsedec,
254                 OPJ_UINT32 compno,
255                 OPJ_UINT32 level,
256                 OPJ_UINT32 orient,
257                 OPJ_INT32 bpno,
258                 OPJ_UINT32 qmfbid,
259                 OPJ_FLOAT64 stepsize,
260                 OPJ_UINT32 numcomps,
261                 const OPJ_FLOAT64 * mct_norms,
262                 OPJ_UINT32 mct_numcomps);
263
264 static void opj_t1_encode_cblk( opj_t1_t *t1,
265                                 opj_tcd_cblk_enc_t* cblk,
266                                 OPJ_UINT32 orient,
267                                 OPJ_UINT32 compno,
268                                 OPJ_UINT32 level,
269                                 OPJ_UINT32 qmfbid,
270                                 OPJ_FLOAT64 stepsize,
271                                 OPJ_UINT32 cblksty,
272                                 OPJ_UINT32 numcomps,
273                                 opj_tcd_tile_t * tile,
274                                 const OPJ_FLOAT64 * mct_norms,
275                                 OPJ_UINT32 mct_numcomps);
276
277 /**
278 Decode 1 code-block
279 @param t1 T1 handle
280 @param cblk Code-block coding parameters
281 @param orient
282 @param roishift Region of interest shifting value
283 @param cblksty Code-block style
284 */
285 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
286                                     opj_tcd_cblk_dec_t* cblk,
287                                     OPJ_UINT32 orient,
288                                     OPJ_UINT32 roishift,
289                                     OPJ_UINT32 cblksty);
290
291 static OPJ_BOOL opj_t1_allocate_buffers(   opj_t1_t *t1,
292                                     OPJ_UINT32 w,
293                                     OPJ_UINT32 h);
294
295 /*@}*/
296
297 /*@}*/
298
299 /* ----------------------------------------------------------------------- */
300
301 static OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
302         return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
303 }
304
305 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
306         return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
307 }
308
309 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
310         OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
311         OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
312         return (tmp2);
313 }
314
315 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
316         return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
317 }
318
319 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
320         if (bitpos > 0) {
321                 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
322         }
323         
324         return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
325 }
326
327 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
328         if (bitpos > 0) {
329                 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
330         }
331
332     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
333 }
334
335 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
336         opj_flag_t *np = flagsp - stride;
337         opj_flag_t *sp = flagsp + stride;
338
339         /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
340         /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
341         /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
342
343         opj_flag_t flag_N = T1_SIG_N | (T1_SIG_N << (4 * s));
344
345         np[-1] |= T1_SIG_SE;
346         np[0]  |= flag_N << 2;
347         np[1]  |= T1_SIG_SW;
348
349         flagsp[-1] |= flag_N << 1;
350         flagsp[0]  |= T1_SIG;
351         flagsp[1]  |= flag_N << 3;
352
353         sp[-1] |= T1_SIG_NE;
354         sp[0]  |= flag_N;
355         sp[1]  |= T1_SIG_NW;
356 }
357
358 static void opj_t1_enc_sigpass_step(   opj_t1_t *t1,
359                                 opj_flag_t *flagsp,
360                                 OPJ_INT32 *datap,
361                                 OPJ_UINT32 orient,
362                                 OPJ_INT32 bpno,
363                                 OPJ_INT32 one,
364                                 OPJ_INT32 *nmsedec,
365                                 OPJ_BYTE type,
366                                 OPJ_UINT32 vsc
367                                 )
368 {
369         OPJ_INT32 v;
370     OPJ_UINT32 flag;
371         
372         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
373         
374         flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
375         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
376                 v = (opj_int_abs(*datap) & one) ? 1 : 0;
377                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));       /* ESSAI */
378                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
379                         opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
380                 } else {
381                         opj_mqc_encode(mqc, (OPJ_UINT32)v);
382                 }
383                 if (v) {
384                         v = *datap < 0 ? 1 : 0;
385                         *nmsedec +=     opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
386                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));       /* ESSAI */
387                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
388                                 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
389                         } else {
390                                 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
391                         }
392                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
393                 }
394                 *flagsp |= T1_VISIT;
395         }
396 }
397
398
399 static INLINE void opj_t1_dec_sigpass_step_raw(
400                 opj_t1_t *t1,
401                 opj_flag_t *flagsp,
402                 OPJ_INT32 *datap,
403                 OPJ_INT32 orient,
404                 OPJ_INT32 oneplushalf,
405                 OPJ_INT32 vsc)
406 {
407         OPJ_INT32 v, flag;
408         opj_raw_t *raw = t1->raw;       /* RAW component */
409         OPJ_ARG_NOT_USED(orient);
410        
411         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
412         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
413                         if (opj_raw_decode(raw)) {
414                                 v = (OPJ_INT32)opj_raw_decode(raw);    /* ESSAI */
415                                 *datap = v ? -oneplushalf : oneplushalf;
416                                 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
417                         }
418                 *flagsp |= T1_VISIT;
419         }
420 }      
421
422 static INLINE void opj_t1_dec_sigpass_step_mqc(
423                 opj_t1_t *t1,
424                 opj_flag_t *flagsp,
425                 OPJ_INT32 *datap,
426                 OPJ_INT32 orient,
427                 OPJ_INT32 oneplushalf)
428 {
429         OPJ_INT32 v, flag;
430        
431         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
432        
433         flag = *flagsp;
434         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
435                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
436                         if (opj_mqc_decode(mqc)) {
437                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
438                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
439                                 *datap = v ? -oneplushalf : oneplushalf;
440                                 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
441                         }
442                 *flagsp |= T1_VISIT;
443         }
444 }                               /* VSC and  BYPASS by Antonin */
445
446 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
447                 opj_t1_t *t1,
448                 opj_flag_t *flagsp,
449                 OPJ_INT32 *datap,
450                 OPJ_INT32 orient,
451                 OPJ_INT32 oneplushalf,
452                 OPJ_INT32 vsc)
453 {
454         OPJ_INT32 v, flag;
455        
456         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
457        
458         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
459         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
460                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
461                 if (opj_mqc_decode(mqc)) {
462                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
463                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
464                         *datap = v ? -oneplushalf : oneplushalf;
465                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
466                 }
467                 *flagsp |= T1_VISIT;
468         }
469 }                               /* VSC and  BYPASS by Antonin */
470
471
472
473 static void opj_t1_enc_sigpass(opj_t1_t *t1,
474                         OPJ_INT32 bpno,
475                         OPJ_UINT32 orient,
476                         OPJ_INT32 *nmsedec,
477                         OPJ_BYTE type,
478                         OPJ_UINT32 cblksty
479                         )
480 {
481         OPJ_UINT32 i, j, k, vsc; 
482     OPJ_INT32 one;
483
484         *nmsedec = 0;
485         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
486         for (k = 0; k < t1->h; k += 4) {
487                 for (i = 0; i < t1->w; ++i) {
488                         for (j = k; j < k + 4 && j < t1->h; ++j) {
489                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
490                                 opj_t1_enc_sigpass_step(
491                                                 t1,
492                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
493                                                 &t1->data[(j * t1->data_stride) + i],
494                                                 orient,
495                                                 bpno,
496                                                 one,
497                                                 nmsedec,
498                                                 type,
499                                                 vsc);
500                         }
501                 }
502         }
503 }
504
505 static void opj_t1_dec_sigpass_raw(
506                 opj_t1_t *t1,
507                 OPJ_INT32 bpno,
508                 OPJ_INT32 orient,
509                 OPJ_INT32 cblksty)
510 {
511         OPJ_INT32 one, half, oneplushalf, vsc;
512         OPJ_UINT32 i, j, k; 
513         one = 1 << bpno;
514         half = one >> 1;
515         oneplushalf = one | half;
516         for (k = 0; k < t1->h; k += 4) {
517                 for (i = 0; i < t1->w; ++i) {
518                         for (j = k; j < k + 4 && j < t1->h; ++j) {
519                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
520                                 opj_t1_dec_sigpass_step_raw(
521                                                 t1,
522                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
523                                                 &t1->data[(j * t1->w) + i],
524                                                 orient,
525                                                 oneplushalf,
526                                                 vsc);
527                         }
528                 }
529         }
530 }                               /* VSC and  BYPASS by Antonin */
531
532 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, orient, w, h, flags_stride) \
533 { \
534         OPJ_INT32 one, half, oneplushalf; \
535         OPJ_UINT32 i, j, k; \
536         OPJ_INT32 *data1 = t1->data; \
537         opj_flag_t *flags1 = &t1->flags[1]; \
538         one = 1 << bpno; \
539         half = one >> 1; \
540         oneplushalf = one | half; \
541         for (k = 0; k < (h & ~3u); k += 4) { \
542                 for (i = 0; i < w; ++i) { \
543                         OPJ_INT32 *data2 = data1 + i; \
544                         opj_flag_t *flags2 = flags1 + i; \
545                         flags2 += flags_stride; \
546                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
547                         data2 += w; \
548                         flags2 += flags_stride; \
549                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
550                         data2 += w; \
551                         flags2 += flags_stride; \
552                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
553                         data2 += w; \
554                         flags2 += flags_stride; \
555                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
556                         data2 += w; \
557                 } \
558                 data1 += w << 2; \
559                 flags1 += flags_stride << 2; \
560         } \
561         for (i = 0; i < w; ++i) { \
562                 OPJ_INT32 *data2 = data1 + i; \
563                 opj_flag_t *flags2 = flags1 + i; \
564                 for (j = k; j < h; ++j) { \
565                         flags2 += flags_stride; \
566                         opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
567                         data2 += w; \
568                 } \
569         } \
570 }
571
572 static void opj_t1_dec_sigpass_mqc_64x64(
573                 opj_t1_t *t1,
574                 OPJ_INT32 bpno,
575                 OPJ_INT32 orient)
576 {
577         opj_t1_dec_sigpass_mqc_internal(t1, bpno, orient, 64, 64, 66);
578 }
579
580 static void opj_t1_dec_sigpass_mqc_generic(
581                 opj_t1_t *t1,
582                 OPJ_INT32 bpno,
583                 OPJ_INT32 orient)
584 {
585         opj_t1_dec_sigpass_mqc_internal(t1, bpno, orient, t1->w, t1->h, t1->flags_stride);
586 }
587
588 /* VSC and  BYPASS by Antonin */
589 static void opj_t1_dec_sigpass_mqc_vsc(
590                 opj_t1_t *t1,
591                 OPJ_INT32 bpno,
592                 OPJ_INT32 orient)
593 {
594         OPJ_INT32 one, half, oneplushalf, vsc;
595         OPJ_UINT32 i, j, k;
596         one = 1 << bpno;
597         half = one >> 1;
598         oneplushalf = one | half;
599         for (k = 0; k < t1->h; k += 4) {
600                 for (i = 0; i < t1->w; ++i) {
601                         for (j = k; j < k + 4 && j < t1->h; ++j) {
602                                 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
603                                 opj_t1_dec_sigpass_step_mqc_vsc(
604                                                 t1,
605                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
606                                                 &t1->data[(j * t1->w) + i],
607                                                 orient,
608                                                 oneplushalf,
609                                                 vsc);
610                         }
611                 }
612         }
613 }                               /* VSC and  BYPASS by Antonin */
614
615
616
617 static void opj_t1_enc_refpass_step(   opj_t1_t *t1,
618                                 opj_flag_t *flagsp,
619                                 OPJ_INT32 *datap,
620                                 OPJ_INT32 bpno,
621                                 OPJ_INT32 one,
622                                 OPJ_INT32 *nmsedec,
623                                 OPJ_BYTE type,
624                                 OPJ_UINT32 vsc)
625 {
626         OPJ_INT32 v;
627         OPJ_UINT32 flag;
628         
629         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
630         
631         flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
632         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
633                 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
634                 v = (opj_int_abs(*datap) & one) ? 1 : 0;
635                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
636                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
637                         opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
638                 } else {
639                         opj_mqc_encode(mqc, (OPJ_UINT32)v);
640                 }
641                 *flagsp |= T1_REFINE;
642         }
643 }
644
645 static INLINE void opj_t1_dec_refpass_step_raw(
646                 opj_t1_t *t1,
647                 opj_flag_t *flagsp,
648                 OPJ_INT32 *datap,
649                 OPJ_INT32 poshalf,
650                 OPJ_INT32 neghalf,
651                 OPJ_INT32 vsc)
652 {
653         OPJ_INT32 v, t, flag;
654        
655         opj_raw_t *raw = t1->raw;       /* RAW component */
656        
657         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
658         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
659                         v = (OPJ_INT32)opj_raw_decode(raw);
660                 t = v ? poshalf : neghalf;
661                 *datap += *datap < 0 ? -t : t;
662                 *flagsp |= T1_REFINE;
663         }
664 }                               /* VSC and  BYPASS by Antonin  */
665
666 static INLINE void opj_t1_dec_refpass_step_mqc(
667                 opj_t1_t *t1,
668                 opj_flag_t *flagsp,
669                 OPJ_INT32 *datap,
670                 OPJ_INT32 poshalf,
671                 OPJ_INT32 neghalf)
672 {
673         OPJ_INT32 v, t, flag;
674        
675         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
676        
677         flag = *flagsp;
678         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
679                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag));      /* ESSAI */
680                         v = opj_mqc_decode(mqc);
681                 t = v ? poshalf : neghalf;
682                 *datap += *datap < 0 ? -t : t;
683                 *flagsp |= T1_REFINE;
684                 }
685 }                               /* VSC and  BYPASS by Antonin  */
686
687 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
688                 opj_t1_t *t1,
689                 opj_flag_t *flagsp,
690                 OPJ_INT32 *datap,
691                 OPJ_INT32 poshalf,
692                 OPJ_INT32 neghalf,
693                 OPJ_INT32 vsc)
694 {
695         OPJ_INT32 v, t, flag;
696        
697         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
698        
699         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
700         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
701                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag));      /* ESSAI */
702                 v = opj_mqc_decode(mqc);
703                 t = v ? poshalf : neghalf;
704                 *datap += *datap < 0 ? -t : t;
705                 *flagsp |= T1_REFINE;
706         }
707 }                               /* VSC and  BYPASS by Antonin  */
708
709
710 static void opj_t1_enc_refpass(
711                 opj_t1_t *t1,
712                 OPJ_INT32 bpno,
713                 OPJ_INT32 *nmsedec,
714                 OPJ_BYTE type,
715                 OPJ_UINT32 cblksty)
716 {
717         OPJ_UINT32 i, j, k, vsc;
718     OPJ_INT32 one;
719
720         *nmsedec = 0;
721         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
722         for (k = 0; k < t1->h; k += 4) {
723                 for (i = 0; i < t1->w; ++i) {
724                         for (j = k; j < k + 4 && j < t1->h; ++j) {
725                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
726                                 opj_t1_enc_refpass_step(
727                                                 t1,
728                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
729                                                 &t1->data[(j * t1->data_stride) + i],
730                                                 bpno,
731                                                 one,
732                                                 nmsedec,
733                                                 type,
734                                                 vsc);
735                         }
736                 }
737         }
738 }
739
740 static void opj_t1_dec_refpass_raw(
741                 opj_t1_t *t1,
742                 OPJ_INT32 bpno,
743                 OPJ_INT32 cblksty)
744 {
745         OPJ_INT32 one, poshalf, neghalf;
746         OPJ_UINT32 i, j, k;
747         OPJ_INT32 vsc;
748         one = 1 << bpno;
749         poshalf = one >> 1;
750         neghalf = bpno > 0 ? -poshalf : -1;
751         for (k = 0; k < t1->h; k += 4) {
752                 for (i = 0; i < t1->w; ++i) {
753                         for (j = k; j < k + 4 && j < t1->h; ++j) {
754                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
755                                 opj_t1_dec_refpass_step_raw(
756                                                 t1,
757                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
758                                                 &t1->data[(j * t1->w) + i],
759                                                 poshalf,
760                                                 neghalf,
761                                                 vsc);
762                         }
763                 }
764         }
765 }                               /* VSC and  BYPASS by Antonin */
766
767 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
768 { \
769         OPJ_INT32 one, poshalf, neghalf; \
770         OPJ_UINT32 i, j, k; \
771         OPJ_INT32 *data1 = t1->data; \
772         opj_flag_t *flags1 = &t1->flags[1]; \
773         one = 1 << bpno; \
774         poshalf = one >> 1; \
775         neghalf = bpno > 0 ? -poshalf : -1; \
776         for (k = 0; k < (h & ~3u); k += 4) { \
777                 for (i = 0; i < w; ++i) { \
778                         OPJ_INT32 *data2 = data1 + i; \
779                         opj_flag_t *flags2 = flags1 + i; \
780                         flags2 += flags_stride; \
781                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
782                         data2 += w; \
783                         flags2 += flags_stride; \
784                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
785                         data2 += w; \
786                         flags2 += flags_stride; \
787                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
788                         data2 += w; \
789                         flags2 += flags_stride; \
790                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
791                         data2 += w; \
792                 } \
793                 data1 += w << 2; \
794                 flags1 += flags_stride << 2; \
795         } \
796         for (i = 0; i < w; ++i) { \
797                 OPJ_INT32 *data2 = data1 + i; \
798                 opj_flag_t *flags2 = flags1 + i; \
799                 for (j = k; j < h; ++j) { \
800                         flags2 += flags_stride; \
801                         opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
802                         data2 += w; \
803                 } \
804         } \
805 }
806
807 static void opj_t1_dec_refpass_mqc_64x64(
808                 opj_t1_t *t1,
809                 OPJ_INT32 bpno)
810 {
811         opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
812 }
813
814 static void opj_t1_dec_refpass_mqc_generic(
815                 opj_t1_t *t1,
816                 OPJ_INT32 bpno)
817 {
818         opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
819 }
820
821 /* VSC and  BYPASS by Antonin */
822 static void opj_t1_dec_refpass_mqc_vsc(
823                 opj_t1_t *t1,
824                 OPJ_INT32 bpno)
825 {
826         OPJ_INT32 one, poshalf, neghalf;
827         OPJ_UINT32 i, j, k;
828         OPJ_INT32 vsc;
829         one = 1 << bpno;
830         poshalf = one >> 1;
831         neghalf = bpno > 0 ? -poshalf : -1;
832         for (k = 0; k < t1->h; k += 4) {
833                 for (i = 0; i < t1->w; ++i) {
834                         for (j = k; j < k + 4 && j < t1->h; ++j) {
835                                 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
836                                 opj_t1_dec_refpass_step_mqc_vsc(
837                                                 t1,
838                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
839                                                 &t1->data[(j * t1->w) + i],
840                                                 poshalf,
841                                                 neghalf,
842                                                 vsc);
843                         }
844                 }
845         }
846 }                               /* VSC and  BYPASS by Antonin */
847
848
849 static void opj_t1_enc_clnpass_step(
850                 opj_t1_t *t1,
851                 opj_flag_t *flagsp,
852                 OPJ_INT32 *datap,
853                 OPJ_UINT32 orient,
854                 OPJ_INT32 bpno,
855                 OPJ_INT32 one,
856                 OPJ_INT32 *nmsedec,
857                 OPJ_UINT32 partial,
858                 OPJ_UINT32 vsc)
859 {
860         OPJ_INT32 v;
861         OPJ_UINT32 flag;
862         
863         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
864         
865         flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
866         if (partial) {
867                 goto LABEL_PARTIAL;
868         }
869         if (!(*flagsp & (T1_SIG | T1_VISIT))) {
870                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
871                 v = (opj_int_abs(*datap) & one) ? 1 : 0;
872                 opj_mqc_encode(mqc, (OPJ_UINT32)v);
873                 if (v) {
874 LABEL_PARTIAL:
875                         *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
876                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
877                         v = *datap < 0 ? 1 : 0;
878                         opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
879                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
880                 }
881         }
882         *flagsp &= ~T1_VISIT;
883 }
884
885 static void opj_t1_dec_clnpass_step_partial(
886                 opj_t1_t *t1,
887                 opj_flag_t *flagsp,
888                 OPJ_INT32 *datap,
889                 OPJ_INT32 orient,
890                 OPJ_INT32 oneplushalf)
891 {
892         OPJ_INT32 v, flag;
893         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
894         
895         OPJ_ARG_NOT_USED(orient);
896         
897         flag = *flagsp;
898         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
899         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
900         *datap = v ? -oneplushalf : oneplushalf;
901         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
902         *flagsp &= ~T1_VISIT;
903 }                               /* VSC and  BYPASS by Antonin */
904
905 static void opj_t1_dec_clnpass_step(
906                 opj_t1_t *t1,
907                 opj_flag_t *flagsp,
908                 OPJ_INT32 *datap,
909                 OPJ_INT32 orient,
910                 OPJ_INT32 oneplushalf)
911 {
912         OPJ_INT32 v, flag;
913         
914         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
915         
916         flag = *flagsp;
917         if (!(flag & (T1_SIG | T1_VISIT))) {
918                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
919                 if (opj_mqc_decode(mqc)) {
920                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
921                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
922                         *datap = v ? -oneplushalf : oneplushalf;
923                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
924                 }
925         }
926         *flagsp &= ~T1_VISIT;
927 }                               /* VSC and  BYPASS by Antonin */
928
929 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
930         opj_t1_t *t1,
931         opj_flag_t *flagsp,
932         OPJ_INT32 *datap,
933         OPJ_INT32 orient,
934         OPJ_INT32 oneplushalf)
935 {
936     OPJ_INT32 v;
937     OPJ_INT32 flag;
938
939     opj_mqc_t *mqc = t1->mqc;   /* MQC component */
940
941     flag = *flagsp;
942     /*if (!(flag & (T1_SIG | T1_VISIT)))*/
943     {
944         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
945         if (opj_mqc_decode(mqc)) {
946             opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
947             v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
948             *datap = v ? -oneplushalf : oneplushalf;
949             opj_t1_updateflags(flagsp, v, t1->flags_stride);
950         }
951     }
952     /*flagsp &= ~T1_VISIT;*/
953 }
954
955 static void opj_t1_dec_clnpass_step_vsc(
956                 opj_t1_t *t1,
957                 opj_flag_t *flagsp,
958                 OPJ_INT32 *datap,
959                 OPJ_INT32 orient,
960                 OPJ_INT32 oneplushalf,
961                 OPJ_INT32 partial,
962                 OPJ_INT32 vsc)
963 {
964         OPJ_INT32 v, flag;
965         
966         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
967         
968         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
969         if (partial) {
970                 goto LABEL_PARTIAL;
971         }
972         if (!(flag & (T1_SIG | T1_VISIT))) {
973                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
974                 if (opj_mqc_decode(mqc)) {
975 LABEL_PARTIAL:
976                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
977                         v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
978                         *datap = v ? -oneplushalf : oneplushalf;
979                         opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
980                 }
981         }
982         *flagsp &= ~T1_VISIT;
983 }
984
985 static void opj_t1_enc_clnpass(
986                 opj_t1_t *t1,
987                 OPJ_INT32 bpno,
988                 OPJ_UINT32 orient,
989                 OPJ_INT32 *nmsedec,
990                 OPJ_UINT32 cblksty)
991 {
992         OPJ_UINT32 i, j, k;
993         OPJ_INT32 one;
994         OPJ_UINT32 agg, runlen, vsc;
995         
996         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
997         
998         *nmsedec = 0;
999         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1000         for (k = 0; k < t1->h; k += 4) {
1001                 for (i = 0; i < t1->w; ++i) {
1002                         if (k + 3 < t1->h) {
1003                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1004                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1005                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1006                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1007                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
1008                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1009                                 } else {
1010                                         agg = !((MACRO_t1_flags(1 + k,1 + i) | 
1011                                                     MACRO_t1_flags(1 + k + 1,1 + i) |
1012                                                     MACRO_t1_flags(1 + k + 2,1 + i) | 
1013                                                      MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1014                                 }
1015                         } else {
1016                                 agg = 0;
1017                         }
1018                         if (agg) {
1019                                 for (runlen = 0; runlen < 4; ++runlen) {
1020                                         if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1021                                                 break;
1022                                 }
1023                                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1024                                 opj_mqc_encode(mqc, runlen != 4);
1025                                 if (runlen == 4) {
1026                                         continue;
1027                                 }
1028                                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1029                                 opj_mqc_encode(mqc, runlen >> 1);
1030                                 opj_mqc_encode(mqc, runlen & 1);
1031                         } else {
1032                                 runlen = 0;
1033                         }
1034                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1035                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1036                                 opj_t1_enc_clnpass_step(
1037                                                 t1,
1038                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1039                                                 &t1->data[(j * t1->data_stride) + i],
1040                                                 orient,
1041                                                 bpno,
1042                                                 one,
1043                                                 nmsedec,
1044                                                 agg && (j == k + runlen),
1045                                                 vsc);
1046                         }
1047                 }
1048         }
1049 }
1050
1051 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1052
1053 #define opj_t1_dec_clnpass_internal(t1, bpno, orient, cblksty, w, h, flags_stride) \
1054 { \
1055         OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1056     OPJ_UINT32 i, j, k; \
1057         OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1058          \
1059         opj_mqc_t *mqc = t1->mqc;       /* MQC component */ \
1060          \
1061         one = 1 << bpno; \
1062         half = one >> 1; \
1063         oneplushalf = one | half; \
1064         if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1065         for (k = 0; k < h; k += 4) { \
1066                 for (i = 0; i < w; ++i) { \
1067                         if (k + 3 < h) { \
1068                                         agg = !(MACRO_t1_flags_internal(1 + k,1 + i,flags_stride) & (T1_SIG | T1_VISIT | T1_SIG_OTH) \
1069                                                 || MACRO_t1_flags_internal(1 + k + 1,1 + i,flags_stride) & (T1_SIG | T1_VISIT | T1_SIG_OTH) \
1070                                                 || MACRO_t1_flags_internal(1 + k + 2,1 + i,flags_stride) & (T1_SIG | T1_VISIT | T1_SIG_OTH) \
1071                                                 || (MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1072                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); \
1073                                 } else { \
1074                                 agg = 0; \
1075                         } \
1076                         if (agg) { \
1077                                 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1078                                 if (!opj_mqc_decode(mqc)) { \
1079                                         continue; \
1080                                 } \
1081                                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1082                                 runlen = opj_mqc_decode(mqc); \
1083                                 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1084                         } else { \
1085                                 runlen = 0; \
1086                         } \
1087                         for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1088                                         vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1089                                         opj_t1_dec_clnpass_step_vsc( \
1090                                                 t1, \
1091                                                 &t1->flags[((j+1) * flags_stride) + i + 1], \
1092                                                 &t1->data[(j * w) + i], \
1093                                                 orient, \
1094                                                 oneplushalf, \
1095                                                 agg && (j == k + (OPJ_UINT32)runlen), \
1096                                                 vsc); \
1097                         } \
1098                 } \
1099         } \
1100         } else { \
1101                 OPJ_INT32 *data1 = t1->data; \
1102                 opj_flag_t *flags1 = &t1->flags[1]; \
1103                 for (k = 0; k < (h & ~3u); k += 4) { \
1104                         for (i = 0; i < w; ++i) { \
1105                                 OPJ_INT32 *data2 = data1 + i; \
1106                                 opj_flag_t *flags2 = flags1 + i; \
1107                                 agg = !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1108                                                         MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1109                                                         MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1110                                                         MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); \
1111                                 if (agg) { \
1112                                         opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1113                                         if (!opj_mqc_decode(mqc)) { \
1114                                                 continue; \
1115                                         } \
1116                                         opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1117                                         runlen = opj_mqc_decode(mqc); \
1118                                         runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1119                                         flags2 += (OPJ_UINT32)runlen * flags_stride; \
1120                                         data2 += (OPJ_UINT32)runlen * w; \
1121                                         for (j = (OPJ_UINT32)runlen; j < 4 && j < h; ++j) { \
1122                                                 flags2 += flags_stride; \
1123                                                 if (agg && (j == (OPJ_UINT32)runlen)) { \
1124                                                         opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf); \
1125                                                 } else { \
1126                                                         opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf); \
1127                                                 } \
1128                                                 data2 += w; \
1129                                         } \
1130                                 } else { \
1131                                         opj_flag_t flag; \
1132                                         flags2 += flags_stride; \
1133                                         flag = *flags2; \
1134                                         if (!(flag & (T1_SIG | T1_VISIT))) \
1135                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1136                                         *flags2 &= ~T1_VISIT; \
1137                                         data2 += w; \
1138                                         flags2 += flags_stride; \
1139                                         flag = *flags2; \
1140                                         if (!(flag & (T1_SIG | T1_VISIT))) \
1141                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1142                                         *flags2 &= ~T1_VISIT; \
1143                                         data2 += w; \
1144                                         flags2 += flags_stride; \
1145                                         flag = *flags2; \
1146                                         if (!(flag & (T1_SIG | T1_VISIT))) \
1147                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1148                                         *flags2 &= ~T1_VISIT; \
1149                                         data2 += w; \
1150                                         flags2 += flags_stride; \
1151                                         flag = *flags2; \
1152                                         if (!(flag & (T1_SIG | T1_VISIT))) \
1153                                                 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1154                                         *flags2 &= ~T1_VISIT; \
1155                                         data2 += w; \
1156                                 } \
1157                         } \
1158                         data1 += w << 2; \
1159                         flags1 += flags_stride << 2; \
1160                 } \
1161                 for (i = 0; i < w; ++i) { \
1162                         OPJ_INT32 *data2 = data1 + i; \
1163                         opj_flag_t *flags2 = flags1 + i; \
1164                         for (j = k; j < h; ++j) { \
1165                                 flags2 += flags_stride; \
1166                                 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf); \
1167                                 data2 += w; \
1168                         } \
1169                 } \
1170         } \
1171  \
1172         if (segsym) { \
1173                 OPJ_INT32 v = 0; \
1174                 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1175                 v = opj_mqc_decode(mqc); \
1176                 v = (v << 1) | opj_mqc_decode(mqc); \
1177                 v = (v << 1) | opj_mqc_decode(mqc); \
1178                 v = (v << 1) | opj_mqc_decode(mqc); \
1179                 /* \
1180                 if (v!=0xa) { \
1181                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1182                 } \
1183                 */ \
1184         } \
1185 }                               /* VSC and  BYPASS by Antonin */
1186
1187 static void opj_t1_dec_clnpass_64x64(
1188                 opj_t1_t *t1,
1189                 OPJ_INT32 bpno,
1190                 OPJ_INT32 orient,
1191                 OPJ_INT32 cblksty)
1192 {
1193         opj_t1_dec_clnpass_internal(t1, bpno, orient, cblksty, 64, 64, 66);
1194 }
1195
1196 static void opj_t1_dec_clnpass_generic(
1197                 opj_t1_t *t1,
1198                 OPJ_INT32 bpno,
1199                 OPJ_INT32 orient,
1200                 OPJ_INT32 cblksty)
1201 {
1202         opj_t1_dec_clnpass_internal(t1, bpno, orient, cblksty, t1->w, t1->h, t1->flags_stride);
1203 }
1204
1205
1206 /** mod fixed_quality */
1207 static OPJ_FLOAT64 opj_t1_getwmsedec(
1208                 OPJ_INT32 nmsedec,
1209                 OPJ_UINT32 compno,
1210                 OPJ_UINT32 level,
1211                 OPJ_UINT32 orient,
1212                 OPJ_INT32 bpno,
1213                 OPJ_UINT32 qmfbid,
1214                 OPJ_FLOAT64 stepsize,
1215                 OPJ_UINT32 numcomps,
1216                 const OPJ_FLOAT64 * mct_norms,
1217                 OPJ_UINT32 mct_numcomps)
1218 {
1219         OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1220         OPJ_ARG_NOT_USED(numcomps);
1221
1222         if (mct_norms && (compno < mct_numcomps)) {
1223                 w1 = mct_norms[compno];
1224         }
1225
1226         if (qmfbid == 1) {
1227                 w2 = opj_dwt_getnorm(level, orient);
1228         } else {        /* if (qmfbid == 0) */
1229                 w2 = opj_dwt_getnorm_real(level, orient);
1230         }
1231
1232         wmsedec = w1 * w2 * stepsize * (1 << bpno);
1233         wmsedec *= wmsedec * nmsedec / 8192.0;
1234
1235         return wmsedec;
1236 }
1237
1238 static OPJ_BOOL opj_t1_allocate_buffers(
1239                 opj_t1_t *t1,
1240                 OPJ_UINT32 w,
1241                 OPJ_UINT32 h)
1242 {
1243         OPJ_UINT32 datasize=w * h;
1244         OPJ_UINT32 flagssize;
1245
1246         /* encoder uses tile buffer, so no need to allocate */
1247         if (!t1->encoder) {
1248                 if(datasize > t1->datasize){
1249                         opj_aligned_free(t1->data);
1250                         t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1251                         if(!t1->data){
1252                                 /* FIXME event manager error callback */
1253                                 return OPJ_FALSE;
1254                         }
1255                         t1->datasize=datasize;
1256                 }
1257                 /* memset first arg is declared to never be null by gcc */
1258                 if (t1->data != NULL) {
1259                         memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1260                 }
1261         }
1262         t1->flags_stride=w+2;
1263         flagssize=t1->flags_stride * (h+2);
1264
1265         if(flagssize > t1->flagssize){
1266                 opj_aligned_free(t1->flags);
1267                 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1268                 if(!t1->flags){
1269                         /* FIXME event manager error callback */
1270                         return OPJ_FALSE;
1271                 }
1272                 t1->flagssize=flagssize;
1273         }
1274         memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1275
1276         t1->w=w;
1277         t1->h=h;
1278
1279         return OPJ_TRUE;
1280 }
1281
1282 /* ----------------------------------------------------------------------- */
1283
1284 /* ----------------------------------------------------------------------- */
1285 /**
1286  * Creates a new Tier 1 handle
1287  * and initializes the look-up tables of the Tier-1 coder/decoder
1288  * @return a new T1 handle if successful, returns NULL otherwise
1289 */
1290 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1291 {
1292         opj_t1_t *l_t1 = 00;
1293
1294         l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1295         if (!l_t1) {
1296                 return 00;
1297         }
1298
1299         /* create MQC and RAW handles */
1300         l_t1->mqc = opj_mqc_create();
1301         if (! l_t1->mqc) {
1302                 opj_t1_destroy(l_t1);
1303                 return 00;
1304         }
1305
1306         l_t1->raw = opj_raw_create();
1307         if (! l_t1->raw) {
1308                 opj_t1_destroy(l_t1);
1309                 return 00;
1310         }
1311         l_t1->encoder = isEncoder;
1312
1313         return l_t1;
1314 }
1315
1316
1317 /**
1318  * Destroys a previously created T1 handle
1319  *
1320  * @param p_t1 Tier 1 handle to destroy
1321 */
1322 void opj_t1_destroy(opj_t1_t *p_t1)
1323 {
1324         if (! p_t1) {
1325                 return;
1326         }
1327
1328         /* destroy MQC and RAW handles */
1329         opj_mqc_destroy(p_t1->mqc);
1330         p_t1->mqc = 00;
1331         opj_raw_destroy(p_t1->raw);
1332         p_t1->raw = 00;
1333         
1334         /* encoder uses tile buffer, so no need to free */
1335         if (!p_t1->encoder && p_t1->data) {
1336                 opj_aligned_free(p_t1->data);
1337                 p_t1->data = 00;
1338         }
1339
1340         if (p_t1->flags) {
1341                 opj_aligned_free(p_t1->flags);
1342                 p_t1->flags = 00;
1343         }
1344
1345         opj_free(p_t1);
1346 }
1347
1348 OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
1349                             opj_tcd_tilecomp_t* tilec,
1350                             opj_tccp_t* tccp
1351                             )
1352 {
1353         OPJ_UINT32 resno, bandno, precno, cblkno;
1354         OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1355
1356         for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1357                 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1358
1359                 for (bandno = 0; bandno < res->numbands; ++bandno) {
1360                         opj_tcd_band_t* restrict band = &res->bands[bandno];
1361
1362                         for (precno = 0; precno < res->pw * res->ph; ++precno) {
1363                                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1364
1365                                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1366                                         opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1367                                         OPJ_INT32* restrict datap;
1368                                         OPJ_UINT32 cblk_w, cblk_h;
1369                                         OPJ_INT32 x, y;
1370                                         OPJ_UINT32 i, j;
1371
1372                     if (OPJ_FALSE == opj_t1_decode_cblk(
1373                                             t1,
1374                                             cblk,
1375                                             band->bandno,
1376                                             (OPJ_UINT32)tccp->roishift,
1377                                             tccp->cblksty)) {
1378                             return OPJ_FALSE;
1379                     }
1380
1381                                         x = cblk->x0 - band->x0;
1382                                         y = cblk->y0 - band->y0;
1383                                         if (band->bandno & 1) {
1384                                                 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1385                                                 x += pres->x1 - pres->x0;
1386                                         }
1387                                         if (band->bandno & 2) {
1388                                                 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1389                                                 y += pres->y1 - pres->y0;
1390                                         }
1391
1392                                         datap=t1->data;
1393                                         cblk_w = t1->w;
1394                                         cblk_h = t1->h;
1395
1396                                         if (tccp->roishift) {
1397                                                 OPJ_INT32 thresh = 1 << tccp->roishift;
1398                                                 for (j = 0; j < cblk_h; ++j) {
1399                                                         for (i = 0; i < cblk_w; ++i) {
1400                                                                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1401                                                                 OPJ_INT32 mag = abs(val);
1402                                                                 if (mag >= thresh) {
1403                                                                         mag >>= tccp->roishift;
1404                                                                         datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1405                                                                 }
1406                                                         }
1407                                                 }
1408                                         }
1409                                         if (tccp->qmfbid == 1) {
1410                         OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1411                                                 for (j = 0; j < cblk_h; ++j) {
1412                                                         for (i = 0; i < cblk_w; ++i) {
1413                                                                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1414                                                                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp/2;
1415                                                         }
1416                                                 }
1417                                         } else {                /* if (tccp->qmfbid == 0) */
1418                         OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1419                                                 for (j = 0; j < cblk_h; ++j) {
1420                             OPJ_FLOAT32* restrict tiledp2 = tiledp;
1421                                                         for (i = 0; i < cblk_w; ++i) {
1422                                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1423                                 *tiledp2 = tmp;
1424                                 datap++;
1425                                 tiledp2++;
1426                                                         }
1427                             tiledp += tile_w;
1428                                                 }
1429                                         }
1430                                 } /* cblkno */
1431                         } /* precno */
1432                 } /* bandno */
1433         } /* resno */
1434         return OPJ_TRUE;
1435 }
1436
1437
1438 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1439                             opj_tcd_cblk_dec_t* cblk,
1440                             OPJ_UINT32 orient,
1441                             OPJ_UINT32 roishift,
1442                             OPJ_UINT32 cblksty)
1443 {
1444         opj_raw_t *raw = t1->raw;       /* RAW component */
1445         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1446
1447         OPJ_INT32 bpno_plus_one;
1448         OPJ_UINT32 passtype;
1449         OPJ_UINT32 segno, passno;
1450         OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1451
1452         if(!opj_t1_allocate_buffers(
1453                                 t1,
1454                                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1455                                 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1456         {
1457                 return OPJ_FALSE;
1458         }
1459
1460         bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1461         passtype = 2;
1462
1463         opj_mqc_resetstates(mqc);
1464         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1465         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1466         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1467
1468         for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1469                 opj_tcd_seg_t *seg = &cblk->segs[segno];
1470
1471                 /* BYPASS mode */
1472                 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1473                 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1474                 if(seg->data == 00){
1475                         continue;
1476                 }
1477                 if (type == T1_TYPE_RAW) {
1478                         opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1479                 } else {
1480             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1481                     return OPJ_FALSE;
1482             }
1483                 }
1484
1485                 if( t1->w == 64 && t1->h == 64 )
1486                 {
1487                   for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1488                           switch (passtype) {
1489                                   case 0:
1490                                           if (type == T1_TYPE_RAW) {
1491                                                   opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1492                                           } else {
1493                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1494                                                           opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one, (OPJ_INT32)orient);
1495                                                   } else {
1496                                                           opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one, (OPJ_INT32)orient);
1497                                                   }
1498                                           }
1499                                           break;
1500                                   case 1:
1501                                           if (type == T1_TYPE_RAW) {
1502                                                           opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1503                                           } else {
1504                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1505                                                           opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1506                                                   } else {
1507                                                           opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1508                                                   }
1509                                           }
1510                                           break;
1511                                   case 2:
1512                                           opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1513                                           break;
1514                           }
1515
1516                           if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1517                                   opj_mqc_resetstates(mqc);
1518                                   opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1519                                   opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1520                                   opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1521                           }
1522                           if (++passtype == 3) {
1523                                   passtype = 0;
1524                                   bpno_plus_one--;
1525                           }
1526                   }
1527                 }
1528                 else
1529                 {
1530                   for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1531                           switch (passtype) {
1532                                   case 0:
1533                                           if (type == T1_TYPE_RAW) {
1534                                                   opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1535                                           } else {
1536                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1537                                                           opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one, (OPJ_INT32)orient);
1538                                                   } else {
1539                                                           opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one, (OPJ_INT32)orient);
1540                                                   }
1541                                           }
1542                                           break;
1543                                   case 1:
1544                                           if (type == T1_TYPE_RAW) {
1545                                                           opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1546                                           } else {
1547                                                   if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1548                                                           opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1549                                                   } else {
1550                                                           opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1551                                                   }
1552                                           }
1553                                           break;
1554                                   case 2:
1555                                           opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1556                                           break;
1557                           }
1558
1559                           if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1560                                   opj_mqc_resetstates(mqc);
1561                                   opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1562                                   opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1563                                   opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1564                           }
1565                           if (++passtype == 3) {
1566                                   passtype = 0;
1567                                   bpno_plus_one--;
1568                           }
1569                   }
1570                 }
1571         }
1572     return OPJ_TRUE;
1573 }
1574
1575
1576
1577
1578 OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
1579                                 opj_tcd_tile_t *tile,
1580                                 opj_tcp_t *tcp,
1581                                 const OPJ_FLOAT64 * mct_norms,
1582                                 OPJ_UINT32 mct_numcomps
1583                                 )
1584 {
1585         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1586
1587         tile->distotile = 0;            /* fixed_quality */
1588
1589         for (compno = 0; compno < tile->numcomps; ++compno) {
1590                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1591                 opj_tccp_t* tccp = &tcp->tccps[compno];
1592                 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1593
1594                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1595                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1596
1597                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1598                                 opj_tcd_band_t* restrict band = &res->bands[bandno];
1599                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1600
1601                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1602                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1603
1604                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1605                                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1606                                                 OPJ_INT32* restrict tiledp;
1607                                                 OPJ_UINT32 cblk_w;
1608                                                 OPJ_UINT32 cblk_h;
1609                                                 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1610
1611                                                 OPJ_INT32 x = cblk->x0 - band->x0;
1612                                                 OPJ_INT32 y = cblk->y0 - band->y0;
1613                                                 if (band->bandno & 1) {
1614                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1615                                                         x += pres->x1 - pres->x0;
1616                                                 }
1617                                                 if (band->bandno & 2) {
1618                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1619                                                         y += pres->y1 - pres->y0;
1620                                                 }
1621
1622                                                 if(!opj_t1_allocate_buffers(
1623                                                                         t1,
1624                                                                         (OPJ_UINT32)(cblk->x1 - cblk->x0),
1625                                                                         (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1626                                                 {
1627                                                         return OPJ_FALSE;
1628                                                 }
1629
1630                                                 cblk_w = t1->w;
1631                                                 cblk_h = t1->h;
1632                                                 tileLineAdvance = tile_w - cblk_w;
1633
1634                                                 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1635                                                 t1->data = tiledp;
1636                                                 t1->data_stride = tile_w;
1637                                                 if (tccp->qmfbid == 1) {
1638                                                         for (j = 0; j < cblk_h; ++j) {
1639                                                                 for (i = 0; i < cblk_w; ++i) {
1640                                                                         tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1641                                                                         tileIndex++;
1642                                                                 }
1643                                                                 tileIndex += tileLineAdvance;
1644                                                         }
1645                                                 } else {                /* if (tccp->qmfbid == 0) */
1646                                                         for (j = 0; j < cblk_h; ++j) {
1647                                                                 for (i = 0; i < cblk_w; ++i) {
1648                                                                         OPJ_INT32 tmp = tiledp[tileIndex];
1649                                                                         tiledp[tileIndex] =
1650                                                                                 opj_int_fix_mul_t1(
1651                                                                                 tmp,
1652                                                                                 bandconst);
1653                                                                         tileIndex++;
1654                                                                 }
1655                                                                 tileIndex += tileLineAdvance;
1656                                                         }
1657                                                 }
1658
1659                                                 opj_t1_encode_cblk(
1660                                                                 t1,
1661                                                                 cblk,
1662                                                                 band->bandno,
1663                                                                 compno,
1664                                                                 tilec->numresolutions - 1 - resno,
1665                                                                 tccp->qmfbid,
1666                                                                 band->stepsize,
1667                                                                 tccp->cblksty,
1668                                                                 tile->numcomps,
1669                                                                 tile,
1670                                                                 mct_norms,
1671                                                                 mct_numcomps);
1672
1673                                         } /* cblkno */
1674                                 } /* precno */
1675                         } /* bandno */
1676                 } /* resno  */
1677         } /* compno  */
1678         return OPJ_TRUE;
1679 }
1680
1681 /** mod fixed_quality */
1682 static void opj_t1_encode_cblk(opj_t1_t *t1,
1683                         opj_tcd_cblk_enc_t* cblk,
1684                         OPJ_UINT32 orient,
1685                         OPJ_UINT32 compno,
1686                         OPJ_UINT32 level,
1687                         OPJ_UINT32 qmfbid,
1688                         OPJ_FLOAT64 stepsize,
1689                         OPJ_UINT32 cblksty,
1690                         OPJ_UINT32 numcomps,
1691                         opj_tcd_tile_t * tile,
1692                         const OPJ_FLOAT64 * mct_norms,
1693                         OPJ_UINT32 mct_numcomps)
1694 {
1695         OPJ_FLOAT64 cumwmsedec = 0.0;
1696
1697         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1698
1699         OPJ_UINT32 passno;
1700         OPJ_INT32 bpno;
1701         OPJ_UINT32 passtype;
1702         OPJ_INT32 nmsedec = 0;
1703         OPJ_INT32 max;
1704         OPJ_UINT32 i, j;
1705         OPJ_BYTE type = T1_TYPE_MQ;
1706         OPJ_FLOAT64 tempwmsedec;
1707
1708         max = 0;
1709         for (i = 0; i < t1->w; ++i) {
1710                 for (j = 0; j < t1->h; ++j) {
1711                         OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1712                         max = opj_int_max(max, tmp);
1713                 }
1714         }
1715
1716         cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1717
1718         bpno = (OPJ_INT32)(cblk->numbps - 1);
1719         passtype = 2;
1720
1721         opj_mqc_resetstates(mqc);
1722         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1723         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1724         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1725         opj_mqc_init_enc(mqc, cblk->data);
1726
1727         for (passno = 0; bpno >= 0; ++passno) {
1728                 opj_tcd_pass_t *pass = &cblk->passes[passno];
1729                 OPJ_UINT32 correction = 3;
1730                 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1731
1732                 switch (passtype) {
1733                         case 0:
1734                                 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1735                                 break;
1736                         case 1:
1737                                 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1738                                 break;
1739                         case 2:
1740                                 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1741                                 /* code switch SEGMARK (i.e. SEGSYM) */
1742                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1743                                         opj_mqc_segmark_enc(mqc);
1744                                 break;
1745                 }
1746
1747                 /* fixed_quality */
1748                 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
1749                 cumwmsedec += tempwmsedec;
1750                 tile->distotile += tempwmsedec;
1751
1752                 /* Code switch "RESTART" (i.e. TERMALL) */
1753                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1754                         if (type == T1_TYPE_RAW) {
1755                                 opj_mqc_flush(mqc);
1756                                 correction = 1;
1757                                 /* correction = mqc_bypass_flush_enc(); */
1758                         } else {                        /* correction = mqc_restart_enc(); */
1759                                 opj_mqc_flush(mqc);
1760                                 correction = 1;
1761                         }
1762                         pass->term = 1;
1763                 } else {
1764                         if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1765                                 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1766                                 if (type == T1_TYPE_RAW) {
1767                                         opj_mqc_flush(mqc);
1768                                         correction = 1;
1769                                         /* correction = mqc_bypass_flush_enc(); */
1770                                 } else {                /* correction = mqc_restart_enc(); */
1771                                         opj_mqc_flush(mqc);
1772                                         correction = 1;
1773                                 }
1774                                 pass->term = 1;
1775                         } else {
1776                                 pass->term = 0;
1777                         }
1778                 }
1779
1780                 if (++passtype == 3) {
1781                         passtype = 0;
1782                         bpno--;
1783                 }
1784
1785                 if (pass->term && bpno > 0) {
1786                         type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1787                         if (type == T1_TYPE_RAW)
1788                                 opj_mqc_bypass_init_enc(mqc);
1789                         else
1790                                 opj_mqc_restart_init_enc(mqc);
1791                 }
1792
1793                 pass->distortiondec = cumwmsedec;
1794                 pass->rate = opj_mqc_numbytes(mqc) + correction;        /* FIXME */
1795
1796                 /* Code-switch "RESET" */
1797                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1798                         opj_mqc_reset_enc(mqc);
1799         }
1800
1801         /* Code switch "ERTERM" (i.e. PTERM) */
1802         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1803                 opj_mqc_erterm_enc(mqc);
1804         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1805                 opj_mqc_flush(mqc);
1806
1807         cblk->totalpasses = passno;
1808
1809         for (passno = 0; passno<cblk->totalpasses; passno++) {
1810                 opj_tcd_pass_t *pass = &cblk->passes[passno];
1811                 if (pass->rate > opj_mqc_numbytes(mqc))
1812                         pass->rate = opj_mqc_numbytes(mqc);
1813                 /*Preventing generation of FF as last data byte of a pass*/
1814                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1815                         pass->rate--;
1816                 }
1817                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1818         }
1819 }
1820
1821 #if 0
1822 static void opj_t1_dec_refpass_step(   opj_t1_t *t1,
1823                                 opj_flag_t *flagsp,
1824                                 OPJ_INT32 *datap,
1825                                 OPJ_INT32 poshalf,
1826                                 OPJ_INT32 neghalf,
1827                                 OPJ_BYTE type,
1828                                 OPJ_UINT32 vsc)
1829 {
1830         OPJ_INT32  t;
1831         OPJ_UINT32 v,flag;
1832
1833         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1834         opj_raw_t *raw = t1->raw;       /* RAW component */
1835
1836         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1837         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1838                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
1839                 if (type == T1_TYPE_RAW) {
1840                         v = opj_raw_decode(raw);
1841                 } else {
1842                         v = opj_mqc_decode(mqc);
1843                 }
1844                 t = v ? poshalf : neghalf;
1845                 *datap += *datap < 0 ? -t : t;
1846                 *flagsp |= T1_REFINE;
1847         }
1848 }                               /* VSC and  BYPASS by Antonin  */
1849 #endif
1850
1851
1852
1853 #if 0
1854 static void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
1855                                 opj_flag_t *flagsp,
1856                                 OPJ_INT32 *datap,
1857                                 OPJ_UINT32 orient,
1858                                 OPJ_INT32 oneplushalf,
1859                                 OPJ_BYTE type,
1860                                 OPJ_UINT32 vsc)
1861 {
1862         OPJ_UINT32 v, flag;
1863
1864         opj_raw_t *raw = t1->raw;       /* RAW component */
1865         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1866
1867         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1868         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1869                 if (type == T1_TYPE_RAW) {
1870                         if (opj_raw_decode(raw)) {
1871                                 v = opj_raw_decode(raw);        /* ESSAI */
1872                                 *datap = v ? -oneplushalf : oneplushalf;
1873                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1874                         }
1875                 } else {
1876                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1877                         if (opj_mqc_decode(mqc)) {
1878                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1879                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1880                                 *datap = v ? -oneplushalf : oneplushalf;
1881                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1882                         }
1883                 }
1884                 *flagsp |= T1_VISIT;
1885         }
1886 }                               /* VSC and  BYPASS by Antonin */
1887 #endif