2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
42 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
45 /** @name Local static functions */
48 static INLINE OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
49 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
50 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
51 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
52 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
53 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
54 static void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride);
56 Encode significant pass
58 static void opj_t1_enc_sigpass_step(opj_t1_t *t1,
69 Decode significant pass
72 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
76 OPJ_INT32 oneplushalf,
81 static INLINE void opj_t1_dec_sigpass_step_raw(
86 OPJ_INT32 oneplushalf,
88 static INLINE void opj_t1_dec_sigpass_step_mqc(
93 OPJ_INT32 oneplushalf);
94 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
99 OPJ_INT32 oneplushalf,
104 Encode significant pass
106 static void opj_t1_enc_sigpass( opj_t1_t *t1,
114 Decode significant pass
116 static void opj_t1_dec_sigpass_raw(
121 static void opj_t1_dec_sigpass_mqc(
125 static void opj_t1_dec_sigpass_mqc_vsc(
133 Encode refinement pass
135 static void opj_t1_enc_refpass_step(opj_t1_t *t1,
146 Encode refinement pass
148 static void opj_t1_enc_refpass( opj_t1_t *t1,
155 Decode refinement pass
157 static void opj_t1_dec_refpass_raw(
161 static void opj_t1_dec_refpass_mqc(
164 static void opj_t1_dec_refpass_mqc_vsc(
170 Decode refinement pass
173 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
182 static INLINE void opj_t1_dec_refpass_step_raw(
189 static INLINE void opj_t1_dec_refpass_step_mqc(
195 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
208 static void opj_t1_enc_clnpass_step(
221 static void opj_t1_dec_clnpass_step_partial(
226 OPJ_INT32 oneplushalf);
227 static void opj_t1_dec_clnpass_step(
232 OPJ_INT32 oneplushalf);
233 static void opj_t1_dec_clnpass_step_vsc(
238 OPJ_INT32 oneplushalf,
244 static void opj_t1_enc_clnpass(
253 static void opj_t1_dec_clnpass(
259 static OPJ_FLOAT64 opj_t1_getwmsedec(
266 OPJ_FLOAT64 stepsize,
268 const OPJ_FLOAT64 * mct_norms,
269 OPJ_UINT32 mct_numcomps);
271 static void opj_t1_encode_cblk( opj_t1_t *t1,
272 opj_tcd_cblk_enc_t* cblk,
277 OPJ_FLOAT64 stepsize,
280 opj_tcd_tile_t * tile,
281 const OPJ_FLOAT64 * mct_norms,
282 OPJ_UINT32 mct_numcomps);
287 @param cblk Code-block coding parameters
289 @param roishift Region of interest shifting value
290 @param cblksty Code-block style
292 static OPJ_BOOL opj_t1_decode_cblk( opj_t1_t *t1,
293 opj_tcd_cblk_dec_t* cblk,
298 OPJ_BOOL opj_t1_allocate_buffers( opj_t1_t *t1,
306 /* ----------------------------------------------------------------------- */
308 OPJ_BYTE opj_t1_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient) {
309 return lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
312 OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f) {
313 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
316 OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f) {
317 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
318 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
322 OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f) {
323 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
326 OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
327 if (bitpos > T1_NMSEDEC_FRACBITS) {
328 return lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
331 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
334 OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos) {
335 if (bitpos > T1_NMSEDEC_FRACBITS) {
336 return lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
339 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
342 void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s, OPJ_UINT32 stride) {
343 opj_flag_t *np = flagsp - stride;
344 opj_flag_t *sp = flagsp + stride;
346 static const opj_flag_t mod[] = {
347 T1_SIG_S, T1_SIG_S|T1_SGN_S,
348 T1_SIG_E, T1_SIG_E|T1_SGN_E,
349 T1_SIG_W, T1_SIG_W|T1_SGN_W,
350 T1_SIG_N, T1_SIG_N|T1_SGN_N
357 flagsp[-1] |= mod[s+2];
359 flagsp[1] |= mod[s+4];
366 void opj_t1_enc_sigpass_step( opj_t1_t *t1,
380 opj_mqc_t *mqc = t1->mqc; /* MQC component */
382 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
383 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
384 v = opj_int_abs(*datap) & one ? 1 : 0;
385 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient)); /* ESSAI */
386 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
387 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
389 opj_mqc_encode(mqc, (OPJ_UINT32)v);
392 v = *datap < 0 ? 1 : 0;
393 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
394 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag)); /* ESSAI */
395 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
396 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
398 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
400 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
407 static INLINE void opj_t1_dec_sigpass_step_raw(
412 OPJ_INT32 oneplushalf,
416 opj_raw_t *raw = t1->raw; /* RAW component */
417 OPJ_ARG_NOT_USED(orient);
419 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
420 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
421 if (opj_raw_decode(raw)) {
422 v = (OPJ_INT32)opj_raw_decode(raw); /* ESSAI */
423 *datap = v ? -oneplushalf : oneplushalf;
424 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
430 INLINE void opj_t1_dec_sigpass_step_mqc(
435 OPJ_INT32 oneplushalf)
439 opj_mqc_t *mqc = t1->mqc; /* MQC component */
442 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
443 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
444 if (opj_mqc_decode(mqc)) {
445 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
446 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
447 *datap = v ? -oneplushalf : oneplushalf;
448 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
452 } /* VSC and BYPASS by Antonin */
454 INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
459 OPJ_INT32 oneplushalf,
464 opj_mqc_t *mqc = t1->mqc; /* MQC component */
466 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
467 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
468 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
469 if (opj_mqc_decode(mqc)) {
470 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
471 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
472 *datap = v ? -oneplushalf : oneplushalf;
473 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
477 } /* VSC and BYPASS by Antonin */
481 void opj_t1_enc_sigpass(opj_t1_t *t1,
489 OPJ_UINT32 i, j, k, vsc;
493 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
494 for (k = 0; k < t1->h; k += 4) {
495 for (i = 0; i < t1->w; ++i) {
496 for (j = k; j < k + 4 && j < t1->h; ++j) {
497 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
498 opj_t1_enc_sigpass_step(
500 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
501 &t1->data[(j * t1->data_stride) + i],
513 void opj_t1_dec_sigpass_raw(
519 OPJ_INT32 one, half, oneplushalf, vsc;
523 oneplushalf = one | half;
524 for (k = 0; k < t1->h; k += 4) {
525 for (i = 0; i < t1->w; ++i) {
526 for (j = k; j < k + 4 && j < t1->h; ++j) {
527 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
528 opj_t1_dec_sigpass_step_raw(
530 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
531 &t1->data[(j * t1->w) + i],
538 } /* VSC and BYPASS by Antonin */
540 void opj_t1_dec_sigpass_mqc(
545 OPJ_INT32 one, half, oneplushalf;
547 OPJ_INT32 *data1 = t1->data;
548 opj_flag_t *flags1 = &t1->flags[1];
551 oneplushalf = one | half;
552 for (k = 0; k < (t1->h & ~3u); k += 4) {
553 for (i = 0; i < t1->w; ++i) {
554 OPJ_INT32 *data2 = data1 + i;
555 opj_flag_t *flags2 = flags1 + i;
556 flags2 += t1->flags_stride;
557 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
559 flags2 += t1->flags_stride;
560 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
562 flags2 += t1->flags_stride;
563 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
565 flags2 += t1->flags_stride;
566 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
570 flags1 += t1->flags_stride << 2;
572 for (i = 0; i < t1->w; ++i) {
573 OPJ_INT32 *data2 = data1 + i;
574 opj_flag_t *flags2 = flags1 + i;
575 for (j = k; j < t1->h; ++j) {
576 flags2 += t1->flags_stride;
577 opj_t1_dec_sigpass_step_mqc(t1, flags2, data2, orient, oneplushalf);
581 } /* VSC and BYPASS by Antonin */
583 void opj_t1_dec_sigpass_mqc_vsc(
588 OPJ_INT32 one, half, oneplushalf, vsc;
592 oneplushalf = one | half;
593 for (k = 0; k < t1->h; k += 4) {
594 for (i = 0; i < t1->w; ++i) {
595 for (j = k; j < k + 4 && j < t1->h; ++j) {
596 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
597 opj_t1_dec_sigpass_step_mqc_vsc(
599 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
600 &t1->data[(j * t1->w) + i],
607 } /* VSC and BYPASS by Antonin */
611 void opj_t1_enc_refpass_step( opj_t1_t *t1,
623 opj_mqc_t *mqc = t1->mqc; /* MQC component */
625 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
626 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
627 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
628 v = opj_int_abs(*datap) & one ? 1 : 0;
629 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
630 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
631 opj_mqc_bypass_enc(mqc, (OPJ_UINT32)v);
633 opj_mqc_encode(mqc, (OPJ_UINT32)v);
635 *flagsp |= T1_REFINE;
639 INLINE void opj_t1_dec_refpass_step_raw(
647 OPJ_INT32 v, t, flag;
649 opj_raw_t *raw = t1->raw; /* RAW component */
651 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
652 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
653 v = (OPJ_INT32)opj_raw_decode(raw);
654 t = v ? poshalf : neghalf;
655 *datap += *datap < 0 ? -t : t;
656 *flagsp |= T1_REFINE;
658 } /* VSC and BYPASS by Antonin */
660 INLINE void opj_t1_dec_refpass_step_mqc(
667 OPJ_INT32 v, t, flag;
669 opj_mqc_t *mqc = t1->mqc; /* MQC component */
672 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
673 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
674 v = opj_mqc_decode(mqc);
675 t = v ? poshalf : neghalf;
676 *datap += *datap < 0 ? -t : t;
677 *flagsp |= T1_REFINE;
679 } /* VSC and BYPASS by Antonin */
681 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
689 OPJ_INT32 v, t, flag;
691 opj_mqc_t *mqc = t1->mqc; /* MQC component */
693 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
694 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
695 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag((OPJ_UINT32)flag)); /* ESSAI */
696 v = opj_mqc_decode(mqc);
697 t = v ? poshalf : neghalf;
698 *datap += *datap < 0 ? -t : t;
699 *flagsp |= T1_REFINE;
701 } /* VSC and BYPASS by Antonin */
704 void opj_t1_enc_refpass(
711 OPJ_UINT32 i, j, k, vsc;
715 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
716 for (k = 0; k < t1->h; k += 4) {
717 for (i = 0; i < t1->w; ++i) {
718 for (j = k; j < k + 4 && j < t1->h; ++j) {
719 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
720 opj_t1_enc_refpass_step(
722 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
723 &t1->data[(j * t1->data_stride) + i],
734 void opj_t1_dec_refpass_raw(
739 OPJ_INT32 one, poshalf, neghalf;
744 neghalf = bpno > 0 ? -poshalf : -1;
745 for (k = 0; k < t1->h; k += 4) {
746 for (i = 0; i < t1->w; ++i) {
747 for (j = k; j < k + 4 && j < t1->h; ++j) {
748 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
749 opj_t1_dec_refpass_step_raw(
751 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
752 &t1->data[(j * t1->w) + i],
759 } /* VSC and BYPASS by Antonin */
761 void opj_t1_dec_refpass_mqc(
765 OPJ_INT32 one, poshalf, neghalf;
767 OPJ_INT32 *data1 = t1->data;
768 opj_flag_t *flags1 = &t1->flags[1];
771 neghalf = bpno > 0 ? -poshalf : -1;
772 for (k = 0; k < (t1->h & ~3u); k += 4) {
773 for (i = 0; i < t1->w; ++i) {
774 OPJ_INT32 *data2 = data1 + i;
775 opj_flag_t *flags2 = flags1 + i;
776 flags2 += t1->flags_stride;
777 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
779 flags2 += t1->flags_stride;
780 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
782 flags2 += t1->flags_stride;
783 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
785 flags2 += t1->flags_stride;
786 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
790 flags1 += t1->flags_stride << 2;
792 for (i = 0; i < t1->w; ++i) {
793 OPJ_INT32 *data2 = data1 + i;
794 opj_flag_t *flags2 = flags1 + i;
795 for (j = k; j < t1->h; ++j) {
796 flags2 += t1->flags_stride;
797 opj_t1_dec_refpass_step_mqc(t1, flags2, data2, poshalf, neghalf);
801 } /* VSC and BYPASS by Antonin */
803 void opj_t1_dec_refpass_mqc_vsc(
807 OPJ_INT32 one, poshalf, neghalf;
812 neghalf = bpno > 0 ? -poshalf : -1;
813 for (k = 0; k < t1->h; k += 4) {
814 for (i = 0; i < t1->w; ++i) {
815 for (j = k; j < k + 4 && j < t1->h; ++j) {
816 vsc = ((j == k + 3 || j == t1->h - 1)) ? 1 : 0;
817 opj_t1_dec_refpass_step_mqc_vsc(
819 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
820 &t1->data[(j * t1->w) + i],
827 } /* VSC and BYPASS by Antonin */
830 void opj_t1_enc_clnpass_step(
844 opj_mqc_t *mqc = t1->mqc; /* MQC component */
846 flag = vsc ? (OPJ_UINT32)((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (OPJ_UINT32)(*flagsp);
850 if (!(*flagsp & (T1_SIG | T1_VISIT))) {
851 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
852 v = opj_int_abs(*datap) & one ? 1 : 0;
853 opj_mqc_encode(mqc, (OPJ_UINT32)v);
856 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap), (OPJ_UINT32)(bpno + T1_NMSEDEC_FRACBITS));
857 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
858 v = *datap < 0 ? 1 : 0;
859 opj_mqc_encode(mqc, (OPJ_UINT32)(v ^ opj_t1_getspb((OPJ_UINT32)flag)));
860 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
863 *flagsp &= ~T1_VISIT;
866 static void opj_t1_dec_clnpass_step_partial(
871 OPJ_INT32 oneplushalf)
874 opj_mqc_t *mqc = t1->mqc; /* MQC component */
876 OPJ_ARG_NOT_USED(orient);
879 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
880 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
881 *datap = v ? -oneplushalf : oneplushalf;
882 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
883 *flagsp &= ~T1_VISIT;
884 } /* VSC and BYPASS by Antonin */
886 static void opj_t1_dec_clnpass_step(
891 OPJ_INT32 oneplushalf)
895 opj_mqc_t *mqc = t1->mqc; /* MQC component */
898 if (!(flag & (T1_SIG | T1_VISIT))) {
899 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
900 if (opj_mqc_decode(mqc)) {
901 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
902 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
903 *datap = v ? -oneplushalf : oneplushalf;
904 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
907 *flagsp &= ~T1_VISIT;
908 } /* VSC and BYPASS by Antonin */
910 static void opj_t1_dec_clnpass_step_vsc(
915 OPJ_INT32 oneplushalf,
921 opj_mqc_t *mqc = t1->mqc; /* MQC component */
923 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
927 if (!(flag & (T1_SIG | T1_VISIT))) {
928 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc((OPJ_UINT32)flag, (OPJ_UINT32)orient));
929 if (opj_mqc_decode(mqc)) {
931 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
932 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
933 *datap = v ? -oneplushalf : oneplushalf;
934 opj_t1_updateflags(flagsp, (OPJ_UINT32)v, t1->flags_stride);
937 *flagsp &= ~T1_VISIT;
940 void opj_t1_enc_clnpass(
949 OPJ_UINT32 agg, runlen, vsc;
951 opj_mqc_t *mqc = t1->mqc; /* MQC component */
954 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
955 for (k = 0; k < t1->h; k += 4) {
956 for (i = 0; i < t1->w; ++i) {
958 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
959 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
960 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
961 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
962 || (MACRO_t1_flags(1 + k + 3,1 + i)
963 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
965 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
966 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
967 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
968 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
974 for (runlen = 0; runlen < 4; ++runlen) {
975 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one)
978 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
979 opj_mqc_encode(mqc, runlen != 4);
983 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
984 opj_mqc_encode(mqc, runlen >> 1);
985 opj_mqc_encode(mqc, runlen & 1);
989 for (j = k + runlen; j < k + 4 && j < t1->h; ++j) {
990 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == t1->h - 1)) ? 1 : 0;
991 opj_t1_enc_clnpass_step(
993 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
994 &t1->data[(j * t1->data_stride) + i],
999 agg && (j == k + runlen),
1006 static void opj_t1_dec_clnpass(
1012 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc;
1014 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
1016 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1020 oneplushalf = one | half;
1021 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1022 for (k = 0; k < t1->h; k += 4) {
1023 for (i = 0; i < t1->w; ++i) {
1024 if (k + 3 < t1->h) {
1025 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1026 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1027 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1028 || (MACRO_t1_flags(1 + k + 3,1 + i)
1029 & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1034 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1035 if (!opj_mqc_decode(mqc)) {
1038 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1039 runlen = opj_mqc_decode(mqc);
1040 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1044 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1045 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
1046 opj_t1_dec_clnpass_step_vsc(
1048 &t1->flags[((j+1) * t1->flags_stride) + i + 1],
1049 &t1->data[(j * t1->w) + i],
1052 agg && (j == k + (OPJ_UINT32)runlen),
1058 OPJ_INT32 *data1 = t1->data;
1059 opj_flag_t *flags1 = &t1->flags[1];
1060 for (k = 0; k < (t1->h & ~3u); k += 4) {
1061 for (i = 0; i < t1->w; ++i) {
1062 OPJ_INT32 *data2 = data1 + i;
1063 opj_flag_t *flags2 = flags1 + i;
1064 agg = !(MACRO_t1_flags(1 + k,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1065 || MACRO_t1_flags(1 + k + 1,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1066 || MACRO_t1_flags(1 + k + 2,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH)
1067 || MACRO_t1_flags(1 + k + 3,1 + i) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
1069 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1070 if (!opj_mqc_decode(mqc)) {
1073 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1074 runlen = opj_mqc_decode(mqc);
1075 runlen = (runlen << 1) | opj_mqc_decode(mqc);
1076 flags2 += (OPJ_UINT32)runlen * t1->flags_stride;
1077 data2 += (OPJ_UINT32)runlen * t1->w;
1078 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < t1->h; ++j) {
1079 flags2 += t1->flags_stride;
1080 if (agg && (j == k + (OPJ_UINT32)runlen)) {
1081 opj_t1_dec_clnpass_step_partial(t1, flags2, data2, orient, oneplushalf);
1083 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1088 flags2 += t1->flags_stride;
1089 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1091 flags2 += t1->flags_stride;
1092 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1094 flags2 += t1->flags_stride;
1095 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1097 flags2 += t1->flags_stride;
1098 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1102 data1 += t1->w << 2;
1103 flags1 += t1->flags_stride << 2;
1105 for (i = 0; i < t1->w; ++i) {
1106 OPJ_INT32 *data2 = data1 + i;
1107 opj_flag_t *flags2 = flags1 + i;
1108 for (j = k; j < t1->h; ++j) {
1109 flags2 += t1->flags_stride;
1110 opj_t1_dec_clnpass_step(t1, flags2, data2, orient, oneplushalf);
1118 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1119 v = opj_mqc_decode(mqc);
1120 v = (v << 1) | opj_mqc_decode(mqc);
1121 v = (v << 1) | opj_mqc_decode(mqc);
1122 v = (v << 1) | opj_mqc_decode(mqc);
1125 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1129 } /* VSC and BYPASS by Antonin */
1132 /** mod fixed_quality */
1133 static OPJ_FLOAT64 opj_t1_getwmsedec(
1140 OPJ_FLOAT64 stepsize,
1141 OPJ_UINT32 numcomps,
1142 const OPJ_FLOAT64 * mct_norms,
1143 OPJ_UINT32 mct_numcomps)
1145 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1146 OPJ_ARG_NOT_USED(numcomps);
1148 if (mct_norms && (compno < mct_numcomps)) {
1149 w1 = mct_norms[compno];
1153 w2 = opj_dwt_getnorm(level, orient);
1154 } else { /* if (qmfbid == 0) */
1155 w2 = opj_dwt_getnorm_real(level, orient);
1158 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1159 wmsedec *= wmsedec * nmsedec / 8192.0;
1164 OPJ_BOOL opj_t1_allocate_buffers(
1169 OPJ_UINT32 datasize=w * h;
1170 OPJ_UINT32 flagssize;
1172 /* encoder uses tile buffer, so no need to allocate */
1174 if(datasize > t1->datasize){
1175 opj_aligned_free(t1->data);
1176 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1178 /* FIXME event manager error callback */
1181 t1->datasize=datasize;
1183 memset(t1->data,0,datasize * sizeof(OPJ_INT32));
1185 t1->flags_stride=w+2;
1186 flagssize=t1->flags_stride * (h+2);
1188 if(flagssize > t1->flagssize){
1189 opj_aligned_free(t1->flags);
1190 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1192 /* FIXME event manager error callback */
1195 t1->flagssize=flagssize;
1197 memset(t1->flags,0,flagssize * sizeof(opj_flag_t));
1205 /* ----------------------------------------------------------------------- */
1207 /* ----------------------------------------------------------------------- */
1209 * Creates a new Tier 1 handle
1210 * and initializes the look-up tables of the Tier-1 coder/decoder
1211 * @return a new T1 handle if successful, returns NULL otherwise
1213 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1215 opj_t1_t *l_t1 = 00;
1217 l_t1 = (opj_t1_t*) opj_calloc(1,sizeof(opj_t1_t));
1222 /* create MQC and RAW handles */
1223 l_t1->mqc = opj_mqc_create();
1225 opj_t1_destroy(l_t1);
1229 l_t1->raw = opj_raw_create();
1231 opj_t1_destroy(l_t1);
1234 l_t1->encoder = isEncoder;
1241 * Destroys a previously created T1 handle
1243 * @param p_t1 Tier 1 handle to destroy
1245 void opj_t1_destroy(opj_t1_t *p_t1)
1251 /* destroy MQC and RAW handles */
1252 opj_mqc_destroy(p_t1->mqc);
1254 opj_raw_destroy(p_t1->raw);
1257 /* encoder uses tile buffer, so no need to free */
1258 if (!p_t1->encoder && p_t1->data) {
1259 opj_aligned_free(p_t1->data);
1264 opj_aligned_free(p_t1->flags);
1271 OPJ_BOOL opj_t1_decode_cblks( opj_t1_t* t1,
1272 opj_tcd_tilecomp_t* tilec,
1276 OPJ_UINT32 resno, bandno, precno, cblkno;
1277 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1279 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1280 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1282 for (bandno = 0; bandno < res->numbands; ++bandno) {
1283 opj_tcd_band_t* restrict band = &res->bands[bandno];
1285 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1286 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1288 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1289 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1290 OPJ_INT32* restrict datap;
1291 /*void* restrict tiledp;*/
1292 OPJ_UINT32 cblk_w, cblk_h;
1296 if (OPJ_FALSE == opj_t1_decode_cblk(
1300 (OPJ_UINT32)tccp->roishift,
1305 x = cblk->x0 - band->x0;
1306 y = cblk->y0 - band->y0;
1307 if (band->bandno & 1) {
1308 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1309 x += pres->x1 - pres->x0;
1311 if (band->bandno & 2) {
1312 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1313 y += pres->y1 - pres->y0;
1320 if (tccp->roishift) {
1321 OPJ_INT32 thresh = 1 << tccp->roishift;
1322 for (j = 0; j < cblk_h; ++j) {
1323 for (i = 0; i < cblk_w; ++i) {
1324 OPJ_INT32 val = datap[(j * cblk_w) + i];
1325 OPJ_INT32 mag = abs(val);
1326 if (mag >= thresh) {
1327 mag >>= tccp->roishift;
1328 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1334 /*tiledp=(void*)&tilec->data[(y * tile_w) + x];*/
1335 if (tccp->qmfbid == 1) {
1336 OPJ_INT32* restrict tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1337 for (j = 0; j < cblk_h; ++j) {
1338 for (i = 0; i < cblk_w; ++i) {
1339 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1340 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1343 } else { /* if (tccp->qmfbid == 0) */
1344 OPJ_FLOAT32* restrict tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1345 for (j = 0; j < cblk_h; ++j) {
1346 OPJ_FLOAT32* restrict tiledp2 = tiledp;
1347 for (i = 0; i < cblk_w; ++i) {
1348 OPJ_FLOAT32 tmp = (OPJ_FLOAT32)*datap * band->stepsize;
1352 /*float tmp = datap[(j * cblk_w) + i] * band->stepsize;
1353 ((float*)tiledp)[(j * tile_w) + i] = tmp;*/
1359 /*opj_free(cblk->data);
1360 opj_free(cblk->segs);*/
1361 /*cblk->segs = 00;*/
1363 /*opj_free(precinct->cblks.dec);*/
1371 OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1372 opj_tcd_cblk_dec_t* cblk,
1374 OPJ_UINT32 roishift,
1377 opj_raw_t *raw = t1->raw; /* RAW component */
1378 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1381 OPJ_UINT32 passtype;
1382 OPJ_UINT32 segno, passno;
1383 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1385 if(!opj_t1_allocate_buffers(
1387 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1388 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1393 bpno = (OPJ_INT32)(roishift + cblk->numbps - 1);
1396 opj_mqc_resetstates(mqc);
1397 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1398 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1399 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1401 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1402 opj_tcd_seg_t *seg = &cblk->segs[segno];
1405 type = ((bpno <= ((OPJ_INT32) (cblk->numbps) - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1406 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1407 if(seg->data == 00){
1410 if (type == T1_TYPE_RAW) {
1411 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1413 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len)) {
1418 for (passno = 0; passno < seg->real_num_passes; ++passno) {
1421 if (type == T1_TYPE_RAW) {
1422 opj_t1_dec_sigpass_raw(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1424 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1425 opj_t1_dec_sigpass_mqc_vsc(t1, bpno+1, (OPJ_INT32)orient);
1427 opj_t1_dec_sigpass_mqc(t1, bpno+1, (OPJ_INT32)orient);
1432 if (type == T1_TYPE_RAW) {
1433 opj_t1_dec_refpass_raw(t1, bpno+1, (OPJ_INT32)cblksty);
1435 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1436 opj_t1_dec_refpass_mqc_vsc(t1, bpno+1);
1438 opj_t1_dec_refpass_mqc(t1, bpno+1);
1443 opj_t1_dec_clnpass(t1, bpno+1, (OPJ_INT32)orient, (OPJ_INT32)cblksty);
1447 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1448 opj_mqc_resetstates(mqc);
1449 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1450 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1451 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1453 if (++passtype == 3) {
1465 OPJ_BOOL opj_t1_encode_cblks( opj_t1_t *t1,
1466 opj_tcd_tile_t *tile,
1468 const OPJ_FLOAT64 * mct_norms,
1469 OPJ_UINT32 mct_numcomps
1472 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1474 tile->distotile = 0; /* fixed_quality */
1476 for (compno = 0; compno < tile->numcomps; ++compno) {
1477 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1478 opj_tccp_t* tccp = &tcp->tccps[compno];
1479 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1481 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1482 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1484 for (bandno = 0; bandno < res->numbands; ++bandno) {
1485 opj_tcd_band_t* restrict band = &res->bands[bandno];
1486 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1488 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1489 opj_tcd_precinct_t *prc = &band->precincts[precno];
1491 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1492 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1493 OPJ_INT32* restrict tiledp;
1496 OPJ_UINT32 i, j, tileIndex=0, tileLineAdvance;
1498 OPJ_INT32 x = cblk->x0 - band->x0;
1499 OPJ_INT32 y = cblk->y0 - band->y0;
1500 if (band->bandno & 1) {
1501 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1502 x += pres->x1 - pres->x0;
1504 if (band->bandno & 2) {
1505 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1506 y += pres->y1 - pres->y0;
1509 if(!opj_t1_allocate_buffers(
1511 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1512 (OPJ_UINT32)(cblk->y1 - cblk->y0)))
1519 tileLineAdvance = tile_w - cblk_w;
1521 tiledp=&tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1523 t1->data_stride = tile_w;
1524 if (tccp->qmfbid == 1) {
1525 for (j = 0; j < cblk_h; ++j) {
1526 for (i = 0; i < cblk_w; ++i) {
1527 tiledp[tileIndex] <<= T1_NMSEDEC_FRACBITS;
1530 tileIndex += tileLineAdvance;
1532 } else { /* if (tccp->qmfbid == 0) */
1533 for (j = 0; j < cblk_h; ++j) {
1534 for (i = 0; i < cblk_w; ++i) {
1535 OPJ_INT32 tmp = tiledp[tileIndex];
1539 bandconst) >> (11 - T1_NMSEDEC_FRACBITS);
1542 tileIndex += tileLineAdvance;
1551 tilec->numresolutions - 1 - resno,
1568 /** mod fixed_quality */
1569 void opj_t1_encode_cblk(opj_t1_t *t1,
1570 opj_tcd_cblk_enc_t* cblk,
1575 OPJ_FLOAT64 stepsize,
1577 OPJ_UINT32 numcomps,
1578 opj_tcd_tile_t * tile,
1579 const OPJ_FLOAT64 * mct_norms,
1580 OPJ_UINT32 mct_numcomps)
1582 OPJ_FLOAT64 cumwmsedec = 0.0;
1584 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1588 OPJ_UINT32 passtype;
1589 OPJ_INT32 nmsedec = 0;
1592 OPJ_BYTE type = T1_TYPE_MQ;
1593 OPJ_FLOAT64 tempwmsedec;
1596 for (i = 0; i < t1->w; ++i) {
1597 for (j = 0; j < t1->h; ++j) {
1598 OPJ_INT32 tmp = abs(t1->data[i + j*t1->data_stride]);
1599 max = opj_int_max(max, tmp);
1603 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS) : 0;
1605 bpno = (OPJ_INT32)(cblk->numbps - 1);
1608 opj_mqc_resetstates(mqc);
1609 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1610 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1611 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1612 opj_mqc_init_enc(mqc, cblk->data);
1614 for (passno = 0; bpno >= 0; ++passno) {
1615 opj_tcd_pass_t *pass = &cblk->passes[passno];
1616 OPJ_UINT32 correction = 3;
1617 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1621 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
1624 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
1627 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
1628 /* code switch SEGMARK (i.e. SEGSYM) */
1629 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
1630 opj_mqc_segmark_enc(mqc);
1635 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps,mct_norms, mct_numcomps) ;
1636 cumwmsedec += tempwmsedec;
1637 tile->distotile += tempwmsedec;
1639 /* Code switch "RESTART" (i.e. TERMALL) */
1640 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
1641 if (type == T1_TYPE_RAW) {
1644 /* correction = mqc_bypass_flush_enc(); */
1645 } else { /* correction = mqc_restart_enc(); */
1651 if (((bpno < ((OPJ_INT32) (cblk->numbps) - 4) && (passtype > 0))
1652 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1653 if (type == T1_TYPE_RAW) {
1656 /* correction = mqc_bypass_flush_enc(); */
1657 } else { /* correction = mqc_restart_enc(); */
1667 if (++passtype == 3) {
1672 if (pass->term && bpno > 0) {
1673 type = ((bpno < ((OPJ_INT32) (cblk->numbps) - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1674 if (type == T1_TYPE_RAW)
1675 opj_mqc_bypass_init_enc(mqc);
1677 opj_mqc_restart_init_enc(mqc);
1680 pass->distortiondec = cumwmsedec;
1681 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
1683 /* Code-switch "RESET" */
1684 if (cblksty & J2K_CCP_CBLKSTY_RESET)
1685 opj_mqc_reset_enc(mqc);
1688 /* Code switch "ERTERM" (i.e. PTERM) */
1689 if (cblksty & J2K_CCP_CBLKSTY_PTERM)
1690 opj_mqc_erterm_enc(mqc);
1691 else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
1694 cblk->totalpasses = passno;
1696 for (passno = 0; passno<cblk->totalpasses; passno++) {
1697 opj_tcd_pass_t *pass = &cblk->passes[passno];
1698 if (pass->rate > opj_mqc_numbytes(mqc))
1699 pass->rate = opj_mqc_numbytes(mqc);
1700 /*Preventing generation of FF as last data byte of a pass*/
1701 if((pass->rate>1) && (cblk->data[pass->rate - 1] == 0xFF)){
1704 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
1709 void opj_t1_dec_refpass_step( opj_t1_t *t1,
1720 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1721 opj_raw_t *raw = t1->raw; /* RAW component */
1723 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1724 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
1725 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
1726 if (type == T1_TYPE_RAW) {
1727 v = opj_raw_decode(raw);
1729 v = opj_mqc_decode(mqc);
1731 t = v ? poshalf : neghalf;
1732 *datap += *datap < 0 ? -t : t;
1733 *flagsp |= T1_REFINE;
1735 } /* VSC and BYPASS by Antonin */
1741 void opj_t1_dec_sigpass_step( opj_t1_t *t1,
1745 OPJ_INT32 oneplushalf,
1751 opj_raw_t *raw = t1->raw; /* RAW component */
1752 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1754 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*flagsp);
1755 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
1756 if (type == T1_TYPE_RAW) {
1757 if (opj_raw_decode(raw)) {
1758 v = opj_raw_decode(raw); /* ESSAI */
1759 *datap = v ? -oneplushalf : oneplushalf;
1760 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1763 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
1764 if (opj_mqc_decode(mqc)) {
1765 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1766 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1767 *datap = v ? -oneplushalf : oneplushalf;
1768 opj_t1_updateflags(flagsp, v, t1->flags_stride);
1771 *flagsp |= T1_VISIT;
1773 } /* VSC and BYPASS by Antonin */