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
187 @param p_manager the event manager
188 @param p_manager_mutex mutex for the event manager
189 @param check_pterm whether PTERM correct termination should be checked
191 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
192 opj_tcd_cblk_dec_t* cblk,
196 opj_event_mgr_t *p_manager,
197 opj_mutex_t* p_manager_mutex,
198 OPJ_BOOL check_pterm);
200 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
208 /* ----------------------------------------------------------------------- */
210 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
212 return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
215 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
221 0 pfX T1_CHI_THIS T1_LUT_SGN_W
222 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
223 2 nfX T1_CHI_THIS T1_LUT_SGN_E
224 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
225 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
226 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
227 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
228 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
231 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
234 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
235 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
237 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
239 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
241 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
245 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
247 return lut_ctxno_sc[lu];
250 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
252 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
253 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
257 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
262 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
265 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
268 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
271 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
274 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
277 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
280 #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride, vsc) \
283 flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
285 /* mark target as significant */ \
286 flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
289 flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
291 /* north-west, north, north-east */ \
292 if (ci == 0U && !(vsc)) { \
293 opj_flag_t* north = flagsp - (stride); \
294 *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
295 north[-1] |= T1_SIGMA_17; \
296 north[1] |= T1_SIGMA_15; \
299 /* south-west, south, south-east */ \
301 opj_flag_t* south = flagsp + (stride); \
302 *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
303 south[-1] |= T1_SIGMA_2; \
304 south[1] |= T1_SIGMA_0; \
309 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
310 OPJ_UINT32 s, OPJ_UINT32 stride,
313 opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride, vsc);
317 Encode significant pass
319 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
331 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
333 OPJ_UINT32 const flags = *flagsp;
335 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
336 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
337 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
338 v = opj_int_abs(*datap) & one ? 1 : 0;
340 fprintf(stderr, " ctxt1=%d\n", ctxt1);
342 opj_mqc_setcurctx(mqc, ctxt1);
343 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
344 opj_mqc_bypass_enc(mqc, v);
346 opj_mqc_encode(mqc, v);
349 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
351 flagsp[-1], flagsp[1],
353 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
354 v = *datap < 0 ? 1U : 0U;
355 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
358 fprintf(stderr, " ctxt2=%d\n", ctxt2);
360 opj_mqc_setcurctx(mqc, ctxt2);
361 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
362 opj_mqc_bypass_enc(mqc, v);
364 OPJ_UINT32 spb = opj_t1_getspb(lu);
366 fprintf(stderr, " spb=%d\n", spb);
368 opj_mqc_encode(mqc, v ^ spb);
370 opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
372 *flagsp |= T1_PI_THIS << (ci * 3U);
376 static INLINE void opj_t1_dec_sigpass_step_raw(
380 OPJ_INT32 oneplushalf,
385 opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
387 OPJ_UINT32 const flags = *flagsp;
389 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
390 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
391 if (opj_mqc_raw_decode(mqc)) {
392 v = opj_mqc_raw_decode(mqc);
393 *datap = v ? -oneplushalf : oneplushalf;
394 opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
396 *flagsp |= T1_PI_THIS << (ci * 3U);
400 #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
401 data_stride, ci, mqc, curctx, \
402 v, a, c, ct, oneplushalf, vsc) \
404 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
405 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
406 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
407 opj_t1_setcurctx(curctx, ctxt1); \
408 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
410 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
412 flagsp[-1], flagsp[1], \
414 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
415 OPJ_UINT32 spb = opj_t1_getspb(lu); \
416 opj_t1_setcurctx(curctx, ctxt2); \
417 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
419 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
420 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
422 flags |= T1_PI_THIS << (ci * 3U); \
426 static INLINE void opj_t1_dec_sigpass_step_mqc(
430 OPJ_INT32 oneplushalf,
432 OPJ_UINT32 flags_stride,
437 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
438 opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
439 0, ci, mqc, mqc->curctx,
440 v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
443 static void opj_t1_enc_sigpass(opj_t1_t *t1,
451 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
452 opj_flag_t* f = &T1_FLAGS(0, 0);
453 OPJ_UINT32 const extra = 2;
457 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
459 for (k = 0; k < (t1->h & ~3U); k += 4) {
461 fprintf(stderr, " k=%d\n", k);
463 for (i = 0; i < t1->w; ++i) {
465 fprintf(stderr, " i=%d\n", i);
468 /* Nothing to do for any of the 4 data points */
472 opj_t1_enc_sigpass_step(
475 &t1->data[((k + 0) * t1->data_stride) + i],
480 0, cblksty & J2K_CCP_CBLKSTY_VSC);
481 opj_t1_enc_sigpass_step(
484 &t1->data[((k + 1) * t1->data_stride) + i],
490 opj_t1_enc_sigpass_step(
493 &t1->data[((k + 2) * t1->data_stride) + i],
499 opj_t1_enc_sigpass_step(
502 &t1->data[((k + 3) * t1->data_stride) + i],
516 fprintf(stderr, " k=%d\n", k);
518 for (i = 0; i < t1->w; ++i) {
520 fprintf(stderr, " i=%d\n", i);
523 /* Nothing to do for any of the 4 data points */
527 for (j = k; j < t1->h; ++j) {
528 opj_t1_enc_sigpass_step(
531 &t1->data[(j * t1->data_stride) + i],
537 (j == k && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
544 static void opj_t1_dec_sigpass_raw(
549 OPJ_INT32 one, half, oneplushalf;
551 OPJ_INT32 *data = t1->data;
552 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
553 const OPJ_UINT32 l_w = t1->w;
556 oneplushalf = one | half;
558 for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
559 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
560 opj_flag_t flags = *flagsp;
562 opj_t1_dec_sigpass_step_raw(
567 cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
569 opj_t1_dec_sigpass_step_raw(
576 opj_t1_dec_sigpass_step_raw(
583 opj_t1_dec_sigpass_step_raw(
594 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
595 for (j = 0; j < t1->h - k; ++j) {
596 opj_t1_dec_sigpass_step_raw(
601 cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
608 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, vsc, w, h, flags_stride) \
610 OPJ_INT32 one, half, oneplushalf; \
611 OPJ_UINT32 i, j, k; \
612 register OPJ_INT32 *data = t1->data; \
613 register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
614 const OPJ_UINT32 l_w = w; \
615 opj_mqc_t* mqc = &(t1->mqc); \
616 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
617 register OPJ_UINT32 v; \
620 oneplushalf = one | half; \
621 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
622 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
623 opj_flag_t flags = *flagsp; \
625 opj_t1_dec_sigpass_step_mqc_macro( \
626 flags, flagsp, flags_stride, data, \
627 l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf, vsc); \
628 opj_t1_dec_sigpass_step_mqc_macro( \
629 flags, flagsp, flags_stride, data, \
630 l_w, 1, 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, 2, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
634 opj_t1_dec_sigpass_step_mqc_macro( \
635 flags, flagsp, flags_stride, data, \
636 l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
641 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
643 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
644 for (j = 0; j < h - k; ++j) { \
645 opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
646 data + j * l_w, oneplushalf, j, flags_stride, vsc); \
652 static void opj_t1_dec_sigpass_mqc_64x64_novsc(
656 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
659 static void opj_t1_dec_sigpass_mqc_64x64_vsc(
663 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
666 static void opj_t1_dec_sigpass_mqc_generic_novsc(
670 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
674 static void opj_t1_dec_sigpass_mqc_generic_vsc(
678 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
682 static void opj_t1_dec_sigpass_mqc(
687 if (t1->w == 64 && t1->h == 64) {
688 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
689 opj_t1_dec_sigpass_mqc_64x64_vsc(t1, bpno);
691 opj_t1_dec_sigpass_mqc_64x64_novsc(t1, bpno);
694 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
695 opj_t1_dec_sigpass_mqc_generic_vsc(t1, bpno);
697 opj_t1_dec_sigpass_mqc_generic_novsc(t1, bpno);
703 Encode refinement pass step
705 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
716 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
718 OPJ_UINT32 const shift_flags =
719 (*flagsp >> (ci * 3U));
721 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
722 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
723 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
725 v = opj_int_abs(*datap) & one ? 1 : 0;
727 fprintf(stderr, " ctxt=%d\n", ctxt);
729 opj_mqc_setcurctx(mqc, ctxt);
730 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
731 opj_mqc_bypass_enc(mqc, v);
733 opj_mqc_encode(mqc, v);
735 *flagsp |= T1_MU_THIS << (ci * 3U);
740 static INLINE void opj_t1_dec_refpass_step_raw(
749 opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
751 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
752 (T1_SIGMA_THIS << (ci * 3U))) {
753 v = opj_mqc_raw_decode(mqc);
754 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
755 *flagsp |= T1_MU_THIS << (ci * 3U);
759 #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
760 mqc, curctx, v, a, c, ct, poshalf) \
762 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
763 (T1_SIGMA_THIS << (ci * 3U))) { \
764 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
765 opj_t1_setcurctx(curctx, ctxt); \
766 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
767 data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
768 flags |= T1_MU_THIS << (ci * 3U); \
772 static INLINE void opj_t1_dec_refpass_step_mqc(
781 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
782 opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
783 mqc, mqc->curctx, v, mqc->a, mqc->c,
787 static void opj_t1_enc_refpass(
794 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
795 opj_flag_t* f = &T1_FLAGS(0, 0);
796 const OPJ_UINT32 extra = 2U;
800 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
802 for (k = 0; k < (t1->h & ~3U); k += 4) {
804 fprintf(stderr, " k=%d\n", k);
806 for (i = 0; i < t1->w; ++i) {
808 fprintf(stderr, " i=%d\n", i);
810 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
811 /* none significant */
815 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
816 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
817 /* all processed by sigpass */
822 opj_t1_enc_refpass_step(
825 &t1->data[((k + 0) * t1->data_stride) + i],
831 opj_t1_enc_refpass_step(
834 &t1->data[((k + 1) * t1->data_stride) + i],
840 opj_t1_enc_refpass_step(
843 &t1->data[((k + 2) * t1->data_stride) + i],
849 opj_t1_enc_refpass_step(
852 &t1->data[((k + 3) * t1->data_stride) + i],
866 fprintf(stderr, " k=%d\n", k);
868 for (i = 0; i < t1->w; ++i) {
870 fprintf(stderr, " i=%d\n", i);
872 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
873 /* none significant */
877 for (j = k; j < t1->h; ++j) {
878 opj_t1_enc_refpass_step(
881 &t1->data[(j * t1->data_stride) + i],
894 static void opj_t1_dec_refpass_raw(
898 OPJ_INT32 one, poshalf;
900 OPJ_INT32 *data = t1->data;
901 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
902 const OPJ_UINT32 l_w = t1->w;
905 for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
906 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
907 opj_flag_t flags = *flagsp;
909 opj_t1_dec_refpass_step_raw(
915 opj_t1_dec_refpass_step_raw(
921 opj_t1_dec_refpass_step_raw(
927 opj_t1_dec_refpass_step_raw(
937 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
938 for (j = 0; j < t1->h - k; ++j) {
939 opj_t1_dec_refpass_step_raw(
950 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
952 OPJ_INT32 one, poshalf; \
953 OPJ_UINT32 i, j, k; \
954 register OPJ_INT32 *data = t1->data; \
955 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
956 const OPJ_UINT32 l_w = w; \
957 opj_mqc_t* mqc = &(t1->mqc); \
958 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
959 register OPJ_UINT32 v; \
961 poshalf = one >> 1; \
962 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
963 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
964 opj_flag_t flags = *flagsp; \
966 opj_t1_dec_refpass_step_mqc_macro( \
967 flags, data, l_w, 0, \
968 mqc, curctx, v, a, c, ct, poshalf); \
969 opj_t1_dec_refpass_step_mqc_macro( \
970 flags, data, l_w, 1, \
971 mqc, curctx, v, a, c, ct, poshalf); \
972 opj_t1_dec_refpass_step_mqc_macro( \
973 flags, data, l_w, 2, \
974 mqc, curctx, v, a, c, ct, poshalf); \
975 opj_t1_dec_refpass_step_mqc_macro( \
976 flags, data, l_w, 3, \
977 mqc, curctx, v, a, c, ct, poshalf); \
982 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
984 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
985 for (j = 0; j < h - k; ++j) { \
986 opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
992 static void opj_t1_dec_refpass_mqc_64x64(
996 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
999 static void opj_t1_dec_refpass_mqc_generic(
1003 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1006 static void opj_t1_dec_refpass_mqc(
1010 if (t1->w == 64 && t1->h == 64) {
1011 opj_t1_dec_refpass_mqc_64x64(t1, bpno);
1013 opj_t1_dec_refpass_mqc_generic(t1, bpno);
1018 Encode clean-up pass step
1020 static void opj_t1_enc_clnpass_step(
1034 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1036 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1037 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1039 if ((*flagsp & check) == check) {
1041 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1042 } else if (runlen == 1) {
1043 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1044 } else if (runlen == 2) {
1045 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1046 } else if (runlen == 3) {
1047 *flagsp &= ~(T1_PI_3);
1052 for (ci = runlen; ci < lim; ++ci) {
1058 if ((agg != 0) && (ci == runlen)) {
1062 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1063 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1064 #ifdef DEBUG_ENC_CLN
1065 printf(" ctxt1=%d\n", ctxt1);
1067 opj_mqc_setcurctx(mqc, ctxt1);
1068 v = opj_int_abs(*datap) & one ? 1 : 0;
1069 opj_mqc_encode(mqc, v);
1071 OPJ_UINT32 ctxt2, spb;
1074 lu = opj_t1_getctxtno_sc_or_spb_index(
1076 flagsp[-1], flagsp[1],
1078 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1080 ctxt2 = opj_t1_getctxno_sc(lu);
1081 #ifdef DEBUG_ENC_CLN
1082 printf(" ctxt2=%d\n", ctxt2);
1084 opj_mqc_setcurctx(mqc, ctxt2);
1086 v = *datap < 0 ? 1U : 0U;
1087 spb = opj_t1_getspb(lu);
1088 #ifdef DEBUG_ENC_CLN
1089 printf(" spb=%d\n", spb);
1091 opj_mqc_encode(mqc, v ^ spb);
1092 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == 0)) ? 1 : 0;
1093 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U, vsc);
1096 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1097 datap += t1->data_stride;
1101 #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
1102 flags, flagsp, flags_stride, data, \
1103 data_stride, ci, mqc, curctx, \
1104 v, a, c, ct, oneplushalf, vsc) \
1106 if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
1109 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
1110 opj_t1_setcurctx(curctx, ctxt1); \
1111 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1116 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
1117 flags, flagsp[-1], flagsp[1], \
1119 opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
1120 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1121 v = v ^ opj_t1_getspb(lu); \
1122 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
1123 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
1129 static void opj_t1_dec_clnpass_step(
1133 OPJ_INT32 oneplushalf,
1139 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1140 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
1141 *flagsp, flagsp, t1->w + 2U, datap,
1142 0, ci, mqc, mqc->curctx,
1143 v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
1146 static void opj_t1_enc_clnpass(
1153 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1154 OPJ_UINT32 agg, runlen;
1156 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1159 #ifdef DEBUG_ENC_CLN
1160 printf("enc_clnpass: bpno=%d\n", bpno);
1162 for (k = 0; k < (t1->h & ~3U); k += 4) {
1163 #ifdef DEBUG_ENC_CLN
1164 printf(" k=%d\n", k);
1166 for (i = 0; i < t1->w; ++i) {
1167 #ifdef DEBUG_ENC_CLN
1168 printf(" i=%d\n", i);
1170 agg = !(T1_FLAGS(i, k));
1171 #ifdef DEBUG_ENC_CLN
1172 printf(" agg=%d\n", agg);
1175 for (runlen = 0; runlen < 4; ++runlen) {
1176 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1180 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1181 opj_mqc_encode(mqc, runlen != 4);
1185 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1186 opj_mqc_encode(mqc, runlen >> 1);
1187 opj_mqc_encode(mqc, runlen & 1);
1191 opj_t1_enc_clnpass_step(
1194 &t1->data[((k + runlen) * t1->data_stride) + i],
1207 #ifdef DEBUG_ENC_CLN
1208 printf(" k=%d\n", k);
1210 for (i = 0; i < t1->w; ++i) {
1211 #ifdef DEBUG_ENC_CLN
1212 printf(" i=%d\n", i);
1213 printf(" agg=%d\n", agg);
1215 opj_t1_enc_clnpass_step(
1218 &t1->data[((k + runlen) * t1->data_stride) + i],
1230 #define opj_t1_dec_clnpass_internal(t1, bpno, vsc, w, h, flags_stride) \
1232 OPJ_INT32 one, half, oneplushalf; \
1233 OPJ_UINT32 runlen; \
1234 OPJ_UINT32 i, j, k; \
1235 const OPJ_UINT32 l_w = w; \
1236 opj_mqc_t* mqc = &(t1->mqc); \
1237 register OPJ_INT32 *data = t1->data; \
1238 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1239 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1240 register OPJ_UINT32 v; \
1243 oneplushalf = one | half; \
1244 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1245 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1246 opj_flag_t flags = *flagsp; \
1248 OPJ_UINT32 partial = OPJ_TRUE; \
1249 opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
1250 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1254 opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
1255 opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
1256 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1257 runlen = (runlen << 1) | v; \
1260 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
1261 flags, flagsp, flags_stride, data, \
1262 l_w, 0, mqc, curctx, \
1263 v, a, c, ct, oneplushalf, vsc); \
1264 partial = OPJ_FALSE; \
1267 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1268 flags, flagsp, flags_stride, data, \
1269 l_w, 1, mqc, curctx, \
1270 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1271 partial = OPJ_FALSE; \
1274 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1275 flags, flagsp, flags_stride, data, \
1276 l_w, 2, mqc, curctx, \
1277 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1278 partial = OPJ_FALSE; \
1281 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1282 flags, flagsp, flags_stride, data, \
1283 l_w, 3, mqc, curctx, \
1284 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1288 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1289 flags, flagsp, flags_stride, data, \
1290 l_w, 0, mqc, curctx, \
1291 v, a, c, ct, oneplushalf, vsc); \
1292 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1293 flags, flagsp, flags_stride, data, \
1294 l_w, 1, mqc, curctx, \
1295 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1296 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1297 flags, flagsp, flags_stride, data, \
1298 l_w, 2, mqc, curctx, \
1299 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1300 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1301 flags, flagsp, flags_stride, data, \
1302 l_w, 3, mqc, curctx, \
1303 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1305 *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1308 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1310 for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
1311 for (j = 0; j < h - k; ++j) { \
1312 opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j, vsc); \
1314 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1319 static void opj_t1_dec_clnpass_check_segsym(opj_t1_t *t1, OPJ_INT32 cblksty)
1321 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
1322 opj_mqc_t* mqc = &(t1->mqc);
1324 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1325 opj_mqc_decode(v, mqc);
1326 opj_mqc_decode(v2, mqc);
1328 opj_mqc_decode(v2, mqc);
1330 opj_mqc_decode(v2, mqc);
1334 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1340 static void opj_t1_dec_clnpass_64x64_novsc(
1344 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
1347 static void opj_t1_dec_clnpass_64x64_vsc(
1351 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
1354 static void opj_t1_dec_clnpass_generic_novsc(
1358 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
1362 static void opj_t1_dec_clnpass_generic_vsc(
1366 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
1370 static void opj_t1_dec_clnpass(
1375 if (t1->w == 64 && t1->h == 64) {
1376 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1377 opj_t1_dec_clnpass_64x64_vsc(t1, bpno);
1379 opj_t1_dec_clnpass_64x64_novsc(t1, bpno);
1382 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1383 opj_t1_dec_clnpass_generic_vsc(t1, bpno);
1385 opj_t1_dec_clnpass_generic_novsc(t1, bpno);
1388 opj_t1_dec_clnpass_check_segsym(t1, cblksty);
1392 /** mod fixed_quality */
1393 static OPJ_FLOAT64 opj_t1_getwmsedec(
1400 OPJ_FLOAT64 stepsize,
1401 OPJ_UINT32 numcomps,
1402 const OPJ_FLOAT64 * mct_norms,
1403 OPJ_UINT32 mct_numcomps)
1405 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1406 OPJ_ARG_NOT_USED(numcomps);
1408 if (mct_norms && (compno < mct_numcomps)) {
1409 w1 = mct_norms[compno];
1413 w2 = opj_dwt_getnorm(level, orient);
1414 } else { /* if (qmfbid == 0) */
1415 w2 = opj_dwt_getnorm_real(level, orient);
1418 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1419 wmsedec *= wmsedec * nmsedec / 8192.0;
1424 static OPJ_BOOL opj_t1_allocate_buffers(
1430 OPJ_UINT32 flags_stride;
1432 /* encoder uses tile buffer, so no need to allocate */
1436 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1437 /* Overflow check */
1438 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1439 /* FIXME event manager error callback */
1443 datasize = (size_t)w * h;
1445 /* Overflow check */
1446 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1447 /* FIXME event manager error callback */
1451 if (datasize > (size_t)t1->datasize) {
1452 opj_aligned_free(t1->data);
1453 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1455 /* FIXME event manager error callback */
1458 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1459 /* TODO remove this if t1->datasize type changes to size_t */
1460 /* Overflow check */
1461 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1462 /* FIXME event manager error callback */
1466 t1->datasize = (OPJ_UINT32)datasize;
1468 /* memset first arg is declared to never be null by gcc */
1469 if (t1->data != NULL) {
1470 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1474 /* Overflow check */
1475 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1476 /* FIXME event manager error callback */
1479 flags_stride = w + 2U; /* can't be 0U */
1481 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1482 /* Overflow check */
1483 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1484 /* FIXME event manager error callback */
1488 flagssize = (h + 3U) / 4U + 2U;
1490 /* Overflow check */
1491 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1492 /* FIXME event manager error callback */
1495 flagssize *= (size_t)flags_stride;
1500 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1502 if (flagssize > (size_t)t1->flagssize) {
1503 /* Overflow check */
1504 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1505 /* FIXME event manager error callback */
1508 opj_aligned_free(t1->flags);
1509 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1512 /* FIXME event manager error callback */
1515 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1516 /* TODO remove this if t1->flagssize type changes to size_t */
1517 /* Overflow check */
1518 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1519 /* FIXME event manager error callback */
1524 t1->flagssize = (OPJ_UINT32)flagssize;
1526 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1529 for (x = 0; x < flags_stride; ++x) {
1530 /* magic value to hopefully stop any passes being interested in this entry */
1531 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1534 p = &t1->flags[((flags_height + 1) * flags_stride)];
1535 for (x = 0; x < flags_stride; ++x) {
1536 /* magic value to hopefully stop any passes being interested in this entry */
1537 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1542 p = &t1->flags[((flags_height) * flags_stride)];
1544 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1545 } else if (h % 4 == 2) {
1546 v |= T1_PI_2 | T1_PI_3;
1547 } else if (h % 4 == 3) {
1550 for (x = 0; x < flags_stride; ++x) {
1562 /* ----------------------------------------------------------------------- */
1564 /* ----------------------------------------------------------------------- */
1566 * Creates a new Tier 1 handle
1567 * and initializes the look-up tables of the Tier-1 coder/decoder
1568 * @return a new T1 handle if successful, returns NULL otherwise
1570 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1572 opj_t1_t *l_t1 = 00;
1574 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1579 l_t1->encoder = isEncoder;
1586 * Destroys a previously created T1 handle
1588 * @param p_t1 Tier 1 handle to destroy
1590 void opj_t1_destroy(opj_t1_t *p_t1)
1596 /* encoder uses tile buffer, so no need to free */
1597 if (!p_t1->encoder && p_t1->data) {
1598 opj_aligned_free(p_t1->data);
1603 opj_aligned_free(p_t1->flags);
1607 opj_free(p_t1->cblkdatabuffer);
1614 opj_tcd_cblk_dec_t* cblk;
1615 opj_tcd_band_t* band;
1616 opj_tcd_tilecomp_t* tilec;
1618 OPJ_BOOL mustuse_cblkdatabuffer;
1619 volatile OPJ_BOOL* pret;
1620 opj_event_mgr_t *p_manager;
1621 opj_mutex_t* p_manager_mutex;
1622 OPJ_BOOL check_pterm;
1623 } opj_t1_cblk_decode_processing_job_t;
1625 static void opj_t1_destroy_wrapper(void* t1)
1627 opj_t1_destroy((opj_t1_t*) t1);
1630 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1632 opj_tcd_cblk_dec_t* cblk;
1633 opj_tcd_band_t* band;
1634 opj_tcd_tilecomp_t* tilec;
1636 OPJ_INT32* OPJ_RESTRICT datap;
1637 OPJ_UINT32 cblk_w, cblk_h;
1640 opj_t1_cblk_decode_processing_job_t* job;
1645 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1651 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1653 if (!*(job->pret)) {
1658 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1660 t1 = opj_t1_create(OPJ_FALSE);
1661 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1663 t1->mustuse_cblkdatabuffer = job->mustuse_cblkdatabuffer;
1665 if (OPJ_FALSE == opj_t1_decode_cblk(
1669 (OPJ_UINT32)tccp->roishift,
1672 job->p_manager_mutex,
1673 job->check_pterm)) {
1674 *(job->pret) = OPJ_FALSE;
1679 x = cblk->x0 - band->x0;
1680 y = cblk->y0 - band->y0;
1681 if (band->bandno & 1) {
1682 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1683 x += pres->x1 - pres->x0;
1685 if (band->bandno & 2) {
1686 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1687 y += pres->y1 - pres->y0;
1694 if (tccp->roishift) {
1695 if (tccp->roishift >= 31) {
1696 for (j = 0; j < cblk_h; ++j) {
1697 for (i = 0; i < cblk_w; ++i) {
1698 datap[(j * cblk_w) + i] = 0;
1702 OPJ_INT32 thresh = 1 << tccp->roishift;
1703 for (j = 0; j < cblk_h; ++j) {
1704 for (i = 0; i < cblk_w; ++i) {
1705 OPJ_INT32 val = datap[(j * cblk_w) + i];
1706 OPJ_INT32 mag = abs(val);
1707 if (mag >= thresh) {
1708 mag >>= tccp->roishift;
1709 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1715 if (tccp->qmfbid == 1) {
1716 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1718 for (j = 0; j < cblk_h; ++j) {
1720 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1721 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1722 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1723 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1724 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1725 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1726 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1727 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1728 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1730 for (; i < cblk_w; ++i) {
1731 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1732 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1735 } else { /* if (tccp->qmfbid == 0) */
1736 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1737 tile_w + (OPJ_UINT32)x];
1738 for (j = 0; j < cblk_h; ++j) {
1739 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1740 for (i = 0; i < cblk_w; ++i) {
1741 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1754 void opj_t1_decode_cblks(opj_tcd_t* tcd,
1755 volatile OPJ_BOOL* pret,
1756 opj_tcd_tilecomp_t* tilec,
1758 opj_event_mgr_t *p_manager,
1759 opj_mutex_t* p_manager_mutex,
1760 OPJ_BOOL check_pterm
1763 opj_thread_pool_t* tp = tcd->thread_pool;
1764 OPJ_UINT32 resno, bandno, precno, cblkno;
1766 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1767 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1769 for (bandno = 0; bandno < res->numbands; ++bandno) {
1770 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1772 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1773 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1775 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1776 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1777 opj_t1_cblk_decode_processing_job_t* job;
1779 if (!opj_tcd_is_subband_area_of_interest(tcd,
1783 (OPJ_UINT32)cblk->x0,
1784 (OPJ_UINT32)cblk->y0,
1785 (OPJ_UINT32)cblk->x1,
1786 (OPJ_UINT32)cblk->y1)) {
1790 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1791 sizeof(opj_t1_cblk_decode_processing_job_t));
1802 job->p_manager_mutex = p_manager_mutex;
1803 job->p_manager = p_manager;
1804 job->check_pterm = check_pterm;
1805 job->mustuse_cblkdatabuffer = opj_thread_pool_get_thread_count(tp) > 1;
1806 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1819 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1820 opj_tcd_cblk_dec_t* cblk,
1822 OPJ_UINT32 roishift,
1824 opj_event_mgr_t *p_manager,
1825 opj_mutex_t* p_manager_mutex,
1826 OPJ_BOOL check_pterm)
1828 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1830 OPJ_INT32 bpno_plus_one;
1831 OPJ_UINT32 passtype;
1832 OPJ_UINT32 segno, passno;
1833 OPJ_BYTE* cblkdata = NULL;
1834 OPJ_UINT32 cblkdataindex = 0;
1835 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1837 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
1839 if (!opj_t1_allocate_buffers(
1841 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1842 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1846 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1847 if (bpno_plus_one >= 31) {
1848 if (p_manager_mutex) {
1849 opj_mutex_lock(p_manager_mutex);
1851 opj_event_msg(p_manager, EVT_WARNING,
1852 "opj_t1_decode_cblk(): unsupported bpno_plus_one = %d >= 31\n",
1854 if (p_manager_mutex) {
1855 opj_mutex_unlock(p_manager_mutex);
1861 opj_mqc_resetstates(mqc);
1862 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1863 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1864 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1866 /* Even if we have a single chunk, in multi-threaded decoding */
1867 /* the insertion of our synthetic marker might potentially override */
1868 /* valid codestream of other codeblocks decoded in parallel. */
1869 if (cblk->numchunks > 1 || t1->mustuse_cblkdatabuffer) {
1871 OPJ_UINT32 cblk_len;
1873 /* Compute whole codeblock length from chunk lengths */
1875 for (i = 0; i < cblk->numchunks; i++) {
1876 cblk_len += cblk->chunks[i].len;
1879 /* Allocate temporary memory if needed */
1880 if (cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA > t1->cblkdatabuffersize) {
1881 cblkdata = (OPJ_BYTE*)opj_realloc(t1->cblkdatabuffer,
1882 cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA);
1883 if (cblkdata == NULL) {
1886 t1->cblkdatabuffer = cblkdata;
1887 memset(t1->cblkdatabuffer + cblk_len, 0, OPJ_COMMON_CBLK_DATA_EXTRA);
1888 t1->cblkdatabuffersize = cblk_len + OPJ_COMMON_CBLK_DATA_EXTRA;
1891 /* Concatenate all chunks */
1892 cblkdata = t1->cblkdatabuffer;
1894 for (i = 0; i < cblk->numchunks; i++) {
1895 memcpy(cblkdata + cblk_len, cblk->chunks[i].data, cblk->chunks[i].len);
1896 cblk_len += cblk->chunks[i].len;
1898 } else if (cblk->numchunks == 1) {
1899 cblkdata = cblk->chunks[0].data;
1902 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1903 opj_tcd_seg_t *seg = &cblk->segs[segno];
1906 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1907 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1909 if (type == T1_TYPE_RAW) {
1910 opj_mqc_raw_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
1911 OPJ_COMMON_CBLK_DATA_EXTRA);
1913 opj_mqc_init_dec(mqc, cblkdata + cblkdataindex, seg->len,
1914 OPJ_COMMON_CBLK_DATA_EXTRA);
1916 cblkdataindex += seg->len;
1918 for (passno = 0; (passno < seg->real_num_passes) &&
1919 (bpno_plus_one >= 1); ++passno) {
1922 if (type == T1_TYPE_RAW) {
1923 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1925 opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1929 if (type == T1_TYPE_RAW) {
1930 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1932 opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
1936 opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1940 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1941 opj_mqc_resetstates(mqc);
1942 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1943 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1944 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1946 if (++passtype == 3) {
1952 opq_mqc_finish_dec(mqc);
1956 if (mqc->bp + 2 < mqc->end) {
1957 if (p_manager_mutex) {
1958 opj_mutex_lock(p_manager_mutex);
1960 opj_event_msg(p_manager, EVT_WARNING,
1961 "PTERM check failure: %d remaining bytes in code block (%d used / %d)\n",
1962 (int)(mqc->end - mqc->bp) - 2,
1963 (int)(mqc->bp - mqc->start),
1964 (int)(mqc->end - mqc->start));
1965 if (p_manager_mutex) {
1966 opj_mutex_unlock(p_manager_mutex);
1968 } else if (mqc->end_of_byte_stream_counter > 2) {
1969 if (p_manager_mutex) {
1970 opj_mutex_lock(p_manager_mutex);
1972 opj_event_msg(p_manager, EVT_WARNING,
1973 "PTERM check failure: %d synthetized 0xFF markers read\n",
1974 mqc->end_of_byte_stream_counter);
1975 if (p_manager_mutex) {
1976 opj_mutex_unlock(p_manager_mutex);
1987 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
1988 opj_tcd_tile_t *tile,
1990 const OPJ_FLOAT64 * mct_norms,
1991 OPJ_UINT32 mct_numcomps
1994 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1996 tile->distotile = 0; /* fixed_quality */
1998 for (compno = 0; compno < tile->numcomps; ++compno) {
1999 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2000 opj_tccp_t* tccp = &tcp->tccps[compno];
2001 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2003 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2004 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2006 for (bandno = 0; bandno < res->numbands; ++bandno) {
2007 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2008 OPJ_INT32 bandconst;
2010 /* Skip empty bands */
2011 if (opj_tcd_is_band_empty(band)) {
2015 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2016 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2017 opj_tcd_precinct_t *prc = &band->precincts[precno];
2019 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2020 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2021 OPJ_INT32* OPJ_RESTRICT tiledp;
2024 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2026 OPJ_INT32 x = cblk->x0 - band->x0;
2027 OPJ_INT32 y = cblk->y0 - band->y0;
2028 if (band->bandno & 1) {
2029 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2030 x += pres->x1 - pres->x0;
2032 if (band->bandno & 2) {
2033 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2034 y += pres->y1 - pres->y0;
2037 if (!opj_t1_allocate_buffers(
2039 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2040 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2046 tileLineAdvance = tile_w - cblk_w;
2048 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2050 t1->data_stride = tile_w;
2051 if (tccp->qmfbid == 1) {
2052 for (j = 0; j < cblk_h; ++j) {
2053 for (i = 0; i < cblk_w; ++i) {
2054 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2057 tileIndex += tileLineAdvance;
2059 } else { /* if (tccp->qmfbid == 0) */
2060 for (j = 0; j < cblk_h; ++j) {
2061 for (i = 0; i < cblk_w; ++i) {
2062 OPJ_INT32 tmp = tiledp[tileIndex];
2069 tileIndex += tileLineAdvance;
2078 tilec->numresolutions - 1 - resno,
2095 /* Returns whether the pass (bpno, passtype) is terminated */
2096 static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
2099 OPJ_UINT32 passtype)
2101 /* Is it the last cleanup pass ? */
2102 if (passtype == 2 && bpno == 0) {
2106 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
2110 if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2111 /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
2112 if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
2115 /* and beyond terminate all the magnitude refinement passes (in raw) */
2116 /* and cleanup passes (in MQC) */
2117 if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
2126 /** mod fixed_quality */
2127 static void opj_t1_encode_cblk(opj_t1_t *t1,
2128 opj_tcd_cblk_enc_t* cblk,
2133 OPJ_FLOAT64 stepsize,
2135 OPJ_UINT32 numcomps,
2136 opj_tcd_tile_t * tile,
2137 const OPJ_FLOAT64 * mct_norms,
2138 OPJ_UINT32 mct_numcomps)
2140 OPJ_FLOAT64 cumwmsedec = 0.0;
2142 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2146 OPJ_UINT32 passtype;
2147 OPJ_INT32 nmsedec = 0;
2150 OPJ_BYTE type = T1_TYPE_MQ;
2151 OPJ_FLOAT64 tempwmsedec;
2154 printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
2155 cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
2158 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2161 for (i = 0; i < t1->w; ++i) {
2162 for (j = 0; j < t1->h; ++j) {
2163 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2164 max = opj_int_max(max, tmp);
2168 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2169 T1_NMSEDEC_FRACBITS) : 0;
2170 if (cblk->numbps == 0) {
2171 cblk->totalpasses = 0;
2175 bpno = (OPJ_INT32)(cblk->numbps - 1);
2178 opj_mqc_resetstates(mqc);
2179 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2180 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2181 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2182 opj_mqc_init_enc(mqc, cblk->data);
2184 for (passno = 0; bpno >= 0; ++passno) {
2185 opj_tcd_pass_t *pass = &cblk->passes[passno];
2186 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2187 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2189 /* If the previous pass was terminating, we need to reset the encoder */
2190 if (passno > 0 && cblk->passes[passno - 1].term) {
2191 if (type == T1_TYPE_RAW) {
2192 opj_mqc_bypass_init_enc(mqc);
2194 opj_mqc_restart_init_enc(mqc);
2200 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2203 opj_t1_enc_refpass(t1, bpno, &nmsedec, type);
2206 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2207 /* code switch SEGMARK (i.e. SEGSYM) */
2208 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2209 opj_mqc_segmark_enc(mqc);
2215 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2216 stepsize, numcomps, mct_norms, mct_numcomps) ;
2217 cumwmsedec += tempwmsedec;
2218 tile->distotile += tempwmsedec;
2219 pass->distortiondec = cumwmsedec;
2221 if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
2222 /* If it is a terminated pass, terminate it */
2223 if (type == T1_TYPE_RAW) {
2224 opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
2226 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2227 opj_mqc_erterm_enc(mqc);
2233 pass->rate = opj_mqc_numbytes(mqc);
2235 /* Non terminated pass */
2236 OPJ_UINT32 rate_extra_bytes;
2237 if (type == T1_TYPE_RAW) {
2238 rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
2239 mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
2241 rate_extra_bytes = 3;
2244 pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
2247 if (++passtype == 3) {
2252 /* Code-switch "RESET" */
2253 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2254 opj_mqc_reset_enc(mqc);
2258 cblk->totalpasses = passno;
2260 if (cblk->totalpasses) {
2261 /* Make sure that pass rates are increasing */
2262 OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
2263 for (passno = cblk->totalpasses; passno > 0;) {
2264 opj_tcd_pass_t *pass = &cblk->passes[--passno];
2265 if (pass->rate > last_pass_rate) {
2266 pass->rate = last_pass_rate;
2268 last_pass_rate = pass->rate;
2273 for (passno = 0; passno < cblk->totalpasses; passno++) {
2274 opj_tcd_pass_t *pass = &cblk->passes[passno];
2276 /* Prevent generation of FF as last data byte of a pass*/
2277 /* For terminating passes, the flushing procedure ensured this already */
2278 assert(pass->rate > 0);
2279 if (cblk->data[pass->rate - 1] == 0xFF) {
2282 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2286 printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
2288 /* Check that there not 0xff >=0x90 sequences */
2289 if (cblk->totalpasses) {
2291 OPJ_UINT32 len = opj_mqc_numbytes(mqc);
2292 for (i = 1; i < len; ++i) {
2293 if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
2294 printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);