2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
9 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
41 #include "opj_malloc.h"
45 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
48 /** @name Local static functions */
51 static INLINE OPJ_BYTE t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
52 static OPJ_BYTE t1_getctxno_sc(OPJ_UINT32 f);
53 static INLINE OPJ_UINT32 t1_getctxno_mag(OPJ_UINT32 f);
54 static OPJ_BYTE t1_getspb(OPJ_UINT32 f);
55 static OPJ_INT16 t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
56 static OPJ_INT16 t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
57 static void t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
59 Encode significant pass
61 static void t1_enc_sigpass_step(
72 Decode significant pass
74 static void t1_dec_sigpass_step(
79 OPJ_INT32 oneplushalf,
83 Encode significant pass
85 static void t1_enc_sigpass(
93 Decode significant pass
95 static void t1_dec_sigpass(
102 Encode refinement pass
104 static void t1_enc_refpass_step(
114 Decode refinement pass
116 static void t1_dec_refpass_step(
125 Encode refinement pass
127 static void t1_enc_refpass(
134 Decode refinement pass
136 static void t1_dec_refpass(
144 static void t1_enc_clnpass_step(
157 static void t1_dec_clnpass_step(
162 OPJ_INT32 oneplushalf,
168 static void t1_enc_clnpass(
177 static void t1_dec_clnpass(
183 static OPJ_FLOAT64 t1_getwmsedec(
190 OPJ_FLOAT64 stepsize,
192 const OPJ_FLOAT64 * mct_norms);
196 @param cblk Code-block coding parameters
198 @param compno Component number
202 @param cblksty Code-block style
206 static void t1_encode_cblk(
208 opj_tcd_cblk_enc_t* cblk,
213 OPJ_FLOAT64 stepsize,
216 opj_tcd_tile_t * tile,
217 const OPJ_FLOAT64 * mct_norms);
221 @param cblk Code-block coding parameters
223 @param roishift Region of interest shifting value
224 @param cblksty Code-block style
226 static void t1_decode_cblk(
228 opj_tcd_cblk_dec_t* cblk,
237 /* ----------------------------------------------------------------------- */
239 static OPJ_BYTE t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
240 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
243 static OPJ_BYTE t1_getctxno_sc(OPJ_UINT32 f) {
244 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
247 static OPJ_UINT32 t1_getctxno_mag(OPJ_UINT32 f) {
248 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
249 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
253 static OPJ_BYTE t1_getspb(OPJ_UINT32 f) {
254 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
257 static OPJ_INT16 t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
259 if (bitpos > T1_NMSEDEC_FRACBITS) {
260 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
263 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
266 static OPJ_INT16 t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
267 if (bitpos > T1_NMSEDEC_FRACBITS) {
268 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
271 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
274 static void t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
275 flag_t *np = flagsp - stride;
276 flag_t *sp = flagsp + stride;
278 static const flag_t mod[] = {
279 T1_SIG_S, T1_SIG_S|T1_SGN_S,
280 T1_SIG_E, T1_SIG_E|T1_SGN_E,
281 T1_SIG_W, T1_SIG_W|T1_SGN_W,
282 T1_SIG_N, T1_SIG_N|T1_SGN_N
289 flagsp[-1] |= mod[s+2];
291 flagsp[1] |= mod[s+4];
298 static void t1_enc_sigpass_step(
312 opj_mqc_t *mqc = t1->mqc; /* MQC component */
314 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
315 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
316 v = int_abs(*datap) & one ? 1 : 0;
317 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient)); /* ESSAI */
318 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
319 mqc_bypass_enc(mqc, v);
324 v = *datap < 0 ? 1 : 0;
325 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
326 mqc_setcurctx(mqc, t1_getctxno_sc(flag)); /* ESSAI */
327 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
328 mqc_bypass_enc(mqc, v);
330 mqc_encode(mqc, v ^ t1_getspb(flag));
332 t1_updateflags(flagsp, v, t1->flags_stride);
338 static void t1_dec_sigpass_step(
343 OPJ_INT32 oneplushalf,
349 opj_raw_t *raw = t1->raw; /* RAW component */
350 opj_mqc_t *mqc = t1->mqc; /* MQC component */
352 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
353 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
354 if (type == T1_TYPE_RAW) {
355 if (raw_decode(raw)) {
356 v = raw_decode(raw); /* ESSAI */
357 *datap = v ? -oneplushalf : oneplushalf;
358 t1_updateflags(flagsp, v, t1->flags_stride);
361 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
362 if (mqc_decode(mqc)) {
363 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
364 v = mqc_decode(mqc) ^ t1_getspb(flag);
365 *datap = v ? -oneplushalf : oneplushalf;
366 t1_updateflags(flagsp, v, t1->flags_stride);
371 } /* VSC and BYPASS by Antonin */
373 static void t1_enc_sigpass(
381 OPJ_UINT32 i, j, k, vsc;
385 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
386 for (k = 0; k < t1->h; k += 4) {
387 for (i = 0; i < t1->w; ++i) {
388 for (j = k; j < k + 4 && j < t1->h; ++j) {
389 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
392 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
393 &t1->data[(j * t1->w) + i],
405 static void t1_dec_sigpass(
412 OPJ_UINT32 i, j, k, vsc;
413 OPJ_INT32 one, half, oneplushalf;
416 oneplushalf = one | half;
417 for (k = 0; k < t1->h; k += 4) {
418 for (i = 0; i < t1->w; ++i) {
419 for (j = k; j < k + 4 && j < t1->h; ++j) {
420 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
423 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
424 &t1->data[(j * t1->w) + i],
432 } /* VSC and BYPASS by Antonin */
434 static void t1_enc_refpass_step(
447 opj_mqc_t *mqc = t1->mqc; /* MQC component */
449 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
450 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
451 *nmsedec += t1_getnmsedec_ref(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
452 v = int_abs(*datap) & one ? 1 : 0;
453 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
454 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
455 mqc_bypass_enc(mqc, v);
459 *flagsp |= T1_REFINE;
463 static void t1_dec_refpass_step(
475 opj_mqc_t *mqc = t1->mqc; /* MQC component */
476 opj_raw_t *raw = t1->raw; /* RAW component */
478 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
479 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
480 mqc_setcurctx(mqc, t1_getctxno_mag(flag)); /* ESSAI */
481 if (type == T1_TYPE_RAW) {
486 t = v ? poshalf : neghalf;
487 *datap += *datap < 0 ? -t : t;
488 *flagsp |= T1_REFINE;
490 } /* VSC and BYPASS by Antonin */
492 static void t1_enc_refpass(
499 OPJ_UINT32 i, j, k, vsc;
503 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
504 for (k = 0; k < t1->h; k += 4) {
505 for (i = 0; i < t1->w; ++i) {
506 for (j = k; j < k + 4 && j < t1->h; ++j) {
507 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
510 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
511 &t1->data[(j * t1->w) + i],
522 static void t1_dec_refpass(
529 OPJ_INT32 one, poshalf, neghalf;
533 neghalf = bpno > 0 ? -poshalf : -1;
534 for (k = 0; k < t1->h; k += 4) {
535 for (i = 0; i < t1->w; ++i) {
536 for (j = k; j < k + 4 && j < t1->h; ++j) {
537 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
540 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
541 &t1->data[(j * t1->w) + i],
549 } /* VSC and BYPASS by Antonin */
551 static void t1_enc_clnpass_step(
565 opj_mqc_t *mqc = t1->mqc; /* MQC component */
567 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
571 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
572 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
573 v = int_abs(*datap) & one ? 1 : 0;
577 *nmsedec += t1_getnmsedec_sig(int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
578 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
579 v = *datap < 0 ? 1 : 0;
580 mqc_encode(mqc, v ^ t1_getspb(flag));
581 t1_updateflags(flagsp, v, t1->flags_stride);
584 *flagsp &= ~T1_VISIT;
587 static void t1_dec_clnpass_step(
592 OPJ_INT32 oneplushalf,
599 opj_mqc_t *mqc = t1->mqc; /* MQC component */
601 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
605 if (!(flag & (T1_SIG | T1_VISIT))) {
606 mqc_setcurctx(mqc, t1_getctxno_zc(flag, orient));
607 if (mqc_decode(mqc)) {
609 mqc_setcurctx(mqc, t1_getctxno_sc(flag));
610 v = mqc_decode(mqc) ^ t1_getspb(flag);
611 *datap = v ? -oneplushalf : oneplushalf;
612 t1_updateflags(flagsp, v, t1->flags_stride);
615 *flagsp &= ~T1_VISIT;
616 } /* VSC and BYPASS by Antonin */
618 static void t1_enc_clnpass(
627 OPJ_UINT32 agg, runlen, vsc;
629 opj_mqc_t *mqc = t1->mqc; /* MQC component */
632 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
633 for (k = 0; k < t1->h; k += 4) {
634 for (i = 0; i < t1->w; ++i) {
636 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
637 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
638 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
639 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
640 || (MACRO_t1_flags(1 + k + 3,1 + i)
641 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
643 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
644 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
645 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
646 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
652 for (runlen = 0; runlen < 4; ++runlen) {
653 if (int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
656 mqc_setcurctx(mqc, T1_CTXNO_AGG);
657 mqc_encode(mqc, runlen != 4);
661 mqc_setcurctx(mqc, T1_CTXNO_UNI);
662 mqc_encode(mqc, runlen >> 1);
663 mqc_encode(mqc, runlen & 1);
667 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
668 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
671 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
672 &t1->data[(j * t1->w) + i],
677 agg && (j == k + runlen),
684 static void t1_dec_clnpass(
690 OPJ_UINT32 i, j, k, one;
691 OPJ_INT32 half, oneplushalf;
692 OPJ_UINT32 agg, runlen, vsc;
693 OPJ_UINT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
695 opj_mqc_t *mqc = t1->mqc; /* MQC component */
699 oneplushalf = one | half;
700 for (k = 0; k < t1->h; k += 4) {
701 for (i = 0; i < t1->w; ++i) {
703 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
704 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
705 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
706 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
707 || (MACRO_t1_flags(1 + k + 3,1 + i)
708 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
710 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
711 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
712 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
713 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
719 mqc_setcurctx(mqc, T1_CTXNO_AGG);
720 if (!mqc_decode(mqc)) {
723 mqc_setcurctx(mqc, T1_CTXNO_UNI);
724 runlen = mqc_decode(mqc);
725 runlen = (runlen << 1) | mqc_decode(mqc);
729 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
730 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
733 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
734 &t1->data[(j * t1->w) + i],
737 agg && (j == k + runlen),
744 mqc_setcurctx(mqc, T1_CTXNO_UNI);
746 v = (v << 1) | mqc_decode(mqc);
747 v = (v << 1) | mqc_decode(mqc);
748 v = (v << 1) | mqc_decode(mqc);
751 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
755 } /* VSC and BYPASS by Antonin */
758 /** mod fixed_quality */
759 static OPJ_FLOAT64 t1_getwmsedec(
766 OPJ_FLOAT64 stepsize,
768 const OPJ_FLOAT64 * mct_norms)
770 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
774 w1 = mct_norms[compno];
778 w2 = dwt_getnorm(level, orient);
779 } else { /* if (qmfbid == 0) */
780 w2 = dwt_getnorm_real(level, orient);
782 wmsedec = w1 * w2 * stepsize * (1 << bpno);
783 wmsedec *= wmsedec * nmsedec / 8192.0;
787 static bool allocate_buffers(
792 OPJ_UINT32 datasize=w * h;
793 OPJ_UINT32 flagssize;
795 if(datasize > t1->datasize){
796 opj_aligned_free(t1->data);
797 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
801 t1->datasize=datasize;
803 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
805 t1->flags_stride=w+2;
806 flagssize=t1->flags_stride * (h+2);
808 if(flagssize > t1->flagssize){
809 opj_aligned_free(t1->flags);
810 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
814 t1->flagssize=flagssize;
816 memset(t1->flags,0,flagssize * sizeof(flag_t));
824 /** mod fixed_quality */
825 static void t1_encode_cblk(
827 opj_tcd_cblk_enc_t* cblk,
832 OPJ_FLOAT64 stepsize,
835 opj_tcd_tile_t * tile,
836 const OPJ_FLOAT64 * mct_norms)
838 OPJ_FLOAT64 cumwmsedec = 0.0;
840 opj_mqc_t *mqc = t1->mqc; /* MQC component */
845 OPJ_INT32 nmsedec = 0;
848 OPJ_BYTE type = T1_TYPE_MQ;
849 OPJ_FLOAT64 tempwmsedec;
852 for (i = 0; i < t1->w * t1->h; ++i) {
853 OPJ_INT32 tmp = abs(t1->data[i]);
854 max = int_max(max, tmp);
857 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
859 bpno = cblk->numbps - 1;
862 mqc_resetstates(mqc);
863 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
864 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
865 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
866 mqc_init_enc(mqc, cblk->data);
868 for (passno = 0; bpno >= 0; ++passno) {
869 opj_tcd_pass_t *pass = &cblk->passes[passno];
870 OPJ_UINT32 correction = 3;
871 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
875 t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
878 t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
881 t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
882 /* code switch SEGMARK (i.e. SEGSYM) */
883 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
884 mqc_segmark_enc(mqc);
889 tempwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
890 cumwmsedec += tempwmsedec;
891 tile->distotile += tempwmsedec;
893 /* Code switch "RESTART" (i.e. TERMALL) */
894 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
895 if (type == T1_TYPE_RAW) {
898 /* correction = mqc_bypass_flush_enc(); */
899 } else { /* correction = mqc_restart_enc(); */
905 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
906 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
907 if (type == T1_TYPE_RAW) {
910 /* correction = mqc_bypass_flush_enc(); */
911 } else { /* correction = mqc_restart_enc(); */
921 if (++passtype == 3) {
926 if (pass->term && bpno > 0) {
927 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
928 if (type == T1_TYPE_RAW)
929 mqc_bypass_init_enc(mqc);
931 mqc_restart_init_enc(mqc);
934 pass->distortiondec = cumwmsedec;
935 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
937 /* Code-switch "RESET" */
938 if (cblksty & J2K_CCP_CBLKSTY_RESET)
942 /* Code switch "ERTERM" (i.e. PTERM) */
943 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
945 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
948 cblk->totalpasses = passno;
950 for (passno = 0; passno<cblk->totalpasses; passno++) {
951 opj_tcd_pass_t *pass = &cblk->passes[passno];
952 if (pass->rate > mqc_numbytes(mqc))
953 pass->rate = mqc_numbytes(mqc);
954 /*Preventing generation of FF as last data byte of a pass*/
955 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
958 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
962 static void t1_decode_cblk(
964 opj_tcd_cblk_dec_t* cblk,
969 opj_raw_t *raw = t1->raw; /* RAW component */
970 opj_mqc_t *mqc = t1->mqc; /* MQC component */
974 OPJ_UINT32 segno, passno;
975 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
977 if(!allocate_buffers(
980 cblk->y1 - cblk->y0))
985 bpno = roishift + cblk->numbps - 1;
988 mqc_resetstates(mqc);
989 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
990 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
991 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
993 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
994 opj_tcd_seg_t *seg = &cblk->segs[segno];
997 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
998 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1002 if (type == T1_TYPE_RAW) {
1003 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1005 mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len);
1008 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1011 t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1014 t1_dec_refpass(t1, bpno+1, type, cblksty);
1017 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1021 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1022 mqc_resetstates(mqc);
1023 mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1024 mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1025 mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1027 if (++passtype == 3) {
1035 /* ----------------------------------------------------------------------- */
1037 * Creates a new Tier 1 handle
1038 * and initializes the look-up tables of the Tier-1 coder/decoder
1039 * @return a new T1 handle if successful, returns NULL otherwise
1041 opj_t1_t* t1_create()
1043 opj_t1_t *l_t1 = 00;
1045 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1051 memset(l_t1,0,sizeof(opj_t1_t));
1053 /* create MQC and RAW handles */
1054 l_t1->mqc = mqc_create();
1061 l_t1->raw = raw_create();
1072 * Destroys a previously created T1 handle
1074 * @param p_t1 Tier 1 handle to destroy
1076 void t1_destroy(opj_t1_t *p_t1)
1084 /* destroy MQC and RAW handles */
1085 mqc_destroy(p_t1->mqc);
1087 raw_destroy(p_t1->raw);
1092 opj_aligned_free(p_t1->data);
1098 opj_aligned_free(p_t1->flags);
1104 bool t1_encode_cblks(
1106 opj_tcd_tile_t *tile,
1108 const OPJ_FLOAT64 * mct_norms)
1110 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1112 tile->distotile = 0; /* fixed_quality */
1114 for (compno = 0; compno < tile->numcomps; ++compno) {
1115 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1116 opj_tccp_t* tccp = &tcp->tccps[compno];
1117 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1119 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1120 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1122 for (bandno = 0; bandno < res->numbands; ++bandno) {
1123 opj_tcd_band_t* restrict band = &res->bands[bandno];
1125 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1126 opj_tcd_precinct_t *prc = &band->precincts[precno];
1128 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1129 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1130 OPJ_INT32 * restrict datap;
1131 OPJ_INT32* restrict tiledp;
1136 OPJ_INT32 x = cblk->x0 - band->x0;
1137 OPJ_INT32 y = cblk->y0 - band->y0;
1138 if (band->bandno & 1) {
1139 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1140 x += pres->x1 - pres->x0;
1142 if (band->bandno & 2) {
1143 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1144 y += pres->y1 - pres->y0;
1147 if(!allocate_buffers(
1149 cblk->x1 - cblk->x0,
1150 cblk->y1 - cblk->y0))
1159 tiledp=&tilec->data[(y * tile_w) + x];
1160 if (tccp->qmfbid == 1) {
1161 for (j = 0; j < cblk_h; ++j) {
1162 for (i = 0; i < cblk_w; ++i) {
1163 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1164 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1167 } else { /* if (tccp->qmfbid == 0) */
1168 for (j = 0; j < cblk_h; ++j) {
1169 for (i = 0; i < cblk_w; ++i) {
1170 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1171 datap[(j * cblk_w) + i] =
1174 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192))) >> (11 - T1_NMSEDEC_FRACBITS);
1184 tilec->numresolutions - 1 - resno,
1200 void t1_decode_cblks(
1202 opj_tcd_tilecomp_t* tilec,
1205 OPJ_UINT32 resno, bandno, precno, cblkno;
1207 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1209 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1210 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1212 for (bandno = 0; bandno < res->numbands; ++bandno) {
1213 opj_tcd_band_t* restrict band = &res->bands[bandno];
1215 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1216 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1218 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1219 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1220 OPJ_INT32* restrict datap;
1221 void* restrict tiledp;
1222 OPJ_UINT32 cblk_w, cblk_h;
1233 x = cblk->x0 - band->x0;
1234 y = cblk->y0 - band->y0;
1235 if (band->bandno & 1) {
1236 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1237 x += pres->x1 - pres->x0;
1239 if (band->bandno & 2) {
1240 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1241 y += pres->y1 - pres->y0;
1248 if (tccp->roishift) {
1249 OPJ_INT32 thresh = 1 << tccp->roishift;
1250 for (j = 0; j < cblk_h; ++j) {
1251 for (i = 0; i < cblk_w; ++i) {
1252 OPJ_INT32 val = datap[(j * cblk_w) + i];
1253 OPJ_INT32 mag = abs(val);
1254 if (mag >= thresh) {
1255 mag >>= tccp->roishift;
1256 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1262 tiledp=(void*)&tilec->data[(y * tile_w) + x];
1263 if (tccp->qmfbid == 1) {
1264 for (j = 0; j < cblk_h; ++j) {
1265 for (i = 0; i < cblk_w; ++i) {
1266 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1267 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1270 } else { /* if (tccp->qmfbid == 0) */
1271 for (j = 0; j < cblk_h; ++j) {
1272 for (i = 0; i < cblk_w; ++i) {
1273 float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1274 ((float*)tiledp)[(j * tile_w) + i] = tmp;
1278 //opj_free(cblk->segs);