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