corrade-vassal – Blame information for rev 1

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