Update to openjpeg r2944
[pdfium.git] / 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->data_stride) + 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->data_stride) + 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->data_stride) + 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->data_stride) + 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         /* encoder uses tile buffer, so no need to allocate */
1170         if (!t1->encoder) {
1171                 if(datasize > t1->datasize){
1172                         opj_aligned_free(t1->data);
1173                         t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1174                         if(!t1->data){
1175                                 /* FIXME event manager error callback */
1176                                 return OPJ_FALSE;
1177                         }
1178                         t1->datasize=datasize;
1179                 }
1180                 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1181         }
1182         t1->flags_stride=w+2;
1183         flagssize=t1->flags_stride * (h+2);
1184
1185         if(flagssize > t1->flagssize){
1186                 opj_aligned_free(t1->flags);
1187                 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1188                 if(!t1->flags){
1189                         /* FIXME event manager error callback */
1190                         return OPJ_FALSE;
1191                 }
1192                 t1->flagssize=flagssize;
1193         }
1194         memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1195
1196         t1->w=w;
1197         t1->h=h;
1198
1199         return OPJ_TRUE;
1200 }
1201
1202 /* ----------------------------------------------------------------------- */
1203
1204 /* ----------------------------------------------------------------------- */
1205 /**
1206  * Creates a new Tier 1 handle
1207  * and initializes the look-up tables of the Tier-1 coder/decoder
1208  * @return a new T1 handle if successful, returns NULL otherwise
1209 */
1210 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1211 {
1212         opj_t1_t *l_t1 = 00;
1213
1214         l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1215         if (!l_t1) {
1216                 return 00;
1217         }
1218
1219         /* create MQC and RAW handles */
1220         l_t1->mqc = opj_mqc_create();
1221         if (! l_t1->mqc) {
1222                 opj_t1_destroy(l_t1);
1223                 return 00;
1224         }
1225
1226         l_t1->raw = opj_raw_create();
1227         if (! l_t1->raw) {
1228                 opj_t1_destroy(l_t1);
1229                 return 00;
1230         }
1231         l_t1->encoder = isEncoder;
1232
1233         return l_t1;
1234 }
1235
1236
1237 /**
1238  * Destroys a previously created T1 handle
1239  *
1240  * @param p_t1 Tier 1 handle to destroy
1241 */
1242 void opj_t1_destroy(opj_t1_t *p_t1)
1243 {
1244         if (! p_t1) {
1245                 return;
1246         }
1247
1248         /* destroy MQC and RAW handles */
1249         opj_mqc_destroy(p_t1->mqc);
1250         p_t1->mqc = 00;
1251         opj_raw_destroy(p_t1->raw);
1252         p_t1->raw = 00;
1253         
1254         /* encoder uses tile buffer, so no need to free */
1255         if (!p_t1->encoder && p_t1->data) {
1256                 opj_aligned_free(p_t1->data);
1257                 p_t1->data = 00;
1258         }
1259
1260         if (p_t1->flags) {
1261                 opj_aligned_free(p_t1->flags);
1262                 p_t1->flags = 00;
1263         }
1264
1265         opj_free(p_t1);
1266 }
1267
1268 OPJ_BOOL opj_t1_decode_cblks(   opj_t1_t* t1,
1269                             opj_tcd_tilecomp_t* tilec,
1270                             opj_tccp_t* tccp
1271                             )
1272 {
1273         OPJ_UINT32 resno, bandno, precno, cblkno;
1274         OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1275
1276         for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1277                 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1278
1279                 for (bandno = 0; bandno < res->numbands; ++bandno) {
1280                         opj_tcd_band_t* restrict band = &res->bands[bandno];
1281
1282                         for (precno = 0; precno < res->pw * res->ph; ++precno) {
1283                                 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1284
1285                                 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1286                                         opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1287                                         OPJ_INT32* restrict datap;
1288                                         /*void* restrict tiledp;*/
1289                                         OPJ_UINT32 cblk_w, cblk_h;
1290                                         OPJ_INT32 x, y;
1291                                         OPJ_UINT32 i, j;
1292
1293                     if (OPJ_FALSE == opj_t1_decode_cblk(
1294                                             t1,
1295                                             cblk,
1296                                             band->bandno,
1297                                             (OPJ_UINT32)tccp->roishift,
1298                                             tccp->cblksty)) {
1299                             return OPJ_FALSE;
1300                     }
1301
1302                                         x = cblk->x0 - band->x0;
1303                                         y = cblk->y0 - band->y0;
1304                                         if (band->bandno & 1) {
1305                                                 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1306                                                 x += pres->x1 - pres->x0;
1307                                         }
1308                                         if (band->bandno & 2) {
1309                                                 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1310                                                 y += pres->y1 - pres->y0;
1311                                         }
1312
1313                                         datap=t1->data;
1314                                         cblk_w = t1->w;
1315                                         cblk_h = t1->h;
1316
1317                                         if (tccp->roishift) {
1318                                                 OPJ_INT32 thresh = 1 << tccp->roishift;
1319                                                 for (j = 0; j < cblk_h; ++j) {
1320                                                         for (i = 0; i < cblk_w; ++i) {
1321                                                                 OPJ_INT32 val = datap[(j * cblk_w) + i];
1322                                                                 OPJ_INT32 mag = abs(val);
1323                                                                 if (mag >= thresh) {
1324                                                                         mag >>= tccp->roishift;
1325                                                                         datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1326                                                                 }
1327                                                         }
1328                                                 }
1329                                         }
1330
1331                                         /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1332                                         if (tccp->qmfbid == 1) {
1333                         OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1334                                                 for (j = 0; j < cblk_h; ++j) {
1335                                                         for (i = 0; i < cblk_w; ++i) {
1336                                                                 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1337                                                                 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1338                                                         }
1339                                                 }
1340                                         } else {                /* if (tccp->qmfbid == 0) */
1341                         OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1342                                                 for (j = 0; j < cblk_h; ++j) {
1343                             OPJ_FLOAT32* restrict tiledp2 = tiledp;
1344                                                         for (i = 0; i < cblk_w; ++i) {
1345                                 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1346                                 *tiledp2 = tmp;
1347                                 datap++;
1348                                 tiledp2++;
1349                                                                 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1350                                                                 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1351
1352                                                         }
1353                             tiledp += tile_w;
1354                                                 }
1355                                         }
1356                     /*opj_free(cblk->data);
1357                                         opj_free(cblk->segs);*/
1358                                         /*cblk->segs = 00;*/
1359                                 } /* cblkno */
1360                 /*opj_free(precinct->cblks.dec);*/
1361                         } /* precno */
1362                 } /* bandno */
1363         } /* resno */
1364         return OPJ_TRUE;
1365 }
1366
1367
1368 OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1369                             opj_tcd_cblk_dec_t* cblk,
1370                             OPJ_UINT32 orient,
1371                             OPJ_UINT32 roishift,
1372                             OPJ_UINT32 cblksty)
1373 {
1374         opj_raw_t *raw = t1->raw;       /* RAW component */
1375         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1376
1377         OPJ_INT32 bpno;
1378         OPJ_UINT32 passtype;
1379         OPJ_UINT32 segno, passno;
1380         OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1381
1382         if(!opj_t1_allocate_buffers(
1383                                 t1,
1384                                 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1385                                 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1386         {
1387                 return OPJ_FALSE;
1388         }
1389
1390         bpno = (OPJ_INT32)(roishift + cblk->numbps - 1);
1391         passtype = 2;
1392
1393         opj_mqc_resetstates(mqc);
1394         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1395         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1396         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1397
1398         for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1399                 opj_tcd_seg_t *seg = &cblk->segs[segno];
1400
1401                 /* BYPASS mode */
1402                 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1403                 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1404                 if(seg->data == 00){
1405                         continue;
1406                 }
1407                 if (type == T1_TYPE_RAW) {
1408                         opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1409                 } else {
1410             if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1411                     return OPJ_FALSE;
1412             }
1413                 }
1414
1415                 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1416             switch (passtype) {
1417                 case 0:
1418                     if (type == T1_TYPE_RAW) {
1419                         opj_t1_dec_sigpass_raw(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1420                     } else {
1421                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1422                             opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, (OPJ_INT32)orient);
1423                         } else {
1424                             opj_t1_dec_sigpass_mqc(t1, bpno+1, (OPJ_INT32)orient);
1425                         }
1426                     }
1427                     break;
1428                 case 1:
1429                     if (type == T1_TYPE_RAW) {
1430                             opj_t1_dec_refpass_raw(t1, bpno+1, (OPJ_INT32)cblksty);
1431                     } else {
1432                         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1433                             opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1434                         } else {
1435                             opj_t1_dec_refpass_mqc(t1, bpno+1);
1436                         }
1437                     }
1438                     break;
1439                 case 2:
1440                     opj_t1_dec_clnpass(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1441                     break;
1442             }
1443
1444                         if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1445                                 opj_mqc_resetstates(mqc);
1446                                 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1447                                 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1448                                 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1449                         }
1450                         if (++passtype == 3) {
1451                                 passtype = 0;
1452                                 bpno--;
1453                         }
1454                 }
1455         }
1456         return OPJ_TRUE;
1457 }
1458
1459
1460
1461
1462 OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
1463                                 opj_tcd_tile_t *tile,
1464                                 opj_tcp_t *tcp,
1465                                 const OPJ_FLOAT64 * mct_norms
1466                                 )
1467 {
1468         OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1469
1470         tile->distotile = 0;            /* fixed_quality */
1471
1472         for (compno = 0; compno < tile->numcomps; ++compno) {
1473                 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1474                 opj_tccp_t* tccp = &tcp->tccps[compno];
1475                 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1476
1477                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1478                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1479
1480                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1481                                 opj_tcd_band_t* restrict band = &res->bands[bandno];
1482                 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1483
1484                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1485                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1486
1487                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1488                                                 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1489                                                 OPJ_INT32* restrict tiledp;
1490                                                 OPJ_UINT32 cblk_w;
1491                                                 OPJ_UINT32 cblk_h;
1492                                                 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1493
1494                                                 OPJ_INT32 x = cblk->x0 - band->x0;
1495                                                 OPJ_INT32 y = cblk->y0 - band->y0;
1496                                                 if (band->bandno & 1) {
1497                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1498                                                         x += pres->x1 - pres->x0;
1499                                                 }
1500                                                 if (band->bandno & 2) {
1501                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1502                                                         y += pres->y1 - pres->y0;
1503                                                 }
1504
1505                                                 if(!opj_t1_allocate_buffers(
1506                                                                         t1,
1507                                                                         (OPJ_UINT32)(cblk->x1 - cblk->x0),
1508                                                                         (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1509                                                 {
1510                                                         return OPJ_FALSE;
1511                                                 }
1512
1513                                                 cblk_w = t1->w;
1514                                                 cblk_h = t1->h;
1515                                                 tileLineAdvance = tile_w - cblk_w;
1516
1517                                                 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1518                                                 t1->data = tiledp;
1519                                                 t1->data_stride = tile_w;
1520                                                 if (tccp->qmfbid == 1) {
1521                                                         for (j = 0; j < cblk_h; ++j) {
1522                                                                 for (i = 0; i < cblk_w; ++i) {
1523                                                                         tiledp[tileIndex] <<= T1_NMSEDEC_FRACBITS;
1524                                                                         tileIndex++;
1525                                                                 }
1526                                                                 tileIndex += tileLineAdvance;
1527                                                         }
1528                                                 } else {                /* if (tccp->qmfbid == 0) */
1529                                                         for (j = 0; j < cblk_h; ++j) {
1530                                                                 for (i = 0; i < cblk_w; ++i) {
1531                                                                         OPJ_INT32 tmp = tiledp[tileIndex];
1532                                                                         tiledp[tileIndex] =
1533                                                                                 opj_int_fix_mul(
1534                                                                                 tmp,
1535                                                                                 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1536                                                                         tileIndex++;
1537                                                                 }
1538                                                                 tileIndex += tileLineAdvance;
1539                                                         }
1540                                                 }
1541
1542                                                 opj_t1_encode_cblk(
1543                                                                 t1,
1544                                                                 cblk,
1545                                                                 band->bandno,
1546                                                                 compno,
1547                                                                 tilec->numresolutions - 1 - resno,
1548                                                                 tccp->qmfbid,
1549                                                                 band->stepsize,
1550                                                                 tccp->cblksty,
1551                                                                 tile->numcomps,
1552                                                                 tile,
1553                                                                 mct_norms);
1554
1555                                         } /* cblkno */
1556                                 } /* precno */
1557                         } /* bandno */
1558                 } /* resno  */
1559         } /* compno  */
1560         return OPJ_TRUE;
1561 }
1562
1563 /** mod fixed_quality */
1564 void opj_t1_encode_cblk(opj_t1_t *t1,
1565                         opj_tcd_cblk_enc_t* cblk,
1566                         OPJ_UINT32 orient,
1567                         OPJ_UINT32 compno,
1568                         OPJ_UINT32 level,
1569                         OPJ_UINT32 qmfbid,
1570                         OPJ_FLOAT64 stepsize,
1571                         OPJ_UINT32 cblksty,
1572                         OPJ_UINT32 numcomps,
1573                         opj_tcd_tile_t * tile,
1574                         const OPJ_FLOAT64 * mct_norms)
1575 {
1576         OPJ_FLOAT64 cumwmsedec = 0.0;
1577
1578         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1579
1580         OPJ_UINT32 passno;
1581         OPJ_INT32 bpno;
1582         OPJ_UINT32 passtype;
1583         OPJ_INT32 nmsedec = 0;
1584         OPJ_INT32 max;
1585         OPJ_UINT32 i, j;
1586         OPJ_BYTE type = T1_TYPE_MQ;
1587         OPJ_FLOAT64 tempwmsedec;
1588
1589         max = 0;
1590         for (i = 0; i < t1->w; ++i) {
1591                 for (j = 0; j < t1->h; ++j) {
1592                         OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1593                         max = opj_int_max(max, tmp);
1594                 }
1595         }
1596
1597         cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1598
1599         bpno = (OPJ_INT32)(cblk->numbps - 1);
1600         passtype = 2;
1601
1602         opj_mqc_resetstates(mqc);
1603         opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1604         opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1605         opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1606         opj_mqc_init_enc(mqc, cblk->data);
1607
1608         for (passno = 0; bpno >= 0; ++passno) {
1609                 opj_tcd_pass_t *pass = &cblk->passes[passno];
1610                 OPJ_UINT32 correction = 3;
1611                 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1612
1613                 switch (passtype) {
1614                         case 0:
1615                                 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1616                                 break;
1617                         case 1:
1618                                 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1619                                 break;
1620                         case 2:
1621                                 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1622                                 /* code switch SEGMARK (i.e. SEGSYM) */
1623                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1624                                         opj_mqc_segmark_enc(mqc);
1625                                 break;
1626                 }
1627
1628                 /* fixed_quality */
1629                 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1630                 cumwmsedec += tempwmsedec;
1631                 tile->distotile += tempwmsedec;
1632
1633                 /* Code switch "RESTART" (i.e. TERMALL) */
1634                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1635                         if (type == T1_TYPE_RAW) {
1636                                 opj_mqc_flush(mqc);
1637                                 correction = 1;
1638                                 /* correction = mqc_bypass_flush_enc(); */
1639                         } else {                        /* correction = mqc_restart_enc(); */
1640                                 opj_mqc_flush(mqc);
1641                                 correction = 1;
1642                         }
1643                         pass->term = 1;
1644                 } else {
1645                         if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1646                                 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1647                                 if (type == T1_TYPE_RAW) {
1648                                         opj_mqc_flush(mqc);
1649                                         correction = 1;
1650                                         /* correction = mqc_bypass_flush_enc(); */
1651                                 } else {                /* correction = mqc_restart_enc(); */
1652                                         opj_mqc_flush(mqc);
1653                                         correction = 1;
1654                                 }
1655                                 pass->term = 1;
1656                         } else {
1657                                 pass->term = 0;
1658                         }
1659                 }
1660
1661                 if (++passtype == 3) {
1662                         passtype = 0;
1663                         bpno--;
1664                 }
1665
1666                 if (pass->term && bpno > 0) {
1667                         type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1668                         if (type == T1_TYPE_RAW)
1669                                 opj_mqc_bypass_init_enc(mqc);
1670                         else
1671                                 opj_mqc_restart_init_enc(mqc);
1672                 }
1673
1674                 pass->distortiondec = cumwmsedec;
1675                 pass->rate = opj_mqc_numbytes(mqc) + correction;        /* FIXME */
1676
1677                 /* Code-switch "RESET" */
1678                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1679                         opj_mqc_reset_enc(mqc);
1680         }
1681
1682         /* Code switch "ERTERM" (i.e. PTERM) */
1683         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1684                 opj_mqc_erterm_enc(mqc);
1685         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1686                 opj_mqc_flush(mqc);
1687
1688         cblk->totalpasses = passno;
1689
1690         for (passno = 0; passno<cblk->totalpasses; passno++) {
1691                 opj_tcd_pass_t *pass = &cblk->passes[passno];
1692                 if (pass->rate > opj_mqc_numbytes(mqc))
1693                         pass->rate = opj_mqc_numbytes(mqc);
1694                 /*Preventing generation of FF as last data byte of a pass*/
1695                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1696                         pass->rate--;
1697                 }
1698                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1699         }
1700 }
1701
1702 #if 0
1703 void opj_t1_dec_refpass_step(   opj_t1_t *t1,
1704                                 opj_flag_t *flagsp,
1705                                 OPJ_INT32 *datap,
1706                                 OPJ_INT32 poshalf,
1707                                 OPJ_INT32 neghalf,
1708                                 OPJ_BYTE type,
1709                                 OPJ_UINT32 vsc)
1710 {
1711         OPJ_INT32  t;
1712         OPJ_UINT32 v,flag;
1713
1714         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1715         opj_raw_t *raw = t1->raw;       /* RAW component */
1716
1717         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1718         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1719                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag));      /* ESSAI */
1720                 if (type == T1_TYPE_RAW) {
1721                         v = opj_raw_decode(raw);
1722                 } else {
1723                         v = opj_mqc_decode(mqc);
1724                 }
1725                 t = v ? poshalf : neghalf;
1726                 *datap += *datap < 0 ? -t : t;
1727                 *flagsp |= T1_REFINE;
1728         }
1729 }                               /* VSC and  BYPASS by Antonin  */
1730 #endif
1731
1732
1733
1734 #if 0
1735 void opj_t1_dec_sigpass_step(   opj_t1_t *t1,
1736                                 opj_flag_t *flagsp,
1737                                 OPJ_INT32 *datap,
1738                                 OPJ_UINT32 orient,
1739                                 OPJ_INT32 oneplushalf,
1740                                 OPJ_BYTE type,
1741                                 OPJ_UINT32 vsc)
1742 {
1743         OPJ_UINT32 v, flag;
1744
1745         opj_raw_t *raw = t1->raw;       /* RAW component */
1746         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
1747
1748         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1749         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1750                 if (type == T1_TYPE_RAW) {
1751                         if (opj_raw_decode(raw)) {
1752                                 v = opj_raw_decode(raw);        /* ESSAI */
1753                                 *datap = v ? -oneplushalf : oneplushalf;
1754                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1755                         }
1756                 } else {
1757                         opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1758                         if (opj_mqc_decode(mqc)) {
1759                                 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1760                                 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1761                                 *datap = v ? -oneplushalf : oneplushalf;
1762                                 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1763                         }
1764                 }
1765                 *flagsp |= T1_VISIT;
1766         }
1767 }                               /* VSC and  BYPASS by Antonin */
1768 #endif