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(
139 static INLINE void opj_t1_dec_refpass_step_mqc(
145 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
157 static void opj_t1_dec_clnpass_step_partial(
161 OPJ_INT32 oneplushalf,
163 static void opj_t1_dec_clnpass_step(
167 OPJ_INT32 oneplushalf,
169 static void opj_t1_dec_clnpass_step_vsc(
173 OPJ_INT32 oneplushalf,
180 static void opj_t1_enc_clnpass(
186 static OPJ_FLOAT64 opj_t1_getwmsedec(
193 OPJ_FLOAT64 stepsize,
195 const OPJ_FLOAT64 * mct_norms,
196 OPJ_UINT32 mct_numcomps);
198 static void opj_t1_encode_cblk(opj_t1_t *t1,
199 opj_tcd_cblk_enc_t* cblk,
204 OPJ_FLOAT64 stepsize,
207 opj_tcd_tile_t * tile,
208 const OPJ_FLOAT64 * mct_norms,
209 OPJ_UINT32 mct_numcomps);
214 @param cblk Code-block coding parameters
216 @param roishift Region of interest shifting value
217 @param cblksty Code-block style
219 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
220 opj_tcd_cblk_dec_t* cblk,
225 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
233 /* ----------------------------------------------------------------------- */
235 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
237 return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
240 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
246 0 pfX T1_CHI_THIS T1_LUT_SGN_W
247 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
248 2 nfX T1_CHI_THIS T1_LUT_SGN_E
249 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
250 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
251 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
252 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
253 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
256 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
259 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
260 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
262 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
264 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
266 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
270 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
272 return lut_ctxno_sc[lu];
275 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
277 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
278 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
282 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
287 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
290 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
293 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
296 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
299 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
302 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
306 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
307 OPJ_UINT32 s, OPJ_UINT32 stride)
310 flagsp[-1] |= T1_SIGMA_5 << (3U * ci);
312 /* mark target as significant */
313 *flagsp |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci);
316 flagsp[1] |= T1_SIGMA_3 << (3U * ci);
318 /* north-west, north, north-east */
320 opj_flag_t* north = flagsp - stride;
321 *north |= (s << T1_CHI_5_I) | T1_SIGMA_16;
322 north[-1] |= T1_SIGMA_17;
323 north[1] |= T1_SIGMA_15;
326 /* south-west, south, south-east */
328 opj_flag_t* south = flagsp + stride;
329 *south |= (s << T1_CHI_0_I) | T1_SIGMA_1;
330 south[-1] |= T1_SIGMA_2;
331 south[1] |= T1_SIGMA_0;
336 Encode significant pass
338 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
351 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
353 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
354 T1_CHI_S) << (ci * 3U)) : ~0U;
355 OPJ_UINT32 const flags = *flagsp & vsc_mask;
357 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
358 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
359 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
360 v = opj_int_abs(*datap) & one ? 1 : 0;
362 fprintf(stderr, " ctxt1=%d\n", ctxt1);
364 opj_mqc_setcurctx(mqc, ctxt1);
365 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
366 opj_mqc_bypass_enc(mqc, v);
368 opj_mqc_encode(mqc, v);
371 /* Note: using flags instead of *flagsp & vsc_mask result */
372 /* in slow down. Probably because of register pressure */
373 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
375 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
377 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
378 v = *datap < 0 ? 1 : 0;
379 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
382 fprintf(stderr, " ctxt2=%d\n", ctxt2);
384 opj_mqc_setcurctx(mqc, ctxt2);
385 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
386 opj_mqc_bypass_enc(mqc, v);
388 OPJ_UINT32 spb = opj_t1_getspb(lu);
390 fprintf(stderr, " spb=%d\n", spb);
392 opj_mqc_encode(mqc, v ^ spb);
394 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
396 *flagsp |= T1_PI_THIS << (ci * 3U);
400 static INLINE void opj_t1_dec_sigpass_step_raw(
404 OPJ_INT32 oneplushalf,
409 opj_raw_t *raw = &(t1->raw); /* RAW component */
411 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
412 T1_CHI_S) << (ci * 3U)) : ~0U;
413 OPJ_UINT32 const flags = *flagsp & vsc_mask;
415 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
416 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
417 if (opj_raw_decode(raw)) {
418 v = opj_raw_decode(raw);
419 *datap = v ? -oneplushalf : oneplushalf;
420 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
422 *flagsp |= T1_PI_THIS << (ci * 3U);
426 static INLINE void opj_t1_dec_sigpass_step_mqc(
430 OPJ_INT32 oneplushalf,
432 OPJ_UINT32 flags_stride)
436 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
437 OPJ_UINT32 const flags = *flagsp;
439 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
440 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
441 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
443 flagsp[-1], flagsp[1],
445 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
446 opj_mqc_setcurctx(mqc, ctxt1);
447 if (opj_mqc_decode(mqc)) {
448 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
449 OPJ_UINT32 spb = opj_t1_getspb(lu);
450 opj_mqc_setcurctx(mqc, ctxt2);
451 v = opj_mqc_decode(mqc) ^ spb;
452 *datap = v ? -oneplushalf : oneplushalf;
453 opj_t1_update_flags(flagsp, ci, v, flags_stride);
455 *flagsp |= T1_PI_THIS << (ci * 3U);
459 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
463 OPJ_INT32 oneplushalf,
469 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
471 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
472 T1_CHI_S) << (ci * 3U)) : ~0U;
473 OPJ_UINT32 const flags = *flagsp & vsc_mask;
475 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
476 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
477 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
479 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
481 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
482 opj_mqc_setcurctx(mqc, ctxt1);
483 if (opj_mqc_decode(mqc)) {
484 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
485 OPJ_UINT32 spb = opj_t1_getspb(lu);
486 opj_mqc_setcurctx(mqc, ctxt2);
487 v = opj_mqc_decode(mqc) ^ spb;
488 *datap = v ? -oneplushalf : oneplushalf;
489 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
491 *flagsp |= T1_PI_THIS << (ci * 3U);
496 static void opj_t1_enc_sigpass(opj_t1_t *t1,
504 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
505 opj_flag_t* f = &T1_FLAGS(0, 0);
506 OPJ_UINT32 const extra = 2;
510 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
512 for (k = 0; k < (t1->h & ~3U); k += 4) {
514 fprintf(stderr, " k=%d\n", k);
516 for (i = 0; i < t1->w; ++i) {
518 fprintf(stderr, " i=%d\n", i);
521 /* Nothing to do for any of the 4 data points */
525 opj_t1_enc_sigpass_step(
528 &t1->data[((k + 0) * t1->data_stride) + i],
534 opj_t1_enc_sigpass_step(
537 &t1->data[((k + 1) * t1->data_stride) + i],
543 opj_t1_enc_sigpass_step(
546 &t1->data[((k + 2) * t1->data_stride) + i],
552 opj_t1_enc_sigpass_step(
555 &t1->data[((k + 3) * t1->data_stride) + i],
560 3, cblksty & J2K_CCP_CBLKSTY_VSC);
569 fprintf(stderr, " k=%d\n", k);
571 for (i = 0; i < t1->w; ++i) {
573 fprintf(stderr, " i=%d\n", i);
576 /* Nothing to do for any of the 4 data points */
580 for (j = k; j < t1->h; ++j) {
581 opj_t1_enc_sigpass_step(
584 &t1->data[(j * t1->data_stride) + i],
590 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
597 static void opj_t1_dec_sigpass_raw(
602 OPJ_INT32 one, half, oneplushalf;
604 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
607 oneplushalf = one | half;
608 if ((cblksty & J2K_CCP_CBLKSTY_VSC)) {
609 OPJ_INT32 *data1 = t1->data;
610 for (k = 0; k < t1->h; k += 4) {
611 for (i = 0; i < t1->w; ++i) {
612 OPJ_INT32* data2 = data1 + i;
613 for (j = k; j < k + 4 && j < t1->h; ++j) {
614 OPJ_INT32 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
615 opj_t1_dec_sigpass_step_raw(
630 OPJ_INT32 *data1 = t1->data;
631 for (k = 0; k < (t1->h & ~3U); k += 4) {
632 for (i = 0; i < t1->w; ++i) {
633 OPJ_INT32* data2 = data1 + i;
634 opj_t1_dec_sigpass_step_raw(
642 opj_t1_dec_sigpass_step_raw(
650 opj_t1_dec_sigpass_step_raw(
658 opj_t1_dec_sigpass_step_raw(
672 for (i = 0; i < t1->w; ++i) {
673 OPJ_INT32* data2 = data1 + i;
674 for (j = 0; j < t1->h - k; ++j) {
675 opj_t1_dec_sigpass_step_raw(
690 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
692 OPJ_INT32 one, half, oneplushalf; \
693 OPJ_UINT32 i, j, k; \
694 OPJ_INT32 *data1 = t1->data; \
695 opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
698 oneplushalf = one | half; \
699 for (k = 0; k < (h & ~3u); k += 4) { \
700 for (i = 0; i < w; ++i) { \
701 OPJ_INT32 *data2 = data1 + i; \
702 if( *flagsp != 0 ) { \
703 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 0U, flags_stride); \
705 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 1U, flags_stride); \
707 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 2U, flags_stride); \
709 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 3U, flags_stride); \
717 for (i = 0; i < w; ++i) { \
718 OPJ_INT32 *data2 = data1 + i; \
719 for (j = k; j < h; ++j) { \
720 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, j - k, flags_stride); \
727 static void opj_t1_dec_sigpass_mqc_64x64(
731 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
734 static void opj_t1_dec_sigpass_mqc_generic(
738 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2);
741 static void opj_t1_dec_sigpass_mqc_vsc(
745 OPJ_INT32 one, half, oneplushalf, vsc;
747 OPJ_INT32 *data1 = t1->data;
748 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
751 oneplushalf = one | half;
752 for (k = 0; k < (t1->h & ~3U); k += 4U) {
753 for (i = 0; i < t1->w; ++i) {
754 OPJ_INT32 *data2 = data1 + i;
755 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 0U);
757 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 1U);
759 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 2U);
761 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 1, 3U);
768 for (i = 0; i < t1->w; ++i) {
769 OPJ_INT32 *data2 = data1 + i;
770 for (j = k; j < t1->h; ++j) {
771 vsc = (j == t1->h - 1) ? 1 : 0;
772 opj_t1_dec_sigpass_step_mqc_vsc(
787 Encode refinement pass step
789 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
801 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
803 OPJ_UINT32 const shift_flags =
805 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
807 (*flagsp >> (ci * 3U));
809 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
810 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
811 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
813 v = opj_int_abs(*datap) & one ? 1 : 0;
815 fprintf(stderr, " ctxt=%d\n", ctxt);
817 opj_mqc_setcurctx(mqc, ctxt);
818 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
819 opj_mqc_bypass_enc(mqc, v);
821 opj_mqc_encode(mqc, v);
823 *flagsp |= T1_MU_THIS << (ci * 3U);
828 static INLINE void opj_t1_dec_refpass_step_raw(
837 opj_raw_t *raw = &(t1->raw); /* RAW component */
839 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
840 (T1_SIGMA_THIS << (ci * 3U))) {
841 v = opj_raw_decode(raw);
842 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
843 *flagsp |= T1_MU_THIS << (ci * 3U);
847 static INLINE void opj_t1_dec_refpass_step_mqc(
856 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
858 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
859 (T1_SIGMA_THIS << (ci * 3U))) {
860 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(*flagsp >> (ci * 3U));
861 opj_mqc_setcurctx(mqc, ctxt);
862 v = opj_mqc_decode(mqc);
863 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
864 *flagsp |= T1_MU_THIS << (ci * 3U);
868 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
878 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
880 OPJ_UINT32 const shift_flags =
882 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
884 (*flagsp >> (ci * 3U));
887 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
888 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
889 opj_mqc_setcurctx(mqc, ctxt);
890 v = opj_mqc_decode(mqc);
891 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
892 *flagsp |= T1_MU_THIS << (ci * 3U);
896 static void opj_t1_enc_refpass(
904 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
905 opj_flag_t* f = &T1_FLAGS(0, 0);
906 const OPJ_UINT32 extra = 2U;
910 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
912 for (k = 0; k < (t1->h & ~3U); k += 4) {
914 fprintf(stderr, " k=%d\n", k);
916 for (i = 0; i < t1->w; ++i) {
918 fprintf(stderr, " i=%d\n", i);
920 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
921 /* none significant */
925 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
926 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
927 /* all processed by sigpass */
932 opj_t1_enc_refpass_step(
935 &t1->data[((k + 0) * t1->data_stride) + i],
941 opj_t1_enc_refpass_step(
944 &t1->data[((k + 1) * t1->data_stride) + i],
950 opj_t1_enc_refpass_step(
953 &t1->data[((k + 2) * t1->data_stride) + i],
959 opj_t1_enc_refpass_step(
962 &t1->data[((k + 3) * t1->data_stride) + i],
967 3, cblksty & J2K_CCP_CBLKSTY_VSC);
976 fprintf(stderr, " k=%d\n", k);
978 for (i = 0; i < t1->w; ++i) {
980 fprintf(stderr, " i=%d\n", i);
982 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
983 /* none significant */
987 for (j = k; j < t1->h; ++j) {
988 opj_t1_enc_refpass_step(
991 &t1->data[(j * t1->data_stride) + i],
997 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1005 static void opj_t1_dec_refpass_raw(
1009 OPJ_INT32 one, poshalf;
1011 OPJ_INT32 *data1 = t1->data;
1012 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1015 for (k = 0; k < (t1->h & ~3U); k += 4) {
1016 for (i = 0; i < t1->w; ++i) {
1017 OPJ_INT32 *data2 = data1 + i;
1018 opj_t1_dec_refpass_step_raw(
1025 opj_t1_dec_refpass_step_raw(
1032 opj_t1_dec_refpass_step_raw(
1039 opj_t1_dec_refpass_step_raw(
1048 data1 += t1->w << 2;
1052 for (i = 0; i < t1->w; ++i) {
1053 OPJ_INT32 *data2 = data1 + i;
1054 for (j = k; j < t1->h; ++j) {
1055 opj_t1_dec_refpass_step_raw(
1068 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1070 OPJ_INT32 one, poshalf; \
1071 OPJ_UINT32 i, j, k; \
1072 OPJ_INT32 *data1 = t1->data; \
1073 opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1075 poshalf = one >> 1; \
1076 for (k = 0; k < (h & ~3u); k += 4) { \
1077 for (i = 0; i < w; ++i) { \
1078 OPJ_INT32 *data2 = data1 + i; \
1079 if( *flagsp != 0 ) { \
1080 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 0U); \
1082 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 1U); \
1084 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 2U); \
1086 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 3U); \
1094 for (i = 0; i < w; ++i) { \
1095 OPJ_INT32 *data2 = data1 + i; \
1096 for (j = k; j < h; ++j) { \
1097 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, j - k); \
1104 static void opj_t1_dec_refpass_mqc_64x64(
1108 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1111 static void opj_t1_dec_refpass_mqc_generic(
1115 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1118 static void opj_t1_dec_refpass_mqc_vsc(
1122 OPJ_INT32 one, poshalf;
1125 OPJ_INT32 *data1 = t1->data;
1126 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1129 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1130 for (i = 0; i < t1->w; ++i) {
1131 OPJ_INT32 *data2 = data1 + i;
1132 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1135 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1138 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1141 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1146 data1 += t1->w << 2;
1149 for (i = 0; i < t1->w; ++i) {
1150 OPJ_INT32 *data2 = data1 + i;
1151 for (j = k; j < t1->h; ++j) {
1152 vsc = (j == t1->h - 1) ? 1 : 0;
1153 opj_t1_dec_refpass_step_mqc_vsc(
1167 Encode clean-up pass step
1169 static void opj_t1_enc_clnpass_step(
1183 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1185 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1186 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1188 if ((*flagsp & check) == check) {
1190 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1191 } else if (runlen == 1) {
1192 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1193 } else if (runlen == 2) {
1194 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1195 } else if (runlen == 3) {
1196 *flagsp &= ~(T1_PI_3);
1201 for (ci = runlen; ci < lim; ++ci) {
1203 OPJ_UINT32 vsc_mask;
1206 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1207 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1209 flags = *flagsp & vsc_mask;
1211 if ((agg != 0) && (ci == runlen)) {
1215 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1216 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1217 #ifdef DEBUG_ENC_CLN
1218 printf(" ctxt1=%d\n", ctxt1);
1220 opj_mqc_setcurctx(mqc, ctxt1);
1221 v = opj_int_abs(*datap) & one ? 1 : 0;
1222 opj_mqc_encode(mqc, v);
1224 OPJ_UINT32 ctxt2, spb;
1227 /* Note: using flags instead of *flagsp & vsc_mask result */
1228 /* in slow down. Probably because of register pressure */
1229 lu = opj_t1_getctxtno_sc_or_spb_index(
1231 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1233 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1235 ctxt2 = opj_t1_getctxno_sc(lu);
1236 #ifdef DEBUG_ENC_CLN
1237 printf(" ctxt2=%d\n", ctxt2);
1239 opj_mqc_setcurctx(mqc, ctxt2);
1241 v = *datap < 0 ? 1 : 0;
1242 spb = opj_t1_getspb(lu);
1243 #ifdef DEBUG_ENC_CLN
1244 printf(" spb=%d\n", spb);
1246 opj_mqc_encode(mqc, v ^ spb);
1247 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1250 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1251 datap += t1->data_stride;
1256 static void opj_t1_dec_clnpass_step_partial(
1260 OPJ_INT32 oneplushalf,
1264 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1266 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1267 *flagsp, flagsp[-1], flagsp[1],
1269 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1270 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1271 *datap = v ? -oneplushalf : oneplushalf;
1272 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1273 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1276 static void opj_t1_dec_clnpass_step(
1280 OPJ_INT32 oneplushalf,
1285 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1286 if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1287 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1288 opj_mqc_setcurctx(mqc, ctxt1);
1289 if (opj_mqc_decode(mqc)) {
1290 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1291 *flagsp, flagsp[-1], flagsp[1],
1293 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1294 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1295 *datap = v ? -oneplushalf : oneplushalf;
1296 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1299 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1302 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1306 OPJ_INT32 oneplushalf,
1308 OPJ_UINT32 flags_stride)
1312 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1314 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1315 opj_mqc_setcurctx(mqc, ctxt1);
1316 if (opj_mqc_decode(mqc)) {
1317 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1318 *flagsp, flagsp[-1], flagsp[1],
1320 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1321 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1322 *datap = v ? -oneplushalf : oneplushalf;
1323 opj_t1_update_flags(flagsp, ci, v, flags_stride);
1327 static void opj_t1_dec_clnpass_step_vsc(
1331 OPJ_INT32 oneplushalf,
1338 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1340 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1343 OPJ_UINT32 flags = *flagsp & vsc_mask;
1347 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1348 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1349 opj_mqc_setcurctx(mqc, ctxt1);
1350 if (opj_mqc_decode(mqc)) {
1353 lu = opj_t1_getctxtno_sc_or_spb_index(
1354 *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1355 flagsp[1] & vsc_mask,
1357 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1358 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1359 *datap = v ? -oneplushalf : oneplushalf;
1360 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1363 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1366 static void opj_t1_enc_clnpass(
1373 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1374 OPJ_UINT32 agg, runlen;
1376 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1378 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1379 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1383 #ifdef DEBUG_ENC_CLN
1384 printf("enc_clnpass: bpno=%d\n", bpno);
1386 for (k = 0; k < (t1->h & ~3U); k += 4) {
1387 #ifdef DEBUG_ENC_CLN
1388 printf(" k=%d\n", k);
1390 for (i = 0; i < t1->w; ++i) {
1391 #ifdef DEBUG_ENC_CLN
1392 printf(" i=%d\n", i);
1394 agg = !(T1_FLAGS(i, k) & agg_mask);
1395 #ifdef DEBUG_ENC_CLN
1396 printf(" agg=%d\n", agg);
1399 for (runlen = 0; runlen < 4; ++runlen) {
1400 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1404 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1405 opj_mqc_encode(mqc, runlen != 4);
1409 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1410 opj_mqc_encode(mqc, runlen >> 1);
1411 opj_mqc_encode(mqc, runlen & 1);
1415 opj_t1_enc_clnpass_step(
1418 &t1->data[((k + runlen) * t1->data_stride) + i],
1431 #ifdef DEBUG_ENC_CLN
1432 printf(" k=%d\n", k);
1434 for (i = 0; i < t1->w; ++i) {
1435 #ifdef DEBUG_ENC_CLN
1436 printf(" i=%d\n", i);
1437 printf(" agg=%d\n", agg);
1439 opj_t1_enc_clnpass_step(
1442 &t1->data[((k + runlen) * t1->data_stride) + i],
1454 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1456 OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1457 OPJ_UINT32 runlen; \
1458 OPJ_UINT32 i, j, k; \
1459 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1461 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ \
1465 oneplushalf = one | half; \
1466 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1467 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1468 const OPJ_UINT32 agg_mask = ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1469 for (k = 0; k < h; k += 4) { \
1470 for (i = 0; i < w; ++i) { \
1471 opj_flag_t *flagsp2 = flagsp1 + i; \
1473 agg = !(*flagsp2 & agg_mask); \
1478 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1479 if (!opj_mqc_decode(mqc)) { \
1482 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1483 runlen = opj_mqc_decode(mqc); \
1484 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1488 for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1489 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1490 opj_t1_dec_clnpass_step_vsc( \
1493 &t1->data[(j * w) + i], \
1495 agg && (j == k + runlen), \
1498 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1500 flagsp1 += flags_stride; \
1503 OPJ_INT32 *data1 = t1->data; \
1504 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1505 for (k = 0; k < (h & ~3u); k += 4) { \
1506 for (i = 0; i < w; ++i) { \
1507 OPJ_INT32 *data2 = data1 + i; \
1508 opj_flag_t *flagsp2 = flagsp1 + i; \
1509 if (*flagsp2 == 0) { \
1510 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1511 if (!opj_mqc_decode(mqc)) { \
1514 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1515 runlen = opj_mqc_decode(mqc); \
1516 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1517 data2 += runlen * w; \
1518 for (j = runlen; j < 4; ++j) { \
1519 if (j == runlen) { \
1520 opj_t1_dec_clnpass_step_partial(t1, flagsp2, data2, oneplushalf, j); \
1522 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j); \
1527 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (0 * 3U)))) {\
1528 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 0U, flags_stride); \
1531 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (1 * 3U)))) {\
1532 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 1U, flags_stride); \
1535 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (2 * 3U)))) {\
1536 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 2U, flags_stride); \
1539 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (3 * 3U)))) {\
1540 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 3U, flags_stride); \
1544 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1547 flagsp1 += flags_stride; \
1549 for (i = 0; i < w; ++i) { \
1550 OPJ_INT32 *data2 = data1 + i; \
1551 opj_flag_t *flagsp2 = flagsp1 + i; \
1552 for (j = k; j < h; ++j) { \
1553 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j - k); \
1556 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1562 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1563 v = opj_mqc_decode(mqc); \
1564 v = (v << 1) | opj_mqc_decode(mqc); \
1565 v = (v << 1) | opj_mqc_decode(mqc); \
1566 v = (v << 1) | opj_mqc_decode(mqc); \
1569 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1575 static void opj_t1_dec_clnpass_64x64(
1580 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1583 static void opj_t1_dec_clnpass_generic(
1588 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1593 /** mod fixed_quality */
1594 static OPJ_FLOAT64 opj_t1_getwmsedec(
1601 OPJ_FLOAT64 stepsize,
1602 OPJ_UINT32 numcomps,
1603 const OPJ_FLOAT64 * mct_norms,
1604 OPJ_UINT32 mct_numcomps)
1606 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1607 OPJ_ARG_NOT_USED(numcomps);
1609 if (mct_norms && (compno < mct_numcomps)) {
1610 w1 = mct_norms[compno];
1614 w2 = opj_dwt_getnorm(level, orient);
1615 } else { /* if (qmfbid == 0) */
1616 w2 = opj_dwt_getnorm_real(level, orient);
1619 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1620 wmsedec *= wmsedec * nmsedec / 8192.0;
1625 static OPJ_BOOL opj_t1_allocate_buffers(
1631 OPJ_UINT32 flags_stride;
1633 /* encoder uses tile buffer, so no need to allocate */
1637 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1638 /* Overflow check */
1639 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1640 /* FIXME event manager error callback */
1644 datasize = (size_t)w * h;
1646 /* Overflow check */
1647 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1648 /* FIXME event manager error callback */
1652 if (datasize > (size_t)t1->datasize) {
1653 opj_aligned_free(t1->data);
1654 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1656 /* FIXME event manager error callback */
1659 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1660 /* TODO remove this if t1->datasize type changes to size_t */
1661 /* Overflow check */
1662 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1663 /* FIXME event manager error callback */
1667 t1->datasize = (OPJ_UINT32)datasize;
1669 /* memset first arg is declared to never be null by gcc */
1670 if (t1->data != NULL) {
1671 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1675 /* Overflow check */
1676 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1677 /* FIXME event manager error callback */
1680 flags_stride = w + 2U; /* can't be 0U */
1682 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1683 /* Overflow check */
1684 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1685 /* FIXME event manager error callback */
1689 flagssize = (h + 3U) / 4U + 2U;
1691 /* Overflow check */
1692 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1693 /* FIXME event manager error callback */
1696 flagssize *= (size_t)flags_stride;
1701 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1703 if (flagssize > (size_t)t1->flagssize) {
1704 /* Overflow check */
1705 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1706 /* FIXME event manager error callback */
1709 opj_aligned_free(t1->flags);
1710 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1713 /* FIXME event manager error callback */
1716 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1717 /* TODO remove this if t1->flagssize type changes to size_t */
1718 /* Overflow check */
1719 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1720 /* FIXME event manager error callback */
1725 t1->flagssize = (OPJ_UINT32)flagssize;
1727 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1730 for (x = 0; x < flags_stride; ++x) {
1731 /* magic value to hopefully stop any passes being interested in this entry */
1732 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1735 p = &t1->flags[((flags_height + 1) * flags_stride)];
1736 for (x = 0; x < flags_stride; ++x) {
1737 /* magic value to hopefully stop any passes being interested in this entry */
1738 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1743 p = &t1->flags[((flags_height) * flags_stride)];
1745 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1746 } else if (h % 4 == 2) {
1747 v |= T1_PI_2 | T1_PI_3;
1748 } else if (h % 4 == 3) {
1751 for (x = 0; x < flags_stride; ++x) {
1763 /* ----------------------------------------------------------------------- */
1765 /* ----------------------------------------------------------------------- */
1767 * Creates a new Tier 1 handle
1768 * and initializes the look-up tables of the Tier-1 coder/decoder
1769 * @return a new T1 handle if successful, returns NULL otherwise
1771 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1773 opj_t1_t *l_t1 = 00;
1775 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1780 l_t1->encoder = isEncoder;
1787 * Destroys a previously created T1 handle
1789 * @param p_t1 Tier 1 handle to destroy
1791 void opj_t1_destroy(opj_t1_t *p_t1)
1797 /* encoder uses tile buffer, so no need to free */
1798 if (!p_t1->encoder && p_t1->data) {
1799 opj_aligned_free(p_t1->data);
1804 opj_aligned_free(p_t1->flags);
1813 opj_tcd_cblk_dec_t* cblk;
1814 opj_tcd_band_t* band;
1815 opj_tcd_tilecomp_t* tilec;
1817 volatile OPJ_BOOL* pret;
1818 } opj_t1_cblk_decode_processing_job_t;
1820 static void opj_t1_destroy_wrapper(void* t1)
1822 opj_t1_destroy((opj_t1_t*) t1);
1825 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1827 opj_tcd_cblk_dec_t* cblk;
1828 opj_tcd_band_t* band;
1829 opj_tcd_tilecomp_t* tilec;
1831 OPJ_INT32* OPJ_RESTRICT datap;
1832 OPJ_UINT32 cblk_w, cblk_h;
1835 opj_t1_cblk_decode_processing_job_t* job;
1840 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1846 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1848 if (!*(job->pret)) {
1853 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1855 t1 = opj_t1_create(OPJ_FALSE);
1856 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1859 if (OPJ_FALSE == opj_t1_decode_cblk(
1863 (OPJ_UINT32)tccp->roishift,
1865 *(job->pret) = OPJ_FALSE;
1870 x = cblk->x0 - band->x0;
1871 y = cblk->y0 - band->y0;
1872 if (band->bandno & 1) {
1873 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1874 x += pres->x1 - pres->x0;
1876 if (band->bandno & 2) {
1877 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1878 y += pres->y1 - pres->y0;
1885 if (tccp->roishift) {
1886 OPJ_INT32 thresh = 1 << tccp->roishift;
1887 for (j = 0; j < cblk_h; ++j) {
1888 for (i = 0; i < cblk_w; ++i) {
1889 OPJ_INT32 val = datap[(j * cblk_w) + i];
1890 OPJ_INT32 mag = abs(val);
1891 if (mag >= thresh) {
1892 mag >>= tccp->roishift;
1893 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1898 if (tccp->qmfbid == 1) {
1899 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1901 for (j = 0; j < cblk_h; ++j) {
1903 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1904 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1905 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1906 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1907 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1908 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1909 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1910 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1911 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1913 for (; i < cblk_w; ++i) {
1914 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1915 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1918 } else { /* if (tccp->qmfbid == 0) */
1919 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1920 tile_w + (OPJ_UINT32)x];
1921 for (j = 0; j < cblk_h; ++j) {
1922 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1923 for (i = 0; i < cblk_w; ++i) {
1924 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1937 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1938 volatile OPJ_BOOL* pret,
1939 opj_tcd_tilecomp_t* tilec,
1943 OPJ_UINT32 resno, bandno, precno, cblkno;
1945 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1946 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1948 for (bandno = 0; bandno < res->numbands; ++bandno) {
1949 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1951 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1952 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1954 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1955 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1956 opj_t1_cblk_decode_processing_job_t* job;
1958 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1959 sizeof(opj_t1_cblk_decode_processing_job_t));
1970 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
1983 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
1984 opj_tcd_cblk_dec_t* cblk,
1986 OPJ_UINT32 roishift,
1989 opj_raw_t *raw = &(t1->raw); /* RAW component */
1990 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1992 OPJ_INT32 bpno_plus_one;
1993 OPJ_UINT32 passtype;
1994 OPJ_UINT32 segno, passno;
1995 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
1997 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
1999 if (!opj_t1_allocate_buffers(
2001 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2002 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2006 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2009 opj_mqc_resetstates(mqc);
2010 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2011 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2012 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2014 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2015 opj_tcd_seg_t *seg = &cblk->segs[segno];
2018 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2019 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2020 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2021 if (seg->data == 00) {
2024 if (type == T1_TYPE_RAW) {
2025 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2027 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2033 if (t1->w == 64 && t1->h == 64) {
2034 for (passno = 0; (passno < seg->real_num_passes) &&
2035 (bpno_plus_one >= 1); ++passno) {
2038 if (type == T1_TYPE_RAW) {
2039 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2041 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2042 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2044 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2049 if (type == T1_TYPE_RAW) {
2050 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2052 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2053 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2055 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2060 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2064 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2065 opj_mqc_resetstates(mqc);
2066 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2067 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2068 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2070 if (++passtype == 3) {
2076 for (passno = 0; (passno < seg->real_num_passes) &&
2077 (bpno_plus_one >= 1); ++passno) {
2080 if (type == T1_TYPE_RAW) {
2081 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2083 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2084 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2086 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2091 if (type == T1_TYPE_RAW) {
2092 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2094 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2095 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2097 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2102 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2106 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2107 opj_mqc_resetstates(mqc);
2108 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2109 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2110 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2112 if (++passtype == 3) {
2125 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2126 opj_tcd_tile_t *tile,
2128 const OPJ_FLOAT64 * mct_norms,
2129 OPJ_UINT32 mct_numcomps
2132 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2134 tile->distotile = 0; /* fixed_quality */
2136 for (compno = 0; compno < tile->numcomps; ++compno) {
2137 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2138 opj_tccp_t* tccp = &tcp->tccps[compno];
2139 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2141 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2142 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2144 for (bandno = 0; bandno < res->numbands; ++bandno) {
2145 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2146 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2148 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2149 opj_tcd_precinct_t *prc = &band->precincts[precno];
2151 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2152 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2153 OPJ_INT32* OPJ_RESTRICT tiledp;
2156 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2158 OPJ_INT32 x = cblk->x0 - band->x0;
2159 OPJ_INT32 y = cblk->y0 - band->y0;
2160 if (band->bandno & 1) {
2161 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2162 x += pres->x1 - pres->x0;
2164 if (band->bandno & 2) {
2165 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2166 y += pres->y1 - pres->y0;
2169 if (!opj_t1_allocate_buffers(
2171 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2172 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2178 tileLineAdvance = tile_w - cblk_w;
2180 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2182 t1->data_stride = tile_w;
2183 if (tccp->qmfbid == 1) {
2184 for (j = 0; j < cblk_h; ++j) {
2185 for (i = 0; i < cblk_w; ++i) {
2186 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2189 tileIndex += tileLineAdvance;
2191 } else { /* if (tccp->qmfbid == 0) */
2192 for (j = 0; j < cblk_h; ++j) {
2193 for (i = 0; i < cblk_w; ++i) {
2194 OPJ_INT32 tmp = tiledp[tileIndex];
2201 tileIndex += tileLineAdvance;
2210 tilec->numresolutions - 1 - resno,
2227 /** mod fixed_quality */
2228 static void opj_t1_encode_cblk(opj_t1_t *t1,
2229 opj_tcd_cblk_enc_t* cblk,
2234 OPJ_FLOAT64 stepsize,
2236 OPJ_UINT32 numcomps,
2237 opj_tcd_tile_t * tile,
2238 const OPJ_FLOAT64 * mct_norms,
2239 OPJ_UINT32 mct_numcomps)
2241 OPJ_FLOAT64 cumwmsedec = 0.0;
2243 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2247 OPJ_UINT32 passtype;
2248 OPJ_INT32 nmsedec = 0;
2251 OPJ_BYTE type = T1_TYPE_MQ;
2252 OPJ_FLOAT64 tempwmsedec;
2254 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2257 for (i = 0; i < t1->w; ++i) {
2258 for (j = 0; j < t1->h; ++j) {
2259 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2260 max = opj_int_max(max, tmp);
2264 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2265 T1_NMSEDEC_FRACBITS) : 0;
2267 bpno = (OPJ_INT32)(cblk->numbps - 1);
2270 opj_mqc_resetstates(mqc);
2271 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2272 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2273 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2274 opj_mqc_init_enc(mqc, cblk->data);
2276 for (passno = 0; bpno >= 0; ++passno) {
2277 opj_tcd_pass_t *pass = &cblk->passes[passno];
2278 OPJ_UINT32 correction = 3;
2279 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2280 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2284 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2287 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2290 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2291 /* code switch SEGMARK (i.e. SEGSYM) */
2292 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2293 opj_mqc_segmark_enc(mqc);
2299 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2300 stepsize, numcomps, mct_norms, mct_numcomps) ;
2301 cumwmsedec += tempwmsedec;
2302 tile->distotile += tempwmsedec;
2304 /* Code switch "RESTART" (i.e. TERMALL) */
2305 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2307 if (type == T1_TYPE_RAW) {
2310 /* correction = mqc_bypass_flush_enc(); */
2311 } else { /* correction = mqc_restart_enc(); */
2317 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2318 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2319 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2320 if (type == T1_TYPE_RAW) {
2323 /* correction = mqc_bypass_flush_enc(); */
2324 } else { /* correction = mqc_restart_enc(); */
2334 if (++passtype == 3) {
2339 if (pass->term && bpno > 0) {
2340 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2341 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2342 if (type == T1_TYPE_RAW) {
2343 opj_mqc_bypass_init_enc(mqc);
2345 opj_mqc_restart_init_enc(mqc);
2349 pass->distortiondec = cumwmsedec;
2350 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2352 /* Code-switch "RESET" */
2353 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2354 opj_mqc_reset_enc(mqc);
2358 /* Code switch "ERTERM" (i.e. PTERM) */
2359 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2360 opj_mqc_erterm_enc(mqc);
2361 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2365 cblk->totalpasses = passno;
2367 for (passno = 0; passno < cblk->totalpasses; passno++) {
2368 opj_tcd_pass_t *pass = &cblk->passes[passno];
2369 if (pass->rate > opj_mqc_numbytes(mqc)) {
2370 pass->rate = opj_mqc_numbytes(mqc);
2372 /*Preventing generation of FF as last data byte of a pass*/
2373 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2376 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);