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);
270 static void opj_t1_encode_cblk( opj_t1_t *t1,
271 opj_tcd_cblk_enc_t* cblk,
276 OPJ_FLOAT64 stepsize,
279 opj_tcd_tile_t * tile,
280 const OPJ_FLOAT64 * mct_norms);
285 @param cblk Code-block coding parameters
287 @param roishift Region of interest shifting value
288 @param cblksty Code-block style
290 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
291 opj_tcd_cblk_dec_t* cblk,
296 OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
304 /* ----------------------------------------------------------------------- */
306 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
307 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
310 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
311 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
314 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
315 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
316 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
320 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
321 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
324 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
325 if (bitpos > T1_NMSEDEC_FRACBITS) {
326 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
329 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
332 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
333 if (bitpos > T1_NMSEDEC_FRACBITS) {
334 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
337 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
340 void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
341 opj_flag_t *np = flagsp - stride;
342 opj_flag_t *sp = flagsp + stride;
344 static const opj_flag_t mod[] = {
345 T1_SIG_S, T1_SIG_S|T1_SGN_S,
346 T1_SIG_E, T1_SIG_E|T1_SGN_E,
347 T1_SIG_W, T1_SIG_W|T1_SGN_W,
348 T1_SIG_N, T1_SIG_N|T1_SGN_N
355 flagsp[-1] |= mod[s+2];
357 flagsp[1] |= mod[s+4];
364 void opj_t1_enc_sigpass_step( opj_t1_t *t1,
378 opj_mqc_t *mqc = t1->mqc; /* MQC component */
380 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
381 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
382 v = opj_int_abs(*datap) & one ? 1 : 0;
383 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
384 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
385 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
387 opj_mqc_encode(mqc, (OPJ_UINT32)v);
390 v = *datap < 0 ? 1 : 0;
391 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
392 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
393 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
394 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
396 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
398 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
405 static INLINE void opj_t1_dec_sigpass_step_raw(
410 OPJ_INT32 oneplushalf,
414 opj_raw_t *raw = t1->raw; /* RAW component */
415 OPJ_ARG_NOT_USED(orient);
417 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
418 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
419 if (opj_raw_decode(raw)) {
420 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
421 *datap = v ? -oneplushalf : oneplushalf;
422 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
428 INLINE void opj_t1_dec_sigpass_step_mqc(
433 OPJ_INT32 oneplushalf)
437 opj_mqc_t *mqc = t1->mqc; /* MQC component */
440 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
441 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
442 if (opj_mqc_decode(mqc)) {
443 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
444 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
445 *datap = v ? -oneplushalf : oneplushalf;
446 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
450 } /* VSC and BYPASS by Antonin */
452 INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
457 OPJ_INT32 oneplushalf,
462 opj_mqc_t *mqc = t1->mqc; /* MQC component */
464 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
465 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
466 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
467 if (opj_mqc_decode(mqc)) {
468 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
469 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
470 *datap = v ? -oneplushalf : oneplushalf;
471 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
475 } /* VSC and BYPASS by Antonin */
479 void opj_t1_enc_sigpass(opj_t1_t *t1,
487 OPJ_UINT32 i, j, k, vsc;
491 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
492 for (k = 0; k < t1->h; k += 4) {
493 for (i = 0; i < t1->w; ++i) {
494 for (j = k; j < k + 4 && j < t1->h; ++j) {
495 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
496 opj_t1_enc_sigpass_step(
498 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
499 &t1->data[(j * t1->w) + i],
511 void opj_t1_dec_sigpass_raw(
517 OPJ_INT32 one, half, oneplushalf, vsc;
521 oneplushalf = one | half;
522 for (k = 0; k < t1->h; k += 4) {
523 for (i = 0; i < t1->w; ++i) {
524 for (j = k; j < k + 4 && j < t1->h; ++j) {
525 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
526 opj_t1_dec_sigpass_step_raw(
528 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
529 &t1->data[(j * t1->w) + i],
536 } /* VSC and BYPASS by Antonin */
538 void opj_t1_dec_sigpass_mqc(
543 OPJ_INT32 one, half, oneplushalf;
545 OPJ_INT32 *data1 = t1->data;
546 opj_flag_t *flags1 = &t1->flags[1];
549 oneplushalf = one | half;
550 for (k = 0; k < (t1->h & ~3u); k += 4) {
551 for (i = 0; i < t1->w; ++i) {
552 OPJ_INT32 *data2 = data1 + i;
553 opj_flag_t *flags2 = flags1 + i;
554 flags2 += t1->flags_stride;
555 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
557 flags2 += t1->flags_stride;
558 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
560 flags2 += t1->flags_stride;
561 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
563 flags2 += t1->flags_stride;
564 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
568 flags1 += t1->flags_stride << 2;
570 for (i = 0; i < t1->w; ++i) {
571 OPJ_INT32 *data2 = data1 + i;
572 opj_flag_t *flags2 = flags1 + i;
573 for (j = k; j < t1->h; ++j) {
574 flags2 += t1->flags_stride;
575 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
579 } /* VSC and BYPASS by Antonin */
581 void opj_t1_dec_sigpass_mqc_vsc(
586 OPJ_INT32 one, half, oneplushalf, vsc;
590 oneplushalf = one | half;
591 for (k = 0; k < t1->h; k += 4) {
592 for (i = 0; i < t1->w; ++i) {
593 for (j = k; j < k + 4 && j < t1->h; ++j) {
594 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
595 opj_t1_dec_sigpass_step_mqc_vsc(
597 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
598 &t1->data[(j * t1->w) + i],
605 } /* VSC and BYPASS by Antonin */
609 void opj_t1_enc_refpass_step( opj_t1_t *t1,
621 opj_mqc_t *mqc = t1->mqc; /* MQC component */
623 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
624 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
625 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
626 v = opj_int_abs(*datap) & one ? 1 : 0;
627 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
628 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
629 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
631 opj_mqc_encode(mqc, (OPJ_UINT32)v);
633 *flagsp |= T1_REFINE;
637 INLINE void opj_t1_dec_refpass_step_raw(
645 OPJ_INT32 v, t, flag;
647 opj_raw_t *raw = t1->raw; /* RAW component */
649 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
650 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
651 v = (OPJ_INT32)opj_raw_decode(raw);
652 t = v ? poshalf : neghalf;
653 *datap += *datap < 0 ? -t : t;
654 *flagsp |= T1_REFINE;
656 } /* VSC and BYPASS by Antonin */
658 INLINE void opj_t1_dec_refpass_step_mqc(
665 OPJ_INT32 v, t, flag;
667 opj_mqc_t *mqc = t1->mqc; /* MQC component */
670 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
671 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
672 v = opj_mqc_decode(mqc);
673 t = v ? poshalf : neghalf;
674 *datap += *datap < 0 ? -t : t;
675 *flagsp |= T1_REFINE;
677 } /* VSC and BYPASS by Antonin */
679 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
687 OPJ_INT32 v, t, flag;
689 opj_mqc_t *mqc = t1->mqc; /* MQC component */
691 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
692 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
693 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
694 v = opj_mqc_decode(mqc);
695 t = v ? poshalf : neghalf;
696 *datap += *datap < 0 ? -t : t;
697 *flagsp |= T1_REFINE;
699 } /* VSC and BYPASS by Antonin */
702 void opj_t1_enc_refpass(
709 OPJ_UINT32 i, j, k, vsc;
713 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
714 for (k = 0; k < t1->h; k += 4) {
715 for (i = 0; i < t1->w; ++i) {
716 for (j = k; j < k + 4 && j < t1->h; ++j) {
717 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
718 opj_t1_enc_refpass_step(
720 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
721 &t1->data[(j * t1->w) + i],
732 void opj_t1_dec_refpass_raw(
737 OPJ_INT32 one, poshalf, neghalf;
742 neghalf = bpno > 0 ? -poshalf : -1;
743 for (k = 0; k < t1->h; k += 4) {
744 for (i = 0; i < t1->w; ++i) {
745 for (j = k; j < k + 4 && j < t1->h; ++j) {
746 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
747 opj_t1_dec_refpass_step_raw(
749 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
750 &t1->data[(j * t1->w) + i],
757 } /* VSC and BYPASS by Antonin */
759 void opj_t1_dec_refpass_mqc(
763 OPJ_INT32 one, poshalf, neghalf;
765 OPJ_INT32 *data1 = t1->data;
766 opj_flag_t *flags1 = &t1->flags[1];
769 neghalf = bpno > 0 ? -poshalf : -1;
770 for (k = 0; k < (t1->h & ~3u); k += 4) {
771 for (i = 0; i < t1->w; ++i) {
772 OPJ_INT32 *data2 = data1 + i;
773 opj_flag_t *flags2 = flags1 + i;
774 flags2 += t1->flags_stride;
775 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
777 flags2 += t1->flags_stride;
778 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
780 flags2 += t1->flags_stride;
781 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
783 flags2 += t1->flags_stride;
784 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
788 flags1 += t1->flags_stride << 2;
790 for (i = 0; i < t1->w; ++i) {
791 OPJ_INT32 *data2 = data1 + i;
792 opj_flag_t *flags2 = flags1 + i;
793 for (j = k; j < t1->h; ++j) {
794 flags2 += t1->flags_stride;
795 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
799 } /* VSC and BYPASS by Antonin */
801 void opj_t1_dec_refpass_mqc_vsc(
805 OPJ_INT32 one, poshalf, neghalf;
810 neghalf = bpno > 0 ? -poshalf : -1;
811 for (k = 0; k < t1->h; k += 4) {
812 for (i = 0; i < t1->w; ++i) {
813 for (j = k; j < k + 4 && j < t1->h; ++j) {
814 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
815 opj_t1_dec_refpass_step_mqc_vsc(
817 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
818 &t1->data[(j * t1->w) + i],
825 } /* VSC and BYPASS by Antonin */
828 void opj_t1_enc_clnpass_step(
842 opj_mqc_t *mqc = t1->mqc; /* MQC component */
844 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
848 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
849 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
850 v = opj_int_abs(*datap) & one ? 1 : 0;
851 opj_mqc_encode(mqc, (OPJ_UINT32)v);
854 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
855 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
856 v = *datap < 0 ? 1 : 0;
857 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
858 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
861 *flagsp &= ~T1_VISIT;
864 static void opj_t1_dec_clnpass_step_partial(
869 OPJ_INT32 oneplushalf)
872 opj_mqc_t *mqc = t1->mqc; /* MQC component */
874 OPJ_ARG_NOT_USED(orient);
877 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
878 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
879 *datap = v ? -oneplushalf : oneplushalf;
880 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
881 *flagsp &= ~T1_VISIT;
882 } /* VSC and BYPASS by Antonin */
884 static void opj_t1_dec_clnpass_step(
889 OPJ_INT32 oneplushalf)
893 opj_mqc_t *mqc = t1->mqc; /* MQC component */
896 if (!(flag & (T1_SIG | T1_VISIT))) {
897 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
898 if (opj_mqc_decode(mqc)) {
899 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
900 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
901 *datap = v ? -oneplushalf : oneplushalf;
902 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
905 *flagsp &= ~T1_VISIT;
906 } /* VSC and BYPASS by Antonin */
908 static void opj_t1_dec_clnpass_step_vsc(
913 OPJ_INT32 oneplushalf,
919 opj_mqc_t *mqc = t1->mqc; /* MQC component */
921 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
925 if (!(flag & (T1_SIG | T1_VISIT))) {
926 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
927 if (opj_mqc_decode(mqc)) {
929 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
930 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
931 *datap = v ? -oneplushalf : oneplushalf;
932 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
935 *flagsp &= ~T1_VISIT;
938 void opj_t1_enc_clnpass(
947 OPJ_UINT32 agg, runlen, vsc;
949 opj_mqc_t *mqc = t1->mqc; /* MQC component */
952 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
953 for (k = 0; k < t1->h; k += 4) {
954 for (i = 0; i < t1->w; ++i) {
956 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
957 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
958 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
959 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960 || (MACRO_t1_flags(1 + k + 3,1 + i)
961 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
963 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
964 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
965 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
966 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
972 for (runlen = 0; runlen < 4; ++runlen) {
973 if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
976 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
977 opj_mqc_encode(mqc, runlen != 4);
981 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
982 opj_mqc_encode(mqc, runlen >> 1);
983 opj_mqc_encode(mqc, runlen & 1);
987 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
988 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
989 opj_t1_enc_clnpass_step(
991 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
992 &t1->data[(j * t1->w) + i],
997 agg && (j == k + runlen),
1004 static void opj_t1_dec_clnpass(
1010 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
1012 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1014 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1018 oneplushalf = one | half;
1019 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1020 for (k = 0; k < t1->h; k += 4) {
1021 for (i = 0; i < t1->w; ++i) {
1022 if (k + 3 < t1->h) {
1023 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1024 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1025 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1026 || (MACRO_t1_flags(1 + k + 3,1 + i)
1027 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1032 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1033 if (!opj_mqc_decode(mqc)) {
1036 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1037 runlen = opj_mqc_decode(mqc);
1038 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1042 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1043 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1044 opj_t1_dec_clnpass_step_vsc(
1046 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1047 &t1->data[(j * t1->w) + i],
1050 agg && (j == k + (OPJ_UINT32)runlen),
1056 OPJ_INT32 *data1 = t1->data;
1057 opj_flag_t *flags1 = &t1->flags[1];
1058 for (k = 0; k < (t1->h & ~3u); k += 4) {
1059 for (i = 0; i < t1->w; ++i) {
1060 OPJ_INT32 *data2 = data1 + i;
1061 opj_flag_t *flags2 = flags1 + i;
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) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1067 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1068 if (!opj_mqc_decode(mqc)) {
1071 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1072 runlen = opj_mqc_decode(mqc);
1073 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1074 flags2 += (OPJ_UINT32)runlen * t1->flags_stride;
1075 data2 += (OPJ_UINT32)runlen * t1->w;
1076 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1077 flags2 += t1->flags_stride;
1078 if (agg && (j == k + (OPJ_UINT32)runlen)) {
1079 opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1081 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1086 flags2 += t1->flags_stride;
1087 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1089 flags2 += t1->flags_stride;
1090 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1092 flags2 += t1->flags_stride;
1093 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1095 flags2 += t1->flags_stride;
1096 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1100 data1 += t1->w << 2;
1101 flags1 += t1->flags_stride << 2;
1103 for (i = 0; i < t1->w; ++i) {
1104 OPJ_INT32 *data2 = data1 + i;
1105 opj_flag_t *flags2 = flags1 + i;
1106 for (j = k; j < t1->h; ++j) {
1107 flags2 += t1->flags_stride;
1108 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1116 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1117 v = opj_mqc_decode(mqc);
1118 v = (v << 1) | opj_mqc_decode(mqc);
1119 v = (v << 1) | opj_mqc_decode(mqc);
1120 v = (v << 1) | opj_mqc_decode(mqc);
1123 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1127 } /* VSC and BYPASS by Antonin */
1130 /** mod fixed_quality */
1131 static OPJ_FLOAT64 opj_t1_getwmsedec(
1138 OPJ_FLOAT64 stepsize,
1139 OPJ_UINT32 numcomps,
1140 const OPJ_FLOAT64 * mct_norms)
1142 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1143 OPJ_ARG_NOT_USED(numcomps);
1146 w1 = mct_norms[compno];
1150 w2 = opj_dwt_getnorm(level, orient);
1151 } else { /* if (qmfbid == 0) */
1152 w2 = opj_dwt_getnorm_real(level, orient);
1155 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1156 wmsedec *= wmsedec * nmsedec / 8192.0;
1161 OPJ_BOOL opj_t1_allocate_buffers(
1166 OPJ_UINT32 datasize=w * h;
1167 OPJ_UINT32 flagssize;
1169 if(datasize > t1->datasize){
1170 opj_aligned_free(t1->data);
1171 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1173 /* FIXME event manager error callback */
1176 t1->datasize=datasize;
1178 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1180 t1->flags_stride=w+2;
1181 flagssize=t1->flags_stride * (h+2);
1183 if(flagssize > t1->flagssize){
1184 opj_aligned_free(t1->flags);
1185 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1187 /* FIXME event manager error callback */
1190 t1->flagssize=flagssize;
1192 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1200 /* ----------------------------------------------------------------------- */
1202 /* ----------------------------------------------------------------------- */
1204 * Creates a new Tier 1 handle
1205 * and initializes the look-up tables of the Tier-1 coder/decoder
1206 * @return a new T1 handle if successful, returns NULL otherwise
1208 opj_t1_t* opj_t1_create()
1210 opj_t1_t *l_t1 = 00;
1212 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1217 /* create MQC and RAW handles */
1218 l_t1->mqc = opj_mqc_create();
1220 opj_t1_destroy(l_t1);
1224 l_t1->raw = opj_raw_create();
1226 opj_t1_destroy(l_t1);
1235 * Destroys a previously created T1 handle
1237 * @param p_t1 Tier 1 handle to destroy
1239 void opj_t1_destroy(opj_t1_t *p_t1)
1245 /* destroy MQC and RAW handles */
1246 opj_mqc_destroy(p_t1->mqc);
1248 opj_raw_destroy(p_t1->raw);
1252 opj_aligned_free(p_t1->data);
1257 opj_aligned_free(p_t1->flags);
1264 OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
1265 opj_tcd_tilecomp_t* tilec,
1269 OPJ_UINT32 resno, bandno, precno, cblkno;
1270 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1272 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1273 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1275 for (bandno = 0; bandno < res->numbands; ++bandno) {
1276 opj_tcd_band_t* restrict band = &res->bands[bandno];
1278 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1279 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1281 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1282 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1283 OPJ_INT32* restrict datap;
1284 /*void* restrict tiledp;*/
1285 OPJ_UINT32 cblk_w, cblk_h;
1289 if (OPJ_FALSE == opj_t1_decode_cblk(
1293 (OPJ_UINT32)tccp->roishift,
1298 x = cblk->x0 - band->x0;
1299 y = cblk->y0 - band->y0;
1300 if (band->bandno & 1) {
1301 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1302 x += pres->x1 - pres->x0;
1304 if (band->bandno & 2) {
1305 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1306 y += pres->y1 - pres->y0;
1313 if (tccp->roishift) {
1314 OPJ_INT32 thresh = 1 << tccp->roishift;
1315 for (j = 0; j < cblk_h; ++j) {
1316 for (i = 0; i < cblk_w; ++i) {
1317 OPJ_INT32 val = datap[(j * cblk_w) + i];
1318 OPJ_INT32 mag = abs(val);
1319 if (mag >= thresh) {
1320 mag >>= tccp->roishift;
1321 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1327 /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1328 if (tccp->qmfbid == 1) {
1329 OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1330 for (j = 0; j < cblk_h; ++j) {
1331 for (i = 0; i < cblk_w; ++i) {
1332 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1333 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1336 } else { /* if (tccp->qmfbid == 0) */
1337 OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1338 for (j = 0; j < cblk_h; ++j) {
1339 OPJ_FLOAT32* restrict tiledp2 = tiledp;
1340 for (i = 0; i < cblk_w; ++i) {
1341 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1345 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1346 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1352 /*opj_free(cblk->data);
1353 opj_free(cblk->segs);*/
1354 /*cblk->segs = 00;*/
1356 /*opj_free(precinct->cblks.dec);*/
1364 OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1365 opj_tcd_cblk_dec_t* cblk,
1367 OPJ_UINT32 roishift,
1370 opj_raw_t *raw = t1->raw; /* RAW component */
1371 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1374 OPJ_UINT32 passtype;
1375 OPJ_UINT32 segno, passno;
1376 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1378 if(!opj_t1_allocate_buffers(
1380 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1381 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1386 bpno = (OPJ_INT32)(roishift + cblk->numbps - 1);
1389 opj_mqc_resetstates(mqc);
1390 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1391 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1392 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1394 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1395 opj_tcd_seg_t *seg = &cblk->segs[segno];
1398 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1399 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1400 if(seg->data == 00){
1403 if (type == T1_TYPE_RAW) {
1404 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1406 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1411 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1414 if (type == T1_TYPE_RAW) {
1415 opj_t1_dec_sigpass_raw(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1417 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1418 opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, (OPJ_INT32)orient);
1420 opj_t1_dec_sigpass_mqc(t1, bpno+1, (OPJ_INT32)orient);
1425 if (type == T1_TYPE_RAW) {
1426 opj_t1_dec_refpass_raw(t1, bpno+1, (OPJ_INT32)cblksty);
1428 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1429 opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1431 opj_t1_dec_refpass_mqc(t1, bpno+1);
1436 opj_t1_dec_clnpass(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1440 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1441 opj_mqc_resetstates(mqc);
1442 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1443 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1444 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1446 if (++passtype == 3) {
1458 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1459 opj_tcd_tile_t *tile,
1461 const OPJ_FLOAT64 * mct_norms
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 datap;
1486 OPJ_INT32* restrict tiledp;
1491 OPJ_INT32 x = cblk->x0 - band->x0;
1492 OPJ_INT32 y = cblk->y0 - band->y0;
1493 if (band->bandno & 1) {
1494 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1495 x += pres->x1 - pres->x0;
1497 if (band->bandno & 2) {
1498 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1499 y += pres->y1 - pres->y0;
1502 if(!opj_t1_allocate_buffers(
1504 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1505 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1514 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1515 if (tccp->qmfbid == 1) {
1516 for (j = 0; j < cblk_h; ++j) {
1517 for (i = 0; i < cblk_w; ++i) {
1518 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1519 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1522 } else { /* if (tccp->qmfbid == 0) */
1523 for (j = 0; j < cblk_h; ++j) {
1524 for (i = 0; i < cblk_w; ++i) {
1525 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1526 datap[(j * cblk_w) + i] =
1529 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1539 tilec->numresolutions - 1 - resno,
1555 /** mod fixed_quality */
1556 void opj_t1_encode_cblk(opj_t1_t *t1,
1557 opj_tcd_cblk_enc_t* cblk,
1562 OPJ_FLOAT64 stepsize,
1564 OPJ_UINT32 numcomps,
1565 opj_tcd_tile_t * tile,
1566 const OPJ_FLOAT64 * mct_norms)
1568 OPJ_FLOAT64 cumwmsedec = 0.0;
1570 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1574 OPJ_UINT32 passtype;
1575 OPJ_INT32 nmsedec = 0;
1578 OPJ_BYTE type = T1_TYPE_MQ;
1579 OPJ_FLOAT64 tempwmsedec;
1582 for (i = 0; i < t1->w * t1->h; ++i) {
1583 OPJ_INT32 tmp = abs(t1->data[i]);
1584 max = opj_int_max(max, tmp);
1587 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1589 bpno = (OPJ_INT32)(cblk->numbps - 1);
1592 opj_mqc_resetstates(mqc);
1593 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1594 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1595 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1596 opj_mqc_init_enc(mqc, cblk->data);
1598 for (passno = 0; bpno >= 0; ++passno) {
1599 opj_tcd_pass_t *pass = &cblk->passes[passno];
1600 OPJ_UINT32 correction = 3;
1601 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1605 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1608 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1611 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1612 /* code switch SEGMARK (i.e. SEGSYM) */
1613 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1614 opj_mqc_segmark_enc(mqc);
1619 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1620 cumwmsedec += tempwmsedec;
1621 tile->distotile += tempwmsedec;
1623 /* Code switch "RESTART" (i.e. TERMALL) */
1624 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1625 if (type == T1_TYPE_RAW) {
1628 /* correction = mqc_bypass_flush_enc(); */
1629 } else { /* correction = mqc_restart_enc(); */
1635 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1636 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1637 if (type == T1_TYPE_RAW) {
1640 /* correction = mqc_bypass_flush_enc(); */
1641 } else { /* correction = mqc_restart_enc(); */
1651 if (++passtype == 3) {
1656 if (pass->term && bpno > 0) {
1657 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1658 if (type == T1_TYPE_RAW)
1659 opj_mqc_bypass_init_enc(mqc);
1661 opj_mqc_restart_init_enc(mqc);
1664 pass->distortiondec = cumwmsedec;
1665 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1667 /* Code-switch "RESET" */
1668 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1669 opj_mqc_reset_enc(mqc);
1672 /* Code switch "ERTERM" (i.e. PTERM) */
1673 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1674 opj_mqc_erterm_enc(mqc);
1675 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1678 cblk->totalpasses = passno;
1680 for (passno = 0; passno<cblk->totalpasses; passno++) {
1681 opj_tcd_pass_t *pass = &cblk->passes[passno];
1682 if (pass->rate > opj_mqc_numbytes(mqc))
1683 pass->rate = opj_mqc_numbytes(mqc);
1684 /*Preventing generation of FF as last data byte of a pass*/
1685 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1688 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1693 void opj_t1_dec_refpass_step( opj_t1_t *t1,
1704 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1705 opj_raw_t *raw = t1->raw; /* RAW component */
1707 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1708 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1709 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1710 if (type == T1_TYPE_RAW) {
1711 v = opj_raw_decode(raw);
1713 v = opj_mqc_decode(mqc);
1715 t = v ? poshalf : neghalf;
1716 *datap += *datap < 0 ? -t : t;
1717 *flagsp |= T1_REFINE;
1719 } /* VSC and BYPASS by Antonin */
1725 void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1729 OPJ_INT32 oneplushalf,
1735 opj_raw_t *raw = t1->raw; /* RAW component */
1736 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1738 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1739 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1740 if (type == T1_TYPE_RAW) {
1741 if (opj_raw_decode(raw)) {
1742 v = opj_raw_decode(raw); /* ESSAI */
1743 *datap = v ? -oneplushalf : oneplushalf;
1744 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1747 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1748 if (opj_mqc_decode(mqc)) {
1749 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1750 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1751 *datap = v ? -oneplushalf : oneplushalf;
1752 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1755 *flagsp |= T1_VISIT;
1757 } /* VSC and BYPASS by Antonin */