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,
194 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
202 /* ----------------------------------------------------------------------- */
204 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
206 return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
209 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
215 0 pfX T1_CHI_THIS T1_LUT_SGN_W
216 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
217 2 nfX T1_CHI_THIS T1_LUT_SGN_E
218 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
219 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
220 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
221 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
222 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
225 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
228 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
229 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
231 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
233 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
235 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
239 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
241 return lut_ctxno_sc[lu];
244 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
246 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
247 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
251 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
256 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
259 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
262 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
265 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
268 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
271 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
274 #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride, vsc) \
277 flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
279 /* mark target as significant */ \
280 flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
283 flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
285 /* north-west, north, north-east */ \
286 if (ci == 0U && !(vsc)) { \
287 opj_flag_t* north = flagsp - (stride); \
288 *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
289 north[-1] |= T1_SIGMA_17; \
290 north[1] |= T1_SIGMA_15; \
293 /* south-west, south, south-east */ \
295 opj_flag_t* south = flagsp + (stride); \
296 *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
297 south[-1] |= T1_SIGMA_2; \
298 south[1] |= T1_SIGMA_0; \
303 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
304 OPJ_UINT32 s, OPJ_UINT32 stride,
307 opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride, vsc);
311 Encode significant pass
313 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
325 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
327 OPJ_UINT32 const flags = *flagsp;
329 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
330 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
331 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
332 v = opj_int_abs(*datap) & one ? 1 : 0;
334 fprintf(stderr, " ctxt1=%d\n", ctxt1);
336 opj_mqc_setcurctx(mqc, ctxt1);
337 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
338 opj_mqc_bypass_enc(mqc, v);
340 opj_mqc_encode(mqc, v);
343 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
345 flagsp[-1], flagsp[1],
347 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
348 v = *datap < 0 ? 1U : 0U;
349 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
352 fprintf(stderr, " ctxt2=%d\n", ctxt2);
354 opj_mqc_setcurctx(mqc, ctxt2);
355 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
356 opj_mqc_bypass_enc(mqc, v);
358 OPJ_UINT32 spb = opj_t1_getspb(lu);
360 fprintf(stderr, " spb=%d\n", spb);
362 opj_mqc_encode(mqc, v ^ spb);
364 opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
366 *flagsp |= T1_PI_THIS << (ci * 3U);
370 static INLINE void opj_t1_dec_sigpass_step_raw(
374 OPJ_INT32 oneplushalf,
379 opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
381 OPJ_UINT32 const flags = *flagsp;
383 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
384 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
385 if (opj_mqc_raw_decode(mqc)) {
386 v = opj_mqc_raw_decode(mqc);
387 *datap = v ? -oneplushalf : oneplushalf;
388 opj_t1_update_flags(flagsp, ci, v, t1->w + 2, vsc);
390 *flagsp |= T1_PI_THIS << (ci * 3U);
394 #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
395 data_stride, ci, mqc, curctx, \
396 v, a, c, ct, oneplushalf, vsc) \
398 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
399 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
400 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
401 opj_t1_setcurctx(curctx, ctxt1); \
402 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
404 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
406 flagsp[-1], flagsp[1], \
408 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
409 OPJ_UINT32 spb = opj_t1_getspb(lu); \
410 opj_t1_setcurctx(curctx, ctxt2); \
411 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
413 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
414 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
416 flags |= T1_PI_THIS << (ci * 3U); \
420 static INLINE void opj_t1_dec_sigpass_step_mqc(
424 OPJ_INT32 oneplushalf,
426 OPJ_UINT32 flags_stride,
431 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
432 opj_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
433 0, ci, mqc, mqc->curctx,
434 v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
437 static void opj_t1_enc_sigpass(opj_t1_t *t1,
445 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
446 opj_flag_t* f = &T1_FLAGS(0, 0);
447 OPJ_UINT32 const extra = 2;
451 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
453 for (k = 0; k < (t1->h & ~3U); k += 4) {
455 fprintf(stderr, " k=%d\n", k);
457 for (i = 0; i < t1->w; ++i) {
459 fprintf(stderr, " i=%d\n", i);
462 /* Nothing to do for any of the 4 data points */
466 opj_t1_enc_sigpass_step(
469 &t1->data[((k + 0) * t1->data_stride) + i],
474 0, cblksty & J2K_CCP_CBLKSTY_VSC);
475 opj_t1_enc_sigpass_step(
478 &t1->data[((k + 1) * t1->data_stride) + i],
484 opj_t1_enc_sigpass_step(
487 &t1->data[((k + 2) * t1->data_stride) + i],
493 opj_t1_enc_sigpass_step(
496 &t1->data[((k + 3) * t1->data_stride) + i],
510 fprintf(stderr, " k=%d\n", k);
512 for (i = 0; i < t1->w; ++i) {
514 fprintf(stderr, " i=%d\n", i);
517 /* Nothing to do for any of the 4 data points */
521 for (j = k; j < t1->h; ++j) {
522 opj_t1_enc_sigpass_step(
525 &t1->data[(j * t1->data_stride) + i],
531 (j == k && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
538 static void opj_t1_dec_sigpass_raw(
543 OPJ_INT32 one, half, oneplushalf;
545 OPJ_INT32 *data = t1->data;
546 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
547 const OPJ_UINT32 l_w = t1->w;
550 oneplushalf = one | half;
552 for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
553 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
554 opj_flag_t flags = *flagsp;
556 opj_t1_dec_sigpass_step_raw(
561 cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
563 opj_t1_dec_sigpass_step_raw(
570 opj_t1_dec_sigpass_step_raw(
577 opj_t1_dec_sigpass_step_raw(
588 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
589 for (j = 0; j < t1->h - k; ++j) {
590 opj_t1_dec_sigpass_step_raw(
595 cblksty & J2K_CCP_CBLKSTY_VSC, /* vsc */
602 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, vsc, w, h, flags_stride) \
604 OPJ_INT32 one, half, oneplushalf; \
605 OPJ_UINT32 i, j, k; \
606 register OPJ_INT32 *data = t1->data; \
607 register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
608 const OPJ_UINT32 l_w = w; \
609 opj_mqc_t* mqc = &(t1->mqc); \
610 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
611 register OPJ_UINT32 v; \
614 oneplushalf = one | half; \
615 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
616 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
617 opj_flag_t flags = *flagsp; \
619 opj_t1_dec_sigpass_step_mqc_macro( \
620 flags, flagsp, flags_stride, data, \
621 l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf, vsc); \
622 opj_t1_dec_sigpass_step_mqc_macro( \
623 flags, flagsp, flags_stride, data, \
624 l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
625 opj_t1_dec_sigpass_step_mqc_macro( \
626 flags, flagsp, flags_stride, data, \
627 l_w, 2, 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, 3, mqc, curctx, v, a, c, ct, oneplushalf, OPJ_FALSE); \
635 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
637 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
638 for (j = 0; j < h - k; ++j) { \
639 opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
640 data + j * l_w, oneplushalf, j, flags_stride, vsc); \
646 static void opj_t1_dec_sigpass_mqc_64x64_novsc(
650 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
653 static void opj_t1_dec_sigpass_mqc_64x64_vsc(
657 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
660 static void opj_t1_dec_sigpass_mqc_generic_novsc(
664 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
668 static void opj_t1_dec_sigpass_mqc_generic_vsc(
672 opj_t1_dec_sigpass_mqc_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
676 static void opj_t1_dec_sigpass_mqc(
681 if (t1->w == 64 && t1->h == 64) {
682 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
683 opj_t1_dec_sigpass_mqc_64x64_vsc(t1, bpno);
685 opj_t1_dec_sigpass_mqc_64x64_novsc(t1, bpno);
688 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
689 opj_t1_dec_sigpass_mqc_generic_vsc(t1, bpno);
691 opj_t1_dec_sigpass_mqc_generic_novsc(t1, bpno);
697 Encode refinement pass step
699 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
710 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
712 OPJ_UINT32 const shift_flags =
713 (*flagsp >> (ci * 3U));
715 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
716 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
717 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
719 v = opj_int_abs(*datap) & one ? 1 : 0;
721 fprintf(stderr, " ctxt=%d\n", ctxt);
723 opj_mqc_setcurctx(mqc, ctxt);
724 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
725 opj_mqc_bypass_enc(mqc, v);
727 opj_mqc_encode(mqc, v);
729 *flagsp |= T1_MU_THIS << (ci * 3U);
734 static INLINE void opj_t1_dec_refpass_step_raw(
743 opj_mqc_t *mqc = &(t1->mqc); /* RAW component */
745 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
746 (T1_SIGMA_THIS << (ci * 3U))) {
747 v = opj_mqc_raw_decode(mqc);
748 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
749 *flagsp |= T1_MU_THIS << (ci * 3U);
753 #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
754 mqc, curctx, v, a, c, ct, poshalf) \
756 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
757 (T1_SIGMA_THIS << (ci * 3U))) { \
758 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
759 opj_t1_setcurctx(curctx, ctxt); \
760 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
761 data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
762 flags |= T1_MU_THIS << (ci * 3U); \
766 static INLINE void opj_t1_dec_refpass_step_mqc(
775 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
776 opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
777 mqc, mqc->curctx, v, mqc->a, mqc->c,
781 static void opj_t1_enc_refpass(
788 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
789 opj_flag_t* f = &T1_FLAGS(0, 0);
790 const OPJ_UINT32 extra = 2U;
794 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
796 for (k = 0; k < (t1->h & ~3U); k += 4) {
798 fprintf(stderr, " k=%d\n", k);
800 for (i = 0; i < t1->w; ++i) {
802 fprintf(stderr, " i=%d\n", i);
804 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
805 /* none significant */
809 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
810 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
811 /* all processed by sigpass */
816 opj_t1_enc_refpass_step(
819 &t1->data[((k + 0) * t1->data_stride) + i],
825 opj_t1_enc_refpass_step(
828 &t1->data[((k + 1) * t1->data_stride) + i],
834 opj_t1_enc_refpass_step(
837 &t1->data[((k + 2) * t1->data_stride) + i],
843 opj_t1_enc_refpass_step(
846 &t1->data[((k + 3) * t1->data_stride) + i],
860 fprintf(stderr, " k=%d\n", k);
862 for (i = 0; i < t1->w; ++i) {
864 fprintf(stderr, " i=%d\n", i);
866 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
867 /* none significant */
871 for (j = k; j < t1->h; ++j) {
872 opj_t1_enc_refpass_step(
875 &t1->data[(j * t1->data_stride) + i],
888 static void opj_t1_dec_refpass_raw(
892 OPJ_INT32 one, poshalf;
894 OPJ_INT32 *data = t1->data;
895 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
896 const OPJ_UINT32 l_w = t1->w;
899 for (k = 0; k < (t1->h & ~3U); k += 4, flagsp += 2, data += 3 * l_w) {
900 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
901 opj_flag_t flags = *flagsp;
903 opj_t1_dec_refpass_step_raw(
909 opj_t1_dec_refpass_step_raw(
915 opj_t1_dec_refpass_step_raw(
921 opj_t1_dec_refpass_step_raw(
931 for (i = 0; i < l_w; ++i, ++flagsp, ++data) {
932 for (j = 0; j < t1->h - k; ++j) {
933 opj_t1_dec_refpass_step_raw(
944 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
946 OPJ_INT32 one, poshalf; \
947 OPJ_UINT32 i, j, k; \
948 register OPJ_INT32 *data = t1->data; \
949 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
950 const OPJ_UINT32 l_w = w; \
951 opj_mqc_t* mqc = &(t1->mqc); \
952 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
953 register OPJ_UINT32 v; \
955 poshalf = one >> 1; \
956 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
957 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
958 opj_flag_t flags = *flagsp; \
960 opj_t1_dec_refpass_step_mqc_macro( \
961 flags, data, l_w, 0, \
962 mqc, curctx, v, a, c, ct, poshalf); \
963 opj_t1_dec_refpass_step_mqc_macro( \
964 flags, data, l_w, 1, \
965 mqc, curctx, v, a, c, ct, poshalf); \
966 opj_t1_dec_refpass_step_mqc_macro( \
967 flags, data, l_w, 2, \
968 mqc, curctx, v, a, c, ct, poshalf); \
969 opj_t1_dec_refpass_step_mqc_macro( \
970 flags, data, l_w, 3, \
971 mqc, curctx, v, a, c, ct, poshalf); \
976 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
978 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
979 for (j = 0; j < h - k; ++j) { \
980 opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
986 static void opj_t1_dec_refpass_mqc_64x64(
990 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
993 static void opj_t1_dec_refpass_mqc_generic(
997 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1000 static void opj_t1_dec_refpass_mqc(
1004 if (t1->w == 64 && t1->h == 64) {
1005 opj_t1_dec_refpass_mqc_64x64(t1, bpno);
1007 opj_t1_dec_refpass_mqc_generic(t1, bpno);
1012 Encode clean-up pass step
1014 static void opj_t1_enc_clnpass_step(
1028 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1030 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1031 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1033 if ((*flagsp & check) == check) {
1035 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1036 } else if (runlen == 1) {
1037 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1038 } else if (runlen == 2) {
1039 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1040 } else if (runlen == 3) {
1041 *flagsp &= ~(T1_PI_3);
1046 for (ci = runlen; ci < lim; ++ci) {
1052 if ((agg != 0) && (ci == runlen)) {
1056 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1057 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1058 #ifdef DEBUG_ENC_CLN
1059 printf(" ctxt1=%d\n", ctxt1);
1061 opj_mqc_setcurctx(mqc, ctxt1);
1062 v = opj_int_abs(*datap) & one ? 1 : 0;
1063 opj_mqc_encode(mqc, v);
1065 OPJ_UINT32 ctxt2, spb;
1068 lu = opj_t1_getctxtno_sc_or_spb_index(
1070 flagsp[-1], flagsp[1],
1072 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1074 ctxt2 = opj_t1_getctxno_sc(lu);
1075 #ifdef DEBUG_ENC_CLN
1076 printf(" ctxt2=%d\n", ctxt2);
1078 opj_mqc_setcurctx(mqc, ctxt2);
1080 v = *datap < 0 ? 1U : 0U;
1081 spb = opj_t1_getspb(lu);
1082 #ifdef DEBUG_ENC_CLN
1083 printf(" spb=%d\n", spb);
1085 opj_mqc_encode(mqc, v ^ spb);
1086 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == 0)) ? 1 : 0;
1087 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U, vsc);
1090 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1091 datap += t1->data_stride;
1095 #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
1096 flags, flagsp, flags_stride, data, \
1097 data_stride, ci, mqc, curctx, \
1098 v, a, c, ct, oneplushalf, vsc) \
1100 if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
1103 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
1104 opj_t1_setcurctx(curctx, ctxt1); \
1105 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1110 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
1111 flags, flagsp[-1], flagsp[1], \
1113 opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
1114 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1115 v = v ^ opj_t1_getspb(lu); \
1116 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
1117 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride, vsc); \
1123 static void opj_t1_dec_clnpass_step(
1127 OPJ_INT32 oneplushalf,
1133 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1134 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
1135 *flagsp, flagsp, t1->w + 2U, datap,
1136 0, ci, mqc, mqc->curctx,
1137 v, mqc->a, mqc->c, mqc->ct, oneplushalf, vsc);
1140 static void opj_t1_enc_clnpass(
1147 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1148 OPJ_UINT32 agg, runlen;
1150 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1153 #ifdef DEBUG_ENC_CLN
1154 printf("enc_clnpass: bpno=%d\n", bpno);
1156 for (k = 0; k < (t1->h & ~3U); k += 4) {
1157 #ifdef DEBUG_ENC_CLN
1158 printf(" k=%d\n", k);
1160 for (i = 0; i < t1->w; ++i) {
1161 #ifdef DEBUG_ENC_CLN
1162 printf(" i=%d\n", i);
1164 agg = !(T1_FLAGS(i, k));
1165 #ifdef DEBUG_ENC_CLN
1166 printf(" agg=%d\n", agg);
1169 for (runlen = 0; runlen < 4; ++runlen) {
1170 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1174 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1175 opj_mqc_encode(mqc, runlen != 4);
1179 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1180 opj_mqc_encode(mqc, runlen >> 1);
1181 opj_mqc_encode(mqc, runlen & 1);
1185 opj_t1_enc_clnpass_step(
1188 &t1->data[((k + runlen) * t1->data_stride) + i],
1201 #ifdef DEBUG_ENC_CLN
1202 printf(" k=%d\n", k);
1204 for (i = 0; i < t1->w; ++i) {
1205 #ifdef DEBUG_ENC_CLN
1206 printf(" i=%d\n", i);
1207 printf(" agg=%d\n", agg);
1209 opj_t1_enc_clnpass_step(
1212 &t1->data[((k + runlen) * t1->data_stride) + i],
1224 #define opj_t1_dec_clnpass_internal(t1, bpno, vsc, w, h, flags_stride) \
1226 OPJ_INT32 one, half, oneplushalf; \
1227 OPJ_UINT32 runlen; \
1228 OPJ_UINT32 i, j, k; \
1229 const OPJ_UINT32 l_w = w; \
1230 opj_mqc_t* mqc = &(t1->mqc); \
1231 register OPJ_INT32 *data = t1->data; \
1232 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1233 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1234 register OPJ_UINT32 v; \
1237 oneplushalf = one | half; \
1238 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1239 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1240 opj_flag_t flags = *flagsp; \
1242 OPJ_UINT32 partial = OPJ_TRUE; \
1243 opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
1244 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1248 opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
1249 opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
1250 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1251 runlen = (runlen << 1) | v; \
1254 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
1255 flags, flagsp, flags_stride, data, \
1256 l_w, 0, mqc, curctx, \
1257 v, a, c, ct, oneplushalf, vsc); \
1258 partial = OPJ_FALSE; \
1261 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1262 flags, flagsp, flags_stride, data, \
1263 l_w, 1, mqc, curctx, \
1264 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1265 partial = OPJ_FALSE; \
1268 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1269 flags, flagsp, flags_stride, data, \
1270 l_w, 2, mqc, curctx, \
1271 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1272 partial = OPJ_FALSE; \
1275 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1276 flags, flagsp, flags_stride, data, \
1277 l_w, 3, mqc, curctx, \
1278 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1282 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1283 flags, flagsp, flags_stride, data, \
1284 l_w, 0, mqc, curctx, \
1285 v, a, c, ct, oneplushalf, vsc); \
1286 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1287 flags, flagsp, flags_stride, data, \
1288 l_w, 1, mqc, curctx, \
1289 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1290 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1291 flags, flagsp, flags_stride, data, \
1292 l_w, 2, mqc, curctx, \
1293 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1294 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1295 flags, flagsp, flags_stride, data, \
1296 l_w, 3, mqc, curctx, \
1297 v, a, c, ct, oneplushalf, OPJ_FALSE); \
1299 *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1302 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1304 for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
1305 for (j = 0; j < h - k; ++j) { \
1306 opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j, vsc); \
1308 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1313 static void opj_t1_dec_clnpass_check_segsym(opj_t1_t *t1, OPJ_INT32 cblksty)
1315 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
1316 opj_mqc_t* mqc = &(t1->mqc);
1318 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1319 opj_mqc_decode(v, mqc);
1320 opj_mqc_decode(v2, mqc);
1322 opj_mqc_decode(v2, mqc);
1324 opj_mqc_decode(v2, mqc);
1328 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
1334 static void opj_t1_dec_clnpass_64x64_novsc(
1338 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, 64, 64, 66);
1341 static void opj_t1_dec_clnpass_64x64_vsc(
1345 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, 64, 64, 66);
1348 static void opj_t1_dec_clnpass_generic_novsc(
1352 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_FALSE, t1->w, t1->h,
1356 static void opj_t1_dec_clnpass_generic_vsc(
1360 opj_t1_dec_clnpass_internal(t1, bpno, OPJ_TRUE, t1->w, t1->h,
1364 static void opj_t1_dec_clnpass(
1369 if (t1->w == 64 && t1->h == 64) {
1370 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1371 opj_t1_dec_clnpass_64x64_vsc(t1, bpno);
1373 opj_t1_dec_clnpass_64x64_novsc(t1, bpno);
1376 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1377 opj_t1_dec_clnpass_generic_vsc(t1, bpno);
1379 opj_t1_dec_clnpass_generic_novsc(t1, bpno);
1382 opj_t1_dec_clnpass_check_segsym(t1, cblksty);
1386 /** mod fixed_quality */
1387 static OPJ_FLOAT64 opj_t1_getwmsedec(
1394 OPJ_FLOAT64 stepsize,
1395 OPJ_UINT32 numcomps,
1396 const OPJ_FLOAT64 * mct_norms,
1397 OPJ_UINT32 mct_numcomps)
1399 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1400 OPJ_ARG_NOT_USED(numcomps);
1402 if (mct_norms && (compno < mct_numcomps)) {
1403 w1 = mct_norms[compno];
1407 w2 = opj_dwt_getnorm(level, orient);
1408 } else { /* if (qmfbid == 0) */
1409 w2 = opj_dwt_getnorm_real(level, orient);
1412 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1413 wmsedec *= wmsedec * nmsedec / 8192.0;
1418 static OPJ_BOOL opj_t1_allocate_buffers(
1424 OPJ_UINT32 flags_stride;
1426 /* encoder uses tile buffer, so no need to allocate */
1430 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1431 /* Overflow check */
1432 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1433 /* FIXME event manager error callback */
1437 datasize = (size_t)w * h;
1439 /* Overflow check */
1440 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1441 /* FIXME event manager error callback */
1445 if (datasize > (size_t)t1->datasize) {
1446 opj_aligned_free(t1->data);
1447 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1449 /* FIXME event manager error callback */
1452 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1453 /* TODO remove this if t1->datasize type changes to size_t */
1454 /* Overflow check */
1455 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1456 /* FIXME event manager error callback */
1460 t1->datasize = (OPJ_UINT32)datasize;
1462 /* memset first arg is declared to never be null by gcc */
1463 if (t1->data != NULL) {
1464 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1468 /* Overflow check */
1469 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1470 /* FIXME event manager error callback */
1473 flags_stride = w + 2U; /* can't be 0U */
1475 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1476 /* Overflow check */
1477 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1478 /* FIXME event manager error callback */
1482 flagssize = (h + 3U) / 4U + 2U;
1484 /* Overflow check */
1485 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1486 /* FIXME event manager error callback */
1489 flagssize *= (size_t)flags_stride;
1494 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1496 if (flagssize > (size_t)t1->flagssize) {
1497 /* Overflow check */
1498 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1499 /* FIXME event manager error callback */
1502 opj_aligned_free(t1->flags);
1503 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1506 /* FIXME event manager error callback */
1509 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1510 /* TODO remove this if t1->flagssize type changes to size_t */
1511 /* Overflow check */
1512 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1513 /* FIXME event manager error callback */
1518 t1->flagssize = (OPJ_UINT32)flagssize;
1520 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1523 for (x = 0; x < flags_stride; ++x) {
1524 /* magic value to hopefully stop any passes being interested in this entry */
1525 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1528 p = &t1->flags[((flags_height + 1) * flags_stride)];
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);
1536 p = &t1->flags[((flags_height) * flags_stride)];
1538 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1539 } else if (h % 4 == 2) {
1540 v |= T1_PI_2 | T1_PI_3;
1541 } else if (h % 4 == 3) {
1544 for (x = 0; x < flags_stride; ++x) {
1556 /* ----------------------------------------------------------------------- */
1558 /* ----------------------------------------------------------------------- */
1560 * Creates a new Tier 1 handle
1561 * and initializes the look-up tables of the Tier-1 coder/decoder
1562 * @return a new T1 handle if successful, returns NULL otherwise
1564 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1566 opj_t1_t *l_t1 = 00;
1568 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1573 l_t1->encoder = isEncoder;
1580 * Destroys a previously created T1 handle
1582 * @param p_t1 Tier 1 handle to destroy
1584 void opj_t1_destroy(opj_t1_t *p_t1)
1590 /* encoder uses tile buffer, so no need to free */
1591 if (!p_t1->encoder && p_t1->data) {
1592 opj_aligned_free(p_t1->data);
1597 opj_aligned_free(p_t1->flags);
1606 opj_tcd_cblk_dec_t* cblk;
1607 opj_tcd_band_t* band;
1608 opj_tcd_tilecomp_t* tilec;
1610 volatile OPJ_BOOL* pret;
1611 } opj_t1_cblk_decode_processing_job_t;
1613 static void opj_t1_destroy_wrapper(void* t1)
1615 opj_t1_destroy((opj_t1_t*) t1);
1618 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1620 opj_tcd_cblk_dec_t* cblk;
1621 opj_tcd_band_t* band;
1622 opj_tcd_tilecomp_t* tilec;
1624 OPJ_INT32* OPJ_RESTRICT datap;
1625 OPJ_UINT32 cblk_w, cblk_h;
1628 opj_t1_cblk_decode_processing_job_t* job;
1633 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1639 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1641 if (!*(job->pret)) {
1646 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1648 t1 = opj_t1_create(OPJ_FALSE);
1649 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1652 if (OPJ_FALSE == opj_t1_decode_cblk(
1656 (OPJ_UINT32)tccp->roishift,
1658 *(job->pret) = OPJ_FALSE;
1663 x = cblk->x0 - band->x0;
1664 y = cblk->y0 - band->y0;
1665 if (band->bandno & 1) {
1666 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1667 x += pres->x1 - pres->x0;
1669 if (band->bandno & 2) {
1670 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1671 y += pres->y1 - pres->y0;
1678 if (tccp->roishift) {
1679 OPJ_INT32 thresh = 1 << tccp->roishift;
1680 for (j = 0; j < cblk_h; ++j) {
1681 for (i = 0; i < cblk_w; ++i) {
1682 OPJ_INT32 val = datap[(j * cblk_w) + i];
1683 OPJ_INT32 mag = abs(val);
1684 if (mag >= thresh) {
1685 mag >>= tccp->roishift;
1686 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1691 if (tccp->qmfbid == 1) {
1692 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1694 for (j = 0; j < cblk_h; ++j) {
1696 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1697 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1698 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1699 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1700 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1701 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1702 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1703 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1704 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1706 for (; i < cblk_w; ++i) {
1707 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1708 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1711 } else { /* if (tccp->qmfbid == 0) */
1712 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1713 tile_w + (OPJ_UINT32)x];
1714 for (j = 0; j < cblk_h; ++j) {
1715 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1716 for (i = 0; i < cblk_w; ++i) {
1717 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1730 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1731 volatile OPJ_BOOL* pret,
1732 opj_tcd_tilecomp_t* tilec,
1736 OPJ_UINT32 resno, bandno, precno, cblkno;
1738 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1739 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1741 for (bandno = 0; bandno < res->numbands; ++bandno) {
1742 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1744 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1745 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1747 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1748 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1749 opj_t1_cblk_decode_processing_job_t* job;
1751 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1752 sizeof(opj_t1_cblk_decode_processing_job_t));
1763 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1776 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1777 opj_tcd_cblk_dec_t* cblk,
1779 OPJ_UINT32 roishift,
1782 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1784 OPJ_INT32 bpno_plus_one;
1785 OPJ_UINT32 passtype;
1786 OPJ_UINT32 segno, passno;
1787 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1789 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
1791 if (!opj_t1_allocate_buffers(
1793 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1794 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1798 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1801 opj_mqc_resetstates(mqc);
1802 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1803 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1804 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1806 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1807 opj_tcd_seg_t *seg = &cblk->segs[segno];
1810 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1811 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1812 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1813 if (seg->data == 00) {
1816 if (type == T1_TYPE_RAW) {
1817 opj_mqc_raw_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len,
1818 OPJ_COMMON_CBLK_DATA_EXTRA);
1820 opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex, seg->len,
1821 OPJ_COMMON_CBLK_DATA_EXTRA);
1824 for (passno = 0; (passno < seg->real_num_passes) &&
1825 (bpno_plus_one >= 1); ++passno) {
1828 if (type == T1_TYPE_RAW) {
1829 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1831 opj_t1_dec_sigpass_mqc(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1835 if (type == T1_TYPE_RAW) {
1836 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
1838 opj_t1_dec_refpass_mqc(t1, bpno_plus_one);
1842 opj_t1_dec_clnpass(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1846 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
1847 opj_mqc_resetstates(mqc);
1848 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1849 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1850 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1852 if (++passtype == 3) {
1858 opq_mqc_finish_dec(mqc);
1867 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
1868 opj_tcd_tile_t *tile,
1870 const OPJ_FLOAT64 * mct_norms,
1871 OPJ_UINT32 mct_numcomps
1874 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
1876 tile->distotile = 0; /* fixed_quality */
1878 for (compno = 0; compno < tile->numcomps; ++compno) {
1879 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
1880 opj_tccp_t* tccp = &tcp->tccps[compno];
1881 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1883 for (resno = 0; resno < tilec->numresolutions; ++resno) {
1884 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1886 for (bandno = 0; bandno < res->numbands; ++bandno) {
1887 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1888 OPJ_INT32 bandconst;
1890 /* Skip empty bands */
1891 if (opj_tcd_is_band_empty(band)) {
1895 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
1896 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1897 opj_tcd_precinct_t *prc = &band->precincts[precno];
1899 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
1900 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
1901 OPJ_INT32* OPJ_RESTRICT tiledp;
1904 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
1906 OPJ_INT32 x = cblk->x0 - band->x0;
1907 OPJ_INT32 y = cblk->y0 - band->y0;
1908 if (band->bandno & 1) {
1909 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1910 x += pres->x1 - pres->x0;
1912 if (band->bandno & 2) {
1913 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1914 y += pres->y1 - pres->y0;
1917 if (!opj_t1_allocate_buffers(
1919 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1920 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1926 tileLineAdvance = tile_w - cblk_w;
1928 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
1930 t1->data_stride = tile_w;
1931 if (tccp->qmfbid == 1) {
1932 for (j = 0; j < cblk_h; ++j) {
1933 for (i = 0; i < cblk_w; ++i) {
1934 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
1937 tileIndex += tileLineAdvance;
1939 } else { /* if (tccp->qmfbid == 0) */
1940 for (j = 0; j < cblk_h; ++j) {
1941 for (i = 0; i < cblk_w; ++i) {
1942 OPJ_INT32 tmp = tiledp[tileIndex];
1949 tileIndex += tileLineAdvance;
1958 tilec->numresolutions - 1 - resno,
1975 /* Returns whether the pass (bpno, passtype) is terminated */
1976 static int opj_t1_enc_is_term_pass(opj_tcd_cblk_enc_t* cblk,
1979 OPJ_UINT32 passtype)
1981 /* Is it the last cleanup pass ? */
1982 if (passtype == 2 && bpno == 0) {
1986 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
1990 if ((cblksty & J2K_CCP_CBLKSTY_LAZY)) {
1991 /* For bypass arithmetic bypass, terminate the 4th cleanup pass */
1992 if ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2)) {
1995 /* and beyond terminate all the magnitude refinement passes (in raw) */
1996 /* and cleanup passes (in MQC) */
1997 if ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype > 0)) {
2006 /** mod fixed_quality */
2007 static void opj_t1_encode_cblk(opj_t1_t *t1,
2008 opj_tcd_cblk_enc_t* cblk,
2013 OPJ_FLOAT64 stepsize,
2015 OPJ_UINT32 numcomps,
2016 opj_tcd_tile_t * tile,
2017 const OPJ_FLOAT64 * mct_norms,
2018 OPJ_UINT32 mct_numcomps)
2020 OPJ_FLOAT64 cumwmsedec = 0.0;
2022 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2026 OPJ_UINT32 passtype;
2027 OPJ_INT32 nmsedec = 0;
2030 OPJ_BYTE type = T1_TYPE_MQ;
2031 OPJ_FLOAT64 tempwmsedec;
2034 printf("encode_cblk(x=%d,y=%d,x1=%d,y1=%d,orient=%d,compno=%d,level=%d\n",
2035 cblk->x0, cblk->y0, cblk->x1, cblk->y1, orient, compno, level);
2038 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2041 for (i = 0; i < t1->w; ++i) {
2042 for (j = 0; j < t1->h; ++j) {
2043 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2044 max = opj_int_max(max, tmp);
2048 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2049 T1_NMSEDEC_FRACBITS) : 0;
2051 bpno = (OPJ_INT32)(cblk->numbps - 1);
2054 opj_mqc_resetstates(mqc);
2055 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2056 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2057 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2058 opj_mqc_init_enc(mqc, cblk->data);
2060 for (passno = 0; bpno >= 0; ++passno) {
2061 opj_tcd_pass_t *pass = &cblk->passes[passno];
2062 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2063 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2065 /* If the previous pass was terminating, we need to reset the encoder */
2066 if (passno > 0 && cblk->passes[passno - 1].term) {
2067 if (type == T1_TYPE_RAW) {
2068 opj_mqc_bypass_init_enc(mqc);
2070 opj_mqc_restart_init_enc(mqc);
2076 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2079 opj_t1_enc_refpass(t1, bpno, &nmsedec, type);
2082 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2083 /* code switch SEGMARK (i.e. SEGSYM) */
2084 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2085 opj_mqc_segmark_enc(mqc);
2091 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2092 stepsize, numcomps, mct_norms, mct_numcomps) ;
2093 cumwmsedec += tempwmsedec;
2094 tile->distotile += tempwmsedec;
2095 pass->distortiondec = cumwmsedec;
2097 if (opj_t1_enc_is_term_pass(cblk, cblksty, bpno, passtype)) {
2098 /* If it is a terminated pass, terminate it */
2099 if (type == T1_TYPE_RAW) {
2100 opj_mqc_bypass_flush_enc(mqc, cblksty & J2K_CCP_CBLKSTY_PTERM);
2102 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2103 opj_mqc_erterm_enc(mqc);
2109 pass->rate = opj_mqc_numbytes(mqc);
2111 /* Non terminated pass */
2112 OPJ_UINT32 rate_extra_bytes;
2113 if (type == T1_TYPE_RAW) {
2114 rate_extra_bytes = opj_mqc_bypass_get_extra_bytes(
2115 mqc, (cblksty & J2K_CCP_CBLKSTY_PTERM));
2117 rate_extra_bytes = 3;
2120 pass->rate = opj_mqc_numbytes(mqc) + rate_extra_bytes;
2123 if (++passtype == 3) {
2128 /* Code-switch "RESET" */
2129 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2130 opj_mqc_reset_enc(mqc);
2134 cblk->totalpasses = passno;
2136 if (cblk->totalpasses) {
2137 /* Make sure that pass rates are increasing */
2138 OPJ_UINT32 last_pass_rate = opj_mqc_numbytes(mqc);
2139 for (passno = cblk->totalpasses; passno > 0;) {
2140 opj_tcd_pass_t *pass = &cblk->passes[--passno];
2141 if (pass->rate > last_pass_rate) {
2142 pass->rate = last_pass_rate;
2144 last_pass_rate = pass->rate;
2149 for (passno = 0; passno < cblk->totalpasses; passno++) {
2150 opj_tcd_pass_t *pass = &cblk->passes[passno];
2152 /* Prevent generation of FF as last data byte of a pass*/
2153 /* For terminating passes, the flushing procedure ensured this already */
2154 assert(pass->rate > 0);
2155 if (cblk->data[pass->rate - 1] == 0xFF) {
2158 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2162 printf(" len=%d\n", (cblk->totalpasses) ? opj_mqc_numbytes(mqc) : 0);
2164 /* Check that there not 0xff >=0x90 sequences */
2165 if (cblk->totalpasses) {
2167 OPJ_UINT32 len = opj_mqc_numbytes(mqc);
2168 for (i = 1; i < len; ++i) {
2169 if (cblk->data[i - 1] == 0xff && cblk->data[i] >= 0x90) {
2170 printf("0xff %02x at offset %d\n", cblk->data[i], i - 1);