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_vsc(
129 Encode refinement pass
131 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
142 Encode refinement pass
144 static void opj_t1_enc_refpass( opj_t1_t *t1,
151 Decode refinement pass
153 static void opj_t1_dec_refpass_raw(
157 static void opj_t1_dec_refpass_mqc_vsc(
163 Decode refinement pass
166 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
175 static INLINE void opj_t1_dec_refpass_step_raw(
182 static INLINE void opj_t1_dec_refpass_step_mqc(
188 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
201 static void opj_t1_enc_clnpass_step(
214 static void opj_t1_dec_clnpass_step_partial(
219 OPJ_INT32 oneplushalf);
220 static void opj_t1_dec_clnpass_step(
225 OPJ_INT32 oneplushalf);
226 static void opj_t1_dec_clnpass_step_vsc(
231 OPJ_INT32 oneplushalf,
237 static void opj_t1_enc_clnpass(
246 static void opj_t1_dec_clnpass(
252 static OPJ_FLOAT64 opj_t1_getwmsedec(
259 OPJ_FLOAT64 stepsize,
261 const OPJ_FLOAT64 * mct_norms,
262 OPJ_UINT32 mct_numcomps);
264 static void opj_t1_encode_cblk( opj_t1_t *t1,
265 opj_tcd_cblk_enc_t* cblk,
270 OPJ_FLOAT64 stepsize,
273 opj_tcd_tile_t * tile,
274 const OPJ_FLOAT64 * mct_norms,
275 OPJ_UINT32 mct_numcomps);
280 @param cblk Code-block coding parameters
282 @param roishift Region of interest shifting value
283 @param cblksty Code-block style
285 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
286 opj_tcd_cblk_dec_t* cblk,
291 static OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
299 /* ----------------------------------------------------------------------- */
301 static OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
302 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
305 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
306 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
309 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
310 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
311 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
315 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
316 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
319 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
321 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
324 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
327 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
329 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
332 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
335 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
336 opj_flag_t *np = flagsp - stride;
337 opj_flag_t *sp = flagsp + stride;
339 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
340 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
341 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
343 opj_flag_t flag_N = T1_SIG_N | (T1_SIG_N << (4 * s));
346 np[0] |= flag_N << 2;
349 flagsp[-1] |= flag_N << 1;
351 flagsp[1] |= flag_N << 3;
358 static void opj_t1_enc_sigpass_step( opj_t1_t *t1,
372 opj_mqc_t *mqc = t1->mqc; /* MQC component */
374 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
375 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
376 v = (opj_int_abs(*datap) & one) ? 1 : 0;
377 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
378 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
379 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
381 opj_mqc_encode(mqc, (OPJ_UINT32)v);
384 v = *datap < 0 ? 1 : 0;
385 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
386 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
387 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
388 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
390 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
392 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
399 static INLINE void opj_t1_dec_sigpass_step_raw(
404 OPJ_INT32 oneplushalf,
408 opj_raw_t *raw = t1->raw; /* RAW component */
409 OPJ_ARG_NOT_USED(orient);
411 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
412 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
413 if (opj_raw_decode(raw)) {
414 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
415 *datap = v ? -oneplushalf : oneplushalf;
416 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
422 static INLINE void opj_t1_dec_sigpass_step_mqc(
427 OPJ_INT32 oneplushalf)
431 opj_mqc_t *mqc = t1->mqc; /* MQC component */
434 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
435 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
436 if (opj_mqc_decode(mqc)) {
437 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
438 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
439 *datap = v ? -oneplushalf : oneplushalf;
440 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
444 } /* VSC and BYPASS by Antonin */
446 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
451 OPJ_INT32 oneplushalf,
456 opj_mqc_t *mqc = t1->mqc; /* MQC component */
458 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
459 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
460 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
461 if (opj_mqc_decode(mqc)) {
462 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
463 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
464 *datap = v ? -oneplushalf : oneplushalf;
465 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
469 } /* VSC and BYPASS by Antonin */
473 static void opj_t1_enc_sigpass(opj_t1_t *t1,
481 OPJ_UINT32 i, j, k, vsc;
485 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
486 for (k = 0; k < t1->h; k += 4) {
487 for (i = 0; i < t1->w; ++i) {
488 for (j = k; j < k + 4 && j < t1->h; ++j) {
489 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
490 opj_t1_enc_sigpass_step(
492 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
493 &t1->data[(j * t1->data_stride) + i],
505 static void opj_t1_dec_sigpass_raw(
511 OPJ_INT32 one, half, oneplushalf, vsc;
515 oneplushalf = one | half;
516 for (k = 0; k < t1->h; k += 4) {
517 for (i = 0; i < t1->w; ++i) {
518 for (j = k; j < k + 4 && j < t1->h; ++j) {
519 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
520 opj_t1_dec_sigpass_step_raw(
522 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
523 &t1->data[(j * t1->w) + i],
530 } /* VSC and BYPASS by Antonin */
532 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, orient, w, h, flags_stride) \
534 OPJ_INT32 one, half, oneplushalf; \
535 OPJ_UINT32 i, j, k; \
536 OPJ_INT32 *data1 = t1->data; \
537 opj_flag_t *flags1 = &t1->flags[1]; \
540 oneplushalf = one | half; \
541 for (k = 0; k < (h & ~3u); k += 4) { \
542 for (i = 0; i < w; ++i) { \
543 OPJ_INT32 *data2 = data1 + i; \
544 opj_flag_t *flags2 = flags1 + i; \
545 flags2 += flags_stride; \
546 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
548 flags2 += flags_stride; \
549 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
551 flags2 += flags_stride; \
552 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
554 flags2 += flags_stride; \
555 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
559 flags1 += flags_stride << 2; \
561 for (i = 0; i < w; ++i) { \
562 OPJ_INT32 *data2 = data1 + i; \
563 opj_flag_t *flags2 = flags1 + i; \
564 for (j = k; j < h; ++j) { \
565 flags2 += flags_stride; \
566 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf); \
572 static void opj_t1_dec_sigpass_mqc_64x64(
577 opj_t1_dec_sigpass_mqc_internal(t1, bpno, orient, 64, 64, 66);
580 static void opj_t1_dec_sigpass_mqc_generic(
585 opj_t1_dec_sigpass_mqc_internal(t1, bpno, orient, t1->w, t1->h, t1->flags_stride);
588 /* VSC and BYPASS by Antonin */
589 static void opj_t1_dec_sigpass_mqc_vsc(
594 OPJ_INT32 one, half, oneplushalf, vsc;
598 oneplushalf = one | half;
599 for (k = 0; k < t1->h; k += 4) {
600 for (i = 0; i < t1->w; ++i) {
601 for (j = k; j < k + 4 && j < t1->h; ++j) {
602 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
603 opj_t1_dec_sigpass_step_mqc_vsc(
605 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
606 &t1->data[(j * t1->w) + i],
613 } /* VSC and BYPASS by Antonin */
617 static void opj_t1_enc_refpass_step( opj_t1_t *t1,
629 opj_mqc_t *mqc = t1->mqc; /* MQC component */
631 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
632 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
633 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
634 v = (opj_int_abs(*datap) & one) ? 1 : 0;
635 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
636 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
637 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
639 opj_mqc_encode(mqc, (OPJ_UINT32)v);
641 *flagsp |= T1_REFINE;
645 static INLINE void opj_t1_dec_refpass_step_raw(
653 OPJ_INT32 v, t, flag;
655 opj_raw_t *raw = t1->raw; /* RAW component */
657 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
658 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
659 v = (OPJ_INT32)opj_raw_decode(raw);
660 t = v ? poshalf : neghalf;
661 *datap += *datap < 0 ? -t : t;
662 *flagsp |= T1_REFINE;
664 } /* VSC and BYPASS by Antonin */
666 static INLINE void opj_t1_dec_refpass_step_mqc(
673 OPJ_INT32 v, t, flag;
675 opj_mqc_t *mqc = t1->mqc; /* MQC component */
678 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
679 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
680 v = opj_mqc_decode(mqc);
681 t = v ? poshalf : neghalf;
682 *datap += *datap < 0 ? -t : t;
683 *flagsp |= T1_REFINE;
685 } /* VSC and BYPASS by Antonin */
687 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
695 OPJ_INT32 v, t, flag;
697 opj_mqc_t *mqc = t1->mqc; /* MQC component */
699 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
700 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
701 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
702 v = opj_mqc_decode(mqc);
703 t = v ? poshalf : neghalf;
704 *datap += *datap < 0 ? -t : t;
705 *flagsp |= T1_REFINE;
707 } /* VSC and BYPASS by Antonin */
710 static void opj_t1_enc_refpass(
717 OPJ_UINT32 i, j, k, vsc;
721 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
722 for (k = 0; k < t1->h; k += 4) {
723 for (i = 0; i < t1->w; ++i) {
724 for (j = k; j < k + 4 && j < t1->h; ++j) {
725 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
726 opj_t1_enc_refpass_step(
728 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
729 &t1->data[(j * t1->data_stride) + i],
740 static void opj_t1_dec_refpass_raw(
745 OPJ_INT32 one, poshalf, neghalf;
750 neghalf = bpno > 0 ? -poshalf : -1;
751 for (k = 0; k < t1->h; k += 4) {
752 for (i = 0; i < t1->w; ++i) {
753 for (j = k; j < k + 4 && j < t1->h; ++j) {
754 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
755 opj_t1_dec_refpass_step_raw(
757 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
758 &t1->data[(j * t1->w) + i],
765 } /* VSC and BYPASS by Antonin */
767 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
769 OPJ_INT32 one, poshalf, neghalf; \
770 OPJ_UINT32 i, j, k; \
771 OPJ_INT32 *data1 = t1->data; \
772 opj_flag_t *flags1 = &t1->flags[1]; \
774 poshalf = one >> 1; \
775 neghalf = bpno > 0 ? -poshalf : -1; \
776 for (k = 0; k < (h & ~3u); k += 4) { \
777 for (i = 0; i < w; ++i) { \
778 OPJ_INT32 *data2 = data1 + i; \
779 opj_flag_t *flags2 = flags1 + i; \
780 flags2 += flags_stride; \
781 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
783 flags2 += flags_stride; \
784 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
786 flags2 += flags_stride; \
787 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
789 flags2 += flags_stride; \
790 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
794 flags1 += flags_stride << 2; \
796 for (i = 0; i < w; ++i) { \
797 OPJ_INT32 *data2 = data1 + i; \
798 opj_flag_t *flags2 = flags1 + i; \
799 for (j = k; j < h; ++j) { \
800 flags2 += flags_stride; \
801 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf); \
807 static void opj_t1_dec_refpass_mqc_64x64(
811 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
814 static void opj_t1_dec_refpass_mqc_generic(
818 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
821 /* VSC and BYPASS by Antonin */
822 static void opj_t1_dec_refpass_mqc_vsc(
826 OPJ_INT32 one, poshalf, neghalf;
831 neghalf = bpno > 0 ? -poshalf : -1;
832 for (k = 0; k < t1->h; k += 4) {
833 for (i = 0; i < t1->w; ++i) {
834 for (j = k; j < k + 4 && j < t1->h; ++j) {
835 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
836 opj_t1_dec_refpass_step_mqc_vsc(
838 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
839 &t1->data[(j * t1->w) + i],
846 } /* VSC and BYPASS by Antonin */
849 static void opj_t1_enc_clnpass_step(
863 opj_mqc_t *mqc = t1->mqc; /* MQC component */
865 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
869 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
870 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
871 v = (opj_int_abs(*datap) & one) ? 1 : 0;
872 opj_mqc_encode(mqc, (OPJ_UINT32)v);
875 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno));
876 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
877 v = *datap < 0 ? 1 : 0;
878 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
879 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
882 *flagsp &= ~T1_VISIT;
885 static void opj_t1_dec_clnpass_step_partial(
890 OPJ_INT32 oneplushalf)
893 opj_mqc_t *mqc = t1->mqc; /* MQC component */
895 OPJ_ARG_NOT_USED(orient);
898 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
899 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
900 *datap = v ? -oneplushalf : oneplushalf;
901 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
902 *flagsp &= ~T1_VISIT;
903 } /* VSC and BYPASS by Antonin */
905 static void opj_t1_dec_clnpass_step(
910 OPJ_INT32 oneplushalf)
914 opj_mqc_t *mqc = t1->mqc; /* MQC component */
917 if (!(flag & (T1_SIG | T1_VISIT))) {
918 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
919 if (opj_mqc_decode(mqc)) {
920 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
921 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
922 *datap = v ? -oneplushalf : oneplushalf;
923 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
926 *flagsp &= ~T1_VISIT;
927 } /* VSC and BYPASS by Antonin */
929 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
934 OPJ_INT32 oneplushalf)
939 opj_mqc_t *mqc = t1->mqc; /* MQC component */
942 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
944 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
945 if (opj_mqc_decode(mqc)) {
946 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
947 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
948 *datap = v ? -oneplushalf : oneplushalf;
949 opj_t1_updateflags(flagsp, v, t1->flags_stride);
952 /*flagsp &= ~T1_VISIT;*/
955 static void opj_t1_dec_clnpass_step_vsc(
960 OPJ_INT32 oneplushalf,
966 opj_mqc_t *mqc = t1->mqc; /* MQC component */
968 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
972 if (!(flag & (T1_SIG | T1_VISIT))) {
973 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
974 if (opj_mqc_decode(mqc)) {
976 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
977 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
978 *datap = v ? -oneplushalf : oneplushalf;
979 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
982 *flagsp &= ~T1_VISIT;
985 static void opj_t1_enc_clnpass(
994 OPJ_UINT32 agg, runlen, vsc;
996 opj_mqc_t *mqc = t1->mqc; /* MQC component */
999 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1000 for (k = 0; k < t1->h; k += 4) {
1001 for (i = 0; i < t1->w; ++i) {
1002 if (k + 3 < t1->h) {
1003 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1004 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1005 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1006 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1007 || (MACRO_t1_flags(1 + k + 3,1 + i)
1008 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1010 agg = !((MACRO_t1_flags(1 + k,1 + i) |
1011 MACRO_t1_flags(1 + k + 1,1 + i) |
1012 MACRO_t1_flags(1 + k + 2,1 + i) |
1013 MACRO_t1_flags(1 + k + 3,1 + i)) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1019 for (runlen = 0; runlen < 4; ++runlen) {
1020 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
1023 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1024 opj_mqc_encode(mqc, runlen != 4);
1028 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1029 opj_mqc_encode(mqc, runlen >> 1);
1030 opj_mqc_encode(mqc, runlen & 1);
1034 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
1035 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
1036 opj_t1_enc_clnpass_step(
1038 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1039 &t1->data[(j * t1->data_stride) + i],
1044 agg && (j == k + runlen),
1051 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1053 #define opj_t1_dec_clnpass_internal(t1, bpno, orient, cblksty, w, h, flags_stride) \
1055 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1056 OPJ_UINT32 i, j, k; \
1057 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1059 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1063 oneplushalf = one | half; \
1064 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1065 for (k = 0; k < h; k += 4) { \
1066 for (i = 0; i < w; ++i) { \
1068 agg = !(MACRO_t1_flags_internal(1 + k,1 + i,flags_stride) & (T1_SIG | T1_VISIT | T1_SIG_OTH) \
1069 || MACRO_t1_flags_internal(1 + k + 1,1 + i,flags_stride) & (T1_SIG | T1_VISIT | T1_SIG_OTH) \
1070 || MACRO_t1_flags_internal(1 + k + 2,1 + i,flags_stride) & (T1_SIG | T1_VISIT | T1_SIG_OTH) \
1071 || (MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1072 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); \
1077 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1078 if (!opj_mqc_decode(mqc)) { \
1081 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1082 runlen = opj_mqc_decode(mqc); \
1083 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1087 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1088 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1089 opj_t1_dec_clnpass_step_vsc( \
1091 &t1->flags[((j+1) * flags_stride) + i + 1], \
1092 &t1->data[(j * w) + i], \
1095 agg && (j == k + (OPJ_UINT32)runlen), \
1101 OPJ_INT32 *data1 = t1->data; \
1102 opj_flag_t *flags1 = &t1->flags[1]; \
1103 for (k = 0; k < (h & ~3u); k += 4) { \
1104 for (i = 0; i < w; ++i) { \
1105 OPJ_INT32 *data2 = data1 + i; \
1106 opj_flag_t *flags2 = flags1 + i; \
1107 agg = !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1108 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1109 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1110 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)); \
1112 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1113 if (!opj_mqc_decode(mqc)) { \
1116 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1117 runlen = opj_mqc_decode(mqc); \
1118 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1119 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1120 data2 += (OPJ_UINT32)runlen * w; \
1121 for (j = (OPJ_UINT32)runlen; j < 4 && j < h; ++j) { \
1122 flags2 += flags_stride; \
1123 if (agg && (j == (OPJ_UINT32)runlen)) { \
1124 opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf); \
1126 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf); \
1132 flags2 += flags_stride; \
1134 if (!(flag & (T1_SIG | T1_VISIT))) \
1135 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1136 *flags2 &= ~T1_VISIT; \
1138 flags2 += flags_stride; \
1140 if (!(flag & (T1_SIG | T1_VISIT))) \
1141 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1142 *flags2 &= ~T1_VISIT; \
1144 flags2 += flags_stride; \
1146 if (!(flag & (T1_SIG | T1_VISIT))) \
1147 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1148 *flags2 &= ~T1_VISIT; \
1150 flags2 += flags_stride; \
1152 if (!(flag & (T1_SIG | T1_VISIT))) \
1153 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, data2, orient, oneplushalf); \
1154 *flags2 &= ~T1_VISIT; \
1159 flags1 += flags_stride << 2; \
1161 for (i = 0; i < w; ++i) { \
1162 OPJ_INT32 *data2 = data1 + i; \
1163 opj_flag_t *flags2 = flags1 + i; \
1164 for (j = k; j < h; ++j) { \
1165 flags2 += flags_stride; \
1166 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf); \
1174 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1175 v = opj_mqc_decode(mqc); \
1176 v = (v << 1) | opj_mqc_decode(mqc); \
1177 v = (v << 1) | opj_mqc_decode(mqc); \
1178 v = (v << 1) | opj_mqc_decode(mqc); \
1181 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1185 } /* VSC and BYPASS by Antonin */
1187 static void opj_t1_dec_clnpass_64x64(
1193 opj_t1_dec_clnpass_internal(t1, bpno, orient, cblksty, 64, 64, 66);
1196 static void opj_t1_dec_clnpass_generic(
1202 opj_t1_dec_clnpass_internal(t1, bpno, orient, cblksty, t1->w, t1->h, t1->flags_stride);
1206 /** mod fixed_quality */
1207 static OPJ_FLOAT64 opj_t1_getwmsedec(
1214 OPJ_FLOAT64 stepsize,
1215 OPJ_UINT32 numcomps,
1216 const OPJ_FLOAT64 * mct_norms,
1217 OPJ_UINT32 mct_numcomps)
1219 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1220 OPJ_ARG_NOT_USED(numcomps);
1222 if (mct_norms && (compno < mct_numcomps)) {
1223 w1 = mct_norms[compno];
1227 w2 = opj_dwt_getnorm(level, orient);
1228 } else { /* if (qmfbid == 0) */
1229 w2 = opj_dwt_getnorm_real(level, orient);
1232 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1233 wmsedec *= wmsedec * nmsedec / 8192.0;
1238 static OPJ_BOOL opj_t1_allocate_buffers(
1243 OPJ_UINT32 datasize=w * h;
1244 OPJ_UINT32 flagssize;
1246 /* encoder uses tile buffer, so no need to allocate */
1248 if(datasize > t1->datasize){
1249 opj_aligned_free(t1->data);
1250 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1252 /* FIXME event manager error callback */
1255 t1->datasize=datasize;
1257 /* memset first arg is declared to never be null by gcc */
1258 if (t1->data != NULL) {
1259 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1262 t1->flags_stride=w+2;
1263 flagssize=t1->flags_stride * (h+2);
1265 if(flagssize > t1->flagssize){
1266 opj_aligned_free(t1->flags);
1267 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1269 /* FIXME event manager error callback */
1272 t1->flagssize=flagssize;
1274 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1282 /* ----------------------------------------------------------------------- */
1284 /* ----------------------------------------------------------------------- */
1286 * Creates a new Tier 1 handle
1287 * and initializes the look-up tables of the Tier-1 coder/decoder
1288 * @return a new T1 handle if successful, returns NULL otherwise
1290 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1292 opj_t1_t *l_t1 = 00;
1294 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1299 /* create MQC and RAW handles */
1300 l_t1->mqc = opj_mqc_create();
1302 opj_t1_destroy(l_t1);
1306 l_t1->raw = opj_raw_create();
1308 opj_t1_destroy(l_t1);
1311 l_t1->encoder = isEncoder;
1318 * Destroys a previously created T1 handle
1320 * @param p_t1 Tier 1 handle to destroy
1322 void opj_t1_destroy(opj_t1_t *p_t1)
1328 /* destroy MQC and RAW handles */
1329 opj_mqc_destroy(p_t1->mqc);
1331 opj_raw_destroy(p_t1->raw);
1334 /* encoder uses tile buffer, so no need to free */
1335 if (!p_t1->encoder && p_t1->data) {
1336 opj_aligned_free(p_t1->data);
1341 opj_aligned_free(p_t1->flags);
1348 OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
1349 opj_tcd_tilecomp_t* tilec,
1353 OPJ_UINT32 resno, bandno, precno, cblkno;
1354 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1356 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1357 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1359 for (bandno = 0; bandno < res->numbands; ++bandno) {
1360 opj_tcd_band_t* restrict band = &res->bands[bandno];
1362 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1363 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1365 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1366 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1367 OPJ_INT32* restrict datap;
1368 OPJ_UINT32 cblk_w, cblk_h;
1372 if (OPJ_FALSE == opj_t1_decode_cblk(
1376 (OPJ_UINT32)tccp->roishift,
1381 x = cblk->x0 - band->x0;
1382 y = cblk->y0 - band->y0;
1383 if (band->bandno & 1) {
1384 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1385 x += pres->x1 - pres->x0;
1387 if (band->bandno & 2) {
1388 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1389 y += pres->y1 - pres->y0;
1396 if (tccp->roishift) {
1397 OPJ_INT32 thresh = 1 << tccp->roishift;
1398 for (j = 0; j < cblk_h; ++j) {
1399 for (i = 0; i < cblk_w; ++i) {
1400 OPJ_INT32 val = datap[(j * cblk_w) + i];
1401 OPJ_INT32 mag = abs(val);
1402 if (mag >= thresh) {
1403 mag >>= tccp->roishift;
1404 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1409 if (tccp->qmfbid == 1) {
1410 OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1411 for (j = 0; j < cblk_h; ++j) {
1412 for (i = 0; i < cblk_w; ++i) {
1413 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1414 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp/2;
1417 } else { /* if (tccp->qmfbid == 0) */
1418 OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1419 for (j = 0; j < cblk_h; ++j) {
1420 OPJ_FLOAT32* restrict tiledp2 = tiledp;
1421 for (i = 0; i < cblk_w; ++i) {
1422 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1438 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1439 opj_tcd_cblk_dec_t* cblk,
1441 OPJ_UINT32 roishift,
1444 opj_raw_t *raw = t1->raw; /* RAW component */
1445 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1447 OPJ_INT32 bpno_plus_one;
1448 OPJ_UINT32 passtype;
1449 OPJ_UINT32 segno, passno;
1450 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1452 if(!opj_t1_allocate_buffers(
1454 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1455 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1460 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1463 opj_mqc_resetstates(mqc);
1464 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1465 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1466 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1468 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1469 opj_tcd_seg_t *seg = &cblk->segs[segno];
1472 type = ((bpno_plus_one <= ((OPJ_INT32) (cblk->numbps)) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1473 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1474 if(seg->data == 00){
1477 if (type == T1_TYPE_RAW) {
1478 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1480 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1485 if( t1->w == 64 && t1->h == 64 )
1487 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1490 if (type == T1_TYPE_RAW) {
1491 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1493 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1494 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one, (OPJ_INT32)orient);
1496 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one, (OPJ_INT32)orient);
1501 if (type == T1_TYPE_RAW) {
1502 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1504 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1505 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1507 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
1512 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1516 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1517 opj_mqc_resetstates(mqc);
1518 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1519 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1520 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1522 if (++passtype == 3) {
1530 for (passno = 0; (passno < seg->real_num_passes) && (bpno_plus_one >= 1); ++passno) {
1533 if (type == T1_TYPE_RAW) {
1534 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1536 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1537 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one, (OPJ_INT32)orient);
1539 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one, (OPJ_INT32)orient);
1544 if (type == T1_TYPE_RAW) {
1545 opj_t1_dec_refpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1547 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1548 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
1550 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
1555 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1559 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1560 opj_mqc_resetstates(mqc);
1561 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1562 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1563 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1565 if (++passtype == 3) {
1578 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1579 opj_tcd_tile_t *tile,
1581 const OPJ_FLOAT64 * mct_norms,
1582 OPJ_UINT32 mct_numcomps
1585 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1587 tile->distotile = 0; /* fixed_quality */
1589 for (compno = 0; compno < tile->numcomps; ++compno) {
1590 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1591 opj_tccp_t* tccp = &tcp->tccps[compno];
1592 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1594 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1595 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1597 for (bandno = 0; bandno < res->numbands; ++bandno) {
1598 opj_tcd_band_t* restrict band = &res->bands[bandno];
1599 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1601 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1602 opj_tcd_precinct_t *prc = &band->precincts[precno];
1604 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1605 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1606 OPJ_INT32* restrict tiledp;
1609 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1611 OPJ_INT32 x = cblk->x0 - band->x0;
1612 OPJ_INT32 y = cblk->y0 - band->y0;
1613 if (band->bandno & 1) {
1614 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1615 x += pres->x1 - pres->x0;
1617 if (band->bandno & 2) {
1618 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1619 y += pres->y1 - pres->y0;
1622 if(!opj_t1_allocate_buffers(
1624 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1625 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1632 tileLineAdvance = tile_w - cblk_w;
1634 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1636 t1->data_stride = tile_w;
1637 if (tccp->qmfbid == 1) {
1638 for (j = 0; j < cblk_h; ++j) {
1639 for (i = 0; i < cblk_w; ++i) {
1640 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1643 tileIndex += tileLineAdvance;
1645 } else { /* if (tccp->qmfbid == 0) */
1646 for (j = 0; j < cblk_h; ++j) {
1647 for (i = 0; i < cblk_w; ++i) {
1648 OPJ_INT32 tmp = tiledp[tileIndex];
1655 tileIndex += tileLineAdvance;
1664 tilec->numresolutions - 1 - resno,
1681 /** mod fixed_quality */
1682 static void opj_t1_encode_cblk(opj_t1_t *t1,
1683 opj_tcd_cblk_enc_t* cblk,
1688 OPJ_FLOAT64 stepsize,
1690 OPJ_UINT32 numcomps,
1691 opj_tcd_tile_t * tile,
1692 const OPJ_FLOAT64 * mct_norms,
1693 OPJ_UINT32 mct_numcomps)
1695 OPJ_FLOAT64 cumwmsedec = 0.0;
1697 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1701 OPJ_UINT32 passtype;
1702 OPJ_INT32 nmsedec = 0;
1705 OPJ_BYTE type = T1_TYPE_MQ;
1706 OPJ_FLOAT64 tempwmsedec;
1709 for (i = 0; i < t1->w; ++i) {
1710 for (j = 0; j < t1->h; ++j) {
1711 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1712 max = opj_int_max(max, tmp);
1716 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1718 bpno = (OPJ_INT32)(cblk->numbps - 1);
1721 opj_mqc_resetstates(mqc);
1722 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1723 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1724 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1725 opj_mqc_init_enc(mqc, cblk->data);
1727 for (passno = 0; bpno >= 0; ++passno) {
1728 opj_tcd_pass_t *pass = &cblk->passes[passno];
1729 OPJ_UINT32 correction = 3;
1730 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1734 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1737 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1740 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1741 /* code switch SEGMARK (i.e. SEGSYM) */
1742 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1743 opj_mqc_segmark_enc(mqc);
1748 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
1749 cumwmsedec += tempwmsedec;
1750 tile->distotile += tempwmsedec;
1752 /* Code switch "RESTART" (i.e. TERMALL) */
1753 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1754 if (type == T1_TYPE_RAW) {
1757 /* correction = mqc_bypass_flush_enc(); */
1758 } else { /* correction = mqc_restart_enc(); */
1764 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1765 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1766 if (type == T1_TYPE_RAW) {
1769 /* correction = mqc_bypass_flush_enc(); */
1770 } else { /* correction = mqc_restart_enc(); */
1780 if (++passtype == 3) {
1785 if (pass->term && bpno > 0) {
1786 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1787 if (type == T1_TYPE_RAW)
1788 opj_mqc_bypass_init_enc(mqc);
1790 opj_mqc_restart_init_enc(mqc);
1793 pass->distortiondec = cumwmsedec;
1794 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1796 /* Code-switch "RESET" */
1797 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1798 opj_mqc_reset_enc(mqc);
1801 /* Code switch "ERTERM" (i.e. PTERM) */
1802 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1803 opj_mqc_erterm_enc(mqc);
1804 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1807 cblk->totalpasses = passno;
1809 for (passno = 0; passno<cblk->totalpasses; passno++) {
1810 opj_tcd_pass_t *pass = &cblk->passes[passno];
1811 if (pass->rate > opj_mqc_numbytes(mqc))
1812 pass->rate = opj_mqc_numbytes(mqc);
1813 /*Preventing generation of FF as last data byte of a pass*/
1814 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1817 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1822 static void opj_t1_dec_refpass_step( opj_t1_t *t1,
1833 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1834 opj_raw_t *raw = t1->raw; /* RAW component */
1836 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1837 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1838 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1839 if (type == T1_TYPE_RAW) {
1840 v = opj_raw_decode(raw);
1842 v = opj_mqc_decode(mqc);
1844 t = v ? poshalf : neghalf;
1845 *datap += *datap < 0 ? -t : t;
1846 *flagsp |= T1_REFINE;
1848 } /* VSC and BYPASS by Antonin */
1854 static void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1858 OPJ_INT32 oneplushalf,
1864 opj_raw_t *raw = t1->raw; /* RAW component */
1865 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1867 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1868 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1869 if (type == T1_TYPE_RAW) {
1870 if (opj_raw_decode(raw)) {
1871 v = opj_raw_decode(raw); /* ESSAI */
1872 *datap = v ? -oneplushalf : oneplushalf;
1873 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1876 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1877 if (opj_mqc_decode(mqc)) {
1878 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1879 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1880 *datap = v ? -oneplushalf : oneplushalf;
1881 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1884 *flagsp |= T1_VISIT;
1886 } /* VSC and BYPASS by Antonin */