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 /** @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->w + 2);
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->w + 2);
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->w + 2);
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 = 2;
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;
635 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
638 oneplushalf = one | half;
639 if ((cblksty & J2K_CCP_CBLKSTY_VSC)) {
640 OPJ_INT32 *data1 = t1->data;
641 for (k = 0; k < t1->h; k += 4) {
642 for (i = 0; i < t1->w; ++i) {
643 OPJ_INT32* data2 = data1 + i;
644 for (j = k; j < k + 4 && j < t1->h; ++j) {
645 OPJ_INT32 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
646 opj_t1_dec_sigpass_step_raw(
661 OPJ_INT32 *data1 = t1->data;
662 for (k = 0; k < (t1->h & ~3U); k += 4) {
663 for (i = 0; i < t1->w; ++i) {
664 OPJ_INT32* data2 = data1 + i;
665 opj_t1_dec_sigpass_step_raw(
673 opj_t1_dec_sigpass_step_raw(
681 opj_t1_dec_sigpass_step_raw(
689 opj_t1_dec_sigpass_step_raw(
703 for (i = 0; i < t1->w; ++i) {
704 OPJ_INT32* data2 = data1 + i;
705 for (j = 0; j < t1->h - k; ++j) {
706 opj_t1_dec_sigpass_step_raw(
721 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
723 OPJ_INT32 one, half, oneplushalf; \
724 OPJ_UINT32 i, j, k; \
725 OPJ_INT32 *data1 = t1->data; \
726 opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
729 oneplushalf = one | half; \
730 for (k = 0; k < (h & ~3u); k += 4) { \
731 for (i = 0; i < w; ++i) { \
732 OPJ_INT32 *data2 = data1 + i; \
733 if( *flagsp != 0 ) { \
734 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 0U, flags_stride); \
736 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 1U, flags_stride); \
738 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 2U, flags_stride); \
740 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 3U, flags_stride); \
748 for (i = 0; i < w; ++i) { \
749 OPJ_INT32 *data2 = data1 + i; \
750 for (j = k; j < h; ++j) { \
751 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, j - k, flags_stride); \
758 static void opj_t1_dec_sigpass_mqc_64x64(
762 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
765 static void opj_t1_dec_sigpass_mqc_generic(
769 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2);
772 static void opj_t1_dec_sigpass_mqc_vsc(
776 OPJ_INT32 one, half, oneplushalf, vsc;
778 OPJ_INT32 *data1 = t1->data;
779 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
782 oneplushalf = one | half;
783 for (k = 0; k < (t1->h & ~3U); k += 4U) {
784 for (i = 0; i < t1->w; ++i) {
785 OPJ_INT32 *data2 = data1 + i;
786 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 0U);
788 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 1U);
790 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 2U);
792 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 1, 3U);
799 for (i = 0; i < t1->w; ++i) {
800 OPJ_INT32 *data2 = data1 + i;
801 for (j = k; j < t1->h; ++j) {
802 vsc = (j == t1->h - 1) ? 1 : 0;
803 opj_t1_dec_sigpass_step_mqc_vsc(
818 Encode refinement pass step
820 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
832 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
834 OPJ_UINT32 const shift_flags =
836 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
838 (*flagsp >> (ci * 3U));
840 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
841 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
842 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
844 v = opj_int_abs(*datap) & one ? 1 : 0;
846 fprintf(stderr, " ctxt=%d\n", ctxt);
848 opj_mqc_setcurctx(mqc, ctxt);
849 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
850 opj_mqc_bypass_enc(mqc, v);
852 opj_mqc_encode(mqc, v);
854 *flagsp |= T1_MU_THIS << (ci * 3U);
859 static INLINE void opj_t1_dec_refpass_step_raw(
870 opj_raw_t *raw = &(t1->raw); /* RAW component */
872 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
873 (T1_SIGMA_THIS << (ci * 3U))) {
874 v = opj_raw_decode(raw);
875 t = v ? poshalf : neghalf;
876 *datap += *datap < 0 ? -t : t;
877 *flagsp |= T1_MU_THIS << (ci * 3U);
881 static INLINE void opj_t1_dec_refpass_step_mqc(
892 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
894 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
895 (T1_SIGMA_THIS << (ci * 3U))) {
896 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(*flagsp >> (ci * 3U));
897 opj_mqc_setcurctx(mqc, ctxt);
898 v = opj_mqc_decode(mqc);
899 t = v ? poshalf : neghalf;
900 *datap += *datap < 0 ? -t : t;
901 *flagsp |= T1_MU_THIS << (ci * 3U);
905 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
917 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
919 OPJ_UINT32 const shift_flags =
921 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
923 (*flagsp >> (ci * 3U));
926 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
927 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
928 opj_mqc_setcurctx(mqc, ctxt);
929 v = opj_mqc_decode(mqc);
930 t = v ? poshalf : neghalf;
931 *datap += *datap < 0 ? -t : t;
932 *flagsp |= T1_MU_THIS << (ci * 3U);
936 static void opj_t1_enc_refpass(
944 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
945 opj_flag_t* f = &T1_FLAGS(0, 0);
946 const OPJ_UINT32 extra = 2U;
950 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
952 for (k = 0; k < (t1->h & ~3U); k += 4) {
954 fprintf(stderr, " k=%d\n", k);
956 for (i = 0; i < t1->w; ++i) {
958 fprintf(stderr, " i=%d\n", i);
960 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
961 /* none significant */
965 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
966 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
967 /* all processed by sigpass */
972 opj_t1_enc_refpass_step(
975 &t1->data[((k + 0) * t1->data_stride) + i],
981 opj_t1_enc_refpass_step(
984 &t1->data[((k + 1) * t1->data_stride) + i],
990 opj_t1_enc_refpass_step(
993 &t1->data[((k + 2) * t1->data_stride) + i],
999 opj_t1_enc_refpass_step(
1002 &t1->data[((k + 3) * t1->data_stride) + i],
1007 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1015 #ifdef DEBUG_ENC_REF
1016 fprintf(stderr, " k=%d\n", k);
1018 for (i = 0; i < t1->w; ++i) {
1019 #ifdef DEBUG_ENC_REF
1020 fprintf(stderr, " i=%d\n", i);
1022 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1023 /* none significant */
1027 for (j = k; j < t1->h; ++j) {
1028 opj_t1_enc_refpass_step(
1031 &t1->data[(j * t1->data_stride) + i],
1037 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1045 static void opj_t1_dec_refpass_raw(
1049 OPJ_INT32 one, poshalf, neghalf;
1051 OPJ_INT32 *data1 = t1->data;
1052 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1055 neghalf = bpno > 0 ? -poshalf : -1;
1056 for (k = 0; k < (t1->h & ~3U); k += 4) {
1057 for (i = 0; i < t1->w; ++i) {
1058 OPJ_INT32 *data2 = data1 + i;
1059 opj_t1_dec_refpass_step_raw(
1066 opj_t1_dec_refpass_step_raw(
1073 opj_t1_dec_refpass_step_raw(
1080 opj_t1_dec_refpass_step_raw(
1089 data1 += t1->w << 2;
1093 for (i = 0; i < t1->w; ++i) {
1094 OPJ_INT32 *data2 = data1 + i;
1095 for (j = k; j < t1->h; ++j) {
1096 opj_t1_dec_refpass_step_raw(
1109 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1111 OPJ_INT32 one, poshalf, neghalf; \
1112 OPJ_UINT32 i, j, k; \
1113 OPJ_INT32 *data1 = t1->data; \
1114 opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1116 poshalf = one >> 1; \
1117 neghalf = bpno > 0 ? -poshalf : -1; \
1118 for (k = 0; k < (h & ~3u); k += 4) { \
1119 for (i = 0; i < w; ++i) { \
1120 OPJ_INT32 *data2 = data1 + i; \
1121 if( *flagsp != 0 ) { \
1122 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 0U); \
1124 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 1U); \
1126 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 2U); \
1128 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, 3U); \
1136 for (i = 0; i < w; ++i) { \
1137 OPJ_INT32 *data2 = data1 + i; \
1138 for (j = k; j < h; ++j) { \
1139 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, neghalf, j - k); \
1146 static void opj_t1_dec_refpass_mqc_64x64(
1150 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1153 static void opj_t1_dec_refpass_mqc_generic(
1157 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1160 static void opj_t1_dec_refpass_mqc_vsc(
1164 OPJ_INT32 one, poshalf, neghalf;
1167 OPJ_INT32 *data1 = t1->data;
1168 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1171 neghalf = bpno > 0 ? -poshalf : -1;
1172 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1173 for (i = 0; i < t1->w; ++i) {
1174 OPJ_INT32 *data2 = data1 + i;
1175 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1178 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1181 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1184 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf, neghalf,
1189 data1 += t1->w << 2;
1192 for (i = 0; i < t1->w; ++i) {
1193 OPJ_INT32 *data2 = data1 + i;
1194 for (j = k; j < t1->h; ++j) {
1195 vsc = (j == t1->h - 1) ? 1 : 0;
1196 opj_t1_dec_refpass_step_mqc_vsc(
1210 Encode clean-up pass step
1212 static void opj_t1_enc_clnpass_step(
1226 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1228 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1229 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1231 if ((*flagsp & check) == check) {
1233 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1234 } else if (runlen == 1) {
1235 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1236 } else if (runlen == 2) {
1237 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1238 } else if (runlen == 3) {
1239 *flagsp &= ~(T1_PI_3);
1244 for (ci = runlen; ci < lim; ++ci) {
1246 OPJ_UINT32 vsc_mask;
1249 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1250 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1252 flags = *flagsp & vsc_mask;
1254 if ((agg != 0) && (ci == runlen)) {
1258 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1259 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1260 #ifdef DEBUG_ENC_CLN
1261 printf(" ctxt1=%d\n", ctxt1);
1263 opj_mqc_setcurctx(mqc, ctxt1);
1264 v = opj_int_abs(*datap) & one ? 1 : 0;
1265 opj_mqc_encode(mqc, v);
1267 OPJ_UINT32 ctxt2, spb;
1270 /* Note: using flags instead of *flagsp & vsc_mask result */
1271 /* in slow down. Probably because of register pressure */
1272 lu = opj_t1_getctxtno_sc_or_spb_index(
1274 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1276 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1278 ctxt2 = opj_t1_getctxno_sc(lu);
1279 #ifdef DEBUG_ENC_CLN
1280 printf(" ctxt2=%d\n", ctxt2);
1282 opj_mqc_setcurctx(mqc, ctxt2);
1284 v = *datap < 0 ? 1 : 0;
1285 spb = opj_t1_getspb(lu);
1286 #ifdef DEBUG_ENC_CLN
1287 printf(" spb=%d\n", spb);
1289 opj_mqc_encode(mqc, v ^ spb);
1290 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1293 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1294 datap += t1->data_stride;
1299 static void opj_t1_dec_clnpass_step_partial(
1303 OPJ_INT32 oneplushalf,
1307 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1309 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1310 *flagsp, flagsp[-1], flagsp[1],
1312 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1313 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1314 *datap = v ? -oneplushalf : oneplushalf;
1315 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1316 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1319 static void opj_t1_dec_clnpass_step(
1323 OPJ_INT32 oneplushalf,
1328 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1329 if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1330 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1331 opj_mqc_setcurctx(mqc, ctxt1);
1332 if (opj_mqc_decode(mqc)) {
1333 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1334 *flagsp, flagsp[-1], flagsp[1],
1336 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1337 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1338 *datap = v ? -oneplushalf : oneplushalf;
1339 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1342 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1345 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1349 OPJ_INT32 oneplushalf,
1351 OPJ_UINT32 flags_stride)
1355 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1357 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1358 opj_mqc_setcurctx(mqc, ctxt1);
1359 if (opj_mqc_decode(mqc)) {
1360 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1361 *flagsp, flagsp[-1], flagsp[1],
1363 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1364 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1365 *datap = v ? -oneplushalf : oneplushalf;
1366 opj_t1_update_flags(flagsp, ci, v, flags_stride);
1370 static void opj_t1_dec_clnpass_step_vsc(
1374 OPJ_INT32 oneplushalf,
1381 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1383 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1386 OPJ_UINT32 flags = *flagsp & vsc_mask;
1390 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1391 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1392 opj_mqc_setcurctx(mqc, ctxt1);
1393 if (opj_mqc_decode(mqc)) {
1396 lu = opj_t1_getctxtno_sc_or_spb_index(
1397 *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1398 flagsp[1] & vsc_mask,
1400 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1401 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1402 *datap = v ? -oneplushalf : oneplushalf;
1403 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1406 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1409 static void opj_t1_enc_clnpass(
1416 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1417 OPJ_UINT32 agg, runlen;
1419 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1421 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1422 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1426 #ifdef DEBUG_ENC_CLN
1427 printf("enc_clnpass: bpno=%d\n", bpno);
1429 for (k = 0; k < (t1->h & ~3U); k += 4) {
1430 #ifdef DEBUG_ENC_CLN
1431 printf(" k=%d\n", k);
1433 for (i = 0; i < t1->w; ++i) {
1434 #ifdef DEBUG_ENC_CLN
1435 printf(" i=%d\n", i);
1437 agg = !(T1_FLAGS(i, k) & agg_mask);
1438 #ifdef DEBUG_ENC_CLN
1439 printf(" agg=%d\n", agg);
1442 for (runlen = 0; runlen < 4; ++runlen) {
1443 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1447 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1448 opj_mqc_encode(mqc, runlen != 4);
1452 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1453 opj_mqc_encode(mqc, runlen >> 1);
1454 opj_mqc_encode(mqc, runlen & 1);
1458 opj_t1_enc_clnpass_step(
1461 &t1->data[((k + runlen) * t1->data_stride) + i],
1474 #ifdef DEBUG_ENC_CLN
1475 printf(" k=%d\n", k);
1477 for (i = 0; i < t1->w; ++i) {
1478 #ifdef DEBUG_ENC_CLN
1479 printf(" i=%d\n", i);
1480 printf(" agg=%d\n", agg);
1482 opj_t1_enc_clnpass_step(
1485 &t1->data[((k + runlen) * t1->data_stride) + i],
1497 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1499 OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1500 OPJ_UINT32 runlen; \
1501 OPJ_UINT32 i, j, k; \
1502 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1504 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ \
1508 oneplushalf = one | half; \
1509 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1510 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1511 const OPJ_UINT32 agg_mask = ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1512 for (k = 0; k < h; k += 4) { \
1513 for (i = 0; i < w; ++i) { \
1514 opj_flag_t *flagsp2 = flagsp1 + i; \
1516 agg = !(*flagsp2 & agg_mask); \
1521 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1522 if (!opj_mqc_decode(mqc)) { \
1525 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1526 runlen = opj_mqc_decode(mqc); \
1527 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1531 for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1532 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1533 opj_t1_dec_clnpass_step_vsc( \
1536 &t1->data[(j * w) + i], \
1538 agg && (j == k + runlen), \
1541 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1543 flagsp1 += flags_stride; \
1546 OPJ_INT32 *data1 = t1->data; \
1547 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1548 for (k = 0; k < (h & ~3u); k += 4) { \
1549 for (i = 0; i < w; ++i) { \
1550 OPJ_INT32 *data2 = data1 + i; \
1551 opj_flag_t *flagsp2 = flagsp1 + i; \
1552 if (*flagsp2 == 0) { \
1553 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1554 if (!opj_mqc_decode(mqc)) { \
1557 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1558 runlen = opj_mqc_decode(mqc); \
1559 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1560 data2 += runlen * w; \
1561 for (j = runlen; j < 4; ++j) { \
1562 if (j == runlen) { \
1563 opj_t1_dec_clnpass_step_partial(t1, flagsp2, data2, oneplushalf, j); \
1565 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j); \
1570 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (0 * 3U)))) {\
1571 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 0U, flags_stride); \
1574 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (1 * 3U)))) {\
1575 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 1U, flags_stride); \
1578 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (2 * 3U)))) {\
1579 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 2U, flags_stride); \
1582 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (3 * 3U)))) {\
1583 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 3U, flags_stride); \
1587 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1590 flagsp1 += flags_stride; \
1592 for (i = 0; i < w; ++i) { \
1593 OPJ_INT32 *data2 = data1 + i; \
1594 opj_flag_t *flagsp2 = flagsp1 + i; \
1595 for (j = k; j < h; ++j) { \
1596 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j - k); \
1599 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1605 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1606 v = opj_mqc_decode(mqc); \
1607 v = (v << 1) | opj_mqc_decode(mqc); \
1608 v = (v << 1) | opj_mqc_decode(mqc); \
1609 v = (v << 1) | opj_mqc_decode(mqc); \
1612 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1618 static void opj_t1_dec_clnpass_64x64(
1623 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1626 static void opj_t1_dec_clnpass_generic(
1631 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1636 /** mod fixed_quality */
1637 static OPJ_FLOAT64 opj_t1_getwmsedec(
1644 OPJ_FLOAT64 stepsize,
1645 OPJ_UINT32 numcomps,
1646 const OPJ_FLOAT64 * mct_norms,
1647 OPJ_UINT32 mct_numcomps)
1649 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1650 OPJ_ARG_NOT_USED(numcomps);
1652 if (mct_norms && (compno < mct_numcomps)) {
1653 w1 = mct_norms[compno];
1657 w2 = opj_dwt_getnorm(level, orient);
1658 } else { /* if (qmfbid == 0) */
1659 w2 = opj_dwt_getnorm_real(level, orient);
1662 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1663 wmsedec *= wmsedec * nmsedec / 8192.0;
1668 static OPJ_BOOL opj_t1_allocate_buffers(
1674 OPJ_UINT32 flags_stride;
1676 /* encoder uses tile buffer, so no need to allocate */
1680 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1681 /* Overflow check */
1682 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1683 /* FIXME event manager error callback */
1687 datasize = (size_t)w * h;
1689 /* Overflow check */
1690 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1691 /* FIXME event manager error callback */
1695 if (datasize > (size_t)t1->datasize) {
1696 opj_aligned_free(t1->data);
1697 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1699 /* FIXME event manager error callback */
1702 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1703 /* TODO remove this if t1->datasize type changes to size_t */
1704 /* Overflow check */
1705 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1706 /* FIXME event manager error callback */
1710 t1->datasize = (OPJ_UINT32)datasize;
1712 /* memset first arg is declared to never be null by gcc */
1713 if (t1->data != NULL) {
1714 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1718 /* Overflow check */
1719 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1720 /* FIXME event manager error callback */
1723 flags_stride = w + 2U; /* can't be 0U */
1725 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1726 /* Overflow check */
1727 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1728 /* FIXME event manager error callback */
1732 flagssize = (h + 3U) / 4U + 2U;
1734 /* Overflow check */
1735 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1736 /* FIXME event manager error callback */
1739 flagssize *= (size_t)flags_stride;
1744 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1746 if (flagssize > (size_t)t1->flagssize) {
1747 /* Overflow check */
1748 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1749 /* FIXME event manager error callback */
1752 opj_aligned_free(t1->flags);
1753 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1756 /* FIXME event manager error callback */
1759 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1760 /* TODO remove this if t1->flagssize type changes to size_t */
1761 /* Overflow check */
1762 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1763 /* FIXME event manager error callback */
1768 t1->flagssize = (OPJ_UINT32)flagssize;
1770 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1773 for (x = 0; x < flags_stride; ++x) {
1774 /* magic value to hopefully stop any passes being interested in this entry */
1775 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1778 p = &t1->flags[((flags_height + 1) * flags_stride)];
1779 for (x = 0; x < flags_stride; ++x) {
1780 /* magic value to hopefully stop any passes being interested in this entry */
1781 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1786 p = &t1->flags[((flags_height) * flags_stride)];
1788 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1789 } else if (h % 4 == 2) {
1790 v |= T1_PI_2 | T1_PI_3;
1791 } else if (h % 4 == 3) {
1794 for (x = 0; x < flags_stride; ++x) {
1806 /* ----------------------------------------------------------------------- */
1808 /* ----------------------------------------------------------------------- */
1810 * Creates a new Tier 1 handle
1811 * and initializes the look-up tables of the Tier-1 coder/decoder
1812 * @return a new T1 handle if successful, returns NULL otherwise
1814 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1816 opj_t1_t *l_t1 = 00;
1818 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1823 l_t1->encoder = isEncoder;
1830 * Destroys a previously created T1 handle
1832 * @param p_t1 Tier 1 handle to destroy
1834 void opj_t1_destroy(opj_t1_t *p_t1)
1840 /* encoder uses tile buffer, so no need to free */
1841 if (!p_t1->encoder && p_t1->data) {
1842 opj_aligned_free(p_t1->data);
1847 opj_aligned_free(p_t1->flags);
1856 opj_tcd_cblk_dec_t* cblk;
1857 opj_tcd_band_t* band;
1858 opj_tcd_tilecomp_t* tilec;
1860 volatile OPJ_BOOL* pret;
1861 } opj_t1_cblk_decode_processing_job_t;
1863 static void opj_t1_destroy_wrapper(void* t1)
1865 opj_t1_destroy((opj_t1_t*) t1);
1868 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1870 opj_tcd_cblk_dec_t* cblk;
1871 opj_tcd_band_t* band;
1872 opj_tcd_tilecomp_t* tilec;
1874 OPJ_INT32* OPJ_RESTRICT datap;
1875 OPJ_UINT32 cblk_w, cblk_h;
1878 opj_t1_cblk_decode_processing_job_t* job;
1883 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1889 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1891 if (!*(job->pret)) {
1896 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1898 t1 = opj_t1_create(OPJ_FALSE);
1899 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1902 if (OPJ_FALSE == opj_t1_decode_cblk(
1906 (OPJ_UINT32)tccp->roishift,
1908 *(job->pret) = OPJ_FALSE;
1913 x = cblk->x0 - band->x0;
1914 y = cblk->y0 - band->y0;
1915 if (band->bandno & 1) {
1916 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1917 x += pres->x1 - pres->x0;
1919 if (band->bandno & 2) {
1920 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1921 y += pres->y1 - pres->y0;
1928 if (tccp->roishift) {
1929 OPJ_INT32 thresh = 1 << tccp->roishift;
1930 for (j = 0; j < cblk_h; ++j) {
1931 for (i = 0; i < cblk_w; ++i) {
1932 OPJ_INT32 val = datap[(j * cblk_w) + i];
1933 OPJ_INT32 mag = abs(val);
1934 if (mag >= thresh) {
1935 mag >>= tccp->roishift;
1936 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1941 if (tccp->qmfbid == 1) {
1942 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1944 for (j = 0; j < cblk_h; ++j) {
1946 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1947 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1948 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1949 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1950 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1951 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1952 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1953 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1954 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1956 for (; i < cblk_w; ++i) {
1957 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1958 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1961 } else { /* if (tccp->qmfbid == 0) */
1962 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1963 tile_w + (OPJ_UINT32)x];
1964 for (j = 0; j < cblk_h; ++j) {
1965 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1966 for (i = 0; i < cblk_w; ++i) {
1967 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1980 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1981 volatile OPJ_BOOL* pret,
1982 opj_tcd_tilecomp_t* tilec,
1986 OPJ_UINT32 resno, bandno, precno, cblkno;
1988 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1989 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1991 for (bandno = 0; bandno < res->numbands; ++bandno) {
1992 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1994 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1995 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1997 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1998 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1999 opj_t1_cblk_decode_processing_job_t* job;
2001 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
2002 sizeof(opj_t1_cblk_decode_processing_job_t));
2013 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2026 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2027 opj_tcd_cblk_dec_t* cblk,
2029 OPJ_UINT32 roishift,
2032 opj_raw_t *raw = &(t1->raw); /* RAW component */
2033 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2035 OPJ_INT32 bpno_plus_one;
2036 OPJ_UINT32 passtype;
2037 OPJ_UINT32 segno, passno;
2038 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2040 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2042 if (!opj_t1_allocate_buffers(
2044 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2045 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2049 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2052 opj_mqc_resetstates(mqc);
2053 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2054 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2055 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2057 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2058 opj_tcd_seg_t *seg = &cblk->segs[segno];
2061 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2062 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2063 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2064 if (seg->data == 00) {
2067 if (type == T1_TYPE_RAW) {
2068 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2070 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2076 if (t1->w == 64 && t1->h == 64) {
2077 for (passno = 0; (passno < seg->real_num_passes) &&
2078 (bpno_plus_one >= 1); ++passno) {
2081 if (type == T1_TYPE_RAW) {
2082 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2084 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2085 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2087 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2092 if (type == T1_TYPE_RAW) {
2093 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2095 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2096 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2098 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2103 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2107 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2108 opj_mqc_resetstates(mqc);
2109 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2110 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2111 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2113 if (++passtype == 3) {
2119 for (passno = 0; (passno < seg->real_num_passes) &&
2120 (bpno_plus_one >= 1); ++passno) {
2123 if (type == T1_TYPE_RAW) {
2124 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2126 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2127 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2129 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2134 if (type == T1_TYPE_RAW) {
2135 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2137 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2138 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2140 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2145 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2149 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2150 opj_mqc_resetstates(mqc);
2151 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2152 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2153 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2155 if (++passtype == 3) {
2168 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2169 opj_tcd_tile_t *tile,
2171 const OPJ_FLOAT64 * mct_norms,
2172 OPJ_UINT32 mct_numcomps
2175 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2177 tile->distotile = 0; /* fixed_quality */
2179 for (compno = 0; compno < tile->numcomps; ++compno) {
2180 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2181 opj_tccp_t* tccp = &tcp->tccps[compno];
2182 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2184 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2185 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2187 for (bandno = 0; bandno < res->numbands; ++bandno) {
2188 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2189 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2191 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2192 opj_tcd_precinct_t *prc = &band->precincts[precno];
2194 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2195 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2196 OPJ_INT32* OPJ_RESTRICT tiledp;
2199 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2201 OPJ_INT32 x = cblk->x0 - band->x0;
2202 OPJ_INT32 y = cblk->y0 - band->y0;
2203 if (band->bandno & 1) {
2204 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2205 x += pres->x1 - pres->x0;
2207 if (band->bandno & 2) {
2208 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2209 y += pres->y1 - pres->y0;
2212 if (!opj_t1_allocate_buffers(
2214 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2215 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2221 tileLineAdvance = tile_w - cblk_w;
2223 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2225 t1->data_stride = tile_w;
2226 if (tccp->qmfbid == 1) {
2227 for (j = 0; j < cblk_h; ++j) {
2228 for (i = 0; i < cblk_w; ++i) {
2229 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2232 tileIndex += tileLineAdvance;
2234 } else { /* if (tccp->qmfbid == 0) */
2235 for (j = 0; j < cblk_h; ++j) {
2236 for (i = 0; i < cblk_w; ++i) {
2237 OPJ_INT32 tmp = tiledp[tileIndex];
2244 tileIndex += tileLineAdvance;
2253 tilec->numresolutions - 1 - resno,
2270 /** mod fixed_quality */
2271 static void opj_t1_encode_cblk(opj_t1_t *t1,
2272 opj_tcd_cblk_enc_t* cblk,
2277 OPJ_FLOAT64 stepsize,
2279 OPJ_UINT32 numcomps,
2280 opj_tcd_tile_t * tile,
2281 const OPJ_FLOAT64 * mct_norms,
2282 OPJ_UINT32 mct_numcomps)
2284 OPJ_FLOAT64 cumwmsedec = 0.0;
2286 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2290 OPJ_UINT32 passtype;
2291 OPJ_INT32 nmsedec = 0;
2294 OPJ_BYTE type = T1_TYPE_MQ;
2295 OPJ_FLOAT64 tempwmsedec;
2297 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2300 for (i = 0; i < t1->w; ++i) {
2301 for (j = 0; j < t1->h; ++j) {
2302 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2303 max = opj_int_max(max, tmp);
2307 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2308 T1_NMSEDEC_FRACBITS) : 0;
2310 bpno = (OPJ_INT32)(cblk->numbps - 1);
2313 opj_mqc_resetstates(mqc);
2314 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2315 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2316 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2317 opj_mqc_init_enc(mqc, cblk->data);
2319 for (passno = 0; bpno >= 0; ++passno) {
2320 opj_tcd_pass_t *pass = &cblk->passes[passno];
2321 OPJ_UINT32 correction = 3;
2322 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2323 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2327 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2330 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2333 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2334 /* code switch SEGMARK (i.e. SEGSYM) */
2335 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2336 opj_mqc_segmark_enc(mqc);
2342 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2343 stepsize, numcomps, mct_norms, mct_numcomps) ;
2344 cumwmsedec += tempwmsedec;
2345 tile->distotile += tempwmsedec;
2347 /* Code switch "RESTART" (i.e. TERMALL) */
2348 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2350 if (type == T1_TYPE_RAW) {
2353 /* correction = mqc_bypass_flush_enc(); */
2354 } else { /* correction = mqc_restart_enc(); */
2360 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2361 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2362 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2363 if (type == T1_TYPE_RAW) {
2366 /* correction = mqc_bypass_flush_enc(); */
2367 } else { /* correction = mqc_restart_enc(); */
2377 if (++passtype == 3) {
2382 if (pass->term && bpno > 0) {
2383 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2384 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2385 if (type == T1_TYPE_RAW) {
2386 opj_mqc_bypass_init_enc(mqc);
2388 opj_mqc_restart_init_enc(mqc);
2392 pass->distortiondec = cumwmsedec;
2393 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2395 /* Code-switch "RESET" */
2396 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2397 opj_mqc_reset_enc(mqc);
2401 /* Code switch "ERTERM" (i.e. PTERM) */
2402 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2403 opj_mqc_erterm_enc(mqc);
2404 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2408 cblk->totalpasses = passno;
2410 for (passno = 0; passno < cblk->totalpasses; passno++) {
2411 opj_tcd_pass_t *pass = &cblk->passes[passno];
2412 if (pass->rate > opj_mqc_numbytes(mqc)) {
2413 pass->rate = opj_mqc_numbytes(mqc);
2415 /*Preventing generation of FF as last data byte of a pass*/
2416 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2419 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);