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(opj_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,
73 static INLINE void opj_t1_dec_sigpass_step_raw(
80 static INLINE void opj_t1_dec_sigpass_step_mqc(
86 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
96 Encode significant pass
98 static void opj_t1_enc_sigpass( opj_t1_t *t1,
106 Decode significant pass
108 static void opj_t1_dec_sigpass_raw(
113 static void opj_t1_dec_sigpass_mqc(
117 static void opj_t1_dec_sigpass_mqc_vsc(
125 Encode refinement pass
127 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
138 Encode refinement pass
140 static void opj_t1_enc_refpass( opj_t1_t *t1,
147 Decode refinement pass
149 static void opj_t1_dec_refpass_raw(
153 static void opj_t1_dec_refpass_mqc(
156 static void opj_t1_dec_refpass_mqc_vsc(
162 Decode refinement pass
164 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
172 static INLINE void opj_t1_dec_refpass_step_raw(
179 static INLINE void opj_t1_dec_refpass_step_mqc(
185 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
198 static void opj_t1_enc_clnpass_step(
211 static void opj_t1_dec_clnpass_step_partial(
217 static void opj_t1_dec_clnpass_step(
223 static void opj_t1_dec_clnpass_step_vsc(
234 static void opj_t1_enc_clnpass(
243 static void opj_t1_dec_clnpass(
249 static OPJ_FLOAT64 opj_t1_getwmsedec(
256 OPJ_FLOAT64 stepsize,
258 const OPJ_FLOAT64 * mct_norms);
260 static void opj_t1_encode_cblk( opj_t1_t *t1,
261 opj_tcd_cblk_enc_t* cblk,
266 OPJ_FLOAT64 stepsize,
269 opj_tcd_tile_t * tile,
270 const OPJ_FLOAT64 * mct_norms);
275 @param cblk Code-block coding parameters
277 @param roishift Region of interest shifting value
278 @param cblksty Code-block style
280 static opj_bool opj_t1_decode_cblk( opj_t1_t *t1,
281 opj_tcd_cblk_dec_t* cblk,
286 opj_bool opj_t1_allocate_buffers( opj_t1_t *t1,
294 /* ----------------------------------------------------------------------- */
296 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
297 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
300 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
301 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
304 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
305 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
306 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
310 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
311 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
314 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
315 if (bitpos > T1_NMSEDEC_FRACBITS) {
316 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
319 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
322 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
323 if (bitpos > T1_NMSEDEC_FRACBITS) {
324 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
327 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
330 void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
331 opj_flag_t *np = flagsp - stride;
332 opj_flag_t *sp = flagsp + stride;
334 static const opj_flag_t mod[] = {
335 T1_SIG_S, T1_SIG_S|T1_SGN_S,
336 T1_SIG_E, T1_SIG_E|T1_SGN_E,
337 T1_SIG_W, T1_SIG_W|T1_SGN_W,
338 T1_SIG_N, T1_SIG_N|T1_SGN_N
345 flagsp[-1] |= mod[s+2];
347 flagsp[1] |= mod[s+4];
354 void opj_t1_enc_sigpass_step( opj_t1_t *t1,
368 opj_mqc_t *mqc = t1->mqc; /* MQC component */
370 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
371 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
372 v = opj_int_abs(*datap) & one ? 1 : 0;
373 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
374 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
375 opj_mqc_bypass_enc(mqc, v);
377 opj_mqc_encode(mqc, v);
380 v = *datap < 0 ? 1 : 0;
381 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
382 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
383 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
384 opj_mqc_bypass_enc(mqc, v);
386 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
388 opj_t1_updateflags(flagsp, v, t1->flags_stride);
395 static INLINE void opj_t1_dec_sigpass_step_raw(
404 opj_raw_t *raw = t1->raw; /* RAW component */
405 OPJ_ARG_NOT_USED(orient);
407 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
408 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
409 if (opj_raw_decode(raw)) {
410 v = opj_raw_decode(raw); /* ESSAI */
411 *datap = v ? -oneplushalf : oneplushalf;
412 opj_t1_updateflags(flagsp, v, t1->flags_stride);
418 INLINE void opj_t1_dec_sigpass_step_mqc(
427 opj_mqc_t *mqc = t1->mqc; /* MQC component */
430 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
431 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
432 if (opj_mqc_decode(mqc)) {
433 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
434 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
435 *datap = v ? -oneplushalf : oneplushalf;
436 opj_t1_updateflags(flagsp, v, t1->flags_stride);
440 } /* VSC and BYPASS by Antonin */
442 INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
452 opj_mqc_t *mqc = t1->mqc; /* MQC component */
454 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
455 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
456 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
457 if (opj_mqc_decode(mqc)) {
458 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
459 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
460 *datap = v ? -oneplushalf : oneplushalf;
461 opj_t1_updateflags(flagsp, v, t1->flags_stride);
465 } /* VSC and BYPASS by Antonin */
469 void opj_t1_enc_sigpass(opj_t1_t *t1,
477 OPJ_UINT32 i, j, k, vsc;
481 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
482 for (k = 0; k < t1->h; k += 4) {
483 for (i = 0; i < t1->w; ++i) {
484 for (j = k; j < k + 4 && j < t1->h; ++j) {
485 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
486 opj_t1_enc_sigpass_step(
488 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
489 &t1->data[(j * t1->w) + i],
501 void opj_t1_dec_sigpass_raw(
507 int one, half, oneplushalf, vsc;
511 oneplushalf = one | half;
512 for (k = 0; k < t1->h; k += 4) {
513 for (i = 0; i < t1->w; ++i) {
514 for (j = k; j < k + 4 && j < t1->h; ++j) {
515 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
516 opj_t1_dec_sigpass_step_raw(
518 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
519 &t1->data[(j * t1->w) + i],
526 } /* VSC and BYPASS by Antonin */
528 void opj_t1_dec_sigpass_mqc(
533 int one, half, oneplushalf;
535 int *data1 = t1->data;
536 opj_flag_t *flags1 = &t1->flags[1];
539 oneplushalf = one | half;
540 for (k = 0; k < (t1->h & ~3); k += 4) {
541 for (i = 0; i < t1->w; ++i) {
542 int *data2 = data1 + i;
543 opj_flag_t *flags2 = flags1 + i;
544 flags2 += t1->flags_stride;
545 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
547 flags2 += t1->flags_stride;
548 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
550 flags2 += t1->flags_stride;
551 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
553 flags2 += t1->flags_stride;
554 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
558 flags1 += t1->flags_stride << 2;
560 for (i = 0; i < t1->w; ++i) {
561 int *data2 = data1 + i;
562 opj_flag_t *flags2 = flags1 + i;
563 for (j = k; j < t1->h; ++j) {
564 flags2 += t1->flags_stride;
565 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
569 } /* VSC and BYPASS by Antonin */
571 void opj_t1_dec_sigpass_mqc_vsc(
576 int one, half, oneplushalf, vsc;
580 oneplushalf = one | half;
581 for (k = 0; k < t1->h; k += 4) {
582 for (i = 0; i < t1->w; ++i) {
583 for (j = k; j < k + 4 && j < t1->h; ++j) {
584 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
585 opj_t1_dec_sigpass_step_mqc_vsc(
587 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
588 &t1->data[(j * t1->w) + i],
595 } /* VSC and BYPASS by Antonin */
599 void opj_t1_enc_refpass_step( opj_t1_t *t1,
611 opj_mqc_t *mqc = t1->mqc; /* MQC component */
613 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
614 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
615 *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
616 v = opj_int_abs(*datap) & one ? 1 : 0;
617 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
618 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
619 opj_mqc_bypass_enc(mqc, v);
621 opj_mqc_encode(mqc, v);
623 *flagsp |= T1_REFINE;
627 INLINE void opj_t1_dec_refpass_step_raw(
637 opj_raw_t *raw = t1->raw; /* RAW component */
639 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
640 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
641 v = opj_raw_decode(raw);
642 t = v ? poshalf : neghalf;
643 *datap += *datap < 0 ? -t : t;
644 *flagsp |= T1_REFINE;
646 } /* VSC and BYPASS by Antonin */
648 INLINE void opj_t1_dec_refpass_step_mqc(
657 opj_mqc_t *mqc = t1->mqc; /* MQC component */
660 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
661 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
662 v = opj_mqc_decode(mqc);
663 t = v ? poshalf : neghalf;
664 *datap += *datap < 0 ? -t : t;
665 *flagsp |= T1_REFINE;
667 } /* VSC and BYPASS by Antonin */
669 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
679 opj_mqc_t *mqc = t1->mqc; /* MQC component */
681 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
682 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
683 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
684 v = opj_mqc_decode(mqc);
685 t = v ? poshalf : neghalf;
686 *datap += *datap < 0 ? -t : t;
687 *flagsp |= T1_REFINE;
689 } /* VSC and BYPASS by Antonin */
692 void opj_t1_enc_refpass(
699 OPJ_UINT32 i, j, k, vsc;
703 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
704 for (k = 0; k < t1->h; k += 4) {
705 for (i = 0; i < t1->w; ++i) {
706 for (j = k; j < k + 4 && j < t1->h; ++j) {
707 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
708 opj_t1_enc_refpass_step(
710 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
711 &t1->data[(j * t1->w) + i],
722 void opj_t1_dec_refpass_raw(
727 int one, poshalf, neghalf;
732 neghalf = bpno > 0 ? -poshalf : -1;
733 for (k = 0; k < t1->h; k += 4) {
734 for (i = 0; i < t1->w; ++i) {
735 for (j = k; j < k + 4 && j < t1->h; ++j) {
736 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
737 opj_t1_dec_refpass_step_raw(
739 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
740 &t1->data[(j * t1->w) + i],
747 } /* VSC and BYPASS by Antonin */
749 void opj_t1_dec_refpass_mqc(
753 int one, poshalf, neghalf;
755 int *data1 = t1->data;
756 opj_flag_t *flags1 = &t1->flags[1];
759 neghalf = bpno > 0 ? -poshalf : -1;
760 for (k = 0; k < (t1->h & ~3); k += 4) {
761 for (i = 0; i < t1->w; ++i) {
762 int *data2 = data1 + i;
763 opj_flag_t *flags2 = flags1 + i;
764 flags2 += t1->flags_stride;
765 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
767 flags2 += t1->flags_stride;
768 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
770 flags2 += t1->flags_stride;
771 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
773 flags2 += t1->flags_stride;
774 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
778 flags1 += t1->flags_stride << 2;
780 for (i = 0; i < t1->w; ++i) {
781 int *data2 = data1 + i;
782 opj_flag_t *flags2 = flags1 + i;
783 for (j = k; j < t1->h; ++j) {
784 flags2 += t1->flags_stride;
785 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
789 } /* VSC and BYPASS by Antonin */
791 void opj_t1_dec_refpass_mqc_vsc(
795 int one, poshalf, neghalf;
800 neghalf = bpno > 0 ? -poshalf : -1;
801 for (k = 0; k < t1->h; k += 4) {
802 for (i = 0; i < t1->w; ++i) {
803 for (j = k; j < k + 4 && j < t1->h; ++j) {
804 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
805 opj_t1_dec_refpass_step_mqc_vsc(
807 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
808 &t1->data[(j * t1->w) + i],
815 } /* VSC and BYPASS by Antonin */
818 void opj_t1_enc_clnpass_step(
832 opj_mqc_t *mqc = t1->mqc; /* MQC component */
834 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
838 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
839 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
840 v = opj_int_abs(*datap) & one ? 1 : 0;
841 opj_mqc_encode(mqc, v);
844 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno + T1_NMSEDEC_FRACBITS);
845 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
846 v = *datap < 0 ? 1 : 0;
847 opj_mqc_encode(mqc, v ^ opj_t1_getspb(flag));
848 opj_t1_updateflags(flagsp, v, t1->flags_stride);
851 *flagsp &= ~T1_VISIT;
854 static void opj_t1_dec_clnpass_step_partial(
862 opj_mqc_t *mqc = t1->mqc; /* MQC component */
864 OPJ_ARG_NOT_USED(orient);
867 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
868 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
869 *datap = v ? -oneplushalf : oneplushalf;
870 opj_t1_updateflags(flagsp, v, t1->flags_stride);
871 *flagsp &= ~T1_VISIT;
872 } /* VSC and BYPASS by Antonin */
874 static void opj_t1_dec_clnpass_step(
883 opj_mqc_t *mqc = t1->mqc; /* MQC component */
886 if (!(flag & (T1_SIG | T1_VISIT))) {
887 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
888 if (opj_mqc_decode(mqc)) {
889 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
890 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
891 *datap = v ? -oneplushalf : oneplushalf;
892 opj_t1_updateflags(flagsp, v, t1->flags_stride);
895 *flagsp &= ~T1_VISIT;
896 } /* VSC and BYPASS by Antonin */
898 static void opj_t1_dec_clnpass_step_vsc(
909 opj_mqc_t *mqc = t1->mqc; /* MQC component */
911 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
915 if (!(flag & (T1_SIG | T1_VISIT))) {
916 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
917 if (opj_mqc_decode(mqc)) {
919 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
920 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
921 *datap = v ? -oneplushalf : oneplushalf;
922 opj_t1_updateflags(flagsp, v, t1->flags_stride);
925 *flagsp &= ~T1_VISIT;
928 void opj_t1_enc_clnpass(
937 OPJ_UINT32 agg, runlen, vsc;
939 opj_mqc_t *mqc = t1->mqc; /* MQC component */
942 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
943 for (k = 0; k < t1->h; k += 4) {
944 for (i = 0; i < t1->w; ++i) {
946 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
947 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
948 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
949 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
950 || (MACRO_t1_flags(1 + k + 3,1 + i)
951 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
953 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
954 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
955 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
956 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
962 for (runlen = 0; runlen < 4; ++runlen) {
963 if (opj_int_abs(t1->data[((k + runlen)*t1->w) + i]) & one)
966 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
967 opj_mqc_encode(mqc, runlen != 4);
971 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
972 opj_mqc_encode(mqc, runlen >> 1);
973 opj_mqc_encode(mqc, runlen & 1);
977 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
978 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
979 opj_t1_enc_clnpass_step(
981 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
982 &t1->data[(j * t1->w) + i],
987 agg && (j == k + runlen),
994 static void opj_t1_dec_clnpass(
1000 int one, half, oneplushalf, agg, runlen, vsc;
1002 int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1004 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1008 oneplushalf = one | half;
1009 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1010 for (k = 0; k < t1->h; k += 4) {
1011 for (i = 0; i < t1->w; ++i) {
1012 if (k + 3 < t1->h) {
1013 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1014 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1015 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1016 || (MACRO_t1_flags(1 + k + 3,1 + i)
1017 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1022 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1023 if (!opj_mqc_decode(mqc)) {
1026 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1027 runlen = opj_mqc_decode(mqc);
1028 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1032 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1033 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1034 opj_t1_dec_clnpass_step_vsc(
1036 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1037 &t1->data[(j * t1->w) + i],
1040 agg && (j == k + runlen),
1046 int *data1 = t1->data;
1047 opj_flag_t *flags1 = &t1->flags[1];
1048 for (k = 0; k < (t1->h & ~3); k += 4) {
1049 for (i = 0; i < t1->w; ++i) {
1050 int *data2 = data1 + i;
1051 opj_flag_t *flags2 = flags1 + i;
1052 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1053 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1054 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1055 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1057 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1058 if (!opj_mqc_decode(mqc)) {
1061 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1062 runlen = opj_mqc_decode(mqc);
1063 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1064 flags2 += runlen * t1->flags_stride;
1065 data2 += runlen * t1->w;
1066 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1067 flags2 += t1->flags_stride;
1068 if (agg && (j == k + runlen)) {
1069 opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1071 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1076 flags2 += t1->flags_stride;
1077 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1079 flags2 += t1->flags_stride;
1080 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1082 flags2 += t1->flags_stride;
1083 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1085 flags2 += t1->flags_stride;
1086 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1090 data1 += t1->w << 2;
1091 flags1 += t1->flags_stride << 2;
1093 for (i = 0; i < t1->w; ++i) {
1094 int *data2 = data1 + i;
1095 opj_flag_t *flags2 = flags1 + i;
1096 for (j = k; j < t1->h; ++j) {
1097 flags2 += t1->flags_stride;
1098 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1106 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1107 v = opj_mqc_decode(mqc);
1108 v = (v << 1) | opj_mqc_decode(mqc);
1109 v = (v << 1) | opj_mqc_decode(mqc);
1110 v = (v << 1) | opj_mqc_decode(mqc);
1113 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1117 } /* VSC and BYPASS by Antonin */
1120 /** mod fixed_quality */
1121 static OPJ_FLOAT64 opj_t1_getwmsedec(
1128 OPJ_FLOAT64 stepsize,
1129 OPJ_UINT32 numcomps,
1130 const OPJ_FLOAT64 * mct_norms)
1132 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1133 OPJ_ARG_NOT_USED(numcomps);
1136 w1 = mct_norms[compno];
1140 w2 = opj_dwt_getnorm(level, orient);
1141 } else { /* if (qmfbid == 0) */
1142 w2 = opj_dwt_getnorm_real(level, orient);
1145 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1146 wmsedec *= wmsedec * nmsedec / 8192.0;
1151 opj_bool opj_t1_allocate_buffers(
1156 OPJ_UINT32 datasize=w * h;
1157 OPJ_UINT32 flagssize;
1159 if(datasize > t1->datasize){
1160 opj_aligned_free(t1->data);
1161 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1165 t1->datasize=datasize;
1167 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1169 t1->flags_stride=w+2;
1170 flagssize=t1->flags_stride * (h+2);
1172 if(flagssize > t1->flagssize){
1173 opj_aligned_free(t1->flags);
1174 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1178 t1->flagssize=flagssize;
1180 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1188 /* ----------------------------------------------------------------------- */
1190 /* ----------------------------------------------------------------------- */
1192 * Creates a new Tier 1 handle
1193 * and initializes the look-up tables of the Tier-1 coder/decoder
1194 * @return a new T1 handle if successful, returns NULL otherwise
1196 opj_t1_t* opj_t1_create()
1198 opj_t1_t *l_t1 = 00;
1200 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1204 memset(l_t1,0,sizeof(opj_t1_t));
1206 /* create MQC and RAW handles */
1207 l_t1->mqc = opj_mqc_create();
1209 opj_t1_destroy(l_t1);
1213 l_t1->raw = opj_raw_create();
1215 opj_t1_destroy(l_t1);
1224 * Destroys a previously created T1 handle
1226 * @param p_t1 Tier 1 handle to destroy
1228 void opj_t1_destroy(opj_t1_t *p_t1)
1234 /* destroy MQC and RAW handles */
1235 opj_mqc_destroy(p_t1->mqc);
1237 opj_raw_destroy(p_t1->raw);
1241 opj_aligned_free(p_t1->data);
1246 opj_aligned_free(p_t1->flags);
1253 opj_bool opj_t1_decode_cblks( opj_t1_t* t1,
1254 opj_tcd_tilecomp_t* tilec,
1258 OPJ_UINT32 resno, bandno, precno, cblkno;
1259 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1261 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1262 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1264 for (bandno = 0; bandno < res->numbands; ++bandno) {
1265 opj_tcd_band_t* restrict band = &res->bands[bandno];
1267 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1268 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1270 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1271 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1272 OPJ_INT32* restrict datap;
1273 /*void* restrict tiledp;*/
1274 OPJ_UINT32 cblk_w, cblk_h;
1278 if (OPJ_FALSE == opj_t1_decode_cblk(
1287 x = cblk->x0 - band->x0;
1288 y = cblk->y0 - band->y0;
1289 if (band->bandno & 1) {
1290 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1291 x += pres->x1 - pres->x0;
1293 if (band->bandno & 2) {
1294 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1295 y += pres->y1 - pres->y0;
1302 if (tccp->roishift) {
1303 OPJ_INT32 thresh = 1 << tccp->roishift;
1304 for (j = 0; j < cblk_h; ++j) {
1305 for (i = 0; i < cblk_w; ++i) {
1306 OPJ_INT32 val = datap[(j * cblk_w) + i];
1307 OPJ_INT32 mag = abs(val);
1308 if (mag >= thresh) {
1309 mag >>= tccp->roishift;
1310 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1316 /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1317 if (tccp->qmfbid == 1) {
1318 int* restrict tiledp = &tilec->data[(y * tile_w) + x];
1319 for (j = 0; j < cblk_h; ++j) {
1320 for (i = 0; i < cblk_w; ++i) {
1321 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1322 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1325 } else { /* if (tccp->qmfbid == 0) */
1326 float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
1327 for (j = 0; j < cblk_h; ++j) {
1328 float* restrict tiledp2 = tiledp;
1329 for (i = 0; i < cblk_w; ++i) {
1330 float tmp = *datap * band->stepsize;
1334 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1335 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1341 /*opj_free(cblk->data);
1342 opj_free(cblk->segs);*/
1343 /*cblk->segs = 00;*/
1345 /*opj_free(precinct->cblks.dec);*/
1353 opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
1354 opj_tcd_cblk_dec_t* cblk,
1356 OPJ_UINT32 roishift,
1359 opj_raw_t *raw = t1->raw; /* RAW component */
1360 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1363 OPJ_UINT32 passtype;
1364 OPJ_UINT32 segno, passno;
1365 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1367 if(!opj_t1_allocate_buffers(
1369 cblk->x1 - cblk->x0,
1370 cblk->y1 - cblk->y0))
1375 bpno = roishift + cblk->numbps - 1;
1378 opj_mqc_resetstates(mqc);
1379 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1380 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1381 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1383 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1384 opj_tcd_seg_t *seg = &cblk->segs[segno];
1387 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1388 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1389 if(seg->data == 00){
1392 if (type == T1_TYPE_RAW) {
1393 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1395 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1400 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1403 if (type == T1_TYPE_RAW) {
1404 opj_t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1406 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1407 opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1409 opj_t1_dec_sigpass_mqc(t1, bpno+1, orient);
1414 if (type == T1_TYPE_RAW) {
1415 opj_t1_dec_refpass_raw(t1, bpno+1, cblksty);
1417 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1418 opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1420 opj_t1_dec_refpass_mqc(t1, bpno+1);
1425 opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1429 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1430 opj_mqc_resetstates(mqc);
1431 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1432 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1433 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1435 if (++passtype == 3) {
1447 opj_bool opj_t1_encode_cblks( opj_t1_t *t1,
1448 opj_tcd_tile_t *tile,
1450 const OPJ_FLOAT64 * mct_norms
1453 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1455 tile->distotile = 0; /* fixed_quality */
1457 for (compno = 0; compno < tile->numcomps; ++compno) {
1458 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1459 opj_tccp_t* tccp = &tcp->tccps[compno];
1460 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1462 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1463 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1465 for (bandno = 0; bandno < res->numbands; ++bandno) {
1466 opj_tcd_band_t* restrict band = &res->bands[bandno];
1467 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1469 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1470 opj_tcd_precinct_t *prc = &band->precincts[precno];
1472 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1473 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1474 OPJ_INT32 * restrict datap;
1475 OPJ_INT32* restrict tiledp;
1480 OPJ_INT32 x = cblk->x0 - band->x0;
1481 OPJ_INT32 y = cblk->y0 - band->y0;
1482 if (band->bandno & 1) {
1483 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1484 x += pres->x1 - pres->x0;
1486 if (band->bandno & 2) {
1487 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1488 y += pres->y1 - pres->y0;
1491 if(!opj_t1_allocate_buffers(
1493 cblk->x1 - cblk->x0,
1494 cblk->y1 - cblk->y0))
1503 tiledp=&tilec->data[(y * tile_w) + x];
1504 if (tccp->qmfbid == 1) {
1505 for (j = 0; j < cblk_h; ++j) {
1506 for (i = 0; i < cblk_w; ++i) {
1507 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1508 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1511 } else { /* if (tccp->qmfbid == 0) */
1512 for (j = 0; j < cblk_h; ++j) {
1513 for (i = 0; i < cblk_w; ++i) {
1514 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1515 datap[(j * cblk_w) + i] =
1518 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1528 tilec->numresolutions - 1 - resno,
1544 /** mod fixed_quality */
1545 void opj_t1_encode_cblk(opj_t1_t *t1,
1546 opj_tcd_cblk_enc_t* cblk,
1551 OPJ_FLOAT64 stepsize,
1553 OPJ_UINT32 numcomps,
1554 opj_tcd_tile_t * tile,
1555 const OPJ_FLOAT64 * mct_norms)
1557 OPJ_FLOAT64 cumwmsedec = 0.0;
1559 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1563 OPJ_UINT32 passtype;
1564 OPJ_INT32 nmsedec = 0;
1567 OPJ_BYTE type = T1_TYPE_MQ;
1568 OPJ_FLOAT64 tempwmsedec;
1571 for (i = 0; i < t1->w * t1->h; ++i) {
1572 OPJ_INT32 tmp = abs(t1->data[i]);
1573 max = opj_int_max(max, tmp);
1576 cblk->numbps = max ? (opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1578 bpno = cblk->numbps - 1;
1581 opj_mqc_resetstates(mqc);
1582 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1583 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1584 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1585 opj_mqc_init_enc(mqc, cblk->data);
1587 for (passno = 0; bpno >= 0; ++passno) {
1588 opj_tcd_pass_t *pass = &cblk->passes[passno];
1589 OPJ_UINT32 correction = 3;
1590 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1594 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1597 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1600 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1601 /* code switch SEGMARK (i.e. SEGSYM) */
1602 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1603 opj_mqc_segmark_enc(mqc);
1608 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1609 cumwmsedec += tempwmsedec;
1610 tile->distotile += tempwmsedec;
1612 /* Code switch "RESTART" (i.e. TERMALL) */
1613 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1614 if (type == T1_TYPE_RAW) {
1617 /* correction = mqc_bypass_flush_enc(); */
1618 } else { /* correction = mqc_restart_enc(); */
1624 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1625 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1626 if (type == T1_TYPE_RAW) {
1629 /* correction = mqc_bypass_flush_enc(); */
1630 } else { /* correction = mqc_restart_enc(); */
1640 if (++passtype == 3) {
1645 if (pass->term && bpno > 0) {
1646 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1647 if (type == T1_TYPE_RAW)
1648 opj_mqc_bypass_init_enc(mqc);
1650 opj_mqc_restart_init_enc(mqc);
1653 pass->distortiondec = cumwmsedec;
1654 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1656 /* Code-switch "RESET" */
1657 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1658 opj_mqc_reset_enc(mqc);
1661 /* Code switch "ERTERM" (i.e. PTERM) */
1662 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1663 opj_mqc_erterm_enc(mqc);
1664 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1667 cblk->totalpasses = passno;
1669 for (passno = 0; passno<cblk->totalpasses; passno++) {
1670 opj_tcd_pass_t *pass = &cblk->passes[passno];
1671 if (pass->rate > opj_mqc_numbytes(mqc))
1672 pass->rate = opj_mqc_numbytes(mqc);
1673 /*Preventing generation of FF as last data byte of a pass*/
1674 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1677 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1681 void opj_t1_dec_refpass_step( opj_t1_t *t1,
1692 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1693 opj_raw_t *raw = t1->raw; /* RAW component */
1695 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1696 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1697 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1698 if (type == T1_TYPE_RAW) {
1699 v = opj_raw_decode(raw);
1701 v = opj_mqc_decode(mqc);
1703 t = v ? poshalf : neghalf;
1704 *datap += *datap < 0 ? -t : t;
1705 *flagsp |= T1_REFINE;
1707 } /* VSC and BYPASS by Antonin */
1711 void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1715 OPJ_INT32 oneplushalf,
1721 opj_raw_t *raw = t1->raw; /* RAW component */
1722 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1724 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1725 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1726 if (type == T1_TYPE_RAW) {
1727 if (opj_raw_decode(raw)) {
1728 v = opj_raw_decode(raw); /* ESSAI */
1729 *datap = v ? -oneplushalf : oneplushalf;
1730 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1733 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1734 if (opj_mqc_decode(mqc)) {
1735 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1736 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1737 *datap = v ? -oneplushalf : oneplushalf;
1738 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1741 *flagsp |= T1_VISIT;
1743 } /* VSC and BYPASS by Antonin */