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