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