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 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);
50 Encode significant pass
52 static void t1_enc_sigpass_step(
64 Decode significant pass
66 static void t1_dec_sigpass_step(
71 OPJ_INT32 oneplushalf,
75 Decode significant pass
77 static INLINE void t1_dec_sigpass_step_raw(
84 static INLINE void t1_dec_sigpass_step_mqc(
90 static INLINE void t1_dec_sigpass_step_mqc_vsc(
98 Encode significant pass
100 static void t1_enc_sigpass(
109 Decode significant pass
111 static void t1_dec_sigpass(
119 Decode significant pass
121 static void t1_dec_sigpass_raw(
126 static void t1_dec_sigpass_mqc(
130 static void t1_dec_sigpass_mqc_vsc(
135 Encode refinement pass
137 static void t1_enc_refpass_step(
147 Decode refinement pass
149 static INLINE void t1_dec_refpass_step_raw(
156 static INLINE void t1_dec_refpass_step_mqc(
162 static INLINE void t1_dec_refpass_step_mqc_vsc(
171 Encode refinement pass
173 static void t1_enc_refpass(
181 Decode refinement pass
183 static void t1_dec_refpass(
190 Decode refinement pass
192 static void t1_dec_refpass_step(
202 Decode refinement pass
204 static void t1_dec_refpass_raw(
208 static void t1_dec_refpass_mqc(
211 static void t1_dec_refpass_mqc_vsc(
217 static void t1_enc_clnpass_step(
230 static void t1_dec_clnpass_step_partial(
236 static void t1_dec_clnpass_step(
242 static void t1_dec_clnpass_step_vsc(
253 static void t1_enc_clnpass(
262 static void t1_dec_clnpass(
267 static double t1_getwmsedec(
278 static OPJ_FLOAT64 t1_getwmsedec_v2(
285 OPJ_FLOAT64 stepsize,
287 const OPJ_FLOAT64 * mct_norms);
291 @param cblk Code-block coding parameters
293 @param compno Component number
297 @param cblksty Code-block style
302 static void t1_encode_cblk(
304 opj_tcd_cblk_enc_t* cblk,
313 opj_tcd_tile_t * tile);
315 static void t1_encode_cblk_v2(
317 opj_tcd_cblk_enc_v2_t* cblk,
322 OPJ_FLOAT64 stepsize,
325 opj_tcd_tile_v2_t * tile,
326 const OPJ_FLOAT64 * mct_norms);
331 @param cblk Code-block coding parameters
333 @param roishift Region of interest shifting value
334 @param cblksty Code-block style
336 static void t1_decode_cblk(
338 opj_tcd_cblk_dec_t* cblk,
346 @param cblk Code-block coding parameters
348 @param roishift Region of interest shifting value
349 @param cblksty Code-block style
351 static void t1_decode_cblk_v2(
353 opj_tcd_cblk_dec_v2_t* cblk,
362 /* ----------------------------------------------------------------------- */
364 static char t1_getctxno_zc(int f, int orient) {
365 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
368 static char t1_getctxno_sc(int f) {
369 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
372 static int t1_getctxno_mag(int f) {
373 int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
374 int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
378 static char t1_getspb(int f) {
379 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
382 static short t1_getnmsedec_sig(int x, int bitpos) {
383 if (bitpos > T1_NMSEDEC_FRACBITS) {
384 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
387 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
390 static short t1_getnmsedec_ref(int x, int bitpos) {
391 if (bitpos > T1_NMSEDEC_FRACBITS) {
392 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
395 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
398 static void t1_updateflags(flag_t *flagsp, int s, int stride) {
399 flag_t *np = flagsp - stride;
400 flag_t *sp = flagsp + stride;
402 static const flag_t mod[] = {
403 T1_SIG_S, T1_SIG_S|T1_SGN_S,
404 T1_SIG_E, T1_SIG_E|T1_SGN_E,
405 T1_SIG_W, T1_SIG_W|T1_SGN_W,
406 T1_SIG_N, T1_SIG_N|T1_SGN_N
413 flagsp[-1] |= mod[s+2];
415 flagsp[1] |= mod[s+4];
422 static void t1_enc_sigpass_step(
435 opj_mqc_t *mqc = t1->mqc; /* MQC component */
437 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
438 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
439 v = int_abs(*datap) & one ? 1 : 0;
440 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
441 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
442 mqc_bypass_enc(mqc, v);
447 v = *datap < 0 ? 1 : 0;
448 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
449 mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
450 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
451 mqc_bypass_enc(mqc, v);
453 mqc_encode(mqc, v ^ t1_getspb(flag));
455 t1_updateflags(flagsp, v, t1->flags_stride);
461 static INLINE void t1_dec_sigpass_step_raw(
470 opj_raw_t *raw = t1->raw; /* RAW component */
472 OPJ_ARG_NOT_USED(orient);
474 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
475 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
476 if (raw_decode(raw)) {
477 v = raw_decode(raw); /* ESSAI */
478 *datap = v ? -oneplushalf : oneplushalf;
479 t1_updateflags(flagsp, v, t1->flags_stride);
483 } /* VSC and BYPASS by Antonin */
485 static INLINE void t1_dec_sigpass_step_mqc(
494 opj_mqc_t *mqc = t1->mqc; /* MQC component */
497 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
498 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
499 if (mqc_decode(mqc)) {
500 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
501 v = mqc_decode(mqc) ^ t1_getspb(flag);
502 *datap = v ? -oneplushalf : oneplushalf;
503 t1_updateflags(flagsp, v, t1->flags_stride);
507 } /* VSC and BYPASS by Antonin */
509 static INLINE void t1_dec_sigpass_step_mqc_vsc(
519 opj_mqc_t *mqc = t1->mqc; /* MQC component */
521 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
522 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
523 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
524 if (mqc_decode(mqc)) {
525 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
526 v = mqc_decode(mqc) ^ t1_getspb(flag);
527 *datap = v ? -oneplushalf : oneplushalf;
528 t1_updateflags(flagsp, v, t1->flags_stride);
532 } /* VSC and BYPASS by Antonin */
534 static void t1_enc_sigpass(
542 int i, j, k, one, vsc;
544 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
545 for (k = 0; k < t1->h; k += 4) {
546 for (i = 0; i < t1->w; ++i) {
547 for (j = k; j < k + 4 && j < t1->h; ++j) {
548 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
551 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
552 &t1->data[(j * t1->w) + i],
564 static void t1_dec_sigpass_raw(
570 int i, j, k, one, half, oneplushalf, vsc;
573 oneplushalf = one | half;
574 for (k = 0; k < t1->h; k += 4) {
575 for (i = 0; i < t1->w; ++i) {
576 for (j = k; j < k + 4 && j < t1->h; ++j) {
577 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
578 t1_dec_sigpass_step_raw(
580 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
581 &t1->data[(j * t1->w) + i],
588 } /* VSC and BYPASS by Antonin */
590 static void t1_dec_sigpass_mqc(
595 int i, j, k, one, half, oneplushalf;
596 int *data1 = t1->data;
597 flag_t *flags1 = &t1->flags[1];
600 oneplushalf = one | half;
601 for (k = 0; k < (t1->h & ~3); k += 4) {
602 for (i = 0; i < t1->w; ++i) {
603 int *data2 = data1 + i;
604 flag_t *flags2 = flags1 + i;
605 flags2 += t1->flags_stride;
606 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
608 flags2 += t1->flags_stride;
609 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
611 flags2 += t1->flags_stride;
612 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
614 flags2 += t1->flags_stride;
615 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
619 flags1 += t1->flags_stride << 2;
621 for (i = 0; i < t1->w; ++i) {
622 int *data2 = data1 + i;
623 flag_t *flags2 = flags1 + i;
624 for (j = k; j < t1->h; ++j) {
625 flags2 += t1->flags_stride;
626 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
630 } /* VSC and BYPASS by Antonin */
632 static void t1_dec_sigpass_mqc_vsc(
637 int i, j, k, one, half, oneplushalf, vsc;
640 oneplushalf = one | half;
641 for (k = 0; k < t1->h; k += 4) {
642 for (i = 0; i < t1->w; ++i) {
643 for (j = k; j < k + 4 && j < t1->h; ++j) {
644 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
645 t1_dec_sigpass_step_mqc_vsc(
647 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
648 &t1->data[(j * t1->w) + i],
655 } /* VSC and BYPASS by Antonin */
657 static void t1_enc_refpass_step(
669 opj_mqc_t *mqc = t1->mqc; /* MQC component */
671 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
672 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
673 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
674 v = int_abs(*datap) & one ? 1 : 0;
675 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
676 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
677 mqc_bypass_enc(mqc, v);
681 *flagsp |= T1_REFINE;
685 static INLINE void t1_dec_refpass_step_raw(
695 opj_raw_t *raw = t1->raw; /* RAW component */
697 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
698 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
700 t = v ? poshalf : neghalf;
701 *datap += *datap < 0 ? -t : t;
702 *flagsp |= T1_REFINE;
704 } /* VSC and BYPASS by Antonin */
706 static INLINE void t1_dec_refpass_step_mqc(
715 opj_mqc_t *mqc = t1->mqc; /* MQC component */
718 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
719 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
721 t = v ? poshalf : neghalf;
722 *datap += *datap < 0 ? -t : t;
723 *flagsp |= T1_REFINE;
725 } /* VSC and BYPASS by Antonin */
727 static INLINE void t1_dec_refpass_step_mqc_vsc(
737 opj_mqc_t *mqc = t1->mqc; /* MQC component */
739 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
740 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
741 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
743 t = v ? poshalf : neghalf;
744 *datap += *datap < 0 ? -t : t;
745 *flagsp |= T1_REFINE;
747 } /* VSC and BYPASS by Antonin */
749 static void t1_enc_refpass(
756 int i, j, k, one, vsc;
758 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
759 for (k = 0; k < t1->h; k += 4) {
760 for (i = 0; i < t1->w; ++i) {
761 for (j = k; j < k + 4 && j < t1->h; ++j) {
762 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
765 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
766 &t1->data[(j * t1->w) + i],
777 static void t1_dec_refpass_raw(
782 int i, j, k, one, poshalf, neghalf;
786 neghalf = bpno > 0 ? -poshalf : -1;
787 for (k = 0; k < t1->h; k += 4) {
788 for (i = 0; i < t1->w; ++i) {
789 for (j = k; j < k + 4 && j < t1->h; ++j) {
790 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
791 t1_dec_refpass_step_raw(
793 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
794 &t1->data[(j * t1->w) + i],
801 } /* VSC and BYPASS by Antonin */
803 static void t1_dec_refpass_mqc(
807 int i, j, k, one, poshalf, neghalf;
808 int *data1 = t1->data;
809 flag_t *flags1 = &t1->flags[1];
812 neghalf = bpno > 0 ? -poshalf : -1;
813 for (k = 0; k < (t1->h & ~3); k += 4) {
814 for (i = 0; i < t1->w; ++i) {
815 int *data2 = data1 + i;
816 flag_t *flags2 = flags1 + i;
817 flags2 += t1->flags_stride;
818 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
820 flags2 += t1->flags_stride;
821 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
823 flags2 += t1->flags_stride;
824 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
826 flags2 += t1->flags_stride;
827 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
831 flags1 += t1->flags_stride << 2;
833 for (i = 0; i < t1->w; ++i) {
834 int *data2 = data1 + i;
835 flag_t *flags2 = flags1 + i;
836 for (j = k; j < t1->h; ++j) {
837 flags2 += t1->flags_stride;
838 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
842 } /* VSC and BYPASS by Antonin */
844 static void t1_dec_refpass_mqc_vsc(
848 int i, j, k, one, poshalf, neghalf;
852 neghalf = bpno > 0 ? -poshalf : -1;
853 for (k = 0; k < t1->h; k += 4) {
854 for (i = 0; i < t1->w; ++i) {
855 for (j = k; j < k + 4 && j < t1->h; ++j) {
856 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
857 t1_dec_refpass_step_mqc_vsc(
859 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
860 &t1->data[(j * t1->w) + i],
867 } /* VSC and BYPASS by Antonin */
869 static void t1_enc_clnpass_step(
882 opj_mqc_t *mqc = t1->mqc; /* MQC component */
884 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
888 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
889 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
890 v = int_abs(*datap) & one ? 1 : 0;
894 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
895 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
896 v = *datap < 0 ? 1 : 0;
897 mqc_encode(mqc, v ^ t1_getspb(flag));
898 t1_updateflags(flagsp, v, t1->flags_stride);
901 *flagsp &= ~T1_VISIT;
904 static void t1_dec_clnpass_step_partial(
912 opj_mqc_t *mqc = t1->mqc; /* MQC component */
914 OPJ_ARG_NOT_USED(orient);
917 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
918 v = mqc_decode(mqc) ^ t1_getspb(flag);
919 *datap = v ? -oneplushalf : oneplushalf;
920 t1_updateflags(flagsp, v, t1->flags_stride);
921 *flagsp &= ~T1_VISIT;
922 } /* VSC and BYPASS by Antonin */
924 static void t1_dec_clnpass_step(
933 opj_mqc_t *mqc = t1->mqc; /* MQC component */
936 if (!(flag & (T1_SIG | T1_VISIT))) {
937 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
938 if (mqc_decode(mqc)) {
939 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
940 v = mqc_decode(mqc) ^ t1_getspb(flag);
941 *datap = v ? -oneplushalf : oneplushalf;
942 t1_updateflags(flagsp, v, t1->flags_stride);
945 *flagsp &= ~T1_VISIT;
946 } /* VSC and BYPASS by Antonin */
948 static void t1_dec_clnpass_step_vsc(
959 opj_mqc_t *mqc = t1->mqc; /* MQC component */
961 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
965 if (!(flag & (T1_SIG | T1_VISIT))) {
966 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
967 if (mqc_decode(mqc)) {
969 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
970 v = mqc_decode(mqc) ^ t1_getspb(flag);
971 *datap = v ? -oneplushalf : oneplushalf;
972 t1_updateflags(flagsp, v, t1->flags_stride);
975 *flagsp &= ~T1_VISIT;
978 static void t1_enc_clnpass(
985 int i, j, k, one, agg, runlen, vsc;
987 opj_mqc_t *mqc = t1->mqc; /* MQC component */
990 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
991 for (k = 0; k < t1->h; k += 4) {
992 for (i = 0; i < t1->w; ++i) {
994 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
995 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
996 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
997 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
998 || (MACRO_t1_flags(1 + k + 3,1 + i)
999 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1001 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1002 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1003 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1004 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1010 for (runlen = 0; runlen < 4; ++runlen) {
1011 if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
1014 mqc_setcurctx(mqc, T1_CTXNO_AGG);
1015 mqc_encode(mqc, runlen != 4);
1019 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1020 mqc_encode(mqc, runlen >> 1);
1021 mqc_encode(mqc, runlen & 1);
1025 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1026 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1027 t1_enc_clnpass_step(
1029 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1030 &t1->data[(j * t1->w) + i],
1035 agg && (j == k + runlen),
1042 static void t1_dec_clnpass(
1048 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
1049 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1051 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1055 oneplushalf = one | half;
1056 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1057 for (k = 0; k < t1->h; k += 4) {
1058 for (i = 0; i < t1->w; ++i) {
1059 if (k + 3 < t1->h) {
1060 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1061 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1062 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1063 || (MACRO_t1_flags(1 + k + 3,1 + i)
1064 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1069 mqc_setcurctx(mqc, T1_CTXNO_AGG);
1070 if (!mqc_decode(mqc)) {
1073 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1074 runlen = mqc_decode(mqc);
1075 runlen = (runlen << 1) | mqc_decode(mqc);
1079 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1080 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1081 t1_dec_clnpass_step_vsc(
1083 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1084 &t1->data[(j * t1->w) + i],
1087 agg && (j == k + runlen),
1093 int *data1 = t1->data;
1094 flag_t *flags1 = &t1->flags[1];
1095 for (k = 0; k < (t1->h & ~3); k += 4) {
1096 for (i = 0; i < t1->w; ++i) {
1097 int *data2 = data1 + i;
1098 flag_t *flags2 = flags1 + i;
1099 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1100 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1101 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1102 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1104 mqc_setcurctx(mqc, T1_CTXNO_AGG);
1105 if (!mqc_decode(mqc)) {
1108 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1109 runlen = mqc_decode(mqc);
1110 runlen = (runlen << 1) | mqc_decode(mqc);
1111 flags2 += runlen * t1->flags_stride;
1112 data2 += runlen * t1->w;
1113 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1114 flags2 += t1->flags_stride;
1115 if (agg && (j == k + runlen)) {
1116 t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1118 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1123 flags2 += t1->flags_stride;
1124 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1126 flags2 += t1->flags_stride;
1127 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1129 flags2 += t1->flags_stride;
1130 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1132 flags2 += t1->flags_stride;
1133 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1137 data1 += t1->w << 2;
1138 flags1 += t1->flags_stride << 2;
1140 for (i = 0; i < t1->w; ++i) {
1141 int *data2 = data1 + i;
1142 flag_t *flags2 = flags1 + i;
1143 for (j = k; j < t1->h; ++j) {
1144 flags2 += t1->flags_stride;
1145 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1153 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1154 v = mqc_decode(mqc);
1155 v = (v << 1) | mqc_decode(mqc);
1156 v = (v << 1) | mqc_decode(mqc);
1157 v = (v << 1) | mqc_decode(mqc);
1160 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1164 } /* VSC and BYPASS by Antonin */
1167 /** mod fixed_quality */
1168 static double t1_getwmsedec(
1179 double w1, w2, wmsedec;
1181 w1 = (mct && numcomps==3) ? mct_getnorm(compno) : 1.0;
1182 w2 = dwt_getnorm(level, orient);
1183 } else { /* if (qmfbid == 0) */
1184 w1 = (mct && numcomps==3) ? mct_getnorm_real(compno) : 1.0;
1185 w2 = dwt_getnorm_real(level, orient);
1187 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1188 wmsedec *= wmsedec * nmsedec / 8192.0;
1193 /** mod fixed_quality */
1194 static OPJ_FLOAT64 t1_getwmsedec_v2(
1201 OPJ_FLOAT64 stepsize,
1202 OPJ_UINT32 numcomps,
1203 const OPJ_FLOAT64 * mct_norms)
1205 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1208 w1 = mct_norms[compno];
1212 w2 = dwt_getnorm(level, orient);
1213 } else { /* if (qmfbid == 0) */
1214 w2 = dwt_getnorm_real(level, orient);
1217 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1218 wmsedec *= wmsedec * nmsedec / 8192.0;
1223 static opj_bool allocate_buffers(
1231 if(datasize > t1->datasize){
1232 opj_aligned_free(t1->data);
1233 t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int));
1237 t1->datasize=datasize;
1239 memset(t1->data,0,datasize * sizeof(int));
1241 t1->flags_stride=w+2;
1242 flagssize=t1->flags_stride * (h+2);
1244 if(flagssize > t1->flagssize){
1245 opj_aligned_free(t1->flags);
1246 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
1250 t1->flagssize=flagssize;
1252 memset(t1->flags,0,flagssize * sizeof(flag_t));
1260 /** mod fixed_quality */
1261 static void t1_encode_cblk(
1263 opj_tcd_cblk_enc_t* cblk,
1272 opj_tcd_tile_t * tile)
1274 double cumwmsedec = 0.0;
1276 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1278 int passno, bpno, passtype;
1281 char type = T1_TYPE_MQ;
1285 for (i = 0; i < t1->w * t1->h; ++i) {
1286 int tmp = abs(t1->data[i]);
1287 max = int_max(max, tmp);
1290 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1292 bpno = cblk->numbps - 1;
1295 mqc_resetstates(mqc);
1296 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1297 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1298 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1299 mqc_init_enc(mqc, cblk->data);
1301 for (passno = 0; bpno >= 0; ++passno) {
1302 opj_tcd_pass_t *pass = &cblk->passes[passno];
1304 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1308 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1311 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1314 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1315 /* code switch SEGMARK (i.e. SEGSYM) */
1316 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1317 mqc_segmark_enc(mqc);
1322 tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps, mct);
1323 cumwmsedec += tempwmsedec;
1324 tile->distotile += tempwmsedec;
1326 /* Code switch "RESTART" (i.e. TERMALL) */
1327 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1328 if (type == T1_TYPE_RAW) {
1331 /* correction = mqc_bypass_flush_enc(); */
1332 } else { /* correction = mqc_restart_enc(); */
1338 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
1339 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1340 if (type == T1_TYPE_RAW) {
1343 /* correction = mqc_bypass_flush_enc(); */
1344 } else { /* correction = mqc_restart_enc(); */
1354 if (++passtype == 3) {
1359 if (pass->term && bpno > 0) {
1360 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1361 if (type == T1_TYPE_RAW)
1362 mqc_bypass_init_enc(mqc);
1364 mqc_restart_init_enc(mqc);
1367 pass->distortiondec = cumwmsedec;
1368 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
1370 /* Code-switch "RESET" */
1371 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1375 /* Code switch "ERTERM" (i.e. PTERM) */
1376 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1377 mqc_erterm_enc(mqc);
1378 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1381 cblk->totalpasses = passno;
1383 for (passno = 0; passno<cblk->totalpasses; passno++) {
1384 opj_tcd_pass_t *pass = &cblk->passes[passno];
1385 if (pass->rate > mqc_numbytes(mqc))
1386 pass->rate = mqc_numbytes(mqc);
1387 /*Preventing generation of FF as last data byte of a pass*/
1388 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1391 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1395 static void t1_decode_cblk(
1397 opj_tcd_cblk_dec_t* cblk,
1402 opj_raw_t *raw = t1->raw; /* RAW component */
1403 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1407 char type = T1_TYPE_MQ; /* BYPASS mode */
1409 if(!allocate_buffers(
1411 cblk->x1 - cblk->x0,
1412 cblk->y1 - cblk->y0))
1417 bpno = roishift + cblk->numbps - 1;
1420 mqc_resetstates(mqc);
1421 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1422 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1423 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1425 for (segno = 0; segno < cblk->numsegs; ++segno) {
1426 opj_tcd_seg_t *seg = &cblk->segs[segno];
1429 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1430 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1431 if(seg->data == NULL){
1434 if (type == T1_TYPE_RAW) {
1435 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1437 mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len);
1440 for (passno = 0; passno < seg->numpasses; ++passno) {
1443 if (type == T1_TYPE_RAW) {
1444 t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1446 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1447 t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1449 t1_dec_sigpass_mqc(t1, bpno+1, orient);
1454 if (type == T1_TYPE_RAW) {
1455 t1_dec_refpass_raw(t1, bpno+1, cblksty);
1457 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1458 t1_dec_refpass_mqc_vsc(t1, bpno+1);
1460 t1_dec_refpass_mqc(t1, bpno+1);
1465 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1469 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1470 mqc_resetstates(mqc);
1471 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1472 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1473 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1475 if (++passtype == 3) {
1483 /* ----------------------------------------------------------------------- */
1485 opj_t1_t* t1_create(opj_common_ptr cinfo) {
1486 opj_t1_t *t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1491 /* create MQC and RAW handles */
1492 t1->mqc = mqc_create();
1493 t1->raw = raw_create();
1503 void t1_destroy(opj_t1_t *t1) {
1505 /* destroy MQC and RAW handles */
1506 mqc_destroy(t1->mqc);
1507 raw_destroy(t1->raw);
1508 opj_aligned_free(t1->data);
1509 opj_aligned_free(t1->flags);
1514 void t1_encode_cblks(
1516 opj_tcd_tile_t *tile,
1519 int compno, resno, bandno, precno, cblkno;
1521 tile->distotile = 0; /* fixed_quality */
1523 for (compno = 0; compno < tile->numcomps; ++compno) {
1524 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1525 opj_tccp_t* tccp = &tcp->tccps[compno];
1526 int tile_w = tilec->x1 - tilec->x0;
1528 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1529 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1531 for (bandno = 0; bandno < res->numbands; ++bandno) {
1532 opj_tcd_band_t* restrict band = &res->bands[bandno];
1533 int bandconst = 8192 * 8192 / ((int) floor(band->stepsize * 8192));
1535 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1536 opj_tcd_precinct_t *prc = &band->precincts[precno];
1538 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1539 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1540 int* restrict datap;
1541 int* restrict tiledp;
1546 int x = cblk->x0 - band->x0;
1547 int y = cblk->y0 - band->y0;
1548 if (band->bandno & 1) {
1549 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1550 x += pres->x1 - pres->x0;
1552 if (band->bandno & 2) {
1553 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1554 y += pres->y1 - pres->y0;
1557 if(!allocate_buffers(
1559 cblk->x1 - cblk->x0,
1560 cblk->y1 - cblk->y0))
1569 tiledp=&tilec->data[(y * tile_w) + x];
1570 if (tccp->qmfbid == 1) {
1571 for (j = 0; j < cblk_h; ++j) {
1572 for (i = 0; i < cblk_w; ++i) {
1573 int tmp = tiledp[(j * tile_w) + i];
1574 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1577 } else { /* if (tccp->qmfbid == 0) */
1578 for (j = 0; j < cblk_h; ++j) {
1579 for (i = 0; i < cblk_w; ++i) {
1580 int tmp = tiledp[(j * tile_w) + i];
1581 datap[(j * cblk_w) + i] =
1584 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1594 tilec->numresolutions - 1 - resno,
1609 void t1_decode_cblks(
1611 opj_tcd_tilecomp_t* tilec,
1614 int resno, bandno, precno, cblkno;
1616 int tile_w = tilec->x1 - tilec->x0;
1618 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1619 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1621 for (bandno = 0; bandno < res->numbands; ++bandno) {
1622 opj_tcd_band_t* restrict band = &res->bands[bandno];
1624 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1625 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1627 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1628 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1629 int* restrict datap;
1641 x = cblk->x0 - band->x0;
1642 y = cblk->y0 - band->y0;
1643 if (band->bandno & 1) {
1644 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1645 x += pres->x1 - pres->x0;
1647 if (band->bandno & 2) {
1648 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1649 y += pres->y1 - pres->y0;
1656 if (tccp->roishift) {
1657 int thresh = 1 << tccp->roishift;
1658 for (j = 0; j < cblk_h; ++j) {
1659 for (i = 0; i < cblk_w; ++i) {
1660 int val = datap[(j * cblk_w) + i];
1662 if (mag >= thresh) {
1663 mag >>= tccp->roishift;
1664 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1670 if (tccp->qmfbid == 1) {
1671 int* restrict tiledp = &tilec->data[(y * tile_w) + x];
1672 for (j = 0; j < cblk_h; ++j) {
1673 for (i = 0; i < cblk_w; ++i) {
1674 int tmp = datap[(j * cblk_w) + i];
1675 ((int*)tiledp)[(j * tile_w) + i] = tmp / 2;
1678 } else { /* if (tccp->qmfbid == 0) */
1679 float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
1680 for (j = 0; j < cblk_h; ++j) {
1681 float* restrict tiledp2 = tiledp;
1682 for (i = 0; i < cblk_w; ++i) {
1683 float tmp = *datap * band->stepsize;
1691 opj_free(cblk->data);
1692 opj_free(cblk->segs);
1694 opj_free(precinct->cblks.dec);
1702 /* ----------------------------------------------------------------------- */
1704 * Creates a new Tier 1 handle
1705 * and initializes the look-up tables of the Tier-1 coder/decoder
1706 * @return a new T1 handle if successful, returns NULL otherwise
1708 opj_t1_t* t1_create_v2()
1710 opj_t1_t *l_t1 = 00;
1712 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1716 memset(l_t1,0,sizeof(opj_t1_t));
1718 /* create MQC and RAW handles */
1719 l_t1->mqc = mqc_create();
1725 l_t1->raw = raw_create();
1736 * Destroys a previously created T1 handle
1738 * @param p_t1 Tier 1 handle to destroy
1740 void t1_destroy_v2(opj_t1_t *p_t1)
1748 /* destroy MQC and RAW handles */
1749 mqc_destroy(p_t1->mqc);
1751 raw_destroy(p_t1->raw);
1756 opj_aligned_free(p_t1->data);
1762 opj_aligned_free(p_t1->flags);
1768 void t1_decode_cblks_v2(
1770 opj_tcd_tilecomp_v2_t* tilec,
1773 OPJ_UINT32 resno, bandno, precno, cblkno;
1774 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1776 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1777 opj_tcd_resolution_v2_t* res = &tilec->resolutions[resno];
1779 for (bandno = 0; bandno < res->numbands; ++bandno) {
1780 opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
1782 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1783 opj_tcd_precinct_v2_t* precinct = &band->precincts[precno];
1785 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1786 opj_tcd_cblk_dec_v2_t* cblk = &precinct->cblks.dec[cblkno];
1787 OPJ_INT32* restrict datap;
1788 void* restrict tiledp;
1789 OPJ_UINT32 cblk_w, cblk_h;
1800 x = cblk->x0 - band->x0;
1801 y = cblk->y0 - band->y0;
1802 if (band->bandno & 1) {
1803 opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
1804 x += pres->x1 - pres->x0;
1806 if (band->bandno & 2) {
1807 opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
1808 y += pres->y1 - pres->y0;
1815 if (tccp->roishift) {
1816 OPJ_INT32 thresh = 1 << tccp->roishift;
1817 for (j = 0; j < cblk_h; ++j) {
1818 for (i = 0; i < cblk_w; ++i) {
1819 OPJ_INT32 val = datap[(j * cblk_w) + i];
1820 OPJ_INT32 mag = abs(val);
1821 if (mag >= thresh) {
1822 mag >>= tccp->roishift;
1823 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1829 tiledp=(void*)&tilec->data[(y * tile_w) + x];
1830 if (tccp->qmfbid == 1) {
1831 for (j = 0; j < cblk_h; ++j) {
1832 for (i = 0; i < cblk_w; ++i) {
1833 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1834 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1837 } else { /* if (tccp->qmfbid == 0) */
1838 for (j = 0; j < cblk_h; ++j) {
1839 for (i = 0; i < cblk_w; ++i) {
1840 float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1841 ((float*)tiledp)[(j * tile_w) + i] = tmp;
1845 //opj_free(cblk->segs);
1854 static void t1_decode_cblk_v2(
1856 opj_tcd_cblk_dec_v2_t* cblk,
1858 OPJ_UINT32 roishift,
1861 opj_raw_t *raw = t1->raw; /* RAW component */
1862 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1865 OPJ_UINT32 passtype;
1866 OPJ_UINT32 segno, passno;
1867 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1869 if(!allocate_buffers(
1871 cblk->x1 - cblk->x0,
1872 cblk->y1 - cblk->y0))
1877 bpno = roishift + cblk->numbps - 1;
1880 mqc_resetstates(mqc);
1881 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1882 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1883 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1885 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1886 opj_tcd_seg_t *seg = &cblk->segs[segno];
1889 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1890 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1891 if(seg->data == 00){
1894 if (type == T1_TYPE_RAW) {
1895 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1897 mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len);
1900 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1903 t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1906 t1_dec_refpass(t1, bpno+1, type, cblksty);
1909 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1913 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1914 mqc_resetstates(mqc);
1915 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1916 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1917 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1919 if (++passtype == 3) {
1927 opj_bool t1_encode_cblks_v2(
1929 opj_tcd_tile_v2_t *tile,
1931 const OPJ_FLOAT64 * mct_norms)
1933 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1935 tile->distotile = 0; /* fixed_quality */
1937 for (compno = 0; compno < tile->numcomps; ++compno) {
1938 opj_tcd_tilecomp_v2_t* tilec = &tile->comps[compno];
1939 opj_tccp_t* tccp = &tcp->tccps[compno];
1940 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1942 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1943 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1945 for (bandno = 0; bandno < res->numbands; ++bandno) {
1946 opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
1948 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1949 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1951 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1952 opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno];
1953 OPJ_INT32 * restrict datap;
1954 OPJ_INT32* restrict tiledp;
1959 OPJ_INT32 x = cblk->x0 - band->x0;
1960 OPJ_INT32 y = cblk->y0 - band->y0;
1961 if (band->bandno & 1) {
1962 opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
1963 x += pres->x1 - pres->x0;
1965 if (band->bandno & 2) {
1966 opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
1967 y += pres->y1 - pres->y0;
1970 if(!allocate_buffers(
1972 cblk->x1 - cblk->x0,
1973 cblk->y1 - cblk->y0))
1982 tiledp=&tilec->data[(y * tile_w) + x];
1983 if (tccp->qmfbid == 1) {
1984 for (j = 0; j < cblk_h; ++j) {
1985 for (i = 0; i < cblk_w; ++i) {
1986 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1987 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1990 } else { /* if (tccp->qmfbid == 0) */
1991 for (j = 0; j < cblk_h; ++j) {
1992 for (i = 0; i < cblk_w; ++i) {
1993 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1994 datap[(j * cblk_w) + i] =
1997 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS);
2007 tilec->numresolutions - 1 - resno,
2023 /** mod fixed_quality */
2024 static void t1_encode_cblk_v2(
2026 opj_tcd_cblk_enc_v2_t* cblk,
2031 OPJ_FLOAT64 stepsize,
2033 OPJ_UINT32 numcomps,
2034 opj_tcd_tile_v2_t * tile,
2035 const OPJ_FLOAT64 * mct_norms)
2037 OPJ_FLOAT64 cumwmsedec = 0.0;
2039 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2043 OPJ_UINT32 passtype;
2044 OPJ_INT32 nmsedec = 0;
2047 OPJ_BYTE type = T1_TYPE_MQ;
2048 OPJ_FLOAT64 tempwmsedec;
2051 for (i = 0; i < t1->w * t1->h; ++i) {
2052 OPJ_INT32 tmp = abs(t1->data[i]);
2053 max = int_max(max, tmp);
2056 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
2058 bpno = cblk->numbps - 1;
2061 mqc_resetstates(mqc);
2062 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2063 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2064 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2065 mqc_init_enc(mqc, cblk->data);
2067 for (passno = 0; bpno >= 0; ++passno) {
2068 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
2069 OPJ_UINT32 correction = 3;
2070 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2074 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
2077 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2080 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
2081 /* code switch SEGMARK (i.e. SEGSYM) */
2082 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
2083 mqc_segmark_enc(mqc);
2088 tempwmsedec = t1_getwmsedec_v2(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
2089 cumwmsedec += tempwmsedec;
2090 tile->distotile += tempwmsedec;
2092 /* Code switch "RESTART" (i.e. TERMALL) */
2093 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
2094 if (type == T1_TYPE_RAW) {
2097 /* correction = mqc_bypass_flush_enc(); */
2098 } else { /* correction = mqc_restart_enc(); */
2104 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
2105 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2106 if (type == T1_TYPE_RAW) {
2109 /* correction = mqc_bypass_flush_enc(); */
2110 } else { /* correction = mqc_restart_enc(); */
2120 if (++passtype == 3) {
2125 if (pass->term && bpno > 0) {
2126 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2127 if (type == T1_TYPE_RAW)
2128 mqc_bypass_init_enc(mqc);
2130 mqc_restart_init_enc(mqc);
2133 pass->distortiondec = cumwmsedec;
2134 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
2136 /* Code-switch "RESET" */
2137 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2141 /* Code switch "ERTERM" (i.e. PTERM) */
2142 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2143 mqc_erterm_enc(mqc);
2144 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2147 cblk->totalpasses = passno;
2149 for (passno = 0; passno<cblk->totalpasses; passno++) {
2150 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
2151 if (pass->rate > mqc_numbytes(mqc))
2152 pass->rate = mqc_numbytes(mqc);
2153 /*Preventing generation of FF as last data byte of a pass*/
2154 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2157 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2162 static void t1_dec_refpass(
2169 OPJ_INT32 one, poshalf, neghalf;
2173 neghalf = bpno > 0 ? -poshalf : -1;
2174 for (k = 0; k < t1->h; k += 4) {
2175 for (i = 0; i < t1->w; ++i) {
2176 for (j = k; j < k + 4 && j < t1->h; ++j) {
2177 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
2178 t1_dec_refpass_step(
2180 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
2181 &t1->data[(j * t1->w) + i],
2189 } /* VSC and BYPASS by Antonin */
2192 static void t1_dec_refpass_step(
2204 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2205 opj_raw_t *raw = t1->raw; /* RAW component */
2207 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2208 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2209 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
2210 if (type == T1_TYPE_RAW) {
2211 v = raw_decode(raw);
2213 v = mqc_decode(mqc);
2215 t = v ? poshalf : neghalf;
2216 *datap += *datap < 0 ? -t : t;
2217 *flagsp |= T1_REFINE;
2219 } /* VSC and BYPASS by Antonin */
2221 static void t1_dec_sigpass(
2228 OPJ_UINT32 i, j, k, vsc;
2229 OPJ_INT32 one, half, oneplushalf;
2232 oneplushalf = one | half;
2233 for (k = 0; k < t1->h; k += 4) {
2234 for (i = 0; i < t1->w; ++i) {
2235 for (j = k; j < k + 4 && j < t1->h; ++j) {
2236 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
2237 t1_dec_sigpass_step(
2239 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
2240 &t1->data[(j * t1->w) + i],
2248 } /* VSC and BYPASS by Antonin */
2250 static void t1_dec_sigpass_step(
2255 OPJ_INT32 oneplushalf,
2261 opj_raw_t *raw = t1->raw; /* RAW component */
2262 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2264 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2265 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2266 if (type == T1_TYPE_RAW) {
2267 if (raw_decode(raw)) {
2268 v = raw_decode(raw); /* ESSAI */
2269 *datap = v ? -oneplushalf : oneplushalf;
2270 t1_updateflags(flagsp, v, t1->flags_stride);
2273 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
2274 if (mqc_decode(mqc)) {
2275 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
2276 v = mqc_decode(mqc) ^ t1_getspb(flag);
2277 *datap = v ? -oneplushalf : oneplushalf;
2278 t1_updateflags(flagsp, v, t1->flags_stride);
2281 *flagsp |= T1_VISIT;
2283 } /* VSC and BYPASS by Antonin */