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->flags_stride])
49 /** @name Local static functions */
52 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
53 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
54 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
55 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
56 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
57 OPJ_UINT32 s, OPJ_UINT32 stride);
61 Decode significant pass
64 static INLINE void opj_t1_dec_sigpass_step_raw(
68 OPJ_INT32 oneplushalf,
71 static INLINE void opj_t1_dec_sigpass_step_mqc(
75 OPJ_INT32 oneplushalf,
77 OPJ_UINT32 flags_stride);
78 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
82 OPJ_INT32 oneplushalf,
88 Encode significant pass
90 static void opj_t1_enc_sigpass(opj_t1_t *t1,
97 Decode significant pass
99 static void opj_t1_dec_sigpass_raw(
103 static void opj_t1_dec_sigpass_mqc_vsc(
110 Encode refinement pass
112 static void opj_t1_enc_refpass(opj_t1_t *t1,
119 Decode refinement pass
121 static void opj_t1_dec_refpass_raw(
124 static void opj_t1_dec_refpass_mqc_vsc(
130 Decode refinement pass
133 static INLINE void opj_t1_dec_refpass_step_raw(
140 static INLINE void opj_t1_dec_refpass_step_mqc(
147 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
160 static void opj_t1_dec_clnpass_step_partial(
164 OPJ_INT32 oneplushalf,
166 static void opj_t1_dec_clnpass_step(
170 OPJ_INT32 oneplushalf,
172 static void opj_t1_dec_clnpass_step_vsc(
176 OPJ_INT32 oneplushalf,
183 static void opj_t1_enc_clnpass(
189 static OPJ_FLOAT64 opj_t1_getwmsedec(
196 OPJ_FLOAT64 stepsize,
198 const OPJ_FLOAT64 * mct_norms,
199 OPJ_UINT32 mct_numcomps);
201 static void opj_t1_encode_cblk(opj_t1_t *t1,
202 opj_tcd_cblk_enc_t* cblk,
207 OPJ_FLOAT64 stepsize,
210 opj_tcd_tile_t * tile,
211 const OPJ_FLOAT64 * mct_norms,
212 OPJ_UINT32 mct_numcomps);
217 @param cblk Code-block coding parameters
219 @param roishift Region of interest shifting value
220 @param cblksty Code-block style
222 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
223 opj_tcd_cblk_dec_t* cblk,
228 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
236 /* ----------------------------------------------------------------------- */
238 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
240 return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
243 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
249 0 pfX T1_CHI_THIS T1_LUT_SGN_W
250 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
251 2 nfX T1_CHI_THIS T1_LUT_SGN_E
252 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
253 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
254 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
255 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
256 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
259 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
262 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
263 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
265 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
267 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
269 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
273 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
275 return lut_ctxno_sc[lu];
278 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
280 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
281 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
285 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
290 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
293 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
296 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
299 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
302 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
305 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
309 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
310 OPJ_UINT32 s, OPJ_UINT32 stride)
312 /* set up to point to the north and south data points' flags words, if required */
316 /* mark target as significant */
317 *flagsp |= T1_SIGMA_4 << (3U * ci);
319 /* north-west, north, north-east */
321 north = flagsp - stride;
322 *north |= T1_SIGMA_16;
323 north[-1] |= T1_SIGMA_17;
324 north[1] |= T1_SIGMA_15;
327 /* south-west, south, south-east */
329 south = flagsp + stride;
330 *south |= T1_SIGMA_1;
331 south[-1] |= T1_SIGMA_2;
332 south[1] |= T1_SIGMA_0;
336 flagsp[-1] |= T1_SIGMA_5 << (3U * ci);
339 flagsp[1] |= T1_SIGMA_3 << (3U * ci);
345 north = flagsp - stride;
357 south = flagsp + stride;
367 Encode significant pass
369 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
382 opj_mqc_t *mqc = t1->mqc; /* MQC component */
384 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
385 T1_CHI_S) << (ci * 3U)) : ~0U;
386 OPJ_UINT32 const flags = *flagsp & vsc_mask;
388 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
389 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
390 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
391 v = opj_int_abs(*datap) & one ? 1 : 0;
393 fprintf(stderr, " ctxt1=%d\n", ctxt1);
395 opj_mqc_setcurctx(mqc, ctxt1);
396 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
397 opj_mqc_bypass_enc(mqc, v);
399 opj_mqc_encode(mqc, v);
402 /* Note: using flags instead of *flagsp & vsc_mask result */
403 /* in slow down. Probably because of register pressure */
404 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
406 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
408 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
409 v = *datap < 0 ? 1 : 0;
410 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
413 fprintf(stderr, " ctxt2=%d\n", ctxt2);
415 opj_mqc_setcurctx(mqc, ctxt2);
416 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
417 opj_mqc_bypass_enc(mqc, v);
419 OPJ_UINT32 spb = opj_t1_getspb(lu);
421 fprintf(stderr, " spb=%d\n", spb);
423 opj_mqc_encode(mqc, v ^ spb);
425 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
427 *flagsp |= T1_PI_THIS << (ci * 3U);
431 static INLINE void opj_t1_dec_sigpass_step_raw(
435 OPJ_INT32 oneplushalf,
440 opj_raw_t *raw = t1->raw; /* RAW component */
442 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
443 T1_CHI_S) << (ci * 3U)) : ~0U;
444 OPJ_UINT32 const flags = *flagsp & vsc_mask;
446 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
447 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
448 if (opj_raw_decode(raw)) {
449 v = opj_raw_decode(raw);
450 *datap = v ? -oneplushalf : oneplushalf;
451 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
453 *flagsp |= T1_PI_THIS << (ci * 3U);
457 static INLINE void opj_t1_dec_sigpass_step_mqc(
461 OPJ_INT32 oneplushalf,
463 OPJ_UINT32 flags_stride)
467 opj_mqc_t *mqc = t1->mqc; /* MQC component */
468 OPJ_UINT32 const flags = *flagsp;
470 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
471 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
472 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
474 flagsp[-1], flagsp[1],
476 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
477 opj_mqc_setcurctx(mqc, ctxt1);
478 if (opj_mqc_decode(mqc)) {
479 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
480 OPJ_UINT32 spb = opj_t1_getspb(lu);
481 opj_mqc_setcurctx(mqc, ctxt2);
482 v = opj_mqc_decode(mqc) ^ spb;
483 *datap = v ? -oneplushalf : oneplushalf;
484 opj_t1_update_flags(flagsp, ci, v, flags_stride);
486 *flagsp |= T1_PI_THIS << (ci * 3U);
490 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
494 OPJ_INT32 oneplushalf,
500 opj_mqc_t *mqc = t1->mqc; /* MQC component */
502 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
503 T1_CHI_S) << (ci * 3U)) : ~0U;
504 OPJ_UINT32 const flags = *flagsp & vsc_mask;
506 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
507 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
508 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
510 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
512 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
513 opj_mqc_setcurctx(mqc, ctxt1);
514 if (opj_mqc_decode(mqc)) {
515 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
516 OPJ_UINT32 spb = opj_t1_getspb(lu);
517 opj_mqc_setcurctx(mqc, ctxt2);
518 v = opj_mqc_decode(mqc) ^ spb;
519 *datap = v ? -oneplushalf : oneplushalf;
520 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
522 *flagsp |= T1_PI_THIS << (ci * 3U);
527 static void opj_t1_enc_sigpass(opj_t1_t *t1,
535 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
536 opj_flag_t* f = &T1_FLAGS(0, 0);
537 OPJ_UINT32 const extra = t1->flags_stride - t1->w;
541 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
543 for (k = 0; k < (t1->h & ~3U); k += 4) {
545 fprintf(stderr, " k=%d\n", k);
547 for (i = 0; i < t1->w; ++i) {
549 fprintf(stderr, " i=%d\n", i);
552 /* Nothing to do for any of the 4 data points */
556 opj_t1_enc_sigpass_step(
559 &t1->data[((k + 0) * t1->data_stride) + i],
565 opj_t1_enc_sigpass_step(
568 &t1->data[((k + 1) * t1->data_stride) + i],
574 opj_t1_enc_sigpass_step(
577 &t1->data[((k + 2) * t1->data_stride) + i],
583 opj_t1_enc_sigpass_step(
586 &t1->data[((k + 3) * t1->data_stride) + i],
591 3, cblksty & J2K_CCP_CBLKSTY_VSC);
600 fprintf(stderr, " k=%d\n", k);
602 for (i = 0; i < t1->w; ++i) {
604 fprintf(stderr, " i=%d\n", i);
607 /* Nothing to do for any of the 4 data points */
611 for (j = k; j < t1->h; ++j) {
612 opj_t1_enc_sigpass_step(
615 &t1->data[(j * t1->data_stride) + i],
621 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
628 static void opj_t1_dec_sigpass_raw(
633 OPJ_INT32 one, half, oneplushalf, vsc;
635 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
638 oneplushalf = one | half;
639 for (k = 0; k < t1->h; k += 4) {
640 for (i = 0; i < t1->w; ++i) {
641 opj_flag_t *flagsp2 = flagsp + i;
642 for (j = k; j < k + 4 && j < t1->h; ++j) {
643 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
644 j == t1->h - 1)) ? 1 : 0;
645 opj_t1_dec_sigpass_step_raw(
648 &t1->data[(j * t1->w) + i],
654 flagsp += t1->flags_stride;
658 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
660 OPJ_INT32 one, half, oneplushalf; \
661 OPJ_UINT32 i, j, k; \
662 OPJ_INT32 *data1 = t1->data; \
663 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
666 oneplushalf = one | half; \
667 for (k = 0; k < (h & ~3u); k += 4) { \
668 for (i = 0; i < w; ++i) { \
669 OPJ_INT32 *data2 = data1 + i; \
670 opj_flag_t *flagsp2 = flagsp1 + i; \
671 if( *flagsp2 == 0 ) continue; \
672 opj_t1_dec_sigpass_step_mqc(t1, flagsp2, data2, oneplushalf, 0U, flags_stride); \
674 opj_t1_dec_sigpass_step_mqc(t1, flagsp2, data2, oneplushalf, 1U, flags_stride); \
676 opj_t1_dec_sigpass_step_mqc(t1, flagsp2, data2, oneplushalf, 2U, flags_stride); \
678 opj_t1_dec_sigpass_step_mqc(t1, flagsp2, data2, oneplushalf, 3U, flags_stride); \
682 flagsp1 += flags_stride; \
684 for (i = 0; i < w; ++i) { \
685 OPJ_INT32 *data2 = data1 + i; \
686 opj_flag_t *flagsp2 = flagsp1 + i; \
687 for (j = k; j < h; ++j) { \
688 opj_t1_dec_sigpass_step_mqc(t1, flagsp2, data2, oneplushalf, j - k, flags_stride); \
694 static void opj_t1_dec_sigpass_mqc_64x64(
698 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
701 static void opj_t1_dec_sigpass_mqc_generic(
705 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
708 static void opj_t1_dec_sigpass_mqc_vsc(
712 OPJ_INT32 one, half, oneplushalf, vsc;
714 OPJ_INT32 *data1 = t1->data;
715 opj_flag_t *flagsp1 = &T1_FLAGS(0, 0);
718 oneplushalf = one | half;
719 for (k = 0; k < (t1->h & ~3U); k += 4U) {
720 for (i = 0; i < t1->w; ++i) {
721 OPJ_INT32 *data2 = data1 + i;
722 opj_flag_t *flagsp2 = flagsp1 + i;
723 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp2, data2, oneplushalf, 0, 0U);
725 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp2, data2, oneplushalf, 0, 1U);
727 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp2, data2, oneplushalf, 0, 2U);
729 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp2, data2, oneplushalf, 1, 3U);
733 flagsp1 += t1->flags_stride;
735 for (i = 0; i < t1->w; ++i) {
736 opj_flag_t *flagsp2 = flagsp1 + i;
737 for (j = k; j < t1->h; ++j) {
738 vsc = (j == t1->h - 1) ? 1 : 0;
739 opj_t1_dec_sigpass_step_mqc_vsc(
742 &t1->data[(j * t1->w) + i],
752 Encode refinement pass step
754 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
766 opj_mqc_t *mqc = t1->mqc; /* MQC component */
768 OPJ_UINT32 const shift_flags =
770 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
772 (*flagsp >> (ci * 3U));
774 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
775 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
776 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
778 v = opj_int_abs(*datap) & one ? 1 : 0;
780 fprintf(stderr, " ctxt=%d\n", ctxt);
782 opj_mqc_setcurctx(mqc, ctxt);
783 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
784 opj_mqc_bypass_enc(mqc, v);
786 opj_mqc_encode(mqc, v);
788 *flagsp |= T1_MU_THIS << (ci * 3U);
793 static INLINE void opj_t1_dec_refpass_step_raw(
803 opj_raw_t *raw = t1->raw; /* RAW component */
805 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
806 (T1_SIGMA_THIS << (ci * 3U))) {
807 v = (OPJ_INT32)opj_raw_decode(raw);
808 t = v ? poshalf : neghalf;
809 *datap += *datap < 0 ? -t : t;
810 *flagsp |= T1_MU_THIS << (ci * 3U);
814 static INLINE void opj_t1_dec_refpass_step_mqc(
825 opj_mqc_t *mqc = t1->mqc; /* MQC component */
827 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
828 (T1_SIGMA_THIS << (ci * 3U))) {
829 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(*flagsp >> (ci * 3U));
830 opj_mqc_setcurctx(mqc, ctxt);
831 v = opj_mqc_decode(mqc);
832 t = v ? poshalf : neghalf;
833 *datap += *datap < 0 ? -t : t;
834 *flagsp |= T1_MU_THIS << (ci * 3U);
838 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
850 opj_mqc_t *mqc = t1->mqc; /* MQC component */
852 OPJ_UINT32 const shift_flags =
854 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
856 (*flagsp >> (ci * 3U));
859 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
860 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
861 opj_mqc_setcurctx(mqc, ctxt);
862 v = opj_mqc_decode(mqc);
863 t = v ? poshalf : neghalf;
864 *datap += *datap < 0 ? -t : t;
865 *flagsp |= T1_MU_THIS << (ci * 3U);
869 static void opj_t1_enc_refpass(
877 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
878 opj_flag_t* f = &T1_FLAGS(0, 0);
879 const OPJ_UINT32 extra = t1->flags_stride - t1->w;
883 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
885 for (k = 0; k < (t1->h & ~3U); k += 4) {
887 fprintf(stderr, " k=%d\n", k);
889 for (i = 0; i < t1->w; ++i) {
891 fprintf(stderr, " i=%d\n", i);
893 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
894 /* none significant */
898 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
899 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
900 /* all processed by sigpass */
905 opj_t1_enc_refpass_step(
908 &t1->data[((k + 0) * t1->data_stride) + i],
914 opj_t1_enc_refpass_step(
917 &t1->data[((k + 1) * t1->data_stride) + i],
923 opj_t1_enc_refpass_step(
926 &t1->data[((k + 2) * t1->data_stride) + i],
932 opj_t1_enc_refpass_step(
935 &t1->data[((k + 3) * t1->data_stride) + i],
940 3, cblksty & J2K_CCP_CBLKSTY_VSC);
949 fprintf(stderr, " k=%d\n", k);
951 for (i = 0; i < t1->w; ++i) {
953 fprintf(stderr, " i=%d\n", i);
955 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
956 /* none significant */
960 for (j = k; j < t1->h; ++j) {
961 opj_t1_enc_refpass_step(
964 &t1->data[(j * t1->data_stride) + i],
970 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
978 static void opj_t1_dec_refpass_raw(
982 OPJ_INT32 one, poshalf, neghalf;
984 opj_flag_t *flagsp1 = &T1_FLAGS(0, 0);
987 neghalf = bpno > 0 ? -poshalf : -1;
988 for (k = 0; k < t1->h; k += 4) {
989 for (i = 0; i < t1->w; ++i) {
990 opj_flag_t *flagsp2 = flagsp1 + i;
991 for (j = k; j < k + 4 && j < t1->h; ++j) {
992 opj_t1_dec_refpass_step_raw(
995 &t1->data[(j * t1->w) + i],
1000 flagsp1 += t1->flags_stride;
1004 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1006 OPJ_INT32 one, poshalf, neghalf; \
1007 OPJ_UINT32 i, j, k; \
1008 OPJ_INT32 *data1 = t1->data; \
1009 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1011 poshalf = one >> 1; \
1012 neghalf = bpno > 0 ? -poshalf : -1; \
1013 for (k = 0; k < (h & ~3u); k += 4) { \
1014 for (i = 0; i < w; ++i) { \
1015 OPJ_INT32 *data2 = data1 + i; \
1016 opj_flag_t *flagsp2 = flagsp1 + i; \
1017 if( *flagsp2 == 0 ) continue; \
1018 opj_t1_dec_refpass_step_mqc(t1, flagsp2, data2, poshalf, neghalf, 0U); \
1020 opj_t1_dec_refpass_step_mqc(t1, flagsp2, data2, poshalf, neghalf, 1U); \
1022 opj_t1_dec_refpass_step_mqc(t1, flagsp2, data2, poshalf, neghalf, 2U); \
1024 opj_t1_dec_refpass_step_mqc(t1, flagsp2, data2, poshalf, neghalf, 3U); \
1028 flagsp1 += flags_stride; \
1030 for (i = 0; i < w; ++i) { \
1031 OPJ_INT32 *data2 = data1 + i; \
1032 opj_flag_t *flagsp2 = flagsp1 + i; \
1033 for (j = k; j < h; ++j) { \
1034 opj_t1_dec_refpass_step_mqc(t1, flagsp2, data2, poshalf, neghalf, j - k); \
1040 static void opj_t1_dec_refpass_mqc_64x64(
1044 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1047 static void opj_t1_dec_refpass_mqc_generic(
1051 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
1054 static void opj_t1_dec_refpass_mqc_vsc(
1058 OPJ_INT32 one, poshalf, neghalf;
1061 OPJ_INT32 *data1 = t1->data;
1062 opj_flag_t *flagsp1 = &T1_FLAGS(0, 0);
1065 neghalf = bpno > 0 ? -poshalf : -1;
1066 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1067 for (i = 0; i < t1->w; ++i) {
1068 OPJ_INT32 *data2 = data1 + i;
1069 opj_flag_t *flagsp2 = flagsp1 + i;
1070 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp2, data2, poshalf, neghalf,
1073 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp2, data2, poshalf, neghalf,
1076 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp2, data2, poshalf, neghalf,
1079 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp2, data2, poshalf, neghalf,
1083 data1 += t1->w << 2;
1084 flagsp1 += t1->flags_stride;
1086 for (i = 0; i < t1->w; ++i) {
1087 opj_flag_t *flagsp2 = flagsp1 + i;
1088 for (j = k; j < t1->h; ++j) {
1089 vsc = (j == t1->h - 1) ? 1 : 0;
1090 opj_t1_dec_refpass_step_mqc_vsc(
1093 &t1->data[(j * t1->w) + i],
1102 Encode clean-up pass step
1104 static void opj_t1_enc_clnpass_step(
1118 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1120 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1121 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1123 if ((*flagsp & check) == check) {
1125 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1126 } else if (runlen == 1) {
1127 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1128 } else if (runlen == 2) {
1129 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1130 } else if (runlen == 3) {
1131 *flagsp &= ~(T1_PI_3);
1136 for (ci = runlen; ci < lim; ++ci) {
1138 OPJ_UINT32 vsc_mask;
1141 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1142 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1144 flags = *flagsp & vsc_mask;
1146 if ((agg != 0) && (ci == runlen)) {
1150 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1151 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1152 #ifdef DEBUG_ENC_CLN
1153 printf(" ctxt1=%d\n", ctxt1);
1155 opj_mqc_setcurctx(mqc, ctxt1);
1156 v = opj_int_abs(*datap) & one ? 1 : 0;
1157 opj_mqc_encode(mqc, v);
1159 OPJ_UINT32 ctxt2, spb;
1162 /* Note: using flags instead of *flagsp & vsc_mask result */
1163 /* in slow down. Probably because of register pressure */
1164 lu = opj_t1_getctxtno_sc_or_spb_index(
1166 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1168 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1170 ctxt2 = opj_t1_getctxno_sc(lu);
1171 #ifdef DEBUG_ENC_CLN
1172 printf(" ctxt2=%d\n", ctxt2);
1174 opj_mqc_setcurctx(mqc, ctxt2);
1176 v = *datap < 0 ? 1 : 0;
1177 spb = opj_t1_getspb(lu);
1178 #ifdef DEBUG_ENC_CLN
1179 printf(" spb=%d\n", spb);
1181 opj_mqc_encode(mqc, v ^ spb);
1182 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
1185 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1186 datap += t1->data_stride;
1191 static void opj_t1_dec_clnpass_step_partial(
1195 OPJ_INT32 oneplushalf,
1199 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1201 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1202 *flagsp, flagsp[-1], flagsp[1],
1204 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1205 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1206 *datap = v ? -oneplushalf : oneplushalf;
1207 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
1208 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1211 static void opj_t1_dec_clnpass_step(
1215 OPJ_INT32 oneplushalf,
1220 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1221 if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1222 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1223 opj_mqc_setcurctx(mqc, ctxt1);
1224 if (opj_mqc_decode(mqc)) {
1225 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1226 *flagsp, flagsp[-1], flagsp[1],
1228 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1229 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1230 *datap = v ? -oneplushalf : oneplushalf;
1231 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
1234 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1237 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1241 OPJ_INT32 oneplushalf,
1243 OPJ_UINT32 flags_stride)
1247 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1249 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1250 opj_mqc_setcurctx(mqc, ctxt1);
1251 if (opj_mqc_decode(mqc)) {
1252 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1253 *flagsp, flagsp[-1], flagsp[1],
1255 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1256 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1257 *datap = v ? -oneplushalf : oneplushalf;
1258 opj_t1_update_flags(flagsp, ci, v, flags_stride);
1262 static void opj_t1_dec_clnpass_step_vsc(
1266 OPJ_INT32 oneplushalf,
1273 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1275 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1278 OPJ_UINT32 flags = *flagsp & vsc_mask;
1282 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1283 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1284 opj_mqc_setcurctx(mqc, ctxt1);
1285 if (opj_mqc_decode(mqc)) {
1288 lu = opj_t1_getctxtno_sc_or_spb_index(
1289 *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1290 flagsp[1] & vsc_mask,
1292 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1293 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1294 *datap = v ? -oneplushalf : oneplushalf;
1295 opj_t1_update_flags(flagsp, ci, v, t1->flags_stride);
1298 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1301 static void opj_t1_enc_clnpass(
1308 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1309 OPJ_UINT32 agg, runlen;
1311 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1313 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1314 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1318 #ifdef DEBUG_ENC_CLN
1319 printf("enc_clnpass: bpno=%d\n", bpno);
1321 for (k = 0; k < (t1->h & ~3U); k += 4) {
1322 #ifdef DEBUG_ENC_CLN
1323 printf(" k=%d\n", k);
1325 for (i = 0; i < t1->w; ++i) {
1326 #ifdef DEBUG_ENC_CLN
1327 printf(" i=%d\n", i);
1329 agg = !(T1_FLAGS(i, k) & agg_mask);
1330 #ifdef DEBUG_ENC_CLN
1331 printf(" agg=%d\n", agg);
1334 for (runlen = 0; runlen < 4; ++runlen) {
1335 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1339 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1340 opj_mqc_encode(mqc, runlen != 4);
1344 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1345 opj_mqc_encode(mqc, runlen >> 1);
1346 opj_mqc_encode(mqc, runlen & 1);
1350 opj_t1_enc_clnpass_step(
1353 &t1->data[((k + runlen) * t1->data_stride) + i],
1366 #ifdef DEBUG_ENC_CLN
1367 printf(" k=%d\n", k);
1369 for (i = 0; i < t1->w; ++i) {
1370 #ifdef DEBUG_ENC_CLN
1371 printf(" i=%d\n", i);
1372 printf(" agg=%d\n", agg);
1374 opj_t1_enc_clnpass_step(
1377 &t1->data[((k + runlen) * t1->data_stride) + i],
1389 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1391 OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1392 OPJ_UINT32 runlen; \
1393 OPJ_UINT32 i, j, k; \
1394 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1396 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1400 oneplushalf = one | half; \
1401 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1402 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1403 const OPJ_UINT32 agg_mask = ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1404 for (k = 0; k < h; k += 4) { \
1405 for (i = 0; i < w; ++i) { \
1406 opj_flag_t *flagsp2 = flagsp1 + i; \
1408 agg = !(*flagsp2 & agg_mask); \
1413 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1414 if (!opj_mqc_decode(mqc)) { \
1417 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1418 runlen = opj_mqc_decode(mqc); \
1419 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1423 for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1424 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1425 opj_t1_dec_clnpass_step_vsc( \
1428 &t1->data[(j * w) + i], \
1430 agg && (j == k + runlen), \
1433 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1435 flagsp1 += flags_stride; \
1438 OPJ_INT32 *data1 = t1->data; \
1439 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1440 for (k = 0; k < (h & ~3u); k += 4) { \
1441 for (i = 0; i < w; ++i) { \
1442 OPJ_INT32 *data2 = data1 + i; \
1443 opj_flag_t *flagsp2 = flagsp1 + i; \
1444 if (*flagsp2 == 0) { \
1445 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1446 if (!opj_mqc_decode(mqc)) { \
1449 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1450 runlen = opj_mqc_decode(mqc); \
1451 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1452 data2 += runlen * w; \
1453 for (j = runlen; j < 4; ++j) { \
1454 if (j == runlen) { \
1455 opj_t1_dec_clnpass_step_partial(t1, flagsp2, data2, oneplushalf, j); \
1457 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j); \
1462 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (0 * 3U)))) {\
1463 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 0U, flags_stride); \
1466 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (1 * 3U)))) {\
1467 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 1U, flags_stride); \
1470 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (2 * 3U)))) {\
1471 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 2U, flags_stride); \
1474 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (3 * 3U)))) {\
1475 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 3U, flags_stride); \
1479 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1482 flagsp1 += flags_stride; \
1484 for (i = 0; i < w; ++i) { \
1485 OPJ_INT32 *data2 = data1 + i; \
1486 opj_flag_t *flagsp2 = flagsp1 + i; \
1487 for (j = k; j < h; ++j) { \
1488 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j - k); \
1491 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1497 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1498 v = opj_mqc_decode(mqc); \
1499 v = (v << 1) | opj_mqc_decode(mqc); \
1500 v = (v << 1) | opj_mqc_decode(mqc); \
1501 v = (v << 1) | opj_mqc_decode(mqc); \
1504 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1510 static void opj_t1_dec_clnpass_64x64(
1515 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1518 static void opj_t1_dec_clnpass_generic(
1523 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1528 /** mod fixed_quality */
1529 static OPJ_FLOAT64 opj_t1_getwmsedec(
1536 OPJ_FLOAT64 stepsize,
1537 OPJ_UINT32 numcomps,
1538 const OPJ_FLOAT64 * mct_norms,
1539 OPJ_UINT32 mct_numcomps)
1541 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1542 OPJ_ARG_NOT_USED(numcomps);
1544 if (mct_norms && (compno < mct_numcomps)) {
1545 w1 = mct_norms[compno];
1549 w2 = opj_dwt_getnorm(level, orient);
1550 } else { /* if (qmfbid == 0) */
1551 w2 = opj_dwt_getnorm_real(level, orient);
1554 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1555 wmsedec *= wmsedec * nmsedec / 8192.0;
1560 static OPJ_BOOL opj_t1_allocate_buffers(
1567 /* encoder uses tile buffer, so no need to allocate */
1571 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1572 /* Overflow check */
1573 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1574 /* FIXME event manager error callback */
1578 datasize = (size_t)w * h;
1580 /* Overflow check */
1581 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1582 /* FIXME event manager error callback */
1586 if (datasize > (size_t)t1->datasize) {
1587 opj_aligned_free(t1->data);
1588 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1590 /* FIXME event manager error callback */
1593 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1594 /* TODO remove this if t1->datasize type changes to size_t */
1595 /* Overflow check */
1596 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1597 /* FIXME event manager error callback */
1601 t1->datasize = (OPJ_UINT32)datasize;
1603 /* memset first arg is declared to never be null by gcc */
1604 if (t1->data != NULL) {
1605 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1609 /* Overflow check */
1610 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1611 /* FIXME event manager error callback */
1614 t1->flags_stride = w + 2U; /* can't be 0U */
1616 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1617 /* Overflow check */
1618 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1619 /* FIXME event manager error callback */
1623 flagssize = (h + 3U) / 4U + 2U;
1625 /* Overflow check */
1626 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1627 /* FIXME event manager error callback */
1630 flagssize *= (size_t)t1->flags_stride;
1635 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1637 if (flagssize > (size_t)t1->flagssize) {
1638 /* Overflow check */
1639 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1640 /* FIXME event manager error callback */
1643 opj_aligned_free(t1->flags);
1644 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1647 /* FIXME event manager error callback */
1650 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1651 /* TODO remove this if t1->flagssize type changes to size_t */
1652 /* Overflow check */
1653 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1654 /* FIXME event manager error callback */
1659 t1->flagssize = (OPJ_UINT32)flagssize;
1661 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1664 for (x = 0; x < t1->flags_stride; ++x) {
1665 /* magic value to hopefully stop any passes being interested in this entry */
1666 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1669 p = &t1->flags[((flags_height + 1) * t1->flags_stride)];
1670 for (x = 0; x < t1->flags_stride; ++x) {
1671 /* magic value to hopefully stop any passes being interested in this entry */
1672 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1677 p = &t1->flags[((flags_height) * t1->flags_stride)];
1679 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1680 } else if (h % 4 == 2) {
1681 v |= T1_PI_2 | T1_PI_3;
1682 } else if (h % 4 == 3) {
1685 for (x = 0; x < t1->flags_stride; ++x) {
1697 /* ----------------------------------------------------------------------- */
1699 /* ----------------------------------------------------------------------- */
1701 * Creates a new Tier 1 handle
1702 * and initializes the look-up tables of the Tier-1 coder/decoder
1703 * @return a new T1 handle if successful, returns NULL otherwise
1705 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1707 opj_t1_t *l_t1 = 00;
1709 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1714 /* create MQC and RAW handles */
1715 l_t1->mqc = opj_mqc_create();
1717 opj_t1_destroy(l_t1);
1721 l_t1->raw = opj_raw_create();
1723 opj_t1_destroy(l_t1);
1726 l_t1->encoder = isEncoder;
1733 * Destroys a previously created T1 handle
1735 * @param p_t1 Tier 1 handle to destroy
1737 void opj_t1_destroy(opj_t1_t *p_t1)
1743 /* destroy MQC and RAW handles */
1744 opj_mqc_destroy(p_t1->mqc);
1746 opj_raw_destroy(p_t1->raw);
1749 /* encoder uses tile buffer, so no need to free */
1750 if (!p_t1->encoder && p_t1->data) {
1751 opj_aligned_free(p_t1->data);
1756 opj_aligned_free(p_t1->flags);
1765 opj_tcd_cblk_dec_t* cblk;
1766 opj_tcd_band_t* band;
1767 opj_tcd_tilecomp_t* tilec;
1769 volatile OPJ_BOOL* pret;
1770 } opj_t1_cblk_decode_processing_job_t;
1772 static void opj_t1_destroy_wrapper(void* t1)
1774 opj_t1_destroy((opj_t1_t*) t1);
1777 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1779 opj_tcd_cblk_dec_t* cblk;
1780 opj_tcd_band_t* band;
1781 opj_tcd_tilecomp_t* tilec;
1783 OPJ_INT32* OPJ_RESTRICT datap;
1784 OPJ_UINT32 cblk_w, cblk_h;
1787 opj_t1_cblk_decode_processing_job_t* job;
1792 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1798 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1800 if (!*(job->pret)) {
1805 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1807 t1 = opj_t1_create(OPJ_FALSE);
1808 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1811 if (OPJ_FALSE == opj_t1_decode_cblk(
1815 (OPJ_UINT32)tccp->roishift,
1817 *(job->pret) = OPJ_FALSE;
1822 x = cblk->x0 - band->x0;
1823 y = cblk->y0 - band->y0;
1824 if (band->bandno & 1) {
1825 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1826 x += pres->x1 - pres->x0;
1828 if (band->bandno & 2) {
1829 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1830 y += pres->y1 - pres->y0;
1837 if (tccp->roishift) {
1838 OPJ_INT32 thresh = 1 << tccp->roishift;
1839 for (j = 0; j < cblk_h; ++j) {
1840 for (i = 0; i < cblk_w; ++i) {
1841 OPJ_INT32 val = datap[(j * cblk_w) + i];
1842 OPJ_INT32 mag = abs(val);
1843 if (mag >= thresh) {
1844 mag >>= tccp->roishift;
1845 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1850 if (tccp->qmfbid == 1) {
1851 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1853 for (j = 0; j < cblk_h; ++j) {
1855 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1856 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1857 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1858 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1859 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1860 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1861 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1862 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1863 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1865 for (; i < cblk_w; ++i) {
1866 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1867 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1870 } else { /* if (tccp->qmfbid == 0) */
1871 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1872 tile_w + (OPJ_UINT32)x];
1873 for (j = 0; j < cblk_h; ++j) {
1874 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1875 for (i = 0; i < cblk_w; ++i) {
1876 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1889 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1890 volatile OPJ_BOOL* pret,
1891 opj_tcd_tilecomp_t* tilec,
1895 OPJ_UINT32 resno, bandno, precno, cblkno;
1897 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1898 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1900 for (bandno = 0; bandno < res->numbands; ++bandno) {
1901 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1903 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1904 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1906 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1907 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1908 opj_t1_cblk_decode_processing_job_t* job;
1910 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1911 sizeof(opj_t1_cblk_decode_processing_job_t));
1922 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1935 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1936 opj_tcd_cblk_dec_t* cblk,
1938 OPJ_UINT32 roishift,
1941 opj_raw_t *raw = t1->raw; /* RAW component */
1942 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1944 OPJ_INT32 bpno_plus_one;
1945 OPJ_UINT32 passtype;
1946 OPJ_UINT32 segno, passno;
1947 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1949 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
1951 if (!opj_t1_allocate_buffers(
1953 (OPJ_UINT32)(cblk->x1 - cblk->x0),
1954 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
1958 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
1961 opj_mqc_resetstates(mqc);
1962 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
1963 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
1964 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
1966 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
1967 opj_tcd_seg_t *seg = &cblk->segs[segno];
1970 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
1971 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
1972 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
1973 if (seg->data == 00) {
1976 if (type == T1_TYPE_RAW) {
1977 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
1979 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
1985 if (t1->w == 64 && t1->h == 64) {
1986 for (passno = 0; (passno < seg->real_num_passes) &&
1987 (bpno_plus_one >= 1); ++passno) {
1990 if (type == T1_TYPE_RAW) {
1991 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
1993 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1994 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
1996 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2001 if (type == T1_TYPE_RAW) {
2002 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2004 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2005 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2007 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2012 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2016 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2017 opj_mqc_resetstates(mqc);
2018 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2019 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2020 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2022 if (++passtype == 3) {
2028 for (passno = 0; (passno < seg->real_num_passes) &&
2029 (bpno_plus_one >= 1); ++passno) {
2032 if (type == T1_TYPE_RAW) {
2033 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2035 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2036 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2038 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2043 if (type == T1_TYPE_RAW) {
2044 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2046 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2047 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2049 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2054 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2058 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2059 opj_mqc_resetstates(mqc);
2060 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2061 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2062 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2064 if (++passtype == 3) {
2077 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2078 opj_tcd_tile_t *tile,
2080 const OPJ_FLOAT64 * mct_norms,
2081 OPJ_UINT32 mct_numcomps
2084 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2086 tile->distotile = 0; /* fixed_quality */
2088 for (compno = 0; compno < tile->numcomps; ++compno) {
2089 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2090 opj_tccp_t* tccp = &tcp->tccps[compno];
2091 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2093 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2094 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2096 for (bandno = 0; bandno < res->numbands; ++bandno) {
2097 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2098 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2100 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2101 opj_tcd_precinct_t *prc = &band->precincts[precno];
2103 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2104 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2105 OPJ_INT32* OPJ_RESTRICT tiledp;
2108 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2110 OPJ_INT32 x = cblk->x0 - band->x0;
2111 OPJ_INT32 y = cblk->y0 - band->y0;
2112 if (band->bandno & 1) {
2113 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2114 x += pres->x1 - pres->x0;
2116 if (band->bandno & 2) {
2117 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2118 y += pres->y1 - pres->y0;
2121 if (!opj_t1_allocate_buffers(
2123 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2124 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2130 tileLineAdvance = tile_w - cblk_w;
2132 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2134 t1->data_stride = tile_w;
2135 if (tccp->qmfbid == 1) {
2136 for (j = 0; j < cblk_h; ++j) {
2137 for (i = 0; i < cblk_w; ++i) {
2138 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2141 tileIndex += tileLineAdvance;
2143 } else { /* if (tccp->qmfbid == 0) */
2144 for (j = 0; j < cblk_h; ++j) {
2145 for (i = 0; i < cblk_w; ++i) {
2146 OPJ_INT32 tmp = tiledp[tileIndex];
2153 tileIndex += tileLineAdvance;
2162 tilec->numresolutions - 1 - resno,
2179 /** mod fixed_quality */
2180 static void opj_t1_encode_cblk(opj_t1_t *t1,
2181 opj_tcd_cblk_enc_t* cblk,
2186 OPJ_FLOAT64 stepsize,
2188 OPJ_UINT32 numcomps,
2189 opj_tcd_tile_t * tile,
2190 const OPJ_FLOAT64 * mct_norms,
2191 OPJ_UINT32 mct_numcomps)
2193 OPJ_FLOAT64 cumwmsedec = 0.0;
2195 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2199 OPJ_UINT32 passtype;
2200 OPJ_INT32 nmsedec = 0;
2203 OPJ_BYTE type = T1_TYPE_MQ;
2204 OPJ_FLOAT64 tempwmsedec;
2206 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2209 for (i = 0; i < t1->w; ++i) {
2210 for (j = 0; j < t1->h; ++j) {
2211 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2212 max = opj_int_max(max, tmp);
2216 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2217 T1_NMSEDEC_FRACBITS) : 0;
2219 bpno = (OPJ_INT32)(cblk->numbps - 1);
2222 opj_mqc_resetstates(mqc);
2223 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2224 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2225 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2226 opj_mqc_init_enc(mqc, cblk->data);
2228 for (passno = 0; bpno >= 0; ++passno) {
2229 opj_tcd_pass_t *pass = &cblk->passes[passno];
2230 OPJ_UINT32 correction = 3;
2231 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2232 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2236 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2239 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2242 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2243 /* code switch SEGMARK (i.e. SEGSYM) */
2244 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2245 opj_mqc_segmark_enc(mqc);
2251 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2252 stepsize, numcomps, mct_norms, mct_numcomps) ;
2253 cumwmsedec += tempwmsedec;
2254 tile->distotile += tempwmsedec;
2256 /* Code switch "RESTART" (i.e. TERMALL) */
2257 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2259 if (type == T1_TYPE_RAW) {
2262 /* correction = mqc_bypass_flush_enc(); */
2263 } else { /* correction = mqc_restart_enc(); */
2269 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2270 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2271 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2272 if (type == T1_TYPE_RAW) {
2275 /* correction = mqc_bypass_flush_enc(); */
2276 } else { /* correction = mqc_restart_enc(); */
2286 if (++passtype == 3) {
2291 if (pass->term && bpno > 0) {
2292 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2293 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2294 if (type == T1_TYPE_RAW) {
2295 opj_mqc_bypass_init_enc(mqc);
2297 opj_mqc_restart_init_enc(mqc);
2301 pass->distortiondec = cumwmsedec;
2302 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2304 /* Code-switch "RESET" */
2305 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2306 opj_mqc_reset_enc(mqc);
2310 /* Code switch "ERTERM" (i.e. PTERM) */
2311 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2312 opj_mqc_erterm_enc(mqc);
2313 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2317 cblk->totalpasses = passno;
2319 for (passno = 0; passno < cblk->totalpasses; passno++) {
2320 opj_tcd_pass_t *pass = &cblk->passes[passno];
2321 if (pass->rate > opj_mqc_numbytes(mqc)) {
2322 pass->rate = opj_mqc_numbytes(mqc);
2324 /*Preventing generation of FF as last data byte of a pass*/
2325 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2328 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);