Consolidated some calls to mqc_setcurctx. Cleaned up t1_generate_luts to output the...
[openjpeg.git] / libopenjpeg / 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 char t1_getctxno_zc(int f, int orient);
43 static char t1_getctxno_sc(int f);
44 static char t1_getctxno_mag(int f);
45 static char t1_getspb(int f);
46 static short t1_getnmsedec_sig(int x, int bitpos);
47 static short t1_getnmsedec_ref(int x, int bitpos);
48 static void t1_updateflags(flag_t *flagsp, int s, int stride);
49 /**
50 Encode significant pass
51 */
52 static void t1_enc_sigpass_step(
53                 opj_t1_t *t1,
54                 flag_t *flagsp,
55                 int *datap,
56                 int orient,
57                 int bpno,
58                 int one,
59                 int *nmsedec,
60                 char type,
61                 int vsc);
62 /**
63 Decode significant pass
64 */
65 static void t1_dec_sigpass_step(
66                 opj_t1_t *t1,
67                 flag_t *flagsp,
68                 int *datap,
69                 int orient,
70                 int oneplushalf,
71                 char type,
72                 int vsc);
73 /**
74 Encode significant pass
75 */
76 static void t1_enc_sigpass(
77                 opj_t1_t *t1,
78                 int bpno,
79                 int orient,
80                 int *nmsedec,
81                 char type,
82                 int cblksty);
83 /**
84 Decode significant pass
85 */
86 static void t1_dec_sigpass(
87                 opj_t1_t *t1,
88                 int bpno,
89                 int orient,
90                 char type,
91                 int cblksty);
92 /**
93 Encode refinement pass
94 */
95 static void t1_enc_refpass_step(
96                 opj_t1_t *t1,
97                 flag_t *flagsp,
98                 int *datap,
99                 int bpno,
100                 int one,
101                 int *nmsedec,
102                 char type,
103                 int vsc);
104 /**
105 Decode refinement pass
106 */
107 static void t1_dec_refpass_step(
108                 opj_t1_t *t1,
109                 flag_t *flagsp,
110                 int *datap,
111                 int poshalf,
112                 int neghalf,
113                 char type,
114                 int vsc);
115 /**
116 Encode refinement pass
117 */
118 static void t1_enc_refpass(
119                 opj_t1_t *t1,
120                 int bpno,
121                 int *nmsedec,
122                 char type,
123                 int cblksty);
124 /**
125 Decode refinement pass
126 */
127 static void t1_dec_refpass(
128                 opj_t1_t *t1,
129                 int bpno,
130                 char type,
131                 int cblksty);
132 /**
133 Encode clean-up pass
134 */
135 static void t1_enc_clnpass_step(
136                 opj_t1_t *t1,
137                 flag_t *flagsp,
138                 int *datap,
139                 int orient,
140                 int bpno,
141                 int one,
142                 int *nmsedec,
143                 int partial,
144                 int vsc);
145 /**
146 Decode clean-up pass
147 */
148 static void t1_dec_clnpass_step(
149                 opj_t1_t *t1,
150                 flag_t *flagsp,
151                 int *datap,
152                 int orient,
153                 int oneplushalf,
154                 int partial,
155                 int vsc);
156 /**
157 Encode clean-up pass
158 */
159 static void t1_enc_clnpass(
160                 opj_t1_t *t1,
161                 int bpno,
162                 int orient,
163                 int *nmsedec,
164                 int cblksty);
165 /**
166 Decode clean-up pass
167 */
168 static void t1_dec_clnpass(
169                 opj_t1_t *t1,
170                 int bpno,
171                 int orient,
172                 int cblksty);
173 static double t1_getwmsedec(
174                 int nmsedec,
175                 int compno,
176                 int level,
177                 int orient,
178                 int bpno,
179                 int qmfbid,
180                 double stepsize,
181                 int numcomps);
182 /**
183 Encode 1 code-block
184 @param t1 T1 handle
185 @param cblk Code-block coding parameters
186 @param orient
187 @param compno Component number
188 @param level
189 @param qmfbid
190 @param stepsize
191 @param cblksty Code-block style
192 @param numcomps
193 @param tile
194 */
195 static void t1_encode_cblk(
196                 opj_t1_t *t1,
197                 opj_tcd_cblk_t * cblk,
198                 int orient,
199                 int compno,
200                 int level,
201                 int qmfbid,
202                 double stepsize,
203                 int cblksty,
204                 int numcomps,
205                 opj_tcd_tile_t * tile);
206 /**
207 Decode 1 code-block
208 @param t1 T1 handle
209 @param cblk Code-block coding parameters
210 @param orient
211 @param roishift Region of interest shifting value
212 @param cblksty Code-block style
213 */
214 static void t1_decode_cblk(
215                 opj_t1_t *t1,
216                 opj_tcd_cblk_t * cblk,
217                 int orient,
218                 int roishift,
219                 int cblksty);
220
221 /*@}*/
222
223 /*@}*/
224
225 /* ----------------------------------------------------------------------- */
226
227 static char t1_getctxno_zc(int f, int orient) {
228         return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
229 }
230
231 static char t1_getctxno_sc(int f) {
232         return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
233 }
234
235 static char t1_getctxno_mag(int f) {
236         return lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
237 }
238
239 static char t1_getspb(int f) {
240         return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
241 }
242
243 static short t1_getnmsedec_sig(int x, int bitpos) {
244         if (bitpos > T1_NMSEDEC_FRACBITS) {
245                 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
246         }
247         
248         return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
249 }
250
251 static short t1_getnmsedec_ref(int x, int bitpos) {
252         if (bitpos > T1_NMSEDEC_FRACBITS) {
253                 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
254         }
255
256     return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
257 }
258
259 static void t1_updateflags(flag_t *flagsp, int s, int stride) {
260         flag_t *np = flagsp - stride;
261         flag_t *sp = flagsp + stride;
262
263         static const flag_t mod[] = {
264                 T1_SIG_S, T1_SIG_S|T1_SGN_S,
265                 T1_SIG_E, T1_SIG_E|T1_SGN_E,
266                 T1_SIG_W, T1_SIG_W|T1_SGN_W,
267                 T1_SIG_N, T1_SIG_N|T1_SGN_N
268         };
269
270         np[-1] |= T1_SIG_SE;
271         np[0]  |= mod[s];
272         np[1]  |= T1_SIG_SW;
273
274         flagsp[-1] |= mod[s+2];
275         flagsp[0]  |= T1_SIG;
276         flagsp[1]  |= mod[s+4];
277
278         sp[-1] |= T1_SIG_NE;
279         sp[0]  |= mod[s+6];
280         sp[1]  |= T1_SIG_NW;
281 }
282
283 static void t1_enc_sigpass_step(
284                 opj_t1_t *t1,
285                 flag_t *flagsp,
286                 int *datap,
287                 int orient,
288                 int bpno,
289                 int one,
290                 int *nmsedec,
291                 char type,
292                 int vsc)
293 {
294         int v, flag;
295         
296         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
297         
298         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
299         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
300                 v = int_abs(*datap) & one ? 1 : 0;
301                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));       /* ESSAI */
302                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
303                         mqc_bypass_enc(mqc, v);
304                 } else {
305                         mqc_encode(mqc, v);
306                 }
307                 if (v) {
308                         v = *datap < 0 ? 1 : 0;
309                         *nmsedec +=     t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
310                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));       /* ESSAI */
311                         if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
312                                 mqc_bypass_enc(mqc, v);
313                         } else {
314                                 mqc_encode(mqc, v ^ t1_getspb(flag));
315                         }
316                         t1_updateflags(flagsp, v, t1->flags_stride);
317                 }
318                 *flagsp |= T1_VISIT;
319         }
320 }
321
322 static void t1_dec_sigpass_step(
323                 opj_t1_t *t1,
324                 flag_t *flagsp,
325                 int *datap,
326                 int orient,
327                 int oneplushalf,
328                 char type,
329                 int vsc)
330 {
331         int v, flag;
332         
333         opj_raw_t *raw = t1->raw;       /* RAW component */
334         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
335         
336         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
337         if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
338                 if (type == T1_TYPE_RAW) {
339                         if (raw_decode(raw)) {
340                                 v = raw_decode(raw);    /* ESSAI */
341                                 *datap = v ? -oneplushalf : oneplushalf;
342                                 t1_updateflags(flagsp, v, t1->flags_stride);
343                         }
344                 } else {
345                         mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
346                         if (mqc_decode(mqc)) {
347                                 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
348                                 v = mqc_decode(mqc) ^ t1_getspb(flag);
349                                 *datap = v ? -oneplushalf : oneplushalf;
350                                 t1_updateflags(flagsp, v, t1->flags_stride);
351                         }
352                 }
353                 *flagsp |= T1_VISIT;
354         }
355 }                               /* VSC and  BYPASS by Antonin */
356
357 static void t1_enc_sigpass(
358                 opj_t1_t *t1,
359                 int bpno,
360                 int orient,
361                 int *nmsedec,
362                 char type,
363                 int cblksty)
364 {
365         int i, j, k, one, vsc;
366         *nmsedec = 0;
367         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
368         for (k = 0; k < t1->h; k += 4) {
369                 for (i = 0; i < t1->w; ++i) {
370                         for (j = k; j < k + 4 && j < t1->h; ++j) {
371                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
372                                 t1_enc_sigpass_step(
373                                                 t1,
374                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
375                                                 &t1->data[(j * t1->w) + i],
376                                                 orient,
377                                                 bpno,
378                                                 one,
379                                                 nmsedec,
380                                                 type,
381                                                 vsc);
382                         }
383                 }
384         }
385 }
386
387 static void t1_dec_sigpass(
388                 opj_t1_t *t1,
389                 int bpno,
390                 int orient,
391                 char type,
392                 int cblksty)
393 {
394         int i, j, k, one, half, oneplushalf, vsc;
395         one = 1 << bpno;
396         half = one >> 1;
397         oneplushalf = one | half;
398         for (k = 0; k < t1->h; k += 4) {
399                 for (i = 0; i < t1->w; ++i) {
400                         for (j = k; j < k + 4 && j < t1->h; ++j) {
401                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
402                                 t1_dec_sigpass_step(
403                                                 t1,
404                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
405                                                 &t1->data[(j * t1->w) + i],
406                                                 orient,
407                                                 oneplushalf,
408                                                 type,
409                                                 vsc);
410                         }
411                 }
412         }
413 }                               /* VSC and  BYPASS by Antonin */
414
415 static void t1_enc_refpass_step(
416                 opj_t1_t *t1,
417                 flag_t *flagsp,
418                 int *datap,
419                 int bpno,
420                 int one,
421                 int *nmsedec,
422                 char type,
423                 int vsc)
424 {
425         int v, flag;
426         
427         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
428         
429         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
430         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
431                 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
432                 v = int_abs(*datap) & one ? 1 : 0;
433                 mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
434                 if (type == T1_TYPE_RAW) {      /* BYPASS/LAZY MODE */
435                         mqc_bypass_enc(mqc, v);
436                 } else {
437                         mqc_encode(mqc, v);
438                 }
439                 *flagsp |= T1_REFINE;
440         }
441 }
442
443 static void t1_dec_refpass_step(
444                 opj_t1_t *t1,
445                 flag_t *flagsp,
446                 int *datap,
447                 int poshalf,
448                 int neghalf,
449                 char type,
450                 int vsc)
451 {
452         int v, t, flag;
453         
454         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
455         opj_raw_t *raw = t1->raw;       /* RAW component */
456         
457         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
458         if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
459                 mqc_setcurctx(mqc, t1_getctxno_mag(flag));      /* ESSAI */
460                 if (type == T1_TYPE_RAW) {
461                         v = raw_decode(raw);
462                 } else {
463                         v = mqc_decode(mqc);
464                 }
465                 t = v ? poshalf : neghalf;
466                 *datap += *datap < 0 ? -t : t;
467                 *flagsp |= T1_REFINE;
468         }
469 }                               /* VSC and  BYPASS by Antonin  */
470
471 static void t1_enc_refpass(
472                 opj_t1_t *t1,
473                 int bpno,
474                 int *nmsedec,
475                 char type,
476                 int cblksty)
477 {
478         int i, j, k, one, vsc;
479         *nmsedec = 0;
480         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
481         for (k = 0; k < t1->h; k += 4) {
482                 for (i = 0; i < t1->w; ++i) {
483                         for (j = k; j < k + 4 && j < t1->h; ++j) {
484                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
485                                 t1_enc_refpass_step(
486                                                 t1,
487                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
488                                                 &t1->data[(j * t1->w) + i],
489                                                 bpno,
490                                                 one,
491                                                 nmsedec,
492                                                 type,
493                                                 vsc);
494                         }
495                 }
496         }
497 }
498
499 static void t1_dec_refpass(
500                 opj_t1_t *t1,
501                 int bpno,
502                 char type,
503                 int cblksty)
504 {
505         int i, j, k, one, poshalf, neghalf;
506         int vsc;
507         one = 1 << bpno;
508         poshalf = one >> 1;
509         neghalf = bpno > 0 ? -poshalf : -1;
510         for (k = 0; k < t1->h; k += 4) {
511                 for (i = 0; i < t1->w; ++i) {
512                         for (j = k; j < k + 4 && j < t1->h; ++j) {
513                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
514                                 t1_dec_refpass_step(
515                                                 t1,
516                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
517                                                 &t1->data[(j * t1->w) + i],
518                                                 poshalf,
519                                                 neghalf,
520                                                 type,
521                                                 vsc);
522                         }
523                 }
524         }
525 }                               /* VSC and  BYPASS by Antonin */
526
527 static void t1_enc_clnpass_step(
528                 opj_t1_t *t1,
529                 flag_t *flagsp,
530                 int *datap,
531                 int orient,
532                 int bpno,
533                 int one,
534                 int *nmsedec,
535                 int partial,
536                 int vsc)
537 {
538         int v, flag;
539         
540         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
541         
542         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
543         if (partial) {
544                 goto LABEL_PARTIAL;
545         }
546         if (!(*flagsp & (T1_SIG | T1_VISIT))) {
547                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
548                 v = int_abs(*datap) & one ? 1 : 0;
549                 mqc_encode(mqc, v);
550                 if (v) {
551 LABEL_PARTIAL:
552                         *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
553                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
554                         v = *datap < 0 ? 1 : 0;
555                         mqc_encode(mqc, v ^ t1_getspb(flag));
556                         t1_updateflags(flagsp, v, t1->flags_stride);
557                 }
558         }
559         *flagsp &= ~T1_VISIT;
560 }
561
562 static void t1_dec_clnpass_step(
563                 opj_t1_t *t1,
564                 flag_t *flagsp,
565                 int *datap,
566                 int orient,
567                 int oneplushalf,
568                 int partial,
569                 int vsc)
570 {
571         int v, flag;
572         
573         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
574         
575         flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
576         if (partial) {
577                 goto LABEL_PARTIAL;
578         }
579         if (!(flag & (T1_SIG | T1_VISIT))) {
580                 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
581                 if (mqc_decode(mqc)) {
582 LABEL_PARTIAL:
583                         mqc_setcurctx(mqc, t1_getctxno_sc(flag));
584                         v = mqc_decode(mqc) ^ t1_getspb(flag);
585                         *datap = v ? -oneplushalf : oneplushalf;
586                         t1_updateflags(flagsp, v, t1->flags_stride);
587                 }
588         }
589         *flagsp &= ~T1_VISIT;
590 }                               /* VSC and  BYPASS by Antonin */
591
592 static void t1_enc_clnpass(
593                 opj_t1_t *t1,
594                 int bpno,
595                 int orient,
596                 int *nmsedec,
597                 int cblksty)
598 {
599         int i, j, k, one, agg, runlen, vsc;
600         
601         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
602         
603         *nmsedec = 0;
604         one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
605         for (k = 0; k < t1->h; k += 4) {
606                 for (i = 0; i < t1->w; ++i) {
607                         if (k + 3 < t1->h) {
608                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
609                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
610                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
611                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
612                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
613                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
614                                 } else {
615                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
616                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
617                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
618                                                 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
619                                 }
620                         } else {
621                                 agg = 0;
622                         }
623                         if (agg) {
624                                 for (runlen = 0; runlen < 4; ++runlen) {
625                                         if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
626                                                 break;
627                                 }
628                                 mqc_setcurctx(mqc, T1_CTXNO_AGG);
629                                 mqc_encode(mqc, runlen != 4);
630                                 if (runlen == 4) {
631                                         continue;
632                                 }
633                                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
634                                 mqc_encode(mqc, runlen >> 1);
635                                 mqc_encode(mqc, runlen & 1);
636                         } else {
637                                 runlen = 0;
638                         }
639                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
640                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
641                                 t1_enc_clnpass_step(
642                                                 t1,
643                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
644                                                 &t1->data[(j * t1->w) + i],
645                                                 orient,
646                                                 bpno,
647                                                 one,
648                                                 nmsedec,
649                                                 agg && (j == k + runlen),
650                                                 vsc);
651                         }
652                 }
653         }
654 }
655
656 static void t1_dec_clnpass(
657                 opj_t1_t *t1,
658                 int bpno,
659                 int orient,
660                 int cblksty)
661 {
662         int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
663         int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
664         
665         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
666         
667         one = 1 << bpno;
668         half = one >> 1;
669         oneplushalf = one | half;
670         for (k = 0; k < t1->h; k += 4) {
671                 for (i = 0; i < t1->w; ++i) {
672                         if (k + 3 < t1->h) {
673                                 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
674                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
675                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
676                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
677                                                 || (MACRO_t1_flags(1 + k + 3,1 + i) 
678                                                 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
679                                 } else {
680                                         agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
681                                                 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
682                                                 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
683                                                 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
684                                 }
685                         } else {
686                                 agg = 0;
687                         }
688                         if (agg) {
689                                 mqc_setcurctx(mqc, T1_CTXNO_AGG);
690                                 if (!mqc_decode(mqc)) {
691                                         continue;
692                                 }
693                                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
694                                 runlen = mqc_decode(mqc);
695                                 runlen = (runlen << 1) | mqc_decode(mqc);
696                         } else {
697                                 runlen = 0;
698                         }
699                         for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
700                                 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
701                                 t1_dec_clnpass_step(
702                                                 t1,
703                                                 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
704                                                 &t1->data[(j * t1->w) + i],
705                                                 orient,
706                                                 oneplushalf,
707                                                 agg && (j == k + runlen),
708                                                 vsc);
709                         }
710                 }
711         }
712         if (segsym) {
713                 int v = 0;
714                 mqc_setcurctx(mqc, T1_CTXNO_UNI);
715                 v = mqc_decode(mqc);
716                 v = (v << 1) | mqc_decode(mqc);
717                 v = (v << 1) | mqc_decode(mqc);
718                 v = (v << 1) | mqc_decode(mqc);
719                 /*
720                 if (v!=0xa) {
721                         opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
722                 } 
723                 */
724         }
725 }                               /* VSC and  BYPASS by Antonin */
726
727
728 /** mod fixed_quality */
729 static double t1_getwmsedec(
730                 int nmsedec,
731                 int compno,
732                 int level,
733                 int orient,
734                 int bpno,
735                 int qmfbid,
736                 double stepsize,
737                 int numcomps)
738 {
739         double w1, w2, wmsedec;
740         if (qmfbid == 1) {
741                 w1 = (numcomps > 1) ? mct_getnorm(compno) : 1.0;
742                 w2 = dwt_getnorm(level, orient);
743         } else {                        /* if (qmfbid == 0) */
744                 w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1.0;
745                 w2 = dwt_getnorm_real(level, orient);
746         }
747         wmsedec = w1 * w2 * stepsize * (1 << bpno);
748         wmsedec *= wmsedec * nmsedec / 8192.0;
749         
750         return wmsedec;
751 }
752
753 static bool allocate_buffers(
754                 opj_t1_t *t1,
755                 int w,
756                 int h)
757 {
758         int datasize=w * h;
759         int flagssize;
760
761         if(datasize > t1->datasize){
762                 opj_aligned_free(t1->data);
763                 t1->data=opj_aligned_malloc(datasize * sizeof(int));
764                 if(!t1->data){
765                         return false;
766                 }
767                 t1->datasize=datasize;
768         }
769         memset(t1->data,0,datasize * sizeof(int));
770
771         t1->flags_stride=w+2;
772         flagssize=t1->flags_stride * (h+2);
773
774         if(flagssize > t1->flagssize){
775                 opj_aligned_free(t1->flags);
776                 t1->flags=opj_aligned_malloc(flagssize * sizeof(flag_t));
777                 if(!t1->flags){
778                         return false;
779                 }
780                 t1->flagssize=flagssize;
781         }
782         memset(t1->flags,0,flagssize * sizeof(flag_t));
783
784         t1->w=w;
785         t1->h=h;
786
787         return true;
788 }
789
790 /** mod fixed_quality */
791 static void t1_encode_cblk(
792                 opj_t1_t *t1,
793                 opj_tcd_cblk_t * cblk,
794                 int orient,
795                 int compno,
796                 int level,
797                 int qmfbid,
798                 double stepsize,
799                 int cblksty,
800                 int numcomps,
801                 opj_tcd_tile_t * tile)
802 {
803         int i, j;
804         int passno;
805         int bpno, passtype;
806         int max;
807         int nmsedec = 0;
808         double cumwmsedec = 0.0;
809         char type = T1_TYPE_MQ;
810         
811         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
812         
813         max = 0;
814         for (j = 0; j < t1->h; ++j) {
815                 for (i = 0; i < t1->w; ++i) {
816                         max = int_max(max, int_abs(t1->data[(j * t1->w) + i]));
817                 }
818         }
819         
820         cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
821         
822         bpno = cblk->numbps - 1;
823         passtype = 2;
824         
825         mqc_resetstates(mqc);
826         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
827         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
828         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
829         mqc_init_enc(mqc, cblk->data);
830         
831         for (passno = 0; bpno >= 0; ++passno) {
832                 opj_tcd_pass_t *pass = &cblk->passes[passno];
833                 int correction = 3;
834                 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
835                 
836                 switch (passtype) {
837                         case 0:
838                                 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
839                                 break;
840                         case 1:
841                                 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
842                                 break;
843                         case 2:
844                                 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
845                                 /* code switch SEGMARK (i.e. SEGSYM) */
846                                 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
847                                         mqc_segmark_enc(mqc);
848                                 break;
849                 }
850                 
851                 /* fixed_quality */
852                 cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
853                 tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
854                 
855                 /* Code switch "RESTART" (i.e. TERMALL) */
856                 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
857                         if (type == T1_TYPE_RAW) {
858                                 mqc_flush(mqc);
859                                 correction = 1;
860                                 /* correction = mqc_bypass_flush_enc(); */
861                         } else {                        /* correction = mqc_restart_enc(); */
862                                 mqc_flush(mqc);
863                                 correction = 1;
864                         }
865                         pass->term = 1;
866                 } else {
867                         if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
868                                 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
869                                 if (type == T1_TYPE_RAW) {
870                                         mqc_flush(mqc);
871                                         correction = 1;
872                                         /* correction = mqc_bypass_flush_enc(); */
873                                 } else {                /* correction = mqc_restart_enc(); */
874                                         mqc_flush(mqc);
875                                         correction = 1;
876                                 }
877                                 pass->term = 1;
878                         } else {
879                                 pass->term = 0;
880                         }
881                 }
882                 
883                 if (++passtype == 3) {
884                         passtype = 0;
885                         bpno--;
886                 }
887                 
888                 if (pass->term && bpno > 0) {
889                         type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
890                         if (type == T1_TYPE_RAW)
891                                 mqc_bypass_init_enc(mqc);
892                         else
893                                 mqc_restart_init_enc(mqc);
894                 }
895                 
896                 pass->distortiondec = cumwmsedec;
897                 pass->rate = mqc_numbytes(mqc) + correction;    /* FIXME */
898                 
899                 /* Code-switch "RESET" */
900                 if (cblksty & J2K_CCP_CBLKSTY_RESET)
901                         mqc_reset_enc(mqc);
902         }
903         
904         /* Code switch "ERTERM" (i.e. PTERM) */
905         if (cblksty & J2K_CCP_CBLKSTY_PTERM)
906                 mqc_erterm_enc(mqc);
907         else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
908                 mqc_flush(mqc);
909         
910         cblk->totalpasses = passno;
911
912         for (passno = 0; passno<cblk->totalpasses; passno++) {
913                 opj_tcd_pass_t *pass = &cblk->passes[passno];
914                 if (pass->rate > mqc_numbytes(mqc))
915                         pass->rate = mqc_numbytes(mqc);
916                 /*Preventing generation of FF as last data byte of a pass*/
917                 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
918                         pass->rate--;
919                 }
920                 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);             
921         }
922 }
923
924 static void t1_decode_cblk(
925                 opj_t1_t *t1,
926                 opj_tcd_cblk_t * cblk,
927                 int orient,
928                 int roishift,
929                 int cblksty)
930 {
931         int bpno, passtype;
932         int segno, passno;
933         char type = T1_TYPE_MQ; /* BYPASS mode */
934         
935         opj_raw_t *raw = t1->raw;       /* RAW component */
936         opj_mqc_t *mqc = t1->mqc;       /* MQC component */
937
938         if(!allocate_buffers(
939                                 t1,
940                                 cblk->x1 - cblk->x0,
941                                 cblk->y1 - cblk->y0))
942         {
943                 return;
944         }
945
946         bpno = roishift + cblk->numbps - 1;
947         passtype = 2;
948         
949         mqc_resetstates(mqc);
950         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
951         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
952         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
953         
954         for (segno = 0; segno < cblk->numsegs; ++segno) {
955                 opj_tcd_seg_t *seg = &cblk->segs[segno];
956                 
957                 /* BYPASS mode */
958                 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
959                 if (type == T1_TYPE_RAW) {
960                         raw_init_dec(raw, seg->data, seg->len);
961                 } else {
962                         mqc_init_dec(mqc, seg->data, seg->len);
963                 }
964                 
965                 for (passno = 0; passno < seg->numpasses; ++passno) {
966                         switch (passtype) {
967                                 case 0:
968                                         t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
969                                         break;
970                                 case 1:
971                                         t1_dec_refpass(t1, bpno+1, type, cblksty);
972                                         break;
973                                 case 2:
974                                         t1_dec_clnpass(t1, bpno+1, orient, cblksty);
975                                         break;
976                         }
977                         
978                         if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
979                                 mqc_resetstates(mqc);
980                                 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);                         
981                                 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
982                                 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
983                         }
984                         if (++passtype == 3) {
985                                 passtype = 0;
986                                 bpno--;
987                         }
988                 }
989         }
990 }
991
992 /* ----------------------------------------------------------------------- */
993
994 opj_t1_t* t1_create(opj_common_ptr cinfo) {
995         opj_t1_t *t1 = (opj_t1_t*) malloc(sizeof(opj_t1_t));
996         if(!t1)
997                 return NULL;
998
999                 t1->cinfo = cinfo;
1000                 /* create MQC and RAW handles */
1001                 t1->mqc = mqc_create();
1002                 t1->raw = raw_create();
1003
1004         t1->datasize=0;
1005         t1->data=NULL;
1006         t1->flagssize=0;
1007         t1->flags=NULL;
1008
1009         return t1;
1010 }
1011
1012 void t1_destroy(opj_t1_t *t1) {
1013         if(t1) {
1014                 /* destroy MQC and RAW handles */
1015                 mqc_destroy(t1->mqc);
1016                 raw_destroy(t1->raw);
1017                 opj_aligned_free(t1->data);
1018                 opj_aligned_free(t1->flags);
1019                 free(t1);
1020         }
1021 }
1022
1023 void t1_encode_cblks(
1024                 opj_t1_t *t1,
1025                 opj_tcd_tile_t *tile,
1026                 opj_tcp_t *tcp)
1027 {
1028         int compno, resno, bandno, precno, cblkno;
1029
1030         tile->distotile = 0;            /* fixed_quality */
1031
1032         for (compno = 0; compno < tile->numcomps; ++compno) {
1033                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1034
1035                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1036                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1037
1038                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1039                                 opj_tcd_band_t *band = &res->bands[bandno];
1040
1041                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1042                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1043
1044                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1045                                                 int x, y, w, i, j, orient;
1046                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1047
1048                                                 x = cblk->x0 - band->x0;
1049                                                 y = cblk->y0 - band->y0;
1050                                                 if (band->bandno & 1) {
1051                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1052                                                         x += pres->x1 - pres->x0;
1053                                                 }
1054                                                 if (band->bandno & 2) {
1055                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1056                                                         y += pres->y1 - pres->y0;
1057                                                 }
1058
1059                                                 if(!allocate_buffers(
1060                                                                         t1,
1061                                                                         cblk->x1 - cblk->x0,
1062                                                                         cblk->y1 - cblk->y0))
1063                                                 {
1064                                                         return;
1065                                                 }
1066
1067                                                 w = tilec->x1 - tilec->x0;
1068                                                 if (tcp->tccps[compno].qmfbid == 1) {
1069                                                         for (j = 0; j < t1->h; ++j) {
1070                                                                 for (i = 0; i < t1->w; ++i) {
1071                                                                         t1->data[(j * t1->w) + i] =
1072                                                                                 tilec->data[(x + i) + (y + j) * w] << T1_NMSEDEC_FRACBITS;
1073                                                                 }
1074                                                         }
1075                                                 } else {                /* if (tcp->tccps[compno].qmfbid == 0) */
1076                                                         for (j = 0; j < t1->h; ++j) {
1077                                                                 for (i = 0; i < t1->w; ++i) {
1078                                                                         t1->data[(j * t1->w) + i] = 
1079                                                                                 fix_mul(
1080                                                                                 tilec->data[x + i + (y + j) * w],
1081                                                                                 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS);
1082                                                                 }
1083                                                         }
1084                                                 }
1085                                                 orient = band->bandno;  /* FIXME */
1086                                                 if (orient == 2) {
1087                                                         orient = 1;
1088                                                 } else if (orient == 1) {
1089                                                         orient = 2;
1090                                                 }
1091
1092                                                 t1_encode_cblk(
1093                                                                 t1,
1094                                                                 cblk,
1095                                                                 orient,
1096                                                                 compno,
1097                                                                 tilec->numresolutions - 1 - resno,
1098                                                                 tcp->tccps[compno].qmfbid,
1099                                                                 band->stepsize,
1100                                                                 tcp->tccps[compno].cblksty,
1101                                                                 tile->numcomps,
1102                                                                 tile);
1103
1104                                         } /* cblkno */
1105                                 } /* precno */
1106                         } /* bandno */
1107                 } /* resno  */
1108         } /* compno  */
1109 }
1110
1111 void t1_decode_cblks(
1112                 opj_t1_t *t1,
1113                 opj_tcd_tile_t *tile,
1114                 opj_tcp_t *tcp)
1115 {
1116         int compno, resno, bandno, precno, cblkno;
1117
1118         for (compno = 0; compno < tile->numcomps; ++compno) {
1119                 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1120
1121                 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1122                         opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1123
1124                         for (bandno = 0; bandno < res->numbands; ++bandno) {
1125                                 opj_tcd_band_t *band = &res->bands[bandno];
1126
1127                                 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1128                                         opj_tcd_precinct_t *prc = &band->precincts[precno];
1129
1130                                         for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1131                                                 int x, y, w, i, j, orient, cblk_w, cblk_h;
1132                                                 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1133
1134                                                 orient = band->bandno;  /* FIXME */
1135                                                 if (orient == 2) {
1136                                                         orient = 1;
1137                                                 } else if (orient == 1) {
1138                                                         orient = 2;
1139                                                 }
1140                                                 
1141                                                 t1_decode_cblk(
1142                                                                 t1,
1143                                                                 cblk,
1144                                                                 orient,
1145                                                                 tcp->tccps[compno].roishift,
1146                                                                 tcp->tccps[compno].cblksty);
1147
1148                                                 x = cblk->x0 - band->x0;
1149                                                 y = cblk->y0 - band->y0;
1150                                                 if (band->bandno & 1) {
1151                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1152                                                         x += pres->x1 - pres->x0;
1153                                                 }
1154                                                 if (band->bandno & 2) {
1155                                                         opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1156                                                         y += pres->y1 - pres->y0;
1157                                                 }
1158
1159                                                 cblk_w = cblk->x1 - cblk->x0;
1160                                                 cblk_h = cblk->y1 - cblk->y0;
1161
1162                                                 if (tcp->tccps[compno].roishift) {
1163                                                         int thresh = 1 << tcp->tccps[compno].roishift;
1164                                                         for (j = 0; j < cblk_h; ++j) {
1165                                                                 for (i = 0; i < cblk_w; ++i) {
1166                                                                         int val = t1->data[(j * t1->w) + i];
1167                                                                         int mag = int_abs(val);
1168                                                                         if (mag >= thresh) {
1169                                                                                 mag >>= tcp->tccps[compno].roishift;
1170                                                                                 t1->data[(j * t1->w) + i] = val < 0 ? -mag : mag;
1171                                                                         }
1172                                                                 }
1173                                                         }
1174                                                 }
1175                                                 
1176                                                 w = tilec->x1 - tilec->x0;
1177                                                 if (tcp->tccps[compno].qmfbid == 1) {
1178                                                         for (j = 0; j < cblk_h; ++j) {
1179                                                                 for (i = 0; i < cblk_w; ++i) {
1180                                                                         tilec->data[x + i + (y + j) * w] = t1->data[(j * t1->w) + i]/2;
1181                                                                 }
1182                                                         }
1183                                                 } else {                /* if (tcp->tccps[compno].qmfbid == 0) */
1184                                                         for (j = 0; j < cblk_h; ++j) {
1185                                                                 for (i = 0; i < cblk_w; ++i) {
1186                                                                         if (t1->data[(j * t1->w) + i] >> 1 == 0) {
1187                                                                                 tilec->data[x + i + (y + j) * w] = 0;
1188                                                                         } else {
1189                                                                                 double tmp = (double)(t1->data[(j * t1->w) + i] * band->stepsize * 4096.0);
1190                                                                                 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);                                                                   
1191                                                                                 tilec->data[x + i + (y + j) * w] = ((tmp<0)?-tmp2:tmp2);
1192                                                                         }
1193                                                                 }
1194                                                         }
1195                                                 }
1196                                         } /* cblkno */
1197                                 } /* precno */
1198                         } /* bandno */
1199                 } /* resno */
1200         } /* compno */
1201 }
1202