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 void INLINE opj_t1_dec_refpass_step_raw(
179 static void INLINE opj_t1_dec_refpass_step_mqc(
185 static void INLINE 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(
405 opj_raw_t *raw = t1->raw; /* RAW component */
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;
1135 w1 = mct_norms[compno];
1139 w2 = opj_dwt_getnorm(level, orient);
1140 } else { /* if (qmfbid == 0) */
1141 w2 = opj_dwt_getnorm_real(level, orient);
1144 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1145 wmsedec *= wmsedec * nmsedec / 8192.0;
1150 opj_bool opj_t1_allocate_buffers(
1155 OPJ_UINT32 datasize=w * h;
1156 OPJ_UINT32 flagssize;
1158 if(datasize > t1->datasize){
1159 opj_aligned_free(t1->data);
1160 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1164 t1->datasize=datasize;
1166 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1168 t1->flags_stride=w+2;
1169 flagssize=t1->flags_stride * (h+2);
1171 if(flagssize > t1->flagssize){
1172 opj_aligned_free(t1->flags);
1173 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1177 t1->flagssize=flagssize;
1179 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1187 /* ----------------------------------------------------------------------- */
1189 /* ----------------------------------------------------------------------- */
1191 * Creates a new Tier 1 handle
1192 * and initializes the look-up tables of the Tier-1 coder/decoder
1193 * @return a new T1 handle if successful, returns NULL otherwise
1195 opj_t1_t* opj_t1_create()
1197 opj_t1_t *l_t1 = 00;
1199 l_t1 = (opj_t1_t*) opj_malloc(sizeof(opj_t1_t));
1203 memset(l_t1,0,sizeof(opj_t1_t));
1205 /* create MQC and RAW handles */
1206 l_t1->mqc = opj_mqc_create();
1208 opj_t1_destroy(l_t1);
1212 l_t1->raw = opj_raw_create();
1214 opj_t1_destroy(l_t1);
1223 * Destroys a previously created T1 handle
1225 * @param p_t1 Tier 1 handle to destroy
1227 void opj_t1_destroy(opj_t1_t *p_t1)
1233 /* destroy MQC and RAW handles */
1234 opj_mqc_destroy(p_t1->mqc);
1236 opj_raw_destroy(p_t1->raw);
1240 opj_aligned_free(p_t1->data);
1245 opj_aligned_free(p_t1->flags);
1252 opj_bool opj_t1_decode_cblks( opj_t1_t* t1,
1253 opj_tcd_tilecomp_t* tilec,
1257 OPJ_UINT32 resno, bandno, precno, cblkno;
1258 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1260 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1261 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1263 for (bandno = 0; bandno < res->numbands; ++bandno) {
1264 opj_tcd_band_t* restrict band = &res->bands[bandno];
1266 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1267 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1269 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1270 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1271 OPJ_INT32* restrict datap;
1272 /*void* restrict tiledp;*/
1273 OPJ_UINT32 cblk_w, cblk_h;
1277 if (OPJ_FALSE == opj_t1_decode_cblk(
1286 x = cblk->x0 - band->x0;
1287 y = cblk->y0 - band->y0;
1288 if (band->bandno & 1) {
1289 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1290 x += pres->x1 - pres->x0;
1292 if (band->bandno & 2) {
1293 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1294 y += pres->y1 - pres->y0;
1301 if (tccp->roishift) {
1302 OPJ_INT32 thresh = 1 << tccp->roishift;
1303 for (j = 0; j < cblk_h; ++j) {
1304 for (i = 0; i < cblk_w; ++i) {
1305 OPJ_INT32 val = datap[(j * cblk_w) + i];
1306 OPJ_INT32 mag = abs(val);
1307 if (mag >= thresh) {
1308 mag >>= tccp->roishift;
1309 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1315 /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1316 if (tccp->qmfbid == 1) {
1317 int* restrict tiledp = &tilec->data[(y * tile_w) + x];
1318 for (j = 0; j < cblk_h; ++j) {
1319 for (i = 0; i < cblk_w; ++i) {
1320 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1321 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1324 } else { /* if (tccp->qmfbid == 0) */
1325 float* restrict tiledp = (float*) &tilec->data[(y * tile_w) + x];
1326 for (j = 0; j < cblk_h; ++j) {
1327 float* restrict tiledp2 = tiledp;
1328 for (i = 0; i < cblk_w; ++i) {
1329 float tmp = *datap * band->stepsize;
1333 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1334 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1340 //opj_free(cblk->data);
1341 //opj_free(cblk->segs);
1342 /*cblk->segs = 00;*/
1344 //opj_free(precinct->cblks.dec);
1352 opj_bool opj_t1_decode_cblk(opj_t1_t *t1,
1353 opj_tcd_cblk_dec_t* cblk,
1355 OPJ_UINT32 roishift,
1358 opj_raw_t *raw = t1->raw; /* RAW component */
1359 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1362 OPJ_UINT32 passtype;
1363 OPJ_UINT32 segno, passno;
1364 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1366 if(!opj_t1_allocate_buffers(
1368 cblk->x1 - cblk->x0,
1369 cblk->y1 - cblk->y0))
1374 bpno = roishift + cblk->numbps - 1;
1377 opj_mqc_resetstates(mqc);
1378 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1379 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1380 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1382 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1383 opj_tcd_seg_t *seg = &cblk->segs[segno];
1386 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1387 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1388 if(seg->data == 00){
1391 if (type == T1_TYPE_RAW) {
1392 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1394 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1399 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1402 if (type == T1_TYPE_RAW) {
1403 opj_t1_dec_sigpass_raw(t1, bpno+1, orient, cblksty);
1405 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1406 opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, orient);
1408 opj_t1_dec_sigpass_mqc(t1, bpno+1, orient);
1413 if (type == T1_TYPE_RAW) {
1414 opj_t1_dec_refpass_raw(t1, bpno+1, cblksty);
1416 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1417 opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1419 opj_t1_dec_refpass_mqc(t1, bpno+1);
1424 opj_t1_dec_clnpass(t1, bpno+1, orient, cblksty);
1428 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1429 opj_mqc_resetstates(mqc);
1430 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1431 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1432 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1434 if (++passtype == 3) {
1446 opj_bool opj_t1_encode_cblks( opj_t1_t *t1,
1447 opj_tcd_tile_t *tile,
1449 const OPJ_FLOAT64 * mct_norms
1452 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1454 tile->distotile = 0; /* fixed_quality */
1456 for (compno = 0; compno < tile->numcomps; ++compno) {
1457 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1458 opj_tccp_t* tccp = &tcp->tccps[compno];
1459 OPJ_UINT32 tile_w = tilec->x1 - tilec->x0;
1461 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1462 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1464 for (bandno = 0; bandno < res->numbands; ++bandno) {
1465 opj_tcd_band_t* restrict band = &res->bands[bandno];
1466 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1468 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1469 opj_tcd_precinct_t *prc = &band->precincts[precno];
1471 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1472 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1473 OPJ_INT32 * restrict datap;
1474 OPJ_INT32* restrict tiledp;
1479 OPJ_INT32 x = cblk->x0 - band->x0;
1480 OPJ_INT32 y = cblk->y0 - band->y0;
1481 if (band->bandno & 1) {
1482 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1483 x += pres->x1 - pres->x0;
1485 if (band->bandno & 2) {
1486 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1487 y += pres->y1 - pres->y0;
1490 if(!opj_t1_allocate_buffers(
1492 cblk->x1 - cblk->x0,
1493 cblk->y1 - cblk->y0))
1502 tiledp=&tilec->data[(y * tile_w) + x];
1503 if (tccp->qmfbid == 1) {
1504 for (j = 0; j < cblk_h; ++j) {
1505 for (i = 0; i < cblk_w; ++i) {
1506 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1507 datap[(j * cblk_w) + i] = tmp << T1_NMSEDEC_FRACBITS;
1510 } else { /* if (tccp->qmfbid == 0) */
1511 for (j = 0; j < cblk_h; ++j) {
1512 for (i = 0; i < cblk_w; ++i) {
1513 OPJ_INT32 tmp = tiledp[(j * tile_w) + i];
1514 datap[(j * cblk_w) + i] =
1517 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1527 tilec->numresolutions - 1 - resno,
1543 /** mod fixed_quality */
1544 void opj_t1_encode_cblk(opj_t1_t *t1,
1545 opj_tcd_cblk_enc_t* cblk,
1550 OPJ_FLOAT64 stepsize,
1552 OPJ_UINT32 numcomps,
1553 opj_tcd_tile_t * tile,
1554 const OPJ_FLOAT64 * mct_norms)
1556 OPJ_FLOAT64 cumwmsedec = 0.0;
1558 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1562 OPJ_UINT32 passtype;
1563 OPJ_INT32 nmsedec = 0;
1566 OPJ_BYTE type = T1_TYPE_MQ;
1567 OPJ_FLOAT64 tempwmsedec;
1570 for (i = 0; i < t1->w * t1->h; ++i) {
1571 OPJ_INT32 tmp = abs(t1->data[i]);
1572 max = opj_int_max(max, tmp);
1575 cblk->numbps = max ? (opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
1577 bpno = cblk->numbps - 1;
1580 opj_mqc_resetstates(mqc);
1581 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1582 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1583 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1584 opj_mqc_init_enc(mqc, cblk->data);
1586 for (passno = 0; bpno >= 0; ++passno) {
1587 opj_tcd_pass_t *pass = &cblk->passes[passno];
1588 OPJ_UINT32 correction = 3;
1589 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1593 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1596 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1599 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1600 /* code switch SEGMARK (i.e. SEGSYM) */
1601 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1602 opj_mqc_segmark_enc(mqc);
1607 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms) ;
1608 cumwmsedec += tempwmsedec;
1609 tile->distotile += tempwmsedec;
1611 /* Code switch "RESTART" (i.e. TERMALL) */
1612 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1613 if (type == T1_TYPE_RAW) {
1616 /* correction = mqc_bypass_flush_enc(); */
1617 } else { /* correction = mqc_restart_enc(); */
1623 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1624 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1625 if (type == T1_TYPE_RAW) {
1628 /* correction = mqc_bypass_flush_enc(); */
1629 } else { /* correction = mqc_restart_enc(); */
1639 if (++passtype == 3) {
1644 if (pass->term && bpno > 0) {
1645 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1646 if (type == T1_TYPE_RAW)
1647 opj_mqc_bypass_init_enc(mqc);
1649 opj_mqc_restart_init_enc(mqc);
1652 pass->distortiondec = cumwmsedec;
1653 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1655 /* Code-switch "RESET" */
1656 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1657 opj_mqc_reset_enc(mqc);
1660 /* Code switch "ERTERM" (i.e. PTERM) */
1661 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1662 opj_mqc_erterm_enc(mqc);
1663 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1666 cblk->totalpasses = passno;
1668 for (passno = 0; passno<cblk->totalpasses; passno++) {
1669 opj_tcd_pass_t *pass = &cblk->passes[passno];
1670 if (pass->rate > opj_mqc_numbytes(mqc))
1671 pass->rate = opj_mqc_numbytes(mqc);
1672 /*Preventing generation of FF as last data byte of a pass*/
1673 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1676 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1680 void opj_t1_dec_refpass_step( opj_t1_t *t1,
1691 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1692 opj_raw_t *raw = t1->raw; /* RAW component */
1694 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1695 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1696 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1697 if (type == T1_TYPE_RAW) {
1698 v = opj_raw_decode(raw);
1700 v = opj_mqc_decode(mqc);
1702 t = v ? poshalf : neghalf;
1703 *datap += *datap < 0 ? -t : t;
1704 *flagsp |= T1_REFINE;
1706 } /* VSC and BYPASS by Antonin */
1710 void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1714 OPJ_INT32 oneplushalf,
1720 opj_raw_t *raw = t1->raw; /* RAW component */
1721 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1723 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1724 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1725 if (type == T1_TYPE_RAW) {
1726 if (opj_raw_decode(raw)) {
1727 v = opj_raw_decode(raw); /* ESSAI */
1728 *datap = v ? -oneplushalf : oneplushalf;
1729 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1732 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1733 if (opj_mqc_decode(mqc)) {
1734 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1735 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1736 *datap = v ? -oneplushalf : oneplushalf;
1737 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1740 *flagsp |= T1_VISIT;
1742 } /* VSC and BYPASS by Antonin */