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 opj_t1_enc_sigpass_step(opj_t1_t *t1,
63 Decode significant pass
65 static void t1_dec_sigpass_step(
70 OPJ_INT32 oneplushalf,
74 Decode significant pass
76 static INLINE void t1_dec_sigpass_step_raw(
83 static INLINE void t1_dec_sigpass_step_mqc(
89 static INLINE void t1_dec_sigpass_step_mqc_vsc(
97 Encode significant pass
99 static void opj_t1_enc_sigpass( opj_t1_t *t1,
107 Decode significant pass
109 static void t1_dec_sigpass(
117 Decode significant pass
119 static void t1_dec_sigpass_raw(
124 static void t1_dec_sigpass_mqc(
128 static void t1_dec_sigpass_mqc_vsc(
133 Encode refinement pass
135 static void t1_enc_refpass_step(
145 Decode refinement pass
147 static INLINE void t1_dec_refpass_step_raw(
154 static INLINE void t1_dec_refpass_step_mqc(
160 static INLINE void t1_dec_refpass_step_mqc_vsc(
169 Encode refinement pass
171 static void t1_enc_refpass(
179 Decode refinement pass
181 static void t1_dec_refpass(
188 Decode refinement pass
190 static void t1_dec_refpass_step(
200 Decode refinement pass
202 static void t1_dec_refpass_raw(
206 static void t1_dec_refpass_mqc(
209 static void t1_dec_refpass_mqc_vsc(
215 static void t1_enc_clnpass_step(
228 static void t1_dec_clnpass_step_partial(
234 static void t1_dec_clnpass_step(
240 static void t1_dec_clnpass_step_vsc(
251 static void t1_enc_clnpass(
260 static void t1_dec_clnpass(
265 static double t1_getwmsedec(
276 static OPJ_FLOAT64 t1_getwmsedec_v2(
283 OPJ_FLOAT64 stepsize,
285 const OPJ_FLOAT64 * mct_norms);
289 @param cblk Code-block coding parameters
291 @param compno Component number
295 @param cblksty Code-block style
300 static void t1_encode_cblk(
302 opj_tcd_cblk_enc_t* cblk,
311 opj_tcd_tile_t * tile);
313 static void t1_encode_cblk_v2(
315 opj_tcd_cblk_enc_v2_t* cblk,
320 OPJ_FLOAT64 stepsize,
323 opj_tcd_tile_v2_t * tile,
324 const OPJ_FLOAT64 * mct_norms);
329 @param cblk Code-block coding parameters
331 @param roishift Region of interest shifting value
332 @param cblksty Code-block style
335 static opj_bool t1_decode_cblk(
337 opj_tcd_cblk_dec_t* cblk,
345 @param cblk Code-block coding parameters
347 @param roishift Region of interest shifting value
348 @param cblksty Code-block style
350 static opj_bool t1_decode_cblk_v2(
352 opj_tcd_cblk_dec_v2_t* cblk,
361 /* ----------------------------------------------------------------------- */
363 static char t1_getctxno_zc(int f, int orient) {
364 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
367 static char t1_getctxno_sc(int f) {
368 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
371 static int t1_getctxno_mag(int f) {
372 int tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
373 int tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
377 static char t1_getspb(int f) {
378 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
381 static short t1_getnmsedec_sig(int x, int bitpos) {
382 if (bitpos > T1_NMSEDEC_FRACBITS) {
383 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
386 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
389 static short t1_getnmsedec_ref(int x, int bitpos) {
390 if (bitpos > T1_NMSEDEC_FRACBITS) {
391 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
394 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
397 static void t1_updateflags(flag_t *flagsp, int s, int stride) {
398 flag_t *np = flagsp - stride;
399 flag_t *sp = flagsp + stride;
401 static const flag_t mod[] = {
402 T1_SIG_S, T1_SIG_S|T1_SGN_S,
403 T1_SIG_E, T1_SIG_E|T1_SGN_E,
404 T1_SIG_W, T1_SIG_W|T1_SGN_W,
405 T1_SIG_N, T1_SIG_N|T1_SGN_N
412 flagsp[-1] |= mod[s+2];
414 flagsp[1] |= mod[s+4];
421 void opj_t1_enc_sigpass_step( opj_t1_t *t1,
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 void opj_t1_enc_sigpass(opj_t1_t *t1,
542 OPJ_UINT32 i, j, k, vsc;
546 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
547 for (k = 0; k < t1->h; k += 4) {
548 for (i = 0; i < t1->w; ++i) {
549 for (j = k; j < k + 4 && j < t1->h; ++j) {
550 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
551 opj_t1_enc_sigpass_step(
553 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
554 &t1->data[(j * t1->w) + i],
566 static void t1_dec_sigpass_raw(
572 int i, j, k, one, half, oneplushalf, vsc;
575 oneplushalf = one | half;
576 for (k = 0; k < t1->h; k += 4) {
577 for (i = 0; i < t1->w; ++i) {
578 for (j = k; j < k + 4 && j < t1->h; ++j) {
579 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
580 t1_dec_sigpass_step_raw(
582 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
583 &t1->data[(j * t1->w) + i],
590 } /* VSC and BYPASS by Antonin */
592 static void t1_dec_sigpass_mqc(
597 int i, j, k, one, half, oneplushalf;
598 int *data1 = t1->data;
599 flag_t *flags1 = &t1->flags[1];
602 oneplushalf = one | half;
603 for (k = 0; k < (t1->h & ~3); k += 4) {
604 for (i = 0; i < t1->w; ++i) {
605 int *data2 = data1 + i;
606 flag_t *flags2 = flags1 + i;
607 flags2 += t1->flags_stride;
608 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
610 flags2 += t1->flags_stride;
611 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
613 flags2 += t1->flags_stride;
614 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
616 flags2 += t1->flags_stride;
617 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
621 flags1 += t1->flags_stride << 2;
623 for (i = 0; i < t1->w; ++i) {
624 int *data2 = data1 + i;
625 flag_t *flags2 = flags1 + i;
626 for (j = k; j < t1->h; ++j) {
627 flags2 += t1->flags_stride;
628 t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
632 } /* VSC and BYPASS by Antonin */
634 static void t1_dec_sigpass_mqc_vsc(
639 int i, j, k, one, half, oneplushalf, vsc;
642 oneplushalf = one | half;
643 for (k = 0; k < t1->h; k += 4) {
644 for (i = 0; i < t1->w; ++i) {
645 for (j = k; j < k + 4 && j < t1->h; ++j) {
646 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
647 t1_dec_sigpass_step_mqc_vsc(
649 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
650 &t1->data[(j * t1->w) + i],
657 } /* VSC and BYPASS by Antonin */
659 static void t1_enc_refpass_step(
671 opj_mqc_t *mqc = t1->mqc; /* MQC component */
673 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
674 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
675 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
676 v = int_abs(*datap) & one ? 1 : 0;
677 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
678 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
679 mqc_bypass_enc(mqc, v);
683 *flagsp |= T1_REFINE;
687 static INLINE void t1_dec_refpass_step_raw(
697 opj_raw_t *raw = t1->raw; /* RAW component */
699 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
700 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
702 t = v ? poshalf : neghalf;
703 *datap += *datap < 0 ? -t : t;
704 *flagsp |= T1_REFINE;
706 } /* VSC and BYPASS by Antonin */
708 static INLINE void t1_dec_refpass_step_mqc(
717 opj_mqc_t *mqc = t1->mqc; /* MQC component */
720 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
721 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
723 t = v ? poshalf : neghalf;
724 *datap += *datap < 0 ? -t : t;
725 *flagsp |= T1_REFINE;
727 } /* VSC and BYPASS by Antonin */
729 static INLINE void t1_dec_refpass_step_mqc_vsc(
739 opj_mqc_t *mqc = t1->mqc; /* MQC component */
741 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
742 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
743 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
745 t = v ? poshalf : neghalf;
746 *datap += *datap < 0 ? -t : t;
747 *flagsp |= T1_REFINE;
749 } /* VSC and BYPASS by Antonin */
751 static void t1_enc_refpass(
758 int i, j, k, one, vsc;
760 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
761 for (k = 0; k < t1->h; k += 4) {
762 for (i = 0; i < t1->w; ++i) {
763 for (j = k; j < k + 4 && j < t1->h; ++j) {
764 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
767 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
768 &t1->data[(j * t1->w) + i],
779 static void t1_dec_refpass_raw(
784 int i, j, k, one, poshalf, neghalf;
788 neghalf = bpno > 0 ? -poshalf : -1;
789 for (k = 0; k < t1->h; k += 4) {
790 for (i = 0; i < t1->w; ++i) {
791 for (j = k; j < k + 4 && j < t1->h; ++j) {
792 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
793 t1_dec_refpass_step_raw(
795 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
796 &t1->data[(j * t1->w) + i],
803 } /* VSC and BYPASS by Antonin */
805 static void t1_dec_refpass_mqc(
809 int i, j, k, one, poshalf, neghalf;
810 int *data1 = t1->data;
811 flag_t *flags1 = &t1->flags[1];
814 neghalf = bpno > 0 ? -poshalf : -1;
815 for (k = 0; k < (t1->h & ~3); k += 4) {
816 for (i = 0; i < t1->w; ++i) {
817 int *data2 = data1 + i;
818 flag_t *flags2 = flags1 + i;
819 flags2 += t1->flags_stride;
820 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
822 flags2 += t1->flags_stride;
823 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
825 flags2 += t1->flags_stride;
826 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
828 flags2 += t1->flags_stride;
829 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
833 flags1 += t1->flags_stride << 2;
835 for (i = 0; i < t1->w; ++i) {
836 int *data2 = data1 + i;
837 flag_t *flags2 = flags1 + i;
838 for (j = k; j < t1->h; ++j) {
839 flags2 += t1->flags_stride;
840 t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
844 } /* VSC and BYPASS by Antonin */
846 static void t1_dec_refpass_mqc_vsc(
850 int i, j, k, one, poshalf, neghalf;
854 neghalf = bpno > 0 ? -poshalf : -1;
855 for (k = 0; k < t1->h; k += 4) {
856 for (i = 0; i < t1->w; ++i) {
857 for (j = k; j < k + 4 && j < t1->h; ++j) {
858 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
859 t1_dec_refpass_step_mqc_vsc(
861 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
862 &t1->data[(j * t1->w) + i],
869 } /* VSC and BYPASS by Antonin */
871 static void t1_enc_clnpass_step(
884 opj_mqc_t *mqc = t1->mqc; /* MQC component */
886 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
890 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
891 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
892 v = int_abs(*datap) & one ? 1 : 0;
896 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
897 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
898 v = *datap < 0 ? 1 : 0;
899 mqc_encode(mqc, v ^ t1_getspb(flag));
900 t1_updateflags(flagsp, v, t1->flags_stride);
903 *flagsp &= ~T1_VISIT;
906 static void t1_dec_clnpass_step_partial(
914 opj_mqc_t *mqc = t1->mqc; /* MQC component */
916 OPJ_ARG_NOT_USED(orient);
919 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
920 v = mqc_decode(mqc) ^ t1_getspb(flag);
921 *datap = v ? -oneplushalf : oneplushalf;
922 t1_updateflags(flagsp, v, t1->flags_stride);
923 *flagsp &= ~T1_VISIT;
924 } /* VSC and BYPASS by Antonin */
926 static void t1_dec_clnpass_step(
935 opj_mqc_t *mqc = t1->mqc; /* MQC component */
938 if (!(flag & (T1_SIG | T1_VISIT))) {
939 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
940 if (mqc_decode(mqc)) {
941 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
942 v = mqc_decode(mqc) ^ t1_getspb(flag);
943 *datap = v ? -oneplushalf : oneplushalf;
944 t1_updateflags(flagsp, v, t1->flags_stride);
947 *flagsp &= ~T1_VISIT;
948 } /* VSC and BYPASS by Antonin */
950 static void t1_dec_clnpass_step_vsc(
961 opj_mqc_t *mqc = t1->mqc; /* MQC component */
963 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
967 if (!(flag & (T1_SIG | T1_VISIT))) {
968 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
969 if (mqc_decode(mqc)) {
971 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
972 v = mqc_decode(mqc) ^ t1_getspb(flag);
973 *datap = v ? -oneplushalf : oneplushalf;
974 t1_updateflags(flagsp, v, t1->flags_stride);
977 *flagsp &= ~T1_VISIT;
980 static void t1_enc_clnpass(
987 int i, j, k, one, agg, runlen, vsc;
989 opj_mqc_t *mqc = t1->mqc; /* MQC component */
992 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
993 for (k = 0; k < t1->h; k += 4) {
994 for (i = 0; i < t1->w; ++i) {
996 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
997 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
998 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
999 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1000 || (MACRO_t1_flags(1 + k + 3,1 + i)
1001 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1003 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1004 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1005 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1006 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1012 for (runlen = 0; runlen < 4; ++runlen) {
1013 if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
1016 mqc_setcurctx(mqc, T1_CTXNO_AGG);
1017 mqc_encode(mqc, runlen != 4);
1021 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1022 mqc_encode(mqc, runlen >> 1);
1023 mqc_encode(mqc, runlen & 1);
1027 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1028 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1029 t1_enc_clnpass_step(
1031 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1032 &t1->data[(j * t1->w) + i],
1037 agg && (j == k + runlen),
1044 static void t1_dec_clnpass(
1050 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
1051 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1053 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1057 oneplushalf = one | half;
1058 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1059 for (k = 0; k < t1->h; k += 4) {
1060 for (i = 0; i < t1->w; ++i) {
1061 if (k + 3 < t1->h) {
1062 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1063 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1064 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1065 || (MACRO_t1_flags(1 + k + 3,1 + i)
1066 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1071 mqc_setcurctx(mqc, T1_CTXNO_AGG);
1072 if (!mqc_decode(mqc)) {
1075 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1076 runlen = mqc_decode(mqc);
1077 runlen = (runlen << 1) | mqc_decode(mqc);
1081 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1082 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1083 t1_dec_clnpass_step_vsc(
1085 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1086 &t1->data[(j * t1->w) + i],
1089 agg && (j == k + runlen),
1095 int *data1 = t1->data;
1096 flag_t *flags1 = &t1->flags[1];
1097 for (k = 0; k < (t1->h & ~3); k += 4) {
1098 for (i = 0; i < t1->w; ++i) {
1099 int *data2 = data1 + i;
1100 flag_t *flags2 = flags1 + i;
1101 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1102 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1103 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1104 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1106 mqc_setcurctx(mqc, T1_CTXNO_AGG);
1107 if (!mqc_decode(mqc)) {
1110 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1111 runlen = mqc_decode(mqc);
1112 runlen = (runlen << 1) | mqc_decode(mqc);
1113 flags2 += runlen * t1->flags_stride;
1114 data2 += runlen * t1->w;
1115 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1116 flags2 += t1->flags_stride;
1117 if (agg && (j == k + runlen)) {
1118 t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1120 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1125 flags2 += t1->flags_stride;
1126 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1128 flags2 += t1->flags_stride;
1129 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1131 flags2 += t1->flags_stride;
1132 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1134 flags2 += t1->flags_stride;
1135 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1139 data1 += t1->w << 2;
1140 flags1 += t1->flags_stride << 2;
1142 for (i = 0; i < t1->w; ++i) {
1143 int *data2 = data1 + i;
1144 flag_t *flags2 = flags1 + i;
1145 for (j = k; j < t1->h; ++j) {
1146 flags2 += t1->flags_stride;
1147 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1155 mqc_setcurctx(mqc, T1_CTXNO_UNI);
1156 v = mqc_decode(mqc);
1157 v = (v << 1) | mqc_decode(mqc);
1158 v = (v << 1) | mqc_decode(mqc);
1159 v = (v << 1) | mqc_decode(mqc);
1162 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1166 } /* VSC and BYPASS by Antonin */
1169 /** mod fixed_quality */
1170 static double t1_getwmsedec(
1181 double w1, w2, wmsedec;
1183 w1 = (mct && numcomps==3) ? mct_getnorm(compno) : 1.0;
1184 w2 = dwt_getnorm(level, orient);
1185 } else { /* if (qmfbid == 0) */
1186 w1 = (mct && numcomps==3) ? mct_getnorm_real(compno) : 1.0;
1187 w2 = dwt_getnorm_real(level, orient);
1189 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1190 wmsedec *= wmsedec * nmsedec / 8192.0;
1195 /** mod fixed_quality */
1196 static OPJ_FLOAT64 t1_getwmsedec_v2(
1203 OPJ_FLOAT64 stepsize,
1204 OPJ_UINT32 numcomps,
1205 const OPJ_FLOAT64 * mct_norms)
1207 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1210 w1 = mct_norms[compno];
1214 w2 = opj_dwt_getnorm(level, orient);
1215 } else { /* if (qmfbid == 0) */
1216 w2 = opj_dwt_getnorm_real(level, orient);
1219 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1220 wmsedec *= wmsedec * nmsedec / 8192.0;
1225 static opj_bool allocate_buffers(
1233 if(datasize > t1->datasize){
1234 opj_aligned_free(t1->data);
1235 t1->data = (int*) opj_aligned_malloc(datasize * sizeof(int));
1239 t1->datasize=datasize;
1241 memset(t1->data,0,datasize * sizeof(int));
1243 t1->flags_stride=w+2;
1244 flagssize=t1->flags_stride * (h+2);
1246 if(flagssize > t1->flagssize){
1247 opj_aligned_free(t1->flags);
1248 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
1252 t1->flagssize=flagssize;
1254 memset(t1->flags,0,flagssize * sizeof(flag_t));
1262 /** mod fixed_quality */
1263 static void t1_encode_cblk(
1265 opj_tcd_cblk_enc_t* cblk,
1274 opj_tcd_tile_t * tile)
1276 double cumwmsedec = 0.0;
1278 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1280 int passno, bpno, passtype;
1283 char type = T1_TYPE_MQ;
1287 for (i = 0; i < t1->w * t1->h; ++i) {
1288 int tmp = abs(t1->data[i]);
1289 max = int_max(max, tmp);
1292 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1294 bpno = cblk->numbps - 1;
1297 mqc_resetstates(mqc);
1298 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1299 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1300 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1301 mqc_init_enc(mqc, cblk->data);
1303 for (passno = 0; bpno >= 0; ++passno) {
1304 opj_tcd_pass_t *pass = &cblk->passes[passno];
1306 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1310 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1313 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1316 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1317 /* code switch SEGMARK (i.e. SEGSYM) */
1318 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1319 mqc_segmark_enc(mqc);
1324 tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps, mct);
1325 cumwmsedec += tempwmsedec;
1326 tile->distotile += tempwmsedec;
1328 /* Code switch "RESTART" (i.e. TERMALL) */
1329 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1330 if (type == T1_TYPE_RAW) {
1333 /* correction = mqc_bypass_flush_enc(); */
1334 } else { /* correction = mqc_restart_enc(); */
1340 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
1341 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1342 if (type == T1_TYPE_RAW) {
1345 /* correction = mqc_bypass_flush_enc(); */
1346 } else { /* correction = mqc_restart_enc(); */
1356 if (++passtype == 3) {
1361 if (pass->term && bpno > 0) {
1362 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1363 if (type == T1_TYPE_RAW)
1364 mqc_bypass_init_enc(mqc);
1366 mqc_restart_init_enc(mqc);
1369 pass->distortiondec = cumwmsedec;
1370 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
1372 /* Code-switch "RESET" */
1373 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1377 /* Code switch "ERTERM" (i.e. PTERM) */
1378 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1379 mqc_erterm_enc(mqc);
1380 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1383 cblk->totalpasses = passno;
1385 for (passno = 0; passno<cblk->totalpasses; passno++) {
1386 opj_tcd_pass_t *pass = &cblk->passes[passno];
1387 if (pass->rate > mqc_numbytes(mqc))
1388 pass->rate = mqc_numbytes(mqc);
1389 /*Preventing generation of FF as last data byte of a pass*/
1390 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1393 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1397 static opj_bool t1_decode_cblk(
1399 opj_tcd_cblk_dec_t* cblk,
1404 opj_raw_t *raw = t1->raw; /* RAW component */
1405 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1409 char type = T1_TYPE_MQ; /* BYPASS mode */
1411 if(!allocate_buffers(
1413 cblk->x1 - cblk->x0,
1414 cblk->y1 - cblk->y0))
1419 bpno = roishift + cblk->numbps - 1;
1422 mqc_resetstates(mqc);
1423 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1424 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1425 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1427 for (segno = 0; segno < cblk->numsegs; ++segno) {
1428 opj_tcd_seg_t *seg = &cblk->segs[segno];
1431 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1432 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1433 if(seg->data == NULL){
1436 if (type == T1_TYPE_RAW) {
1437 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1439 if (OPJ_FALSE == mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1444 for (passno = 0; passno < seg->numpasses; ++passno) {
1447 if (type == T1_TYPE_RAW) {
1448 t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1450 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1451 t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1453 t1_dec_sigpass_mqc(t1, bpno+1, orient);
1458 if (type == T1_TYPE_RAW) {
1459 t1_dec_refpass_raw(t1, bpno+1, cblksty);
1461 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1462 t1_dec_refpass_mqc_vsc(t1, bpno+1);
1464 t1_dec_refpass_mqc(t1, bpno+1);
1469 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1473 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1474 mqc_resetstates(mqc);
1475 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1476 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1477 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1479 if (++passtype == 3) {
1488 /* ----------------------------------------------------------------------- */
1490 void t1_encode_cblks(
1492 opj_tcd_tile_t *tile,
1495 int compno, resno, bandno, precno, cblkno;
1497 tile->distotile = 0; /* fixed_quality */
1499 for (compno = 0; compno < tile->numcomps; ++compno) {
1500 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1501 opj_tccp_t* tccp = &tcp->tccps[compno];
1502 int tile_w = tilec->x1 - tilec->x0;
1504 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1505 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1507 for (bandno = 0; bandno < res->numbands; ++bandno) {
1508 opj_tcd_band_t* restrict band = &res->bands[bandno];
1509 int bandconst = 8192 * 8192 / ((int) floor(band->stepsize * 8192));
1511 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1512 opj_tcd_precinct_t *prc = &band->precincts[precno];
1514 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1515 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1516 int* restrict datap;
1517 int* restrict tiledp;
1522 int x = cblk->x0 - band->x0;
1523 int y = cblk->y0 - band->y0;
1524 if (band->bandno & 1) {
1525 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1526 x += pres->x1 - pres->x0;
1528 if (band->bandno & 2) {
1529 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1530 y += pres->y1 - pres->y0;
1533 if(!allocate_buffers(
1535 cblk->x1 - cblk->x0,
1536 cblk->y1 - cblk->y0))
1545 tiledp=&tilec->data[(y * tile_w) + x];
1546 if (tccp->qmfbid == 1) {
1547 for (j = 0; j < cblk_h; ++j) {
1548 for (i = 0; i < cblk_w; ++i) {
1549 int tmp = tiledp[(j * tile_w) + i];
1550 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1553 } else { /* if (tccp->qmfbid == 0) */
1554 for (j = 0; j < cblk_h; ++j) {
1555 for (i = 0; i < cblk_w; ++i) {
1556 int tmp = tiledp[(j * tile_w) + i];
1557 datap[(j * cblk_w) + i] =
1560 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1570 tilec->numresolutions - 1 - resno,
1588 /* ----------------------------------------------------------------------- */
1590 * Creates a new Tier 1 handle
1591 * and initializes the look-up tables of the Tier-1 coder/decoder
1592 * @return a new T1 handle if successful, returns NULL otherwise
1594 opj_t1_t* opj_t1_create()
1596 opj_t1_t *l_t1 = 00;
1598 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1602 memset(l_t1,0,sizeof(opj_t1_t));
1604 /* create MQC and RAW handles */
1605 l_t1->mqc = mqc_create();
1607 opj_t1_destroy(l_t1);
1611 l_t1->raw = raw_create();
1613 opj_t1_destroy(l_t1);
1622 * Destroys a previously created T1 handle
1624 * @param p_t1 Tier 1 handle to destroy
1626 void opj_t1_destroy(opj_t1_t *p_t1)
1632 /* destroy MQC and RAW handles */
1633 mqc_destroy(p_t1->mqc);
1635 raw_destroy(p_t1->raw);
1639 opj_aligned_free(p_t1->data);
1644 opj_aligned_free(p_t1->flags);
1651 opj_bool opj_t1_decode_cblks( opj_t1_t* t1,
1652 opj_tcd_tilecomp_v2_t* tilec,
1656 OPJ_UINT32 resno, bandno, precno, cblkno;
1657 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1659 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1660 opj_tcd_resolution_v2_t* res = &tilec->resolutions[resno];
1662 for (bandno = 0; bandno < res->numbands; ++bandno) {
1663 opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
1665 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1666 opj_tcd_precinct_v2_t* precinct = &band->precincts[precno];
1668 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1669 opj_tcd_cblk_dec_v2_t* cblk = &precinct->cblks.dec[cblkno];
1670 OPJ_INT32* restrict datap;
1671 void* restrict tiledp;
1672 OPJ_UINT32 cblk_w, cblk_h;
1676 if (OPJ_FALSE == t1_decode_cblk_v2(
1685 x = cblk->x0 - band->x0;
1686 y = cblk->y0 - band->y0;
1687 if (band->bandno & 1) {
1688 opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
1689 x += pres->x1 - pres->x0;
1691 if (band->bandno & 2) {
1692 opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
1693 y += pres->y1 - pres->y0;
1700 if (tccp->roishift) {
1701 OPJ_INT32 thresh = 1 << tccp->roishift;
1702 for (j = 0; j < cblk_h; ++j) {
1703 for (i = 0; i < cblk_w; ++i) {
1704 OPJ_INT32 val = datap[(j * cblk_w) + i];
1705 OPJ_INT32 mag = abs(val);
1706 if (mag >= thresh) {
1707 mag >>= tccp->roishift;
1708 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1714 tiledp=(void*)&tilec->data[(y * tile_w) + x];
1715 if (tccp->qmfbid == 1) {
1716 for (j = 0; j < cblk_h; ++j) {
1717 for (i = 0; i < cblk_w; ++i) {
1718 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1719 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1722 } else { /* if (tccp->qmfbid == 0) */
1723 for (j = 0; j < cblk_h; ++j) {
1724 for (i = 0; i < cblk_w; ++i) {
1725 float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1726 ((float*)tiledp)[(j * tile_w) + i] = tmp;
1730 /*opj_free(cblk->segs);*/
1731 /*cblk->segs = 00;*/
1740 static opj_bool t1_decode_cblk_v2(
1742 opj_tcd_cblk_dec_v2_t* cblk,
1744 OPJ_UINT32 roishift,
1747 opj_raw_t *raw = t1->raw; /* RAW component */
1748 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1751 OPJ_UINT32 passtype;
1752 OPJ_UINT32 segno, passno;
1753 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1755 if(!allocate_buffers(
1757 cblk->x1 - cblk->x0,
1758 cblk->y1 - cblk->y0))
1763 bpno = roishift + cblk->numbps - 1;
1766 mqc_resetstates(mqc);
1767 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1768 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1769 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1771 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1772 opj_tcd_seg_t *seg = &cblk->segs[segno];
1775 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1776 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1777 if(seg->data == 00){
1780 if (type == T1_TYPE_RAW) {
1781 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1783 if (OPJ_FALSE == mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1788 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1791 t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1794 t1_dec_refpass(t1, bpno+1, type, cblksty);
1797 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1801 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1802 mqc_resetstates(mqc);
1803 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1804 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1805 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1807 if (++passtype == 3) {
1816 opj_bool opj_t1_encode_cblks( opj_t1_t *t1,
1817 opj_tcd_tile_v2_t *tile,
1819 const OPJ_FLOAT64 * mct_norms
1822 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1824 tile->distotile = 0; /* fixed_quality */
1826 for (compno = 0; compno < tile->numcomps; ++compno) {
1827 opj_tcd_tilecomp_v2_t* tilec = &tile->comps[compno];
1828 opj_tccp_t* tccp = &tcp->tccps[compno];
1829 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1831 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1832 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1834 for (bandno = 0; bandno < res->numbands; ++bandno) {
1835 opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
1836 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1838 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1839 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1841 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1842 opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno];
1843 OPJ_INT32 * restrict datap;
1844 OPJ_INT32* restrict tiledp;
1849 OPJ_INT32 x = cblk->x0 - band->x0;
1850 OPJ_INT32 y = cblk->y0 - band->y0;
1851 if (band->bandno & 1) {
1852 opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
1853 x += pres->x1 - pres->x0;
1855 if (band->bandno & 2) {
1856 opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
1857 y += pres->y1 - pres->y0;
1860 if(!allocate_buffers(
1862 cblk->x1 - cblk->x0,
1863 cblk->y1 - cblk->y0))
1872 tiledp=&tilec->data[(y * tile_w) + x];
1873 if (tccp->qmfbid == 1) {
1874 for (j = 0; j < cblk_h; ++j) {
1875 for (i = 0; i < cblk_w; ++i) {
1876 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1877 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1880 } else { /* if (tccp->qmfbid == 0) */
1881 for (j = 0; j < cblk_h; ++j) {
1882 for (i = 0; i < cblk_w; ++i) {
1883 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1884 datap[(j * cblk_w) + i] =
1887 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1897 tilec->numresolutions - 1 - resno,
1913 /** mod fixed_quality */
1914 static void t1_encode_cblk_v2(
1916 opj_tcd_cblk_enc_v2_t* cblk,
1921 OPJ_FLOAT64 stepsize,
1923 OPJ_UINT32 numcomps,
1924 opj_tcd_tile_v2_t * tile,
1925 const OPJ_FLOAT64 * mct_norms)
1927 OPJ_FLOAT64 cumwmsedec = 0.0;
1929 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1933 OPJ_UINT32 passtype;
1934 OPJ_INT32 nmsedec = 0;
1937 OPJ_BYTE type = T1_TYPE_MQ;
1938 OPJ_FLOAT64 tempwmsedec;
1941 for (i = 0; i < t1->w * t1->h; ++i) {
1942 OPJ_INT32 tmp = abs(t1->data[i]);
1943 max = int_max(max, tmp);
1946 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1948 bpno = cblk->numbps - 1;
1951 mqc_resetstates(mqc);
1952 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1953 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1954 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1955 mqc_init_enc(mqc, cblk->data);
1957 for (passno = 0; bpno >= 0; ++passno) {
1958 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1959 OPJ_UINT32 correction = 3;
1960 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1964 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1967 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1970 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1971 /* code switch SEGMARK (i.e. SEGSYM) */
1972 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1973 mqc_segmark_enc(mqc);
1978 tempwmsedec = t1_getwmsedec_v2(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1979 cumwmsedec += tempwmsedec;
1980 tile->distotile += tempwmsedec;
1982 /* Code switch "RESTART" (i.e. TERMALL) */
1983 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1984 if (type == T1_TYPE_RAW) {
1987 /* correction = mqc_bypass_flush_enc(); */
1988 } else { /* correction = mqc_restart_enc(); */
1994 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1995 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1996 if (type == T1_TYPE_RAW) {
1999 /* correction = mqc_bypass_flush_enc(); */
2000 } else { /* correction = mqc_restart_enc(); */
2010 if (++passtype == 3) {
2015 if (pass->term && bpno > 0) {
2016 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2017 if (type == T1_TYPE_RAW)
2018 mqc_bypass_init_enc(mqc);
2020 mqc_restart_init_enc(mqc);
2023 pass->distortiondec = cumwmsedec;
2024 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
2026 /* Code-switch "RESET" */
2027 if (cblksty & J2K_CCP_CBLKSTY_RESET)
2031 /* Code switch "ERTERM" (i.e. PTERM) */
2032 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
2033 mqc_erterm_enc(mqc);
2034 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
2037 cblk->totalpasses = passno;
2039 for (passno = 0; passno<cblk->totalpasses; passno++) {
2040 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
2041 if (pass->rate > mqc_numbytes(mqc))
2042 pass->rate = mqc_numbytes(mqc);
2043 /*Preventing generation of FF as last data byte of a pass*/
2044 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
2047 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2052 static void t1_dec_refpass(
2059 OPJ_INT32 one, poshalf, neghalf;
2063 neghalf = bpno > 0 ? -poshalf : -1;
2064 for (k = 0; k < t1->h; k += 4) {
2065 for (i = 0; i < t1->w; ++i) {
2066 for (j = k; j < k + 4 && j < t1->h; ++j) {
2067 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
2068 t1_dec_refpass_step(
2070 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
2071 &t1->data[(j * t1->w) + i],
2079 } /* VSC and BYPASS by Antonin */
2082 static void t1_dec_refpass_step(
2094 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2095 opj_raw_t *raw = t1->raw; /* RAW component */
2097 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2098 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2099 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
2100 if (type == T1_TYPE_RAW) {
2101 v = raw_decode(raw);
2103 v = mqc_decode(mqc);
2105 t = v ? poshalf : neghalf;
2106 *datap += *datap < 0 ? -t : t;
2107 *flagsp |= T1_REFINE;
2109 } /* VSC and BYPASS by Antonin */
2111 static void t1_dec_sigpass(
2118 OPJ_UINT32 i, j, k, vsc;
2119 OPJ_INT32 one, half, oneplushalf;
2122 oneplushalf = one | half;
2123 for (k = 0; k < t1->h; k += 4) {
2124 for (i = 0; i < t1->w; ++i) {
2125 for (j = k; j < k + 4 && j < t1->h; ++j) {
2126 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
2127 t1_dec_sigpass_step(
2129 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
2130 &t1->data[(j * t1->w) + i],
2138 } /* VSC and BYPASS by Antonin */
2140 static void t1_dec_sigpass_step(
2145 OPJ_INT32 oneplushalf,
2151 opj_raw_t *raw = t1->raw; /* RAW component */
2152 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2154 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
2155 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2156 if (type == T1_TYPE_RAW) {
2157 if (raw_decode(raw)) {
2158 v = raw_decode(raw); /* ESSAI */
2159 *datap = v ? -oneplushalf : oneplushalf;
2160 t1_updateflags(flagsp, v, t1->flags_stride);
2163 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
2164 if (mqc_decode(mqc)) {
2165 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
2166 v = mqc_decode(mqc) ^ t1_getspb(flag);
2167 *datap = v ? -oneplushalf : oneplushalf;
2168 t1_updateflags(flagsp, v, t1->flags_stride);
2171 *flagsp |= T1_VISIT;
2173 } /* VSC and BYPASS by Antonin */