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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include "opj_includes.h"
36 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
39 /** @name Local static functions */
42 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
43 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
44 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
45 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
46 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
47 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
48 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
50 Encode significant pass
52 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
63 Decode significant pass
65 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
69 OPJ_INT32 oneplushalf,
73 static INLINE void opj_t1_dec_sigpass_step_raw(
80 static INLINE void opj_t1_dec_sigpass_step_mqc(
86 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
96 Encode significant pass
98 static void opj_t1_enc_sigpass( opj_t1_t *t1,
106 Decode significant pass
108 static void opj_t1_dec_sigpass( opj_t1_t *t1,
114 static void opj_t1_dec_sigpass_raw(
119 static void opj_t1_dec_sigpass_mqc(
123 static void opj_t1_dec_sigpass_mqc_vsc(
131 Encode refinement pass
133 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
144 Encode refinement pass
146 static void opj_t1_enc_refpass( opj_t1_t *t1,
153 Decode refinement pass
155 static void opj_t1_dec_refpass( opj_t1_t *t1,
159 static void opj_t1_dec_refpass_raw(
163 static void opj_t1_dec_refpass_mqc(
166 static void opj_t1_dec_refpass_mqc_vsc(
172 Decode refinement pass
174 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
182 static void INLINE opj_t1_dec_refpass_step_raw(
189 static void INLINE opj_t1_dec_refpass_step_mqc(
195 static void INLINE opj_t1_dec_refpass_step_mqc_vsc(
208 static void opj_t1_enc_clnpass_step(
221 static void opj_t1_dec_clnpass_step_partial(
227 static void opj_t1_dec_clnpass_step(
233 static void opj_t1_dec_clnpass_step_vsc(
244 static void opj_t1_enc_clnpass(
253 static void opj_t1_dec_clnpass(
259 static OPJ_FLOAT64 opj_t1_getwmsedec(
266 OPJ_FLOAT64 stepsize,
268 const OPJ_FLOAT64 * mct_norms);
270 static void opj_t1_encode_cblk( opj_t1_t *t1,
271 opj_tcd_cblk_enc_t* cblk,
276 OPJ_FLOAT64 stepsize,
279 opj_tcd_tile_t * tile,
280 const OPJ_FLOAT64 * mct_norms);
285 @param cblk Code-block coding parameters
287 @param roishift Region of interest shifting value
288 @param cblksty Code-block style
290 static opj_bool opj_t1_decode_cblk( opj_t1_t *t1,
291 opj_tcd_cblk_dec_t* cblk,
296 opj_bool opj_t1_allocate_buffers( opj_t1_t *t1,
304 /* ----------------------------------------------------------------------- */
306 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
307 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
310 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
311 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
314 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
315 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
316 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
320 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
321 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
324 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
325 if (bitpos > T1_NMSEDEC_FRACBITS) {
326 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
329 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
332 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
333 if (bitpos > T1_NMSEDEC_FRACBITS) {
334 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
337 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
340 void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
341 opj_flag_t *np = flagsp - stride;
342 opj_flag_t *sp = flagsp + stride;
344 static const opj_flag_t mod[] = {
345 T1_SIG_S, T1_SIG_S|T1_SGN_S,
346 T1_SIG_E, T1_SIG_E|T1_SGN_E,
347 T1_SIG_W, T1_SIG_W|T1_SGN_W,
348 T1_SIG_N, T1_SIG_N|T1_SGN_N
355 flagsp[-1] |= mod[s+2];
357 flagsp[1] |= mod[s+4];
364 void opj_t1_enc_sigpass_step( opj_t1_t *t1,
378 opj_mqc_t *mqc = t1->mqc; /* MQC component */
380 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
381 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
382 v = opj_int_abs(*datap) & one ? 1 : 0;
383 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
384 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
385 opj_mqc_bypass_enc(mqc, v);
387 opj_mqc_encode(mqc, v);
390 v = *datap < 0 ? 1 : 0;
391 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
392 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
393 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
394 opj_mqc_bypass_enc(mqc, v);
396 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
398 opj_t1_updateflags(flagsp, v, t1->flags_stride);
405 static INLINE void opj_t1_dec_sigpass_step_raw(
415 opj_raw_t *raw = t1->raw; /* RAW component */
417 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
418 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
419 if (opj_raw_decode(raw)) {
420 v = opj_raw_decode(raw); /* ESSAI */
421 *datap = v ? -oneplushalf : oneplushalf;
422 opj_t1_updateflags(flagsp, v, t1->flags_stride);
428 INLINE void opj_t1_dec_sigpass_step_mqc(
437 opj_mqc_t *mqc = t1->mqc; /* MQC component */
440 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
441 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
442 if (opj_mqc_decode(mqc)) {
443 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
444 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
445 *datap = v ? -oneplushalf : oneplushalf;
446 opj_t1_updateflags(flagsp, v, t1->flags_stride);
450 } /* VSC and BYPASS by Antonin */
452 INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
462 opj_mqc_t *mqc = t1->mqc; /* MQC component */
464 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
465 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
466 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
467 if (opj_mqc_decode(mqc)) {
468 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
469 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
470 *datap = v ? -oneplushalf : oneplushalf;
471 opj_t1_updateflags(flagsp, v, t1->flags_stride);
475 } /* VSC and BYPASS by Antonin */
479 void opj_t1_enc_sigpass(opj_t1_t *t1,
487 OPJ_UINT32 i, j, k, vsc;
491 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
492 for (k = 0; k < t1->h; k += 4) {
493 for (i = 0; i < t1->w; ++i) {
494 for (j = k; j < k + 4 && j < t1->h; ++j) {
495 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
496 opj_t1_enc_sigpass_step(
498 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
499 &t1->data[(j * t1->w) + i],
511 void opj_t1_dec_sigpass_raw(
517 int one, half, oneplushalf, vsc;
521 oneplushalf = one | half;
522 for (k = 0; k < t1->h; k += 4) {
523 for (i = 0; i < t1->w; ++i) {
524 for (j = k; j < k + 4 && j < t1->h; ++j) {
525 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
526 opj_t1_dec_sigpass_step_raw(
528 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
529 &t1->data[(j * t1->w) + i],
536 } /* VSC and BYPASS by Antonin */
538 void opj_t1_dec_sigpass_mqc(
543 int one, half, oneplushalf;
545 int *data1 = t1->data;
546 opj_flag_t *flags1 = &t1->flags[1];
549 oneplushalf = one | half;
550 for (k = 0; k < (t1->h & ~3); k += 4) {
551 for (i = 0; i < t1->w; ++i) {
552 int *data2 = data1 + i;
553 opj_flag_t *flags2 = flags1 + i;
554 flags2 += t1->flags_stride;
555 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
557 flags2 += t1->flags_stride;
558 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
560 flags2 += t1->flags_stride;
561 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
563 flags2 += t1->flags_stride;
564 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
568 flags1 += t1->flags_stride << 2;
570 for (i = 0; i < t1->w; ++i) {
571 int *data2 = data1 + i;
572 opj_flag_t *flags2 = flags1 + i;
573 for (j = k; j < t1->h; ++j) {
574 flags2 += t1->flags_stride;
575 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
579 } /* VSC and BYPASS by Antonin */
581 void opj_t1_dec_sigpass_mqc_vsc(
586 int one, half, oneplushalf, vsc;
590 oneplushalf = one | half;
591 for (k = 0; k < t1->h; k += 4) {
592 for (i = 0; i < t1->w; ++i) {
593 for (j = k; j < k + 4 && j < t1->h; ++j) {
594 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
595 opj_t1_dec_sigpass_step_mqc_vsc(
597 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
598 &t1->data[(j * t1->w) + i],
605 } /* VSC and BYPASS by Antonin */
609 void opj_t1_enc_refpass_step( opj_t1_t *t1,
621 opj_mqc_t *mqc = t1->mqc; /* MQC component */
623 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
624 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
625 *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
626 v = opj_int_abs(*datap) & one ? 1 : 0;
627 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
628 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
629 opj_mqc_bypass_enc(mqc, v);
631 opj_mqc_encode(mqc, v);
633 *flagsp |= T1_REFINE;
637 INLINE void opj_t1_dec_refpass_step_raw(
647 opj_raw_t *raw = t1->raw; /* RAW component */
649 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
650 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
651 v = opj_raw_decode(raw);
652 t = v ? poshalf : neghalf;
653 *datap += *datap < 0 ? -t : t;
654 *flagsp |= T1_REFINE;
656 } /* VSC and BYPASS by Antonin */
658 INLINE void opj_t1_dec_refpass_step_mqc(
667 opj_mqc_t *mqc = t1->mqc; /* MQC component */
670 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
671 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
672 v = opj_mqc_decode(mqc);
673 t = v ? poshalf : neghalf;
674 *datap += *datap < 0 ? -t : t;
675 *flagsp |= T1_REFINE;
677 } /* VSC and BYPASS by Antonin */
679 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
689 opj_mqc_t *mqc = t1->mqc; /* MQC component */
691 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
692 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
693 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
694 v = opj_mqc_decode(mqc);
695 t = v ? poshalf : neghalf;
696 *datap += *datap < 0 ? -t : t;
697 *flagsp |= T1_REFINE;
699 } /* VSC and BYPASS by Antonin */
702 void opj_t1_enc_refpass(
709 OPJ_UINT32 i, j, k, vsc;
713 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
714 for (k = 0; k < t1->h; k += 4) {
715 for (i = 0; i < t1->w; ++i) {
716 for (j = k; j < k + 4 && j < t1->h; ++j) {
717 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
718 opj_t1_enc_refpass_step(
720 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
721 &t1->data[(j * t1->w) + i],
732 void opj_t1_dec_refpass_raw(
737 int one, poshalf, neghalf;
742 neghalf = bpno > 0 ? -poshalf : -1;
743 for (k = 0; k < t1->h; k += 4) {
744 for (i = 0; i < t1->w; ++i) {
745 for (j = k; j < k + 4 && j < t1->h; ++j) {
746 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
747 opj_t1_dec_refpass_step_raw(
749 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
750 &t1->data[(j * t1->w) + i],
757 } /* VSC and BYPASS by Antonin */
759 void opj_t1_dec_refpass_mqc(
763 int one, poshalf, neghalf;
765 int *data1 = t1->data;
766 opj_flag_t *flags1 = &t1->flags[1];
769 neghalf = bpno > 0 ? -poshalf : -1;
770 for (k = 0; k < (t1->h & ~3); k += 4) {
771 for (i = 0; i < t1->w; ++i) {
772 int *data2 = data1 + i;
773 opj_flag_t *flags2 = flags1 + i;
774 flags2 += t1->flags_stride;
775 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
777 flags2 += t1->flags_stride;
778 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
780 flags2 += t1->flags_stride;
781 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
783 flags2 += t1->flags_stride;
784 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
788 flags1 += t1->flags_stride << 2;
790 for (i = 0; i < t1->w; ++i) {
791 int *data2 = data1 + i;
792 opj_flag_t *flags2 = flags1 + i;
793 for (j = k; j < t1->h; ++j) {
794 flags2 += t1->flags_stride;
795 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
799 } /* VSC and BYPASS by Antonin */
801 void opj_t1_dec_refpass_mqc_vsc(
805 int one, poshalf, neghalf;
810 neghalf = bpno > 0 ? -poshalf : -1;
811 for (k = 0; k < t1->h; k += 4) {
812 for (i = 0; i < t1->w; ++i) {
813 for (j = k; j < k + 4 && j < t1->h; ++j) {
814 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
815 opj_t1_dec_refpass_step_mqc_vsc(
817 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
818 &t1->data[(j * t1->w) + i],
825 } /* VSC and BYPASS by Antonin */
828 void opj_t1_enc_clnpass_step(
842 opj_mqc_t *mqc = t1->mqc; /* MQC component */
844 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
848 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
849 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
850 v = opj_int_abs(*datap) & one ? 1 : 0;
851 opj_mqc_encode(mqc, v);
854 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
855 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
856 v = *datap < 0 ? 1 : 0;
857 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
858 opj_t1_updateflags(flagsp, v, t1->flags_stride);
861 *flagsp &= ~T1_VISIT;
864 static void opj_t1_dec_clnpass_step_partial(
872 opj_mqc_t *mqc = t1->mqc; /* MQC component */
874 OPJ_ARG_NOT_USED(orient);
877 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
878 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
879 *datap = v ? -oneplushalf : oneplushalf;
880 opj_t1_updateflags(flagsp, v, t1->flags_stride);
881 *flagsp &= ~T1_VISIT;
882 } /* VSC and BYPASS by Antonin */
884 static void opj_t1_dec_clnpass_step(
893 opj_mqc_t *mqc = t1->mqc; /* MQC component */
896 if (!(flag & (T1_SIG | T1_VISIT))) {
897 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
898 if (opj_mqc_decode(mqc)) {
899 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
900 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
901 *datap = v ? -oneplushalf : oneplushalf;
902 opj_t1_updateflags(flagsp, v, t1->flags_stride);
905 *flagsp &= ~T1_VISIT;
906 } /* VSC and BYPASS by Antonin */
908 static void opj_t1_dec_clnpass_step_vsc(
919 opj_mqc_t *mqc = t1->mqc; /* MQC component */
921 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
925 if (!(flag & (T1_SIG | T1_VISIT))) {
926 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
927 if (opj_mqc_decode(mqc)) {
929 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
930 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
931 *datap = v ? -oneplushalf : oneplushalf;
932 opj_t1_updateflags(flagsp, v, t1->flags_stride);
935 *flagsp &= ~T1_VISIT;
938 void opj_t1_enc_clnpass(
947 OPJ_UINT32 agg, runlen, vsc;
949 opj_mqc_t *mqc = t1->mqc; /* MQC component */
952 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
953 for (k = 0; k < t1->h; k += 4) {
954 for (i = 0; i < t1->w; ++i) {
956 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
957 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
958 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
959 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960 || (MACRO_t1_flags(1 + k + 3,1 + i)
961 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
963 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
964 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
965 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
966 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
972 for (runlen = 0; runlen < 4; ++runlen) {
973 if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
976 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
977 opj_mqc_encode(mqc, runlen != 4);
981 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
982 opj_mqc_encode(mqc, runlen >> 1);
983 opj_mqc_encode(mqc, runlen & 1);
987 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
988 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
989 opj_t1_enc_clnpass_step(
991 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
992 &t1->data[(j * t1->w) + i],
997 agg && (j == k + runlen),
1004 static void opj_t1_dec_clnpass(
1010 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
1011 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1013 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1017 oneplushalf = one | half;
1018 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1019 for (k = 0; k < t1->h; k += 4) {
1020 for (i = 0; i < t1->w; ++i) {
1021 if (k + 3 < t1->h) {
1022 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1023 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1024 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1025 || (MACRO_t1_flags(1 + k + 3,1 + i)
1026 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1031 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1032 if (!opj_mqc_decode(mqc)) {
1035 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1036 runlen = opj_mqc_decode(mqc);
1037 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1041 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1042 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1043 opj_t1_dec_clnpass_step_vsc(
1045 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1046 &t1->data[(j * t1->w) + i],
1049 agg && (j == k + runlen),
1055 int *data1 = t1->data;
1056 opj_flag_t *flags1 = &t1->flags[1];
1057 for (k = 0; k < (t1->h & ~3); k += 4) {
1058 for (i = 0; i < t1->w; ++i) {
1059 int *data2 = data1 + i;
1060 opj_flag_t *flags2 = flags1 + i;
1061 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1062 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1063 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1064 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1066 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1067 if (!opj_mqc_decode(mqc)) {
1070 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1071 runlen = opj_mqc_decode(mqc);
1072 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1073 flags2 += runlen * t1->flags_stride;
1074 data2 += runlen * t1->w;
1075 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1076 flags2 += t1->flags_stride;
1077 if (agg && (j == k + runlen)) {
1078 opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1080 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1085 flags2 += t1->flags_stride;
1086 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1088 flags2 += t1->flags_stride;
1089 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1091 flags2 += t1->flags_stride;
1092 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1094 flags2 += t1->flags_stride;
1095 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1099 data1 += t1->w << 2;
1100 flags1 += t1->flags_stride << 2;
1102 for (i = 0; i < t1->w; ++i) {
1103 int *data2 = data1 + i;
1104 opj_flag_t *flags2 = flags1 + i;
1105 for (j = k; j < t1->h; ++j) {
1106 flags2 += t1->flags_stride;
1107 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1115 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1116 v = opj_mqc_decode(mqc);
1117 v = (v << 1) | opj_mqc_decode(mqc);
1118 v = (v << 1) | opj_mqc_decode(mqc);
1119 v = (v << 1) | opj_mqc_decode(mqc);
1122 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1126 } /* VSC and BYPASS by Antonin */
1129 /** mod fixed_quality */
1130 static OPJ_FLOAT64 opj_t1_getwmsedec(
1137 OPJ_FLOAT64 stepsize,
1138 OPJ_UINT32 numcomps,
1139 const OPJ_FLOAT64 * mct_norms)
1141 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1144 w1 = mct_norms[compno];
1148 w2 = opj_dwt_getnorm(level, orient);
1149 } else { /* if (qmfbid == 0) */
1150 w2 = opj_dwt_getnorm_real(level, orient);
1153 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1154 wmsedec *= wmsedec * nmsedec / 8192.0;
1159 opj_bool opj_t1_allocate_buffers(
1164 OPJ_UINT32 datasize=w * h;
1165 OPJ_UINT32 flagssize;
1167 if(datasize > t1->datasize){
1168 opj_aligned_free(t1->data);
1169 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1173 t1->datasize=datasize;
1175 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1177 t1->flags_stride=w+2;
1178 flagssize=t1->flags_stride * (h+2);
1180 if(flagssize > t1->flagssize){
1181 opj_aligned_free(t1->flags);
1182 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1186 t1->flagssize=flagssize;
1188 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1196 /* ----------------------------------------------------------------------- */
1198 /* ----------------------------------------------------------------------- */
1200 * Creates a new Tier 1 handle
1201 * and initializes the look-up tables of the Tier-1 coder/decoder
1202 * @return a new T1 handle if successful, returns NULL otherwise
1204 opj_t1_t* opj_t1_create()
1206 opj_t1_t *l_t1 = 00;
1208 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1212 memset(l_t1,0,sizeof(opj_t1_t));
1214 /* create MQC and RAW handles */
1215 l_t1->mqc = opj_mqc_create();
1217 opj_t1_destroy(l_t1);
1221 l_t1->raw = opj_raw_create();
1223 opj_t1_destroy(l_t1);
1232 * Destroys a previously created T1 handle
1234 * @param p_t1 Tier 1 handle to destroy
1236 void opj_t1_destroy(opj_t1_t *p_t1)
1242 /* destroy MQC and RAW handles */
1243 opj_mqc_destroy(p_t1->mqc);
1245 opj_raw_destroy(p_t1->raw);
1249 opj_aligned_free(p_t1->data);
1254 opj_aligned_free(p_t1->flags);
1261 opj_bool opj_t1_decode_cblks( opj_t1_t* t1,
1262 opj_tcd_tilecomp_t* tilec,
1266 OPJ_UINT32 resno, bandno, precno, cblkno;
1267 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1269 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1270 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1272 for (bandno = 0; bandno < res->numbands; ++bandno) {
1273 opj_tcd_band_t* restrict band = &res->bands[bandno];
1275 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1276 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1278 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1279 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1280 OPJ_INT32* restrict datap;
1281 /*void* restrict tiledp;*/
1282 OPJ_UINT32 cblk_w, cblk_h;
1286 if (OPJ_FALSE == opj_t1_decode_cblk(
1295 x = cblk->x0 - band->x0;
1296 y = cblk->y0 - band->y0;
1297 if (band->bandno & 1) {
1298 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1299 x += pres->x1 - pres->x0;
1301 if (band->bandno & 2) {
1302 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1303 y += pres->y1 - pres->y0;
1310 if (tccp->roishift) {
1311 OPJ_INT32 thresh = 1 << tccp->roishift;
1312 for (j = 0; j < cblk_h; ++j) {
1313 for (i = 0; i < cblk_w; ++i) {
1314 OPJ_INT32 val = datap[(j * cblk_w) + i];
1315 OPJ_INT32 mag = abs(val);
1316 if (mag >= thresh) {
1317 mag >>= tccp->roishift;
1318 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1324 /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1325 if (tccp->qmfbid == 1) {
1326 int* restrict tiledp = &tilec->data[(y * tile_w) + x];
1327 for (j = 0; j < cblk_h; ++j) {
1328 for (i = 0; i < cblk_w; ++i) {
1329 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1330 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1333 } else { /* if (tccp->qmfbid == 0) */
1334 float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
1335 for (j = 0; j < cblk_h; ++j) {
1336 float* restrict tiledp2 = tiledp;
1337 for (i = 0; i < cblk_w; ++i) {
1338 float tmp = *datap * band->stepsize;
1342 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1343 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1349 //opj_free(cblk->data);
1350 //opj_free(cblk->segs);
1351 /*cblk->segs = 00;*/
1353 //opj_free(precinct->cblks.dec);
1361 opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
1362 opj_tcd_cblk_dec_t* cblk,
1364 OPJ_UINT32 roishift,
1367 opj_raw_t *raw = t1->raw; /* RAW component */
1368 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1371 OPJ_UINT32 passtype;
1372 OPJ_UINT32 segno, passno;
1373 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1375 if(!opj_t1_allocate_buffers(
1377 cblk->x1 - cblk->x0,
1378 cblk->y1 - cblk->y0))
1383 bpno = roishift + cblk->numbps - 1;
1386 opj_mqc_resetstates(mqc);
1387 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1388 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1389 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1391 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1392 opj_tcd_seg_t *seg = &cblk->segs[segno];
1395 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1396 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1397 if(seg->data == 00){
1400 if (type == T1_TYPE_RAW) {
1401 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1403 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1408 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1409 /* switch (passtype) {
1411 opj_t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1414 opj_t1_dec_refpass(t1, bpno+1, type, cblksty);
1417 opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1422 if (type == T1_TYPE_RAW) {
1423 opj_t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1425 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1426 opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1428 opj_t1_dec_sigpass_mqc(t1, bpno+1, orient);
1433 if (type == T1_TYPE_RAW) {
1434 opj_t1_dec_refpass_raw(t1, bpno+1, cblksty);
1436 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1437 opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1439 opj_t1_dec_refpass_mqc(t1, bpno+1);
1444 opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1448 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1449 opj_mqc_resetstates(mqc);
1450 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1451 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1452 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1454 if (++passtype == 3) {
1466 opj_bool opj_t1_encode_cblks( opj_t1_t *t1,
1467 opj_tcd_tile_t *tile,
1469 const OPJ_FLOAT64 * mct_norms
1472 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1474 tile->distotile = 0; /* fixed_quality */
1476 for (compno = 0; compno < tile->numcomps; ++compno) {
1477 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1478 opj_tccp_t* tccp = &tcp->tccps[compno];
1479 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1481 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1482 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1484 for (bandno = 0; bandno < res->numbands; ++bandno) {
1485 opj_tcd_band_t* restrict band = &res->bands[bandno];
1486 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1488 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1489 opj_tcd_precinct_t *prc = &band->precincts[precno];
1491 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1492 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1493 OPJ_INT32 * restrict datap;
1494 OPJ_INT32* restrict tiledp;
1499 OPJ_INT32 x = cblk->x0 - band->x0;
1500 OPJ_INT32 y = cblk->y0 - band->y0;
1501 if (band->bandno & 1) {
1502 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1503 x += pres->x1 - pres->x0;
1505 if (band->bandno & 2) {
1506 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1507 y += pres->y1 - pres->y0;
1510 if(!opj_t1_allocate_buffers(
1512 cblk->x1 - cblk->x0,
1513 cblk->y1 - cblk->y0))
1522 tiledp=&tilec->data[(y * tile_w) + x];
1523 if (tccp->qmfbid == 1) {
1524 for (j = 0; j < cblk_h; ++j) {
1525 for (i = 0; i < cblk_w; ++i) {
1526 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1527 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1530 } else { /* if (tccp->qmfbid == 0) */
1531 for (j = 0; j < cblk_h; ++j) {
1532 for (i = 0; i < cblk_w; ++i) {
1533 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1534 datap[(j * cblk_w) + i] =
1537 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1547 tilec->numresolutions - 1 - resno,
1563 /** mod fixed_quality */
1564 void opj_t1_encode_cblk(opj_t1_t *t1,
1565 opj_tcd_cblk_enc_t* cblk,
1570 OPJ_FLOAT64 stepsize,
1572 OPJ_UINT32 numcomps,
1573 opj_tcd_tile_t * tile,
1574 const OPJ_FLOAT64 * mct_norms)
1576 OPJ_FLOAT64 cumwmsedec = 0.0;
1578 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1582 OPJ_UINT32 passtype;
1583 OPJ_INT32 nmsedec = 0;
1586 OPJ_BYTE type = T1_TYPE_MQ;
1587 OPJ_FLOAT64 tempwmsedec;
1590 for (i = 0; i < t1->w * t1->h; ++i) {
1591 OPJ_INT32 tmp = abs(t1->data[i]);
1592 max = opj_int_max(max, tmp);
1595 cblk->numbps = max ? (opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1597 bpno = cblk->numbps - 1;
1600 opj_mqc_resetstates(mqc);
1601 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1602 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1603 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1604 opj_mqc_init_enc(mqc, cblk->data);
1606 for (passno = 0; bpno >= 0; ++passno) {
1607 opj_tcd_pass_t *pass = &cblk->passes[passno];
1608 OPJ_UINT32 correction = 3;
1609 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1613 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1616 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1619 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1620 /* code switch SEGMARK (i.e. SEGSYM) */
1621 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1622 opj_mqc_segmark_enc(mqc);
1627 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1628 cumwmsedec += tempwmsedec;
1629 tile->distotile += tempwmsedec;
1631 /* Code switch "RESTART" (i.e. TERMALL) */
1632 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1633 if (type == T1_TYPE_RAW) {
1636 /* correction = mqc_bypass_flush_enc(); */
1637 } else { /* correction = mqc_restart_enc(); */
1643 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1644 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1645 if (type == T1_TYPE_RAW) {
1648 /* correction = mqc_bypass_flush_enc(); */
1649 } else { /* correction = mqc_restart_enc(); */
1659 if (++passtype == 3) {
1664 if (pass->term && bpno > 0) {
1665 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1666 if (type == T1_TYPE_RAW)
1667 opj_mqc_bypass_init_enc(mqc);
1669 opj_mqc_restart_init_enc(mqc);
1672 pass->distortiondec = cumwmsedec;
1673 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1675 /* Code-switch "RESET" */
1676 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1677 opj_mqc_reset_enc(mqc);
1680 /* Code switch "ERTERM" (i.e. PTERM) */
1681 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1682 opj_mqc_erterm_enc(mqc);
1683 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1686 cblk->totalpasses = passno;
1688 for (passno = 0; passno<cblk->totalpasses; passno++) {
1689 opj_tcd_pass_t *pass = &cblk->passes[passno];
1690 if (pass->rate > opj_mqc_numbytes(mqc))
1691 pass->rate = opj_mqc_numbytes(mqc);
1692 /*Preventing generation of FF as last data byte of a pass*/
1693 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1696 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1700 void opj_t1_dec_refpass(opj_t1_t *t1,
1706 OPJ_INT32 one, poshalf, neghalf;
1710 neghalf = bpno > 0 ? -poshalf : -1;
1711 for (k = 0; k < t1->h; k += 4) {
1712 for (i = 0; i < t1->w; ++i) {
1713 for (j = k; j < k + 4 && j < t1->h; ++j) {
1714 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1715 opj_t1_dec_refpass_step(
1717 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1718 &t1->data[(j * t1->w) + i],
1726 } /* VSC and BYPASS by Antonin */
1729 void opj_t1_dec_refpass_step( opj_t1_t *t1,
1740 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1741 opj_raw_t *raw = t1->raw; /* RAW component */
1743 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1744 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1745 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1746 if (type == T1_TYPE_RAW) {
1747 v = opj_raw_decode(raw);
1749 v = opj_mqc_decode(mqc);
1751 t = v ? poshalf : neghalf;
1752 *datap += *datap < 0 ? -t : t;
1753 *flagsp |= T1_REFINE;
1755 } /* VSC and BYPASS by Antonin */
1757 void opj_t1_dec_sigpass(opj_t1_t *t1,
1763 OPJ_UINT32 i, j, k, vsc;
1764 OPJ_INT32 one, half, oneplushalf;
1767 oneplushalf = one | half;
1768 for (k = 0; k < t1->h; k += 4) {
1769 for (i = 0; i < t1->w; ++i) {
1770 for (j = k; j < k + 4 && j < t1->h; ++j) {
1771 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1772 opj_t1_dec_sigpass_step(
1774 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1775 &t1->data[(j * t1->w) + i],
1783 } /* VSC and BYPASS by Antonin */
1785 void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1789 OPJ_INT32 oneplushalf,
1795 opj_raw_t *raw = t1->raw; /* RAW component */
1796 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1798 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1799 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1800 if (type == T1_TYPE_RAW) {
1801 if (opj_raw_decode(raw)) {
1802 v = opj_raw_decode(raw); /* ESSAI */
1803 *datap = v ? -oneplushalf : oneplushalf;
1804 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1807 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1808 if (opj_mqc_decode(mqc)) {
1809 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1810 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1811 *datap = v ? -oneplushalf : oneplushalf;
1812 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1815 *flagsp |= T1_VISIT;
1817 } /* VSC and BYPASS by Antonin */