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