2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
36 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
39 /** @name Local static functions */
42 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
43 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
44 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
45 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
46 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
47 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
48 static void opj_t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
50 Encode significant pass
52 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
63 Decode significant pass
65 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
69 OPJ_INT32 oneplushalf,
74 Encode significant pass
76 static void opj_t1_enc_sigpass( opj_t1_t *t1,
84 Decode significant pass
86 static void opj_t1_dec_sigpass( opj_t1_t *t1,
94 Encode refinement pass
96 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
107 Encode refinement pass
109 static void opj_t1_enc_refpass( opj_t1_t *t1,
116 Decode refinement pass
118 static void opj_t1_dec_refpass( opj_t1_t *t1,
124 Decode refinement pass
126 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
137 static void opj_t1_enc_clnpass_step(
150 static void t1_dec_clnpass_step_partial(
156 static void t1_dec_clnpass_step(
162 static void t1_dec_clnpass_step_vsc(
173 static void opj_t1_enc_clnpass(
182 static void t1_dec_clnpass(
187 static double t1_getwmsedec(
198 static OPJ_FLOAT64 t1_getwmsedec_v2(
205 OPJ_FLOAT64 stepsize,
207 const OPJ_FLOAT64 * mct_norms);
209 static void opj_t1_encode_cblk( opj_t1_t *t1,
210 opj_tcd_cblk_enc_v2_t* cblk,
215 OPJ_FLOAT64 stepsize,
218 opj_tcd_tile_v2_t * tile,
219 const OPJ_FLOAT64 * mct_norms);
224 @param cblk Code-block coding parameters
226 @param roishift Region of interest shifting value
227 @param cblksty Code-block style
229 static opj_bool opj_t1_decode_cblk( opj_t1_t *t1,
230 opj_tcd_cblk_dec_v2_t* cblk,
235 opj_bool opj_t1_allocate_buffers( opj_t1_t *t1,
243 /* ----------------------------------------------------------------------- */
245 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
246 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
249 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
250 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
253 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
254 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
255 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
259 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
260 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
263 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
264 if (bitpos > T1_NMSEDEC_FRACBITS) {
265 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
268 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
271 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
272 if (bitpos > T1_NMSEDEC_FRACBITS) {
273 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
276 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
279 void opj_t1_updateflags(flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
280 flag_t *np = flagsp - stride;
281 flag_t *sp = flagsp + stride;
283 static const flag_t mod[] = {
284 T1_SIG_S, T1_SIG_S|T1_SGN_S,
285 T1_SIG_E, T1_SIG_E|T1_SGN_E,
286 T1_SIG_W, T1_SIG_W|T1_SGN_W,
287 T1_SIG_N, T1_SIG_N|T1_SGN_N
294 flagsp[-1] |= mod[s+2];
296 flagsp[1] |= mod[s+4];
303 void opj_t1_enc_sigpass_step( opj_t1_t *t1,
317 opj_mqc_t *mqc = t1->mqc; /* MQC component */
319 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
320 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
321 v = opj_int_abs(*datap) & one ? 1 : 0;
322 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
323 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
324 opj_mqc_bypass_enc(mqc, v);
326 opj_mqc_encode(mqc, v);
329 v = *datap < 0 ? 1 : 0;
330 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
331 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
332 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
333 opj_mqc_bypass_enc(mqc, v);
335 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
337 opj_t1_updateflags(flagsp, v, t1->flags_stride);
342 void opj_t1_enc_sigpass(opj_t1_t *t1,
350 OPJ_UINT32 i, j, k, vsc;
354 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
355 for (k = 0; k < t1->h; k += 4) {
356 for (i = 0; i < t1->w; ++i) {
357 for (j = k; j < k + 4 && j < t1->h; ++j) {
358 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
359 opj_t1_enc_sigpass_step(
361 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
362 &t1->data[(j * t1->w) + i],
375 void opj_t1_enc_refpass_step( opj_t1_t *t1,
387 opj_mqc_t *mqc = t1->mqc; /* MQC component */
389 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
390 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
391 *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
392 v = opj_int_abs(*datap) & one ? 1 : 0;
393 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
394 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
395 opj_mqc_bypass_enc(mqc, v);
397 opj_mqc_encode(mqc, v);
399 *flagsp |= T1_REFINE;
404 void opj_t1_enc_refpass(
411 OPJ_UINT32 i, j, k, vsc;
415 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
416 for (k = 0; k < t1->h; k += 4) {
417 for (i = 0; i < t1->w; ++i) {
418 for (j = k; j < k + 4 && j < t1->h; ++j) {
419 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
420 opj_t1_enc_refpass_step(
422 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
423 &t1->data[(j * t1->w) + i],
435 void opj_t1_enc_clnpass_step(
449 opj_mqc_t *mqc = t1->mqc; /* MQC component */
451 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
455 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
456 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
457 v = opj_int_abs(*datap) & one ? 1 : 0;
458 opj_mqc_encode(mqc, v);
461 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
462 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
463 v = *datap < 0 ? 1 : 0;
464 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
465 opj_t1_updateflags(flagsp, v, t1->flags_stride);
468 *flagsp &= ~T1_VISIT;
471 static void t1_dec_clnpass_step_partial(
479 opj_mqc_t *mqc = t1->mqc; /* MQC component */
481 OPJ_ARG_NOT_USED(orient);
484 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
485 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
486 *datap = v ? -oneplushalf : oneplushalf;
487 opj_t1_updateflags(flagsp, v, t1->flags_stride);
488 *flagsp &= ~T1_VISIT;
489 } /* VSC and BYPASS by Antonin */
491 static void t1_dec_clnpass_step(
500 opj_mqc_t *mqc = t1->mqc; /* MQC component */
503 if (!(flag & (T1_SIG | T1_VISIT))) {
504 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
505 if (opj_mqc_decode(mqc)) {
506 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
507 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
508 *datap = v ? -oneplushalf : oneplushalf;
509 opj_t1_updateflags(flagsp, v, t1->flags_stride);
512 *flagsp &= ~T1_VISIT;
513 } /* VSC and BYPASS by Antonin */
515 static void t1_dec_clnpass_step_vsc(
526 opj_mqc_t *mqc = t1->mqc; /* MQC component */
528 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
532 if (!(flag & (T1_SIG | T1_VISIT))) {
533 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
534 if (opj_mqc_decode(mqc)) {
536 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
537 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
538 *datap = v ? -oneplushalf : oneplushalf;
539 opj_t1_updateflags(flagsp, v, t1->flags_stride);
542 *flagsp &= ~T1_VISIT;
545 void opj_t1_enc_clnpass(
554 OPJ_UINT32 agg, runlen, vsc;
556 opj_mqc_t *mqc = t1->mqc; /* MQC component */
559 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
560 for (k = 0; k < t1->h; k += 4) {
561 for (i = 0; i < t1->w; ++i) {
563 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
564 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
565 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
566 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
567 || (MACRO_t1_flags(1 + k + 3,1 + i)
568 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
570 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
571 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
572 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
573 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
579 for (runlen = 0; runlen < 4; ++runlen) {
580 if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
583 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
584 opj_mqc_encode(mqc, runlen != 4);
588 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
589 opj_mqc_encode(mqc, runlen >> 1);
590 opj_mqc_encode(mqc, runlen & 1);
594 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
595 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
596 opj_t1_enc_clnpass_step(
598 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
599 &t1->data[(j * t1->w) + i],
604 agg && (j == k + runlen),
611 static void t1_dec_clnpass(
617 int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
618 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
620 opj_mqc_t *mqc = t1->mqc; /* MQC component */
624 oneplushalf = one | half;
625 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
626 for (k = 0; k < t1->h; k += 4) {
627 for (i = 0; i < t1->w; ++i) {
629 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
630 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
631 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
632 || (MACRO_t1_flags(1 + k + 3,1 + i)
633 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
638 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
639 if (!opj_mqc_decode(mqc)) {
642 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
643 runlen = opj_mqc_decode(mqc);
644 runlen = (runlen << 1) | opj_mqc_decode(mqc);
648 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
649 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
650 t1_dec_clnpass_step_vsc(
652 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
653 &t1->data[(j * t1->w) + i],
656 agg && (j == k + runlen),
662 int *data1 = t1->data;
663 flag_t *flags1 = &t1->flags[1];
664 for (k = 0; k < (t1->h & ~3); k += 4) {
665 for (i = 0; i < t1->w; ++i) {
666 int *data2 = data1 + i;
667 flag_t *flags2 = flags1 + i;
668 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
669 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
670 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
671 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
673 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
674 if (!opj_mqc_decode(mqc)) {
677 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
678 runlen = opj_mqc_decode(mqc);
679 runlen = (runlen << 1) | opj_mqc_decode(mqc);
680 flags2 += runlen * t1->flags_stride;
681 data2 += runlen * t1->w;
682 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
683 flags2 += t1->flags_stride;
684 if (agg && (j == k + runlen)) {
685 t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
687 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
692 flags2 += t1->flags_stride;
693 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
695 flags2 += t1->flags_stride;
696 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
698 flags2 += t1->flags_stride;
699 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
701 flags2 += t1->flags_stride;
702 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
707 flags1 += t1->flags_stride << 2;
709 for (i = 0; i < t1->w; ++i) {
710 int *data2 = data1 + i;
711 flag_t *flags2 = flags1 + i;
712 for (j = k; j < t1->h; ++j) {
713 flags2 += t1->flags_stride;
714 t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
722 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
723 v = opj_mqc_decode(mqc);
724 v = (v << 1) | opj_mqc_decode(mqc);
725 v = (v << 1) | opj_mqc_decode(mqc);
726 v = (v << 1) | opj_mqc_decode(mqc);
729 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
733 } /* VSC and BYPASS by Antonin */
736 /** mod fixed_quality */
737 static double t1_getwmsedec(
748 double w1, w2, wmsedec;
750 w1 = (mct && numcomps==3) ? opj_mct_getnorm(compno) : 1.0;
751 w2 = dwt_getnorm(level, orient);
752 } else { /* if (qmfbid == 0) */
753 w1 = (mct && numcomps==3) ? opj_mct_getnorm_real(compno) : 1.0;
754 w2 = dwt_getnorm_real(level, orient);
756 wmsedec = w1 * w2 * stepsize * (1 << bpno);
757 wmsedec *= wmsedec * nmsedec / 8192.0;
762 /** mod fixed_quality */
763 static OPJ_FLOAT64 t1_getwmsedec_v2(
770 OPJ_FLOAT64 stepsize,
772 const OPJ_FLOAT64 * mct_norms)
774 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
777 w1 = mct_norms[compno];
781 w2 = opj_dwt_getnorm(level, orient);
782 } else { /* if (qmfbid == 0) */
783 w2 = opj_dwt_getnorm_real(level, orient);
786 wmsedec = w1 * w2 * stepsize * (1 << bpno);
787 wmsedec *= wmsedec * nmsedec / 8192.0;
792 opj_bool opj_t1_allocate_buffers(
797 OPJ_UINT32 datasize=w * h;
798 OPJ_UINT32 flagssize;
800 if(datasize > t1->datasize){
801 opj_aligned_free(t1->data);
802 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
806 t1->datasize=datasize;
808 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
810 t1->flags_stride=w+2;
811 flagssize=t1->flags_stride * (h+2);
813 if(flagssize > t1->flagssize){
814 opj_aligned_free(t1->flags);
815 t1->flags = (flag_t*) opj_aligned_malloc(flagssize * sizeof(flag_t));
819 t1->flagssize=flagssize;
821 memset(t1->flags,0,flagssize * sizeof(flag_t));
829 /* ----------------------------------------------------------------------- */
831 /* ----------------------------------------------------------------------- */
833 * Creates a new Tier 1 handle
834 * and initializes the look-up tables of the Tier-1 coder/decoder
835 * @return a new T1 handle if successful, returns NULL otherwise
837 opj_t1_t* opj_t1_create()
841 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
845 memset(l_t1,0,sizeof(opj_t1_t));
847 /* create MQC and RAW handles */
848 l_t1->mqc = opj_mqc_create();
850 opj_t1_destroy(l_t1);
854 l_t1->raw = raw_create();
856 opj_t1_destroy(l_t1);
865 * Destroys a previously created T1 handle
867 * @param p_t1 Tier 1 handle to destroy
869 void opj_t1_destroy(opj_t1_t *p_t1)
875 /* destroy MQC and RAW handles */
876 opj_mqc_destroy(p_t1->mqc);
878 raw_destroy(p_t1->raw);
882 opj_aligned_free(p_t1->data);
887 opj_aligned_free(p_t1->flags);
894 opj_bool opj_t1_decode_cblks( opj_t1_t* t1,
895 opj_tcd_tilecomp_v2_t* tilec,
899 OPJ_UINT32 resno, bandno, precno, cblkno;
900 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
902 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
903 opj_tcd_resolution_v2_t* res = &tilec->resolutions[resno];
905 for (bandno = 0; bandno < res->numbands; ++bandno) {
906 opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
908 for (precno = 0; precno < res->pw * res->ph; ++precno) {
909 opj_tcd_precinct_v2_t* precinct = &band->precincts[precno];
911 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
912 opj_tcd_cblk_dec_v2_t* cblk = &precinct->cblks.dec[cblkno];
913 OPJ_INT32* restrict datap;
914 void* restrict tiledp;
915 OPJ_UINT32 cblk_w, cblk_h;
919 if (OPJ_FALSE == opj_t1_decode_cblk(
928 x = cblk->x0 - band->x0;
929 y = cblk->y0 - band->y0;
930 if (band->bandno & 1) {
931 opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
932 x += pres->x1 - pres->x0;
934 if (band->bandno & 2) {
935 opj_tcd_resolution_v2_t* pres = &tilec->resolutions[resno - 1];
936 y += pres->y1 - pres->y0;
943 if (tccp->roishift) {
944 OPJ_INT32 thresh = 1 << tccp->roishift;
945 for (j = 0; j < cblk_h; ++j) {
946 for (i = 0; i < cblk_w; ++i) {
947 OPJ_INT32 val = datap[(j * cblk_w) + i];
948 OPJ_INT32 mag = abs(val);
950 mag >>= tccp->roishift;
951 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
957 tiledp=(void*)&tilec->data[(y * tile_w) + x];
958 if (tccp->qmfbid == 1) {
959 for (j = 0; j < cblk_h; ++j) {
960 for (i = 0; i < cblk_w; ++i) {
961 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
962 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
965 } else { /* if (tccp->qmfbid == 0) */
966 for (j = 0; j < cblk_h; ++j) {
967 for (i = 0; i < cblk_w; ++i) {
968 float tmp = datap[(j * cblk_w) + i] * band->stepsize;
969 ((float*)tiledp)[(j * tile_w) + i] = tmp;
973 /*opj_free(cblk->segs);*/
983 opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
984 opj_tcd_cblk_dec_v2_t* cblk,
989 opj_raw_t *raw = t1->raw; /* RAW component */
990 opj_mqc_t *mqc = t1->mqc; /* MQC component */
994 OPJ_UINT32 segno, passno;
995 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
997 if(!opj_t1_allocate_buffers(
1000 cblk->y1 - cblk->y0))
1005 bpno = roishift + cblk->numbps - 1;
1008 opj_mqc_resetstates(mqc);
1009 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1010 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1011 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1013 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1014 opj_tcd_seg_t *seg = &cblk->segs[segno];
1017 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1018 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1019 if(seg->data == 00){
1022 if (type == T1_TYPE_RAW) {
1023 raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1025 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1030 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1033 opj_t1_dec_sigpass(t1, bpno+1, orient, type, cblksty);
1036 opj_t1_dec_refpass(t1, bpno+1, type, cblksty);
1039 t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1043 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1044 opj_mqc_resetstates(mqc);
1045 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1046 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1047 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1049 if (++passtype == 3) {
1058 opj_bool opj_t1_encode_cblks( opj_t1_t *t1,
1059 opj_tcd_tile_v2_t *tile,
1061 const OPJ_FLOAT64 * mct_norms
1064 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1066 tile->distotile = 0; /* fixed_quality */
1068 for (compno = 0; compno < tile->numcomps; ++compno) {
1069 opj_tcd_tilecomp_v2_t* tilec = &tile->comps[compno];
1070 opj_tccp_t* tccp = &tcp->tccps[compno];
1071 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1073 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1074 opj_tcd_resolution_v2_t *res = &tilec->resolutions[resno];
1076 for (bandno = 0; bandno < res->numbands; ++bandno) {
1077 opj_tcd_band_v2_t* restrict band = &res->bands[bandno];
1078 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1080 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1081 opj_tcd_precinct_v2_t *prc = &band->precincts[precno];
1083 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1084 opj_tcd_cblk_enc_v2_t* cblk = &prc->cblks.enc[cblkno];
1085 OPJ_INT32 * restrict datap;
1086 OPJ_INT32* restrict tiledp;
1091 OPJ_INT32 x = cblk->x0 - band->x0;
1092 OPJ_INT32 y = cblk->y0 - band->y0;
1093 if (band->bandno & 1) {
1094 opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
1095 x += pres->x1 - pres->x0;
1097 if (band->bandno & 2) {
1098 opj_tcd_resolution_v2_t *pres = &tilec->resolutions[resno - 1];
1099 y += pres->y1 - pres->y0;
1102 if(!opj_t1_allocate_buffers(
1104 cblk->x1 - cblk->x0,
1105 cblk->y1 - cblk->y0))
1114 tiledp=&tilec->data[(y * tile_w) + x];
1115 if (tccp->qmfbid == 1) {
1116 for (j = 0; j < cblk_h; ++j) {
1117 for (i = 0; i < cblk_w; ++i) {
1118 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1119 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1122 } else { /* if (tccp->qmfbid == 0) */
1123 for (j = 0; j < cblk_h; ++j) {
1124 for (i = 0; i < cblk_w; ++i) {
1125 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1126 datap[(j * cblk_w) + i] =
1129 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1139 tilec->numresolutions - 1 - resno,
1155 /** mod fixed_quality */
1156 void opj_t1_encode_cblk(opj_t1_t *t1,
1157 opj_tcd_cblk_enc_v2_t* cblk,
1162 OPJ_FLOAT64 stepsize,
1164 OPJ_UINT32 numcomps,
1165 opj_tcd_tile_v2_t * tile,
1166 const OPJ_FLOAT64 * mct_norms)
1168 OPJ_FLOAT64 cumwmsedec = 0.0;
1170 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1174 OPJ_UINT32 passtype;
1175 OPJ_INT32 nmsedec = 0;
1178 OPJ_BYTE type = T1_TYPE_MQ;
1179 OPJ_FLOAT64 tempwmsedec;
1182 for (i = 0; i < t1->w * t1->h; ++i) {
1183 OPJ_INT32 tmp = abs(t1->data[i]);
1184 max = opj_int_max(max, tmp);
1187 cblk->numbps = max ? (opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1189 bpno = cblk->numbps - 1;
1192 opj_mqc_resetstates(mqc);
1193 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1194 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1195 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1196 opj_mqc_init_enc(mqc, cblk->data);
1198 for (passno = 0; bpno >= 0; ++passno) {
1199 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1200 OPJ_UINT32 correction = 3;
1201 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1205 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1208 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1211 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1212 /* code switch SEGMARK (i.e. SEGSYM) */
1213 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1214 opj_mqc_segmark_enc(mqc);
1219 tempwmsedec = t1_getwmsedec_v2(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1220 cumwmsedec += tempwmsedec;
1221 tile->distotile += tempwmsedec;
1223 /* Code switch "RESTART" (i.e. TERMALL) */
1224 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1225 if (type == T1_TYPE_RAW) {
1228 /* correction = mqc_bypass_flush_enc(); */
1229 } else { /* correction = mqc_restart_enc(); */
1235 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1236 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1237 if (type == T1_TYPE_RAW) {
1240 /* correction = mqc_bypass_flush_enc(); */
1241 } else { /* correction = mqc_restart_enc(); */
1251 if (++passtype == 3) {
1256 if (pass->term && bpno > 0) {
1257 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1258 if (type == T1_TYPE_RAW)
1259 opj_mqc_bypass_init_enc(mqc);
1261 opj_mqc_restart_init_enc(mqc);
1264 pass->distortiondec = cumwmsedec;
1265 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1267 /* Code-switch "RESET" */
1268 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1269 opj_mqc_reset_enc(mqc);
1272 /* Code switch "ERTERM" (i.e. PTERM) */
1273 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1274 opj_mqc_erterm_enc(mqc);
1275 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1278 cblk->totalpasses = passno;
1280 for (passno = 0; passno<cblk->totalpasses; passno++) {
1281 opj_tcd_pass_v2_t *pass = &cblk->passes[passno];
1282 if (pass->rate > opj_mqc_numbytes(mqc))
1283 pass->rate = opj_mqc_numbytes(mqc);
1284 /*Preventing generation of FF as last data byte of a pass*/
1285 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1288 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1292 void opj_t1_dec_refpass(opj_t1_t *t1,
1298 OPJ_INT32 one, poshalf, neghalf;
1302 neghalf = bpno > 0 ? -poshalf : -1;
1303 for (k = 0; k < t1->h; k += 4) {
1304 for (i = 0; i < t1->w; ++i) {
1305 for (j = k; j < k + 4 && j < t1->h; ++j) {
1306 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1307 opj_t1_dec_refpass_step(
1309 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1310 &t1->data[(j * t1->w) + i],
1318 } /* VSC and BYPASS by Antonin */
1321 void opj_t1_dec_refpass_step( opj_t1_t *t1,
1332 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1333 opj_raw_t *raw = t1->raw; /* RAW component */
1335 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1336 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1337 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1338 if (type == T1_TYPE_RAW) {
1339 v = raw_decode(raw);
1341 v = opj_mqc_decode(mqc);
1343 t = v ? poshalf : neghalf;
1344 *datap += *datap < 0 ? -t : t;
1345 *flagsp |= T1_REFINE;
1347 } /* VSC and BYPASS by Antonin */
1349 void opj_t1_dec_sigpass(opj_t1_t *t1,
1355 OPJ_UINT32 i, j, k, vsc;
1356 OPJ_INT32 one, half, oneplushalf;
1359 oneplushalf = one | half;
1360 for (k = 0; k < t1->h; k += 4) {
1361 for (i = 0; i < t1->w; ++i) {
1362 for (j = k; j < k + 4 && j < t1->h; ++j) {
1363 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1364 opj_t1_dec_sigpass_step(
1366 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1367 &t1->data[(j * t1->w) + i],
1375 } /* VSC and BYPASS by Antonin */
1377 void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1381 OPJ_INT32 oneplushalf,
1387 opj_raw_t *raw = t1->raw; /* RAW component */
1388 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1390 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1391 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1392 if (type == T1_TYPE_RAW) {
1393 if (raw_decode(raw)) {
1394 v = raw_decode(raw); /* ESSAI */
1395 *datap = v ? -oneplushalf : oneplushalf;
1396 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1399 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1400 if (opj_mqc_decode(mqc)) {
1401 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1402 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1403 *datap = v ? -oneplushalf : oneplushalf;
1404 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1407 *flagsp |= T1_VISIT;
1409 } /* VSC and BYPASS by Antonin */