2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
42 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
45 /** @name Local static functions */
48 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
49 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
50 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
52 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
53 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
54 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
56 Encode significant pass
58 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
69 Decode significant pass
72 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
76 OPJ_INT32 oneplushalf,
81 static INLINE void opj_t1_dec_sigpass_step_raw(
86 OPJ_INT32 oneplushalf,
88 static INLINE void opj_t1_dec_sigpass_step_mqc(
93 OPJ_INT32 oneplushalf);
94 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
99 OPJ_INT32 oneplushalf,
104 Encode significant pass
106 static void opj_t1_enc_sigpass( opj_t1_t *t1,
114 Decode significant pass
116 static void opj_t1_dec_sigpass_raw(
121 static void opj_t1_dec_sigpass_mqc(
125 static void opj_t1_dec_sigpass_mqc_vsc(
133 Encode refinement pass
135 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
146 Encode refinement pass
148 static void opj_t1_enc_refpass( opj_t1_t *t1,
155 Decode refinement pass
157 static void opj_t1_dec_refpass_raw(
161 static void opj_t1_dec_refpass_mqc(
164 static void opj_t1_dec_refpass_mqc_vsc(
170 Decode refinement pass
173 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
182 static INLINE void opj_t1_dec_refpass_step_raw(
189 static INLINE void opj_t1_dec_refpass_step_mqc(
195 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
208 static void opj_t1_enc_clnpass_step(
221 static void opj_t1_dec_clnpass_step_partial(
226 OPJ_INT32 oneplushalf);
227 static void opj_t1_dec_clnpass_step(
232 OPJ_INT32 oneplushalf);
233 static void opj_t1_dec_clnpass_step_vsc(
238 OPJ_INT32 oneplushalf,
244 static void opj_t1_enc_clnpass(
253 static void opj_t1_dec_clnpass(
259 static OPJ_FLOAT64 opj_t1_getwmsedec(
266 OPJ_FLOAT64 stepsize,
268 const OPJ_FLOAT64 * mct_norms,
269 OPJ_UINT32 mct_numcomps);
271 static void opj_t1_encode_cblk( opj_t1_t *t1,
272 opj_tcd_cblk_enc_t* cblk,
277 OPJ_FLOAT64 stepsize,
280 opj_tcd_tile_t * tile,
281 const OPJ_FLOAT64 * mct_norms,
282 OPJ_UINT32 mct_numcomps);
287 @param cblk Code-block coding parameters
289 @param roishift Region of interest shifting value
290 @param cblksty Code-block style
292 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
293 opj_tcd_cblk_dec_t* cblk,
298 static OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
306 /* ----------------------------------------------------------------------- */
308 static OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
309 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
312 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
313 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
316 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
317 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
318 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
322 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
323 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
326 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
328 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
331 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
334 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
336 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
339 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
342 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
343 opj_flag_t *np = flagsp - stride;
344 opj_flag_t *sp = flagsp + stride;
346 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
347 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
348 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
350 opj_flag_t flag_N = T1_SIG_N | (T1_SIG_N << (4 * s));
353 np[0] |= flag_N << 2;
356 flagsp[-1] |= flag_N << 1;
358 flagsp[1] |= flag_N << 3;
365 static void opj_t1_enc_sigpass_step( opj_t1_t *t1,
379 opj_mqc_t *mqc = t1->mqc; /* MQC component */
381 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
382 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
383 v = (opj_int_abs(*datap) & one) ? 1 : 0;
384 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
385 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
386 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
388 opj_mqc_encode(mqc, (OPJ_UINT32)v);
391 v = *datap < 0 ? 1 : 0;
392 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
393 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
394 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
395 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
397 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
399 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
406 static INLINE void opj_t1_dec_sigpass_step_raw(
411 OPJ_INT32 oneplushalf,
415 opj_raw_t *raw = t1->raw; /* RAW component */
416 OPJ_ARG_NOT_USED(orient);
418 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
419 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
420 if (opj_raw_decode(raw)) {
421 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
422 *datap = v ? -oneplushalf : oneplushalf;
423 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
429 static INLINE void opj_t1_dec_sigpass_step_mqc(
434 OPJ_INT32 oneplushalf)
438 opj_mqc_t *mqc = t1->mqc; /* MQC component */
441 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
442 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
443 if (opj_mqc_decode(mqc)) {
444 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
445 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
446 *datap = v ? -oneplushalf : oneplushalf;
447 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
451 } /* VSC and BYPASS by Antonin */
453 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
458 OPJ_INT32 oneplushalf,
463 opj_mqc_t *mqc = t1->mqc; /* MQC component */
465 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
466 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
467 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
468 if (opj_mqc_decode(mqc)) {
469 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
470 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
471 *datap = v ? -oneplushalf : oneplushalf;
472 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
476 } /* VSC and BYPASS by Antonin */
480 static void opj_t1_enc_sigpass(opj_t1_t *t1,
488 OPJ_UINT32 i, j, k, vsc;
492 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
493 for (k = 0; k < t1->h; k += 4) {
494 for (i = 0; i < t1->w; ++i) {
495 for (j = k; j < k + 4 && j < t1->h; ++j) {
496 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
497 opj_t1_enc_sigpass_step(
499 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
500 &t1->data[(j * t1->data_stride) + i],
512 static void opj_t1_dec_sigpass_raw(
518 OPJ_INT32 one, half, oneplushalf, vsc;
522 oneplushalf = one | half;
523 for (k = 0; k < t1->h; k += 4) {
524 for (i = 0; i < t1->w; ++i) {
525 for (j = k; j < k + 4 && j < t1->h; ++j) {
526 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
527 opj_t1_dec_sigpass_step_raw(
529 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
530 &t1->data[(j * t1->w) + i],
537 } /* VSC and BYPASS by Antonin */
539 static void opj_t1_dec_sigpass_mqc(
544 OPJ_INT32 one, half, oneplushalf;
546 OPJ_INT32 *data1 = t1->data;
547 opj_flag_t *flags1 = &t1->flags[1];
550 oneplushalf = one | half;
551 for (k = 0; k < (t1->h & ~3u); k += 4) {
552 for (i = 0; i < t1->w; ++i) {
553 OPJ_INT32 *data2 = data1 + i;
554 opj_flag_t *flags2 = flags1 + i;
555 flags2 += t1->flags_stride;
556 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
558 flags2 += t1->flags_stride;
559 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
561 flags2 += t1->flags_stride;
562 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
564 flags2 += t1->flags_stride;
565 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
569 flags1 += t1->flags_stride << 2;
571 for (i = 0; i < t1->w; ++i) {
572 OPJ_INT32 *data2 = data1 + i;
573 opj_flag_t *flags2 = flags1 + i;
574 for (j = k; j < t1->h; ++j) {
575 flags2 += t1->flags_stride;
576 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
580 } /* VSC and BYPASS by Antonin */
582 static void opj_t1_dec_sigpass_mqc_vsc(
587 OPJ_INT32 one, half, oneplushalf, vsc;
591 oneplushalf = one | half;
592 for (k = 0; k < t1->h; k += 4) {
593 for (i = 0; i < t1->w; ++i) {
594 for (j = k; j < k + 4 && j < t1->h; ++j) {
595 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
596 opj_t1_dec_sigpass_step_mqc_vsc(
598 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
599 &t1->data[(j * t1->w) + i],
606 } /* VSC and BYPASS by Antonin */
610 static void opj_t1_enc_refpass_step( opj_t1_t *t1,
622 opj_mqc_t *mqc = t1->mqc; /* MQC component */
624 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
625 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
626 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
627 v = (opj_int_abs(*datap) & one) ? 1 : 0;
628 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
629 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
630 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
632 opj_mqc_encode(mqc, (OPJ_UINT32)v);
634 *flagsp |= T1_REFINE;
638 static INLINE void opj_t1_dec_refpass_step_raw(
646 OPJ_INT32 v, t, flag;
648 opj_raw_t *raw = t1->raw; /* RAW component */
650 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
651 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
652 v = (OPJ_INT32)opj_raw_decode(raw);
653 t = v ? poshalf : neghalf;
654 *datap += *datap < 0 ? -t : t;
655 *flagsp |= T1_REFINE;
657 } /* VSC and BYPASS by Antonin */
659 static INLINE void opj_t1_dec_refpass_step_mqc(
666 OPJ_INT32 v, t, flag;
668 opj_mqc_t *mqc = t1->mqc; /* MQC component */
671 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
672 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
673 v = opj_mqc_decode(mqc);
674 t = v ? poshalf : neghalf;
675 *datap += *datap < 0 ? -t : t;
676 *flagsp |= T1_REFINE;
678 } /* VSC and BYPASS by Antonin */
680 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
688 OPJ_INT32 v, t, flag;
690 opj_mqc_t *mqc = t1->mqc; /* MQC component */
692 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
693 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
694 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
695 v = opj_mqc_decode(mqc);
696 t = v ? poshalf : neghalf;
697 *datap += *datap < 0 ? -t : t;
698 *flagsp |= T1_REFINE;
700 } /* VSC and BYPASS by Antonin */
703 static void opj_t1_enc_refpass(
710 OPJ_UINT32 i, j, k, vsc;
714 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
715 for (k = 0; k < t1->h; k += 4) {
716 for (i = 0; i < t1->w; ++i) {
717 for (j = k; j < k + 4 && j < t1->h; ++j) {
718 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
719 opj_t1_enc_refpass_step(
721 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
722 &t1->data[(j * t1->data_stride) + i],
733 static void opj_t1_dec_refpass_raw(
738 OPJ_INT32 one, poshalf, neghalf;
743 neghalf = bpno > 0 ? -poshalf : -1;
744 for (k = 0; k < t1->h; k += 4) {
745 for (i = 0; i < t1->w; ++i) {
746 for (j = k; j < k + 4 && j < t1->h; ++j) {
747 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
748 opj_t1_dec_refpass_step_raw(
750 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
751 &t1->data[(j * t1->w) + i],
758 } /* VSC and BYPASS by Antonin */
760 static void opj_t1_dec_refpass_mqc(
764 OPJ_INT32 one, poshalf, neghalf;
766 OPJ_INT32 *data1 = t1->data;
767 opj_flag_t *flags1 = &t1->flags[1];
770 neghalf = bpno > 0 ? -poshalf : -1;
771 for (k = 0; k < (t1->h & ~3u); k += 4) {
772 for (i = 0; i < t1->w; ++i) {
773 OPJ_INT32 *data2 = data1 + i;
774 opj_flag_t *flags2 = flags1 + i;
775 flags2 += t1->flags_stride;
776 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
778 flags2 += t1->flags_stride;
779 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
781 flags2 += t1->flags_stride;
782 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
784 flags2 += t1->flags_stride;
785 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
789 flags1 += t1->flags_stride << 2;
791 for (i = 0; i < t1->w; ++i) {
792 OPJ_INT32 *data2 = data1 + i;
793 opj_flag_t *flags2 = flags1 + i;
794 for (j = k; j < t1->h; ++j) {
795 flags2 += t1->flags_stride;
796 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
800 } /* VSC and BYPASS by Antonin */
802 static void opj_t1_dec_refpass_mqc_vsc(
806 OPJ_INT32 one, poshalf, neghalf;
811 neghalf = bpno > 0 ? -poshalf : -1;
812 for (k = 0; k < t1->h; k += 4) {
813 for (i = 0; i < t1->w; ++i) {
814 for (j = k; j < k + 4 && j < t1->h; ++j) {
815 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
816 opj_t1_dec_refpass_step_mqc_vsc(
818 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
819 &t1->data[(j * t1->w) + i],
826 } /* VSC and BYPASS by Antonin */
829 static void opj_t1_enc_clnpass_step(
843 opj_mqc_t *mqc = t1->mqc; /* MQC component */
845 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
849 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
850 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
851 v = (opj_int_abs(*datap) & one) ? 1 : 0;
852 opj_mqc_encode(mqc, (OPJ_UINT32)v);
855 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
856 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
857 v = *datap < 0 ? 1 : 0;
858 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
859 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
862 *flagsp &= ~T1_VISIT;
865 static void opj_t1_dec_clnpass_step_partial(
870 OPJ_INT32 oneplushalf)
873 opj_mqc_t *mqc = t1->mqc; /* MQC component */
875 OPJ_ARG_NOT_USED(orient);
878 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
879 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
880 *datap = v ? -oneplushalf : oneplushalf;
881 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
882 *flagsp &= ~T1_VISIT;
883 } /* VSC and BYPASS by Antonin */
885 static void opj_t1_dec_clnpass_step(
890 OPJ_INT32 oneplushalf)
894 opj_mqc_t *mqc = t1->mqc; /* MQC component */
897 if (!(flag & (T1_SIG | T1_VISIT))) {
898 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
899 if (opj_mqc_decode(mqc)) {
900 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
901 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
902 *datap = v ? -oneplushalf : oneplushalf;
903 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
906 *flagsp &= ~T1_VISIT;
907 } /* VSC and BYPASS by Antonin */
909 static void opj_t1_dec_clnpass_step_vsc(
914 OPJ_INT32 oneplushalf,
920 opj_mqc_t *mqc = t1->mqc; /* MQC component */
922 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
926 if (!(flag & (T1_SIG | T1_VISIT))) {
927 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
928 if (opj_mqc_decode(mqc)) {
930 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
931 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
932 *datap = v ? -oneplushalf : oneplushalf;
933 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
936 *flagsp &= ~T1_VISIT;
939 static void opj_t1_enc_clnpass(
948 OPJ_UINT32 agg, runlen, vsc;
950 opj_mqc_t *mqc = t1->mqc; /* MQC component */
953 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
954 for (k = 0; k < t1->h; k += 4) {
955 for (i = 0; i < t1->w; ++i) {
957 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
958 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
959 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
961 || (MACRO_t1_flags(1 + k + 3,1 + i)
962 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
964 agg = !((MACRO_t1_flags(1 + k,1 + i) |
965 MACRO_t1_flags(1 + k + 1,1 + i) |
966 MACRO_t1_flags(1 + k + 2,1 + i) |
967 MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
973 for (runlen = 0; runlen < 4; ++runlen) {
974 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
977 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
978 opj_mqc_encode(mqc, runlen != 4);
982 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
983 opj_mqc_encode(mqc, runlen >> 1);
984 opj_mqc_encode(mqc, runlen & 1);
988 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
989 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
990 opj_t1_enc_clnpass_step(
992 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
993 &t1->data[(j * t1->data_stride) + i],
998 agg && (j == k + runlen),
1005 static void opj_t1_dec_clnpass(
1011 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
1013 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1015 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1019 oneplushalf = one | half;
1020 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1021 for (k = 0; k < t1->h; k += 4) {
1022 for (i = 0; i < t1->w; ++i) {
1023 if (k + 3 < t1->h) {
1024 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1025 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1026 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1027 || (MACRO_t1_flags(1 + k + 3,1 + i)
1028 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1033 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1034 if (!opj_mqc_decode(mqc)) {
1037 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1038 runlen = opj_mqc_decode(mqc);
1039 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1043 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1044 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1045 opj_t1_dec_clnpass_step_vsc(
1047 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1048 &t1->data[(j * t1->w) + i],
1051 agg && (j == k + (OPJ_UINT32)runlen),
1057 OPJ_INT32 *data1 = t1->data;
1058 opj_flag_t *flags1 = &t1->flags[1];
1059 for (k = 0; k < (t1->h & ~3u); k += 4) {
1060 for (i = 0; i < t1->w; ++i) {
1061 OPJ_INT32 *data2 = data1 + i;
1062 opj_flag_t *flags2 = flags1 + i;
1063 agg = !((MACRO_t1_flags(1 + k, 1 + i) |
1064 MACRO_t1_flags(1 + k + 1, 1 + i) |
1065 MACRO_t1_flags(1 + k + 2, 1 + i) |
1066 MACRO_t1_flags(1 + k + 3, 1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1068 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1069 if (!opj_mqc_decode(mqc)) {
1072 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1073 runlen = opj_mqc_decode(mqc);
1074 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1075 flags2 += (OPJ_UINT32)runlen * t1->flags_stride;
1076 data2 += (OPJ_UINT32)runlen * t1->w;
1077 for (j = (OPJ_UINT32)runlen; j < 4 && j < t1->h; ++j) {
1078 flags2 += t1->flags_stride;
1079 if (agg && (j == (OPJ_UINT32)runlen)) {
1080 opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1082 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1087 flags2 += t1->flags_stride;
1088 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1090 flags2 += t1->flags_stride;
1091 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1093 flags2 += t1->flags_stride;
1094 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1096 flags2 += t1->flags_stride;
1097 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1101 data1 += t1->w << 2;
1102 flags1 += t1->flags_stride << 2;
1104 for (i = 0; i < t1->w; ++i) {
1105 OPJ_INT32 *data2 = data1 + i;
1106 opj_flag_t *flags2 = flags1 + i;
1107 for (j = k; j < t1->h; ++j) {
1108 flags2 += t1->flags_stride;
1109 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1117 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1118 v = opj_mqc_decode(mqc);
1119 v = (v << 1) | opj_mqc_decode(mqc);
1120 v = (v << 1) | opj_mqc_decode(mqc);
1121 v = (v << 1) | opj_mqc_decode(mqc);
1124 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1128 } /* VSC and BYPASS by Antonin */
1131 /** mod fixed_quality */
1132 static OPJ_FLOAT64 opj_t1_getwmsedec(
1139 OPJ_FLOAT64 stepsize,
1140 OPJ_UINT32 numcomps,
1141 const OPJ_FLOAT64 * mct_norms,
1142 OPJ_UINT32 mct_numcomps)
1144 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1145 OPJ_ARG_NOT_USED(numcomps);
1147 if (mct_norms && (compno < mct_numcomps)) {
1148 w1 = mct_norms[compno];
1152 w2 = opj_dwt_getnorm(level, orient);
1153 } else { /* if (qmfbid == 0) */
1154 w2 = opj_dwt_getnorm_real(level, orient);
1157 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1158 wmsedec *= wmsedec * nmsedec / 8192.0;
1163 static OPJ_BOOL opj_t1_allocate_buffers(
1168 OPJ_UINT32 datasize=w * h;
1169 OPJ_UINT32 flagssize;
1171 /* encoder uses tile buffer, so no need to allocate */
1173 if(datasize > t1->datasize){
1174 opj_aligned_free(t1->data);
1175 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1177 /* FIXME event manager error callback */
1180 t1->datasize=datasize;
1182 /* memset first arg is declared to never be null by gcc */
1183 if (t1->data != NULL) {
1184 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1187 t1->flags_stride=w+2;
1188 flagssize=t1->flags_stride * (h+2);
1190 if(flagssize > t1->flagssize){
1191 opj_aligned_free(t1->flags);
1192 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1194 /* FIXME event manager error callback */
1197 t1->flagssize=flagssize;
1199 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1207 /* ----------------------------------------------------------------------- */
1209 /* ----------------------------------------------------------------------- */
1211 * Creates a new Tier 1 handle
1212 * and initializes the look-up tables of the Tier-1 coder/decoder
1213 * @return a new T1 handle if successful, returns NULL otherwise
1215 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1217 opj_t1_t *l_t1 = 00;
1219 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1224 /* create MQC and RAW handles */
1225 l_t1->mqc = opj_mqc_create();
1227 opj_t1_destroy(l_t1);
1231 l_t1->raw = opj_raw_create();
1233 opj_t1_destroy(l_t1);
1236 l_t1->encoder = isEncoder;
1243 * Destroys a previously created T1 handle
1245 * @param p_t1 Tier 1 handle to destroy
1247 void opj_t1_destroy(opj_t1_t *p_t1)
1253 /* destroy MQC and RAW handles */
1254 opj_mqc_destroy(p_t1->mqc);
1256 opj_raw_destroy(p_t1->raw);
1259 /* encoder uses tile buffer, so no need to free */
1260 if (!p_t1->encoder && p_t1->data) {
1261 opj_aligned_free(p_t1->data);
1266 opj_aligned_free(p_t1->flags);
1273 OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
1274 opj_tcd_tilecomp_t* tilec,
1278 OPJ_UINT32 resno, bandno, precno, cblkno;
1279 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1281 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1282 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1284 for (bandno = 0; bandno < res->numbands; ++bandno) {
1285 opj_tcd_band_t* restrict band = &res->bands[bandno];
1287 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1288 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1290 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1291 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1292 OPJ_INT32* restrict datap;
1293 OPJ_UINT32 cblk_w, cblk_h;
1297 if (OPJ_FALSE == opj_t1_decode_cblk(
1301 (OPJ_UINT32)tccp->roishift,
1306 x = cblk->x0 - band->x0;
1307 y = cblk->y0 - band->y0;
1308 if (band->bandno & 1) {
1309 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1310 x += pres->x1 - pres->x0;
1312 if (band->bandno & 2) {
1313 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1314 y += pres->y1 - pres->y0;
1321 if (tccp->roishift) {
1322 OPJ_INT32 thresh = 1 << tccp->roishift;
1323 for (j = 0; j < cblk_h; ++j) {
1324 for (i = 0; i < cblk_w; ++i) {
1325 OPJ_INT32 val = datap[(j * cblk_w) + i];
1326 OPJ_INT32 mag = abs(val);
1327 if (mag >= thresh) {
1328 mag >>= tccp->roishift;
1329 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1334 if (tccp->qmfbid == 1) {
1335 OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1336 for (j = 0; j < cblk_h; ++j) {
1337 for (i = 0; i < cblk_w; ++i) {
1338 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1339 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp/2;
1342 } else { /* if (tccp->qmfbid == 0) */
1343 OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1344 for (j = 0; j < cblk_h; ++j) {
1345 OPJ_FLOAT32* restrict tiledp2 = tiledp;
1346 for (i = 0; i < cblk_w; ++i) {
1347 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1363 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1364 opj_tcd_cblk_dec_t* cblk,
1366 OPJ_UINT32 roishift,
1369 opj_raw_t *raw = t1->raw; /* RAW component */
1370 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1372 OPJ_INT32 bpno_plus_one;
1373 OPJ_UINT32 passtype;
1374 OPJ_UINT32 segno, passno;
1375 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1377 if(!opj_t1_allocate_buffers(
1379 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1380 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1385 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1388 opj_mqc_resetstates(mqc);
1389 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1390 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1391 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1393 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1394 opj_tcd_seg_t *seg = &cblk->segs[segno];
1397 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1398 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1399 if(seg->data == 00){
1402 if (type == T1_TYPE_RAW) {
1403 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1405 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1410 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1413 if (type == T1_TYPE_RAW) {
1414 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1416 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1417 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one, (OPJ_INT32)orient);
1419 opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)orient);
1424 if (type == T1_TYPE_RAW) {
1425 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1427 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1428 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1430 opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
1435 opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1439 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1440 opj_mqc_resetstates(mqc);
1441 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1442 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1443 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1445 if (++passtype == 3) {
1457 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1458 opj_tcd_tile_t *tile,
1460 const OPJ_FLOAT64 * mct_norms,
1461 OPJ_UINT32 mct_numcomps
1464 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1466 tile->distotile = 0; /* fixed_quality */
1468 for (compno = 0; compno < tile->numcomps; ++compno) {
1469 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1470 opj_tccp_t* tccp = &tcp->tccps[compno];
1471 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1473 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1474 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1476 for (bandno = 0; bandno < res->numbands; ++bandno) {
1477 opj_tcd_band_t* restrict band = &res->bands[bandno];
1478 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1480 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1481 opj_tcd_precinct_t *prc = &band->precincts[precno];
1483 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1484 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1485 OPJ_INT32* restrict tiledp;
1488 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1490 OPJ_INT32 x = cblk->x0 - band->x0;
1491 OPJ_INT32 y = cblk->y0 - band->y0;
1492 if (band->bandno & 1) {
1493 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1494 x += pres->x1 - pres->x0;
1496 if (band->bandno & 2) {
1497 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1498 y += pres->y1 - pres->y0;
1501 if(!opj_t1_allocate_buffers(
1503 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1504 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1511 tileLineAdvance = tile_w - cblk_w;
1513 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1515 t1->data_stride = tile_w;
1516 if (tccp->qmfbid == 1) {
1517 for (j = 0; j < cblk_h; ++j) {
1518 for (i = 0; i < cblk_w; ++i) {
1519 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1522 tileIndex += tileLineAdvance;
1524 } else { /* if (tccp->qmfbid == 0) */
1525 for (j = 0; j < cblk_h; ++j) {
1526 for (i = 0; i < cblk_w; ++i) {
1527 OPJ_INT32 tmp = tiledp[tileIndex];
1534 tileIndex += tileLineAdvance;
1543 tilec->numresolutions - 1 - resno,
1560 /** mod fixed_quality */
1561 static void opj_t1_encode_cblk(opj_t1_t *t1,
1562 opj_tcd_cblk_enc_t* cblk,
1567 OPJ_FLOAT64 stepsize,
1569 OPJ_UINT32 numcomps,
1570 opj_tcd_tile_t * tile,
1571 const OPJ_FLOAT64 * mct_norms,
1572 OPJ_UINT32 mct_numcomps)
1574 OPJ_FLOAT64 cumwmsedec = 0.0;
1576 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1580 OPJ_UINT32 passtype;
1581 OPJ_INT32 nmsedec = 0;
1584 OPJ_BYTE type = T1_TYPE_MQ;
1585 OPJ_FLOAT64 tempwmsedec;
1588 for (i = 0; i < t1->w; ++i) {
1589 for (j = 0; j < t1->h; ++j) {
1590 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1591 max = opj_int_max(max, tmp);
1595 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1597 bpno = (OPJ_INT32)(cblk->numbps - 1);
1600 opj_mqc_resetstates(mqc);
1601 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1602 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1603 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1604 opj_mqc_init_enc(mqc, cblk->data);
1606 for (passno = 0; bpno >= 0; ++passno) {
1607 opj_tcd_pass_t *pass = &cblk->passes[passno];
1608 OPJ_UINT32 correction = 3;
1609 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1613 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1616 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1619 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1620 /* code switch SEGMARK (i.e. SEGSYM) */
1621 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1622 opj_mqc_segmark_enc(mqc);
1627 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
1628 cumwmsedec += tempwmsedec;
1629 tile->distotile += tempwmsedec;
1631 /* Code switch "RESTART" (i.e. TERMALL) */
1632 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1633 if (type == T1_TYPE_RAW) {
1636 /* correction = mqc_bypass_flush_enc(); */
1637 } else { /* correction = mqc_restart_enc(); */
1643 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1644 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1645 if (type == T1_TYPE_RAW) {
1648 /* correction = mqc_bypass_flush_enc(); */
1649 } else { /* correction = mqc_restart_enc(); */
1659 if (++passtype == 3) {
1664 if (pass->term && bpno > 0) {
1665 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1666 if (type == T1_TYPE_RAW)
1667 opj_mqc_bypass_init_enc(mqc);
1669 opj_mqc_restart_init_enc(mqc);
1672 pass->distortiondec = cumwmsedec;
1673 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1675 /* Code-switch "RESET" */
1676 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1677 opj_mqc_reset_enc(mqc);
1680 /* Code switch "ERTERM" (i.e. PTERM) */
1681 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1682 opj_mqc_erterm_enc(mqc);
1683 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1686 cblk->totalpasses = passno;
1688 for (passno = 0; passno<cblk->totalpasses; passno++) {
1689 opj_tcd_pass_t *pass = &cblk->passes[passno];
1690 if (pass->rate > opj_mqc_numbytes(mqc))
1691 pass->rate = opj_mqc_numbytes(mqc);
1692 /*Preventing generation of FF as last data byte of a pass*/
1693 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1696 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1701 static void opj_t1_dec_refpass_step( opj_t1_t *t1,
1712 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1713 opj_raw_t *raw = t1->raw; /* RAW component */
1715 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1716 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1717 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1718 if (type == T1_TYPE_RAW) {
1719 v = opj_raw_decode(raw);
1721 v = opj_mqc_decode(mqc);
1723 t = v ? poshalf : neghalf;
1724 *datap += *datap < 0 ? -t : t;
1725 *flagsp |= T1_REFINE;
1727 } /* VSC and BYPASS by Antonin */
1733 static void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1737 OPJ_INT32 oneplushalf,
1743 opj_raw_t *raw = t1->raw; /* RAW component */
1744 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1746 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1747 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1748 if (type == T1_TYPE_RAW) {
1749 if (opj_raw_decode(raw)) {
1750 v = opj_raw_decode(raw); /* ESSAI */
1751 *datap = v ? -oneplushalf : oneplushalf;
1752 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1755 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1756 if (opj_mqc_decode(mqc)) {
1757 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1758 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1759 *datap = v ? -oneplushalf : oneplushalf;
1760 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1763 *flagsp |= T1_VISIT;
1765 } /* VSC and BYPASS by Antonin */