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 * Copyright (c) 2012, Carl Hetherington
16 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
44 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
47 #define T1_FLAGS(x, y) (t1->flags[x + 1 + ((y / 4) + 1) * (t1->w+2)])
49 #define opj_t1_setcurctx(curctx, ctxno) curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
51 /** @name Local static functions */
54 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
55 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
56 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
57 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
58 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
59 OPJ_UINT32 s, OPJ_UINT32 stride,
64 Decode significant pass
67 static INLINE void opj_t1_dec_sigpass_step_raw(
71 OPJ_INT32 oneplushalf,
74 static INLINE void opj_t1_dec_sigpass_step_mqc(
78 OPJ_INT32 oneplushalf,
80 OPJ_UINT32 flags_stride,
84 Encode significant pass
86 static void opj_t1_enc_sigpass(opj_t1_t *t1,
93 Decode significant pass
95 static void opj_t1_dec_sigpass_raw(
101 Encode refinement pass
103 static void opj_t1_enc_refpass(opj_t1_t *t1,
109 Decode refinement pass
111 static void opj_t1_dec_refpass_raw(
117 Decode refinement pass
120 static INLINE void opj_t1_dec_refpass_step_raw(
126 static INLINE void opj_t1_dec_refpass_step_mqc(
138 static void opj_t1_dec_clnpass_step(
142 OPJ_INT32 oneplushalf,
149 static void opj_t1_enc_clnpass(
155 static OPJ_FLOAT64 opj_t1_getwmsedec(
162 OPJ_FLOAT64 stepsize,
164 const OPJ_FLOAT64 * mct_norms,
165 OPJ_UINT32 mct_numcomps);
167 static void opj_t1_encode_cblk(opj_t1_t *t1,
168 opj_tcd_cblk_enc_t* cblk,
173 OPJ_FLOAT64 stepsize,
176 opj_tcd_tile_t * tile,
177 const OPJ_FLOAT64 * mct_norms,
178 OPJ_UINT32 mct_numcomps);
183 @param cblk Code-block coding parameters
185 @param roishift Region of interest shifting value
186 @param cblksty Code-block style
188 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
189 opj_tcd_cblk_dec_t* cblk,
193 opj_event_mgr_t *p_manager,
194 opj_mutex_t* p_manager_mutex,
195 OPJ_BOOL check_pterm);
197 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
205 /* ----------------------------------------------------------------------- */
207 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
209 return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
212 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
218 0 pfX T1_CHI_THIS T1_LUT_SGN_W
219 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
220 2 nfX T1_CHI_THIS T1_LUT_SGN_E
221 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
222 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
223 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
224 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
225 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
228 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
231 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
232 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
234 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
236 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
238 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
242 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
244 return lut_ctxno_sc[lu];
247 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
249 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
250 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
254 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
259 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
262 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
265 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
268 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
271 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
274 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
277 #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride, vsc) \
280 flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
282 /* mark target as significant */ \
283 flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
286 flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
288 /* north-west, north, north-east */ \
289 if (ci == 0U && !(vsc)) { \
290 opj_flag_t* north = flagsp - (stride); \
291 *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
292 north[-1] |= T1_SIGMA_17; \
293 north[1] |= T1_SIGMA_15; \
296 /* south-west, south, south-east */ \
298 opj_flag_t* south = flagsp + (stride); \
299 *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
300 south[-1] |= T1_SIGMA_2; \
301 south[1] |= T1_SIGMA_0; \
306 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
307 OPJ_UINT32 s, OPJ_UINT32 stride,
310 opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride, vsc);
314 Encode significant pass
316 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
328 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
330 OPJ_UINT32 const flags = *flagsp;
332 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
333 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
334 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
335 v = opj_int_abs(*datap) & one ? 1 : 0;
337 fprintf(stderr, " ctxt1=%d\n", ctxt1);
339 opj_mqc_setcurctx(mqc, ctxt1);
340 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
341 opj_mqc_bypass_enc(mqc, v);
343 opj_mqc_encode(mqc, v);
346 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
348 flagsp[-1], flagsp[1],
350 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
351 v = *datap < 0 ? 1U : 0U;
352 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
355 fprintf(stderr, " ctxt2=%d\n", ctxt2);
357 opj_mqc_setcurctx(mqc, ctxt2);
358 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
359 opj_mqc_bypass_enc(mqc, v);
361 OPJ_UINT32 spb = opj_t1_getspb(lu);
363 fprintf(stderr, " spb=%d\n", spb);
365 opj_mqc_encode(mqc, v ^ spb);
367 opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
369 *flagsp |= T1_PI_THIS << (ci * 3U);
373 static INLINE void opj_t1_dec_sigpass_step_raw(
377 OPJ_INT32 oneplushalf,
382 opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
384 OPJ_UINT32 const flags = *flagsp;
386 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
387 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
388 if (opj_mqc_raw_decode(mqc)) {
389 v = opj_mqc_raw_decode(mqc);
390 *datap = v ? -oneplushalf : oneplushalf;
391 opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
393 *flagsp |= T1_PI_THIS << (ci * 3U);
397 #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
398 data_stride, ci, mqc, curctx, \
399 v, a, c, ct, oneplushalf, vsc) \
401 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
402 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
403 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
404 opj_t1_setcurctx(curctx, ctxt1); \
405 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
407 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
409 flagsp[-1], flagsp[1], \
411 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
412 OPJ_UINT32 spb = opj_t1_getspb(lu); \
413 opj_t1_setcurctx(curctx, ctxt2); \
414 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
416 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
417 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
419 flags |= T1_PI_THIS << (ci * 3U); \
423 static INLINE void opj_t1_dec_sigpass_step_mqc(
427 OPJ_INT32 oneplushalf,
429 OPJ_UINT32 flags_stride,
434 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
435 opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
436 0, ci, mqc, mqc->curctx,
437 v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
440 static void opj_t1_enc_sigpass(opj_t1_t *t1,
448 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
449 opj_flag_t* f = &T1_FLAGS(0, 0);
450 OPJ_UINT32 const extra = 2;
454 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
456 for (k = 0; k < (t1->h & ~3U); k += 4) {
458 fprintf(stderr, " k=%d\n", k);
460 for (i = 0; i < t1->w; ++i) {
462 fprintf(stderr, " i=%d\n", i);
465 /* Nothing to do for any of the 4 data points */
469 opj_t1_enc_sigpass_step(
472 &t1->data[((k + 0) * t1->data_stride) + i],
477 0, cblksty & J2K_CCP_CBLKSTY_VSC);
478 opj_t1_enc_sigpass_step(
481 &t1->data[((k + 1) * t1->data_stride) + i],
487 opj_t1_enc_sigpass_step(
490 &t1->data[((k + 2) * t1->data_stride) + i],
496 opj_t1_enc_sigpass_step(
499 &t1->data[((k + 3) * t1->data_stride) + i],
513 fprintf(stderr, " k=%d\n", k);
515 for (i = 0; i < t1->w; ++i) {
517 fprintf(stderr, " i=%d\n", i);
520 /* Nothing to do for any of the 4 data points */
524 for (j = k; j < t1->h; ++j) {
525 opj_t1_enc_sigpass_step(
528 &t1->data[(j * t1->data_stride) + i],
534 (j == k && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
541 static void opj_t1_dec_sigpass_raw(
546 OPJ_INT32 one, half, oneplushalf;
548 OPJ_INT32 *data = t1->data;
549 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
550 const OPJ_UINT32 l_w = t1->w;
553 oneplushalf = one | half;
555 for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
556 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
557 opj_flag_t flags = *flagsp;
559 opj_t1_dec_sigpass_step_raw(
564 cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
566 opj_t1_dec_sigpass_step_raw(
573 opj_t1_dec_sigpass_step_raw(
580 opj_t1_dec_sigpass_step_raw(
591 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
592 for (j = 0; j < t1->h - k; ++j) {
593 opj_t1_dec_sigpass_step_raw(
598 cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
605 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, vsc, w, h, flags_stride) \
607 OPJ_INT32 one, half, oneplushalf; \
608 OPJ_UINT32 i, j, k; \
609 register OPJ_INT32 *data = t1->data; \
610 register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
611 const OPJ_UINT32 l_w = w; \
612 opj_mqc_t* mqc = &(t1->mqc); \
613 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
614 register OPJ_UINT32 v; \
617 oneplushalf = one | half; \
618 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
619 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
620 opj_flag_t flags = *flagsp; \
622 opj_t1_dec_sigpass_step_mqc_macro( \
623 flags, flagsp, flags_stride, data, \
624 l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf, vsc); \
625 opj_t1_dec_sigpass_step_mqc_macro( \
626 flags, flagsp, flags_stride, data, \
627 l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
628 opj_t1_dec_sigpass_step_mqc_macro( \
629 flags, flagsp, flags_stride, data, \
630 l_w, 2, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
631 opj_t1_dec_sigpass_step_mqc_macro( \
632 flags, flagsp, flags_stride, data, \
633 l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
638 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
640 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
641 for (j = 0; j < h - k; ++j) { \
642 opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
643 data + j * l_w, oneplushalf, j, flags_stride, vsc); \
649 static void opj_t1_dec_sigpass_mqc_64x64_novsc(
653 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
656 static void opj_t1_dec_sigpass_mqc_64x64_vsc(
660 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
663 static void opj_t1_dec_sigpass_mqc_generic_novsc(
667 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
671 static void opj_t1_dec_sigpass_mqc_generic_vsc(
675 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
679 static void opj_t1_dec_sigpass_mqc(
684 if (t1->w == 64 && t1->h == 64) {
685 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
686 opj_t1_dec_sigpass_mqc_64x64_vsc(t1, bpno);
688 opj_t1_dec_sigpass_mqc_64x64_novsc(t1, bpno);
691 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
692 opj_t1_dec_sigpass_mqc_generic_vsc(t1, bpno);
694 opj_t1_dec_sigpass_mqc_generic_novsc(t1, bpno);
700 Encode refinement pass step
702 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
713 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
715 OPJ_UINT32 const shift_flags =
716 (*flagsp >> (ci * 3U));
718 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
719 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
720 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
722 v = opj_int_abs(*datap) & one ? 1 : 0;
724 fprintf(stderr, " ctxt=%d\n", ctxt);
726 opj_mqc_setcurctx(mqc, ctxt);
727 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
728 opj_mqc_bypass_enc(mqc, v);
730 opj_mqc_encode(mqc, v);
732 *flagsp |= T1_MU_THIS << (ci * 3U);
737 static INLINE void opj_t1_dec_refpass_step_raw(
746 opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
748 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
749 (T1_SIGMA_THIS << (ci * 3U))) {
750 v = opj_mqc_raw_decode(mqc);
751 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
752 *flagsp |= T1_MU_THIS << (ci * 3U);
756 #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
757 mqc, curctx, v, a, c, ct, poshalf) \
759 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
760 (T1_SIGMA_THIS << (ci * 3U))) { \
761 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
762 opj_t1_setcurctx(curctx, ctxt); \
763 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
764 data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
765 flags |= T1_MU_THIS << (ci * 3U); \
769 static INLINE void opj_t1_dec_refpass_step_mqc(
778 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
779 opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
780 mqc, mqc->curctx, v, mqc->a, mqc->c,
784 static void opj_t1_enc_refpass(
791 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
792 opj_flag_t* f = &T1_FLAGS(0, 0);
793 const OPJ_UINT32 extra = 2U;
797 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
799 for (k = 0; k < (t1->h & ~3U); k += 4) {
801 fprintf(stderr, " k=%d\n", k);
803 for (i = 0; i < t1->w; ++i) {
805 fprintf(stderr, " i=%d\n", i);
807 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
808 /* none significant */
812 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
813 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
814 /* all processed by sigpass */
819 opj_t1_enc_refpass_step(
822 &t1->data[((k + 0) * t1->data_stride) + i],
828 opj_t1_enc_refpass_step(
831 &t1->data[((k + 1) * t1->data_stride) + i],
837 opj_t1_enc_refpass_step(
840 &t1->data[((k + 2) * t1->data_stride) + i],
846 opj_t1_enc_refpass_step(
849 &t1->data[((k + 3) * t1->data_stride) + i],
863 fprintf(stderr, " k=%d\n", k);
865 for (i = 0; i < t1->w; ++i) {
867 fprintf(stderr, " i=%d\n", i);
869 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
870 /* none significant */
874 for (j = k; j < t1->h; ++j) {
875 opj_t1_enc_refpass_step(
878 &t1->data[(j * t1->data_stride) + i],
891 static void opj_t1_dec_refpass_raw(
895 OPJ_INT32 one, poshalf;
897 OPJ_INT32 *data = t1->data;
898 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
899 const OPJ_UINT32 l_w = t1->w;
902 for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
903 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
904 opj_flag_t flags = *flagsp;
906 opj_t1_dec_refpass_step_raw(
912 opj_t1_dec_refpass_step_raw(
918 opj_t1_dec_refpass_step_raw(
924 opj_t1_dec_refpass_step_raw(
934 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
935 for (j = 0; j < t1->h - k; ++j) {
936 opj_t1_dec_refpass_step_raw(
947 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
949 OPJ_INT32 one, poshalf; \
950 OPJ_UINT32 i, j, k; \
951 register OPJ_INT32 *data = t1->data; \
952 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
953 const OPJ_UINT32 l_w = w; \
954 opj_mqc_t* mqc = &(t1->mqc); \
955 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
956 register OPJ_UINT32 v; \
958 poshalf = one >> 1; \
959 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
960 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
961 opj_flag_t flags = *flagsp; \
963 opj_t1_dec_refpass_step_mqc_macro( \
964 flags, data, l_w, 0, \
965 mqc, curctx, v, a, c, ct, poshalf); \
966 opj_t1_dec_refpass_step_mqc_macro( \
967 flags, data, l_w, 1, \
968 mqc, curctx, v, a, c, ct, poshalf); \
969 opj_t1_dec_refpass_step_mqc_macro( \
970 flags, data, l_w, 2, \
971 mqc, curctx, v, a, c, ct, poshalf); \
972 opj_t1_dec_refpass_step_mqc_macro( \
973 flags, data, l_w, 3, \
974 mqc, curctx, v, a, c, ct, poshalf); \
979 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
981 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
982 for (j = 0; j < h - k; ++j) { \
983 opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
989 static void opj_t1_dec_refpass_mqc_64x64(
993 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
996 static void opj_t1_dec_refpass_mqc_generic(
1000 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1003 static void opj_t1_dec_refpass_mqc(
1007 if (t1->w == 64 && t1->h == 64) {
1008 opj_t1_dec_refpass_mqc_64x64(t1, bpno);
1010 opj_t1_dec_refpass_mqc_generic(t1, bpno);
1015 Encode clean-up pass step
1017 static void opj_t1_enc_clnpass_step(
1031 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1033 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1034 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1036 if ((*flagsp & check) == check) {
1038 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1039 } else if (runlen == 1) {
1040 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1041 } else if (runlen == 2) {
1042 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1043 } else if (runlen == 3) {
1044 *flagsp &= ~(T1_PI_3);
1049 for (ci = runlen; ci < lim; ++ci) {
1055 if ((agg != 0) && (ci == runlen)) {
1059 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1060 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1061 #ifdef DEBUG_ENC_CLN
1062 printf(" ctxt1=%d\n", ctxt1);
1064 opj_mqc_setcurctx(mqc, ctxt1);
1065 v = opj_int_abs(*datap) & one ? 1 : 0;
1066 opj_mqc_encode(mqc, v);
1068 OPJ_UINT32 ctxt2, spb;
1071 lu = opj_t1_getctxtno_sc_or_spb_index(
1073 flagsp[-1], flagsp[1],
1075 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1077 ctxt2 = opj_t1_getctxno_sc(lu);
1078 #ifdef DEBUG_ENC_CLN
1079 printf(" ctxt2=%d\n", ctxt2);
1081 opj_mqc_setcurctx(mqc, ctxt2);
1083 v = *datap < 0 ? 1U : 0U;
1084 spb = opj_t1_getspb(lu);
1085 #ifdef DEBUG_ENC_CLN
1086 printf(" spb=%d\n", spb);
1088 opj_mqc_encode(mqc, v ^ spb);
1089 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == 0)) ? 1 : 0;
1090 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U, vsc);
1093 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1094 datap += t1->data_stride;
1098 #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
1099 flags, flagsp, flags_stride, data, \
1100 data_stride, ci, mqc, curctx, \
1101 v, a, c, ct, oneplushalf, vsc) \
1103 if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
1106 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
1107 opj_t1_setcurctx(curctx, ctxt1); \
1108 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1113 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
1114 flags, flagsp[-1], flagsp[1], \
1116 opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
1117 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1118 v = v ^ opj_t1_getspb(lu); \
1119 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
1120 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
1126 static void opj_t1_dec_clnpass_step(
1130 OPJ_INT32 oneplushalf,
1136 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1137 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
1138 *flagsp, flagsp, t1->w + 2U, datap,
1139 0, ci, mqc, mqc->curctx,
1140 v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
1143 static void opj_t1_enc_clnpass(
1150 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1151 OPJ_UINT32 agg, runlen;
1153 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1156 #ifdef DEBUG_ENC_CLN
1157 printf("enc_clnpass: bpno=%d\n", bpno);
1159 for (k = 0; k < (t1->h & ~3U); k += 4) {
1160 #ifdef DEBUG_ENC_CLN
1161 printf(" k=%d\n", k);
1163 for (i = 0; i < t1->w; ++i) {
1164 #ifdef DEBUG_ENC_CLN
1165 printf(" i=%d\n", i);
1167 agg = !(T1_FLAGS(i, k));
1168 #ifdef DEBUG_ENC_CLN
1169 printf(" agg=%d\n", agg);
1172 for (runlen = 0; runlen < 4; ++runlen) {
1173 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1177 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1178 opj_mqc_encode(mqc, runlen != 4);
1182 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1183 opj_mqc_encode(mqc, runlen >> 1);
1184 opj_mqc_encode(mqc, runlen & 1);
1188 opj_t1_enc_clnpass_step(
1191 &t1->data[((k + runlen) * t1->data_stride) + i],
1204 #ifdef DEBUG_ENC_CLN
1205 printf(" k=%d\n", k);
1207 for (i = 0; i < t1->w; ++i) {
1208 #ifdef DEBUG_ENC_CLN
1209 printf(" i=%d\n", i);
1210 printf(" agg=%d\n", agg);
1212 opj_t1_enc_clnpass_step(
1215 &t1->data[((k + runlen) * t1->data_stride) + i],
1227 #define opj_t1_dec_clnpass_internal(t1, bpno, vsc, w, h, flags_stride) \
1229 OPJ_INT32 one, half, oneplushalf; \
1230 OPJ_UINT32 runlen; \
1231 OPJ_UINT32 i, j, k; \
1232 const OPJ_UINT32 l_w = w; \
1233 opj_mqc_t* mqc = &(t1->mqc); \
1234 register OPJ_INT32 *data = t1->data; \
1235 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1236 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1237 register OPJ_UINT32 v; \
1240 oneplushalf = one | half; \
1241 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1242 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1243 opj_flag_t flags = *flagsp; \
1245 OPJ_UINT32 partial = OPJ_TRUE; \
1246 opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
1247 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1251 opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
1252 opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
1253 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1254 runlen = (runlen << 1) | v; \
1257 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
1258 flags, flagsp, flags_stride, data, \
1259 l_w, 0, mqc, curctx, \
1260 v, a, c, ct, oneplushalf, vsc); \
1261 partial = OPJ_FALSE; \
1264 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1265 flags, flagsp, flags_stride, data, \
1266 l_w, 1, mqc, curctx, \
1267 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1268 partial = OPJ_FALSE; \
1271 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1272 flags, flagsp, flags_stride, data, \
1273 l_w, 2, mqc, curctx, \
1274 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1275 partial = OPJ_FALSE; \
1278 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1279 flags, flagsp, flags_stride, data, \
1280 l_w, 3, mqc, curctx, \
1281 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1285 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1286 flags, flagsp, flags_stride, data, \
1287 l_w, 0, mqc, curctx, \
1288 v, a, c, ct, oneplushalf, vsc); \
1289 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1290 flags, flagsp, flags_stride, data, \
1291 l_w, 1, mqc, curctx, \
1292 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1293 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1294 flags, flagsp, flags_stride, data, \
1295 l_w, 2, mqc, curctx, \
1296 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1297 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1298 flags, flagsp, flags_stride, data, \
1299 l_w, 3, mqc, curctx, \
1300 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1302 *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1305 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1307 for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
1308 for (j = 0; j < h - k; ++j) { \
1309 opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j, vsc); \
1311 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1316 static void opj_t1_dec_clnpass_check_segsym(opj_t1_t *t1, OPJ_INT32 cblksty)
1318 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
1319 opj_mqc_t* mqc = &(t1->mqc);
1321 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1322 opj_mqc_decode(v, mqc);
1323 opj_mqc_decode(v2, mqc);
1325 opj_mqc_decode(v2, mqc);
1327 opj_mqc_decode(v2, mqc);
1331 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1337 static void opj_t1_dec_clnpass_64x64_novsc(
1341 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
1344 static void opj_t1_dec_clnpass_64x64_vsc(
1348 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
1351 static void opj_t1_dec_clnpass_generic_novsc(
1355 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
1359 static void opj_t1_dec_clnpass_generic_vsc(
1363 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
1367 static void opj_t1_dec_clnpass(
1372 if (t1->w == 64 && t1->h == 64) {
1373 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1374 opj_t1_dec_clnpass_64x64_vsc(t1, bpno);
1376 opj_t1_dec_clnpass_64x64_novsc(t1, bpno);
1379 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1380 opj_t1_dec_clnpass_generic_vsc(t1, bpno);
1382 opj_t1_dec_clnpass_generic_novsc(t1, bpno);
1385 opj_t1_dec_clnpass_check_segsym(t1, cblksty);
1389 /** mod fixed_quality */
1390 static OPJ_FLOAT64 opj_t1_getwmsedec(
1397 OPJ_FLOAT64 stepsize,
1398 OPJ_UINT32 numcomps,
1399 const OPJ_FLOAT64 * mct_norms,
1400 OPJ_UINT32 mct_numcomps)
1402 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1403 OPJ_ARG_NOT_USED(numcomps);
1405 if (mct_norms && (compno < mct_numcomps)) {
1406 w1 = mct_norms[compno];
1410 w2 = opj_dwt_getnorm(level, orient);
1411 } else { /* if (qmfbid == 0) */
1412 w2 = opj_dwt_getnorm_real(level, orient);
1415 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1416 wmsedec *= wmsedec * nmsedec / 8192.0;
1421 static OPJ_BOOL opj_t1_allocate_buffers(
1427 OPJ_UINT32 flags_stride;
1429 /* encoder uses tile buffer, so no need to allocate */
1433 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1434 /* Overflow check */
1435 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1436 /* FIXME event manager error callback */
1440 datasize = (size_t)w * h;
1442 /* Overflow check */
1443 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1444 /* FIXME event manager error callback */
1448 if (datasize > (size_t)t1->datasize) {
1449 opj_aligned_free(t1->data);
1450 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1452 /* FIXME event manager error callback */
1455 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1456 /* TODO remove this if t1->datasize type changes to size_t */
1457 /* Overflow check */
1458 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1459 /* FIXME event manager error callback */
1463 t1->datasize = (OPJ_UINT32)datasize;
1465 /* memset first arg is declared to never be null by gcc */
1466 if (t1->data != NULL) {
1467 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1471 /* Overflow check */
1472 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1473 /* FIXME event manager error callback */
1476 flags_stride = w + 2U; /* can't be 0U */
1478 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1479 /* Overflow check */
1480 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1481 /* FIXME event manager error callback */
1485 flagssize = (h + 3U) / 4U + 2U;
1487 /* Overflow check */
1488 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1489 /* FIXME event manager error callback */
1492 flagssize *= (size_t)flags_stride;
1497 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1499 if (flagssize > (size_t)t1->flagssize) {
1500 /* Overflow check */
1501 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1502 /* FIXME event manager error callback */
1505 opj_aligned_free(t1->flags);
1506 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1509 /* FIXME event manager error callback */
1512 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1513 /* TODO remove this if t1->flagssize type changes to size_t */
1514 /* Overflow check */
1515 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1516 /* FIXME event manager error callback */
1521 t1->flagssize = (OPJ_UINT32)flagssize;
1523 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1526 for (x = 0; x < flags_stride; ++x) {
1527 /* magic value to hopefully stop any passes being interested in this entry */
1528 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1531 p = &t1->flags[((flags_height + 1) * flags_stride)];
1532 for (x = 0; x < flags_stride; ++x) {
1533 /* magic value to hopefully stop any passes being interested in this entry */
1534 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1539 p = &t1->flags[((flags_height) * flags_stride)];
1541 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1542 } else if (h % 4 == 2) {
1543 v |= T1_PI_2 | T1_PI_3;
1544 } else if (h % 4 == 3) {
1547 for (x = 0; x < flags_stride; ++x) {
1559 /* ----------------------------------------------------------------------- */
1561 /* ----------------------------------------------------------------------- */
1563 * Creates a new Tier 1 handle
1564 * and initializes the look-up tables of the Tier-1 coder/decoder
1565 * @return a new T1 handle if successful, returns NULL otherwise
1567 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1569 opj_t1_t *l_t1 = 00;
1571 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1576 l_t1->encoder = isEncoder;
1583 * Destroys a previously created T1 handle
1585 * @param p_t1 Tier 1 handle to destroy
1587 void opj_t1_destroy(opj_t1_t *p_t1)
1593 /* encoder uses tile buffer, so no need to free */
1594 if (!p_t1->encoder && p_t1->data) {
1595 opj_aligned_free(p_t1->data);
1600 opj_aligned_free(p_t1->flags);
1609 opj_tcd_cblk_dec_t* cblk;
1610 opj_tcd_band_t* band;
1611 opj_tcd_tilecomp_t* tilec;
1613 volatile OPJ_BOOL* pret;
1614 opj_event_mgr_t *p_manager;
1615 opj_mutex_t* p_manager_mutex;
1616 OPJ_BOOL check_pterm;
1617 } opj_t1_cblk_decode_processing_job_t;
1619 static void opj_t1_destroy_wrapper(void* t1)
1621 opj_t1_destroy((opj_t1_t*) t1);
1624 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1626 opj_tcd_cblk_dec_t* cblk;
1627 opj_tcd_band_t* band;
1628 opj_tcd_tilecomp_t* tilec;
1630 OPJ_INT32* OPJ_RESTRICT datap;
1631 OPJ_UINT32 cblk_w, cblk_h;
1634 opj_t1_cblk_decode_processing_job_t* job;
1639 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1645 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1647 if (!*(job->pret)) {
1652 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1654 t1 = opj_t1_create(OPJ_FALSE);
1655 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1658 if (OPJ_FALSE == opj_t1_decode_cblk(
1662 (OPJ_UINT32)tccp->roishift,
1665 job->p_manager_mutex,
1666 job->check_pterm)) {
1667 *(job->pret) = OPJ_FALSE;
1672 x = cblk->x0 - band->x0;
1673 y = cblk->y0 - band->y0;
1674 if (band->bandno & 1) {
1675 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1676 x += pres->x1 - pres->x0;
1678 if (band->bandno & 2) {
1679 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1680 y += pres->y1 - pres->y0;
1687 if (tccp->roishift) {
1688 if (tccp->roishift >= 31) {
1689 for (j = 0; j < cblk_h; ++j) {
1690 for (i = 0; i < cblk_w; ++i) {
1691 datap[(j * cblk_w) + i] = 0;
1695 OPJ_INT32 thresh = 1 << tccp->roishift;
1696 for (j = 0; j < cblk_h; ++j) {
1697 for (i = 0; i < cblk_w; ++i) {
1698 OPJ_INT32 val = datap[(j * cblk_w) + i];
1699 OPJ_INT32 mag = abs(val);
1700 if (mag >= thresh) {
1701 mag >>= tccp->roishift;
1702 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1708 if (tccp->qmfbid == 1) {
1709 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1711 for (j = 0; j < cblk_h; ++j) {
1713 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1714 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1715 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1716 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1717 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1718 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1719 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1720 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1721 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1723 for (; i < cblk_w; ++i) {
1724 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1725 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1728 } else { /* if (tccp->qmfbid == 0) */
1729 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1730 tile_w + (OPJ_UINT32)x];
1731 for (j = 0; j < cblk_h; ++j) {
1732 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1733 for (i = 0; i < cblk_w; ++i) {
1734 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1747 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1748 volatile OPJ_BOOL* pret,
1749 opj_tcd_tilecomp_t* tilec,
1751 opj_event_mgr_t *p_manager,
1752 opj_mutex_t* p_manager_mutex,
1753 OPJ_BOOL check_pterm
1756 OPJ_UINT32 resno, bandno, precno, cblkno;
1758 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1759 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1761 for (bandno = 0; bandno < res->numbands; ++bandno) {
1762 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1764 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1765 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1767 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1768 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1769 opj_t1_cblk_decode_processing_job_t* job;
1771 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1772 sizeof(opj_t1_cblk_decode_processing_job_t));
1783 job->p_manager_mutex = p_manager_mutex;
1784 job->p_manager = p_manager;
1785 job->check_pterm = check_pterm;
1786 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1799 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1800 opj_tcd_cblk_dec_t* cblk,
1802 OPJ_UINT32 roishift,
1804 opj_event_mgr_t *p_manager,
1805 opj_mutex_t* p_manager_mutex,
1806 OPJ_BOOL check_pterm)
1808 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1810 OPJ_INT32 bpno_plus_one;
1811 OPJ_UINT32 passtype;
1812 OPJ_UINT32 segno, passno;
1813 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1815 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
1817 if (!opj_t1_allocate_buffers(
1819 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1820 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1824 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1827 opj_mqc_resetstates(mqc);
1828 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1829 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1830 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1832 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1833 opj_tcd_seg_t *seg = &cblk->segs[segno];
1836 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1837 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1838 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1839 if (seg->data == 00) {
1842 if (type == T1_TYPE_RAW) {
1843 opj_mqc_raw_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len,
1844 OPJ_COMMON_CBLK_DATA_EXTRA);
1846 opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len,
1847 OPJ_COMMON_CBLK_DATA_EXTRA);
1850 for (passno = 0; (passno < seg->real_num_passes) &&
1851 (bpno_plus_one >= 1); ++passno) {
1854 if (type == T1_TYPE_RAW) {
1855 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1857 opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1861 if (type == T1_TYPE_RAW) {
1862 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1864 opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
1868 opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1872 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1873 opj_mqc_resetstates(mqc);
1874 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1875 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1876 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1878 if (++passtype == 3) {
1884 opq_mqc_finish_dec(mqc);
1888 if (mqc->bp + 2 < mqc->end) {
1889 if (p_manager_mutex) {
1890 opj_mutex_lock(p_manager_mutex);
1892 opj_event_msg(p_manager, EVT_WARNING,
1893 "PTERM check failure: %d remaining bytes in code block (%d used / %d)\n",
1894 (int)(mqc->end - mqc->bp) - 2,
1895 (int)(mqc->bp - mqc->start),
1896 (int)(mqc->end - mqc->start));
1897 if (p_manager_mutex) {
1898 opj_mutex_unlock(p_manager_mutex);
1900 } else if (mqc->end_of_byte_stream_counter > 2) {
1901 if (p_manager_mutex) {
1902 opj_mutex_lock(p_manager_mutex);
1904 opj_event_msg(p_manager, EVT_WARNING,
1905 "PTERM check failure: %d synthetized 0xFF markers read\n",
1906 mqc->end_of_byte_stream_counter);
1907 if (p_manager_mutex) {
1908 opj_mutex_unlock(p_manager_mutex);
1919 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
1920 opj_tcd_tile_t *tile,
1922 const OPJ_FLOAT64 * mct_norms,
1923 OPJ_UINT32 mct_numcomps
1926 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1928 tile->distotile = 0; /* fixed_quality */
1930 for (compno = 0; compno < tile->numcomps; ++compno) {
1931 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1932 opj_tccp_t* tccp = &tcp->tccps[compno];
1933 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1935 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1936 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1938 for (bandno = 0; bandno < res->numbands; ++bandno) {
1939 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1940 OPJ_INT32 bandconst;
1942 /* Skip empty bands */
1943 if (opj_tcd_is_band_empty(band)) {
1947 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1948 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1949 opj_tcd_precinct_t *prc = &band->precincts[precno];
1951 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1952 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1953 OPJ_INT32* OPJ_RESTRICT tiledp;
1956 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
1958 OPJ_INT32 x = cblk->x0 - band->x0;
1959 OPJ_INT32 y = cblk->y0 - band->y0;
1960 if (band->bandno & 1) {
1961 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1962 x += pres->x1 - pres->x0;
1964 if (band->bandno & 2) {
1965 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1966 y += pres->y1 - pres->y0;
1969 if (!opj_t1_allocate_buffers(
1971 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1972 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1978 tileLineAdvance = tile_w - cblk_w;
1980 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1982 t1->data_stride = tile_w;
1983 if (tccp->qmfbid == 1) {
1984 for (j = 0; j < cblk_h; ++j) {
1985 for (i = 0; i < cblk_w; ++i) {
1986 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1989 tileIndex += tileLineAdvance;
1991 } else { /* if (tccp->qmfbid == 0) */
1992 for (j = 0; j < cblk_h; ++j) {
1993 for (i = 0; i < cblk_w; ++i) {
1994 OPJ_INT32 tmp = tiledp[tileIndex];
2001 tileIndex += tileLineAdvance;
2010 tilec->numresolutions - 1 - resno,
2027 /* Returns whether the pass (bpno, passtype) is terminated */
2028 static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
2031 OPJ_UINT32 passtype)
2033 /* Is it the last cleanup pass ? */
2034 if (passtype == 2 && bpno == 0) {
2038 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
2042 if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2043 /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
2044 if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
2047 /* and beyond terminate all the magnitude refinement passes (in raw) */
2048 /* and cleanup passes (in MQC) */
2049 if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
2058 /** mod fixed_quality */
2059 static void opj_t1_encode_cblk(opj_t1_t *t1,
2060 opj_tcd_cblk_enc_t* cblk,
2065 OPJ_FLOAT64 stepsize,
2067 OPJ_UINT32 numcomps,
2068 opj_tcd_tile_t * tile,
2069 const OPJ_FLOAT64 * mct_norms,
2070 OPJ_UINT32 mct_numcomps)
2072 OPJ_FLOAT64 cumwmsedec = 0.0;
2074 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2078 OPJ_UINT32 passtype;
2079 OPJ_INT32 nmsedec = 0;
2082 OPJ_BYTE type = T1_TYPE_MQ;
2083 OPJ_FLOAT64 tempwmsedec;
2086 printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
2087 cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
2090 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2093 for (i = 0; i < t1->w; ++i) {
2094 for (j = 0; j < t1->h; ++j) {
2095 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2096 max = opj_int_max(max, tmp);
2100 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2101 T1_NMSEDEC_FRACBITS) : 0;
2103 bpno = (OPJ_INT32)(cblk->numbps - 1);
2106 opj_mqc_resetstates(mqc);
2107 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2108 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2109 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2110 opj_mqc_init_enc(mqc, cblk->data);
2112 for (passno = 0; bpno >= 0; ++passno) {
2113 opj_tcd_pass_t *pass = &cblk->passes[passno];
2114 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2115 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2117 /* If the previous pass was terminating, we need to reset the encoder */
2118 if (passno > 0 && cblk->passes[passno - 1].term) {
2119 if (type == T1_TYPE_RAW) {
2120 opj_mqc_bypass_init_enc(mqc);
2122 opj_mqc_restart_init_enc(mqc);
2128 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2131 opj_t1_enc_refpass(t1, bpno, &nmsedec, type);
2134 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2135 /* code switch SEGMARK (i.e. SEGSYM) */
2136 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2137 opj_mqc_segmark_enc(mqc);
2143 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2144 stepsize, numcomps, mct_norms, mct_numcomps) ;
2145 cumwmsedec += tempwmsedec;
2146 tile->distotile += tempwmsedec;
2147 pass->distortiondec = cumwmsedec;
2149 if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
2150 /* If it is a terminated pass, terminate it */
2151 if (type == T1_TYPE_RAW) {
2152 opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
2154 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2155 opj_mqc_erterm_enc(mqc);
2161 pass->rate = opj_mqc_numbytes(mqc);
2163 /* Non terminated pass */
2164 OPJ_UINT32 rate_extra_bytes;
2165 if (type == T1_TYPE_RAW) {
2166 rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
2167 mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
2169 rate_extra_bytes = 3;
2172 pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
2175 if (++passtype == 3) {
2180 /* Code-switch "RESET" */
2181 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2182 opj_mqc_reset_enc(mqc);
2186 cblk->totalpasses = passno;
2188 if (cblk->totalpasses) {
2189 /* Make sure that pass rates are increasing */
2190 OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
2191 for (passno = cblk->totalpasses; passno > 0;) {
2192 opj_tcd_pass_t *pass = &cblk->passes[--passno];
2193 if (pass->rate > last_pass_rate) {
2194 pass->rate = last_pass_rate;
2196 last_pass_rate = pass->rate;
2201 for (passno = 0; passno < cblk->totalpasses; passno++) {
2202 opj_tcd_pass_t *pass = &cblk->passes[passno];
2204 /* Prevent generation of FF as last data byte of a pass*/
2205 /* For terminating passes, the flushing procedure ensured this already */
2206 assert(pass->rate > 0);
2207 if (cblk->data[pass->rate - 1] == 0xFF) {
2210 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2214 printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
2216 /* Check that there not 0xff >=0x90 sequences */
2217 if (cblk->totalpasses) {
2219 OPJ_UINT32 len = opj_mqc_numbytes(mqc);
2220 for (i = 1; i < len; ++i) {
2221 if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
2222 printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);