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)
309 /* set up to point to the north and south data points' flags words, if required */
313 /* mark target as significant */
314 *flagsp |= T1_SIGMA_4 << (3U * ci);
316 /* north-west, north, north-east */
318 north = flagsp - stride;
319 *north |= T1_SIGMA_16;
320 north[-1] |= T1_SIGMA_17;
321 north[1] |= T1_SIGMA_15;
324 /* south-west, south, south-east */
326 south = flagsp + stride;
327 *south |= T1_SIGMA_1;
328 south[-1] |= T1_SIGMA_2;
329 south[1] |= T1_SIGMA_0;
333 flagsp[-1] |= T1_SIGMA_5 << (3U * ci);
336 flagsp[1] |= T1_SIGMA_3 << (3U * ci);
342 north = flagsp - stride;
354 south = flagsp + stride;
364 Encode significant pass
366 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
379 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
381 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
382 T1_CHI_S) << (ci * 3U)) : ~0U;
383 OPJ_UINT32 const flags = *flagsp & vsc_mask;
385 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
386 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
387 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
388 v = opj_int_abs(*datap) & one ? 1 : 0;
390 fprintf(stderr, " ctxt1=%d\n", ctxt1);
392 opj_mqc_setcurctx(mqc, ctxt1);
393 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
394 opj_mqc_bypass_enc(mqc, v);
396 opj_mqc_encode(mqc, v);
399 /* Note: using flags instead of *flagsp & vsc_mask result */
400 /* in slow down. Probably because of register pressure */
401 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
403 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
405 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
406 v = *datap < 0 ? 1 : 0;
407 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
410 fprintf(stderr, " ctxt2=%d\n", ctxt2);
412 opj_mqc_setcurctx(mqc, ctxt2);
413 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
414 opj_mqc_bypass_enc(mqc, v);
416 OPJ_UINT32 spb = opj_t1_getspb(lu);
418 fprintf(stderr, " spb=%d\n", spb);
420 opj_mqc_encode(mqc, v ^ spb);
422 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
424 *flagsp |= T1_PI_THIS << (ci * 3U);
428 static INLINE void opj_t1_dec_sigpass_step_raw(
432 OPJ_INT32 oneplushalf,
437 opj_raw_t *raw = &(t1->raw); /* RAW component */
439 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
440 T1_CHI_S) << (ci * 3U)) : ~0U;
441 OPJ_UINT32 const flags = *flagsp & vsc_mask;
443 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
444 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
445 if (opj_raw_decode(raw)) {
446 v = opj_raw_decode(raw);
447 *datap = v ? -oneplushalf : oneplushalf;
448 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
450 *flagsp |= T1_PI_THIS << (ci * 3U);
454 static INLINE void opj_t1_dec_sigpass_step_mqc(
458 OPJ_INT32 oneplushalf,
460 OPJ_UINT32 flags_stride)
464 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
465 OPJ_UINT32 const flags = *flagsp;
467 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
468 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
469 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
471 flagsp[-1], flagsp[1],
473 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
474 opj_mqc_setcurctx(mqc, ctxt1);
475 if (opj_mqc_decode(mqc)) {
476 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
477 OPJ_UINT32 spb = opj_t1_getspb(lu);
478 opj_mqc_setcurctx(mqc, ctxt2);
479 v = opj_mqc_decode(mqc) ^ spb;
480 *datap = v ? -oneplushalf : oneplushalf;
481 opj_t1_update_flags(flagsp, ci, v, flags_stride);
483 *flagsp |= T1_PI_THIS << (ci * 3U);
487 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
491 OPJ_INT32 oneplushalf,
497 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
499 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
500 T1_CHI_S) << (ci * 3U)) : ~0U;
501 OPJ_UINT32 const flags = *flagsp & vsc_mask;
503 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
504 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
505 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
507 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
509 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
510 opj_mqc_setcurctx(mqc, ctxt1);
511 if (opj_mqc_decode(mqc)) {
512 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
513 OPJ_UINT32 spb = opj_t1_getspb(lu);
514 opj_mqc_setcurctx(mqc, ctxt2);
515 v = opj_mqc_decode(mqc) ^ spb;
516 *datap = v ? -oneplushalf : oneplushalf;
517 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
519 *flagsp |= T1_PI_THIS << (ci * 3U);
524 static void opj_t1_enc_sigpass(opj_t1_t *t1,
532 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
533 opj_flag_t* f = &T1_FLAGS(0, 0);
534 OPJ_UINT32 const extra = 2;
538 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
540 for (k = 0; k < (t1->h & ~3U); k += 4) {
542 fprintf(stderr, " k=%d\n", k);
544 for (i = 0; i < t1->w; ++i) {
546 fprintf(stderr, " i=%d\n", i);
549 /* Nothing to do for any of the 4 data points */
553 opj_t1_enc_sigpass_step(
556 &t1->data[((k + 0) * t1->data_stride) + i],
562 opj_t1_enc_sigpass_step(
565 &t1->data[((k + 1) * t1->data_stride) + i],
571 opj_t1_enc_sigpass_step(
574 &t1->data[((k + 2) * t1->data_stride) + i],
580 opj_t1_enc_sigpass_step(
583 &t1->data[((k + 3) * t1->data_stride) + i],
588 3, cblksty & J2K_CCP_CBLKSTY_VSC);
597 fprintf(stderr, " k=%d\n", k);
599 for (i = 0; i < t1->w; ++i) {
601 fprintf(stderr, " i=%d\n", i);
604 /* Nothing to do for any of the 4 data points */
608 for (j = k; j < t1->h; ++j) {
609 opj_t1_enc_sigpass_step(
612 &t1->data[(j * t1->data_stride) + i],
618 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
625 static void opj_t1_dec_sigpass_raw(
630 OPJ_INT32 one, half, oneplushalf;
632 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
635 oneplushalf = one | half;
636 if ((cblksty & J2K_CCP_CBLKSTY_VSC)) {
637 OPJ_INT32 *data1 = t1->data;
638 for (k = 0; k < t1->h; k += 4) {
639 for (i = 0; i < t1->w; ++i) {
640 OPJ_INT32* data2 = data1 + i;
641 for (j = k; j < k + 4 && j < t1->h; ++j) {
642 OPJ_INT32 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
643 opj_t1_dec_sigpass_step_raw(
658 OPJ_INT32 *data1 = t1->data;
659 for (k = 0; k < (t1->h & ~3U); k += 4) {
660 for (i = 0; i < t1->w; ++i) {
661 OPJ_INT32* data2 = data1 + i;
662 opj_t1_dec_sigpass_step_raw(
670 opj_t1_dec_sigpass_step_raw(
678 opj_t1_dec_sigpass_step_raw(
686 opj_t1_dec_sigpass_step_raw(
700 for (i = 0; i < t1->w; ++i) {
701 OPJ_INT32* data2 = data1 + i;
702 for (j = 0; j < t1->h - k; ++j) {
703 opj_t1_dec_sigpass_step_raw(
718 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
720 OPJ_INT32 one, half, oneplushalf; \
721 OPJ_UINT32 i, j, k; \
722 OPJ_INT32 *data1 = t1->data; \
723 opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
726 oneplushalf = one | half; \
727 for (k = 0; k < (h & ~3u); k += 4) { \
728 for (i = 0; i < w; ++i) { \
729 OPJ_INT32 *data2 = data1 + i; \
730 if( *flagsp != 0 ) { \
731 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 0U, flags_stride); \
733 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 1U, flags_stride); \
735 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 2U, flags_stride); \
737 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, 3U, flags_stride); \
745 for (i = 0; i < w; ++i) { \
746 OPJ_INT32 *data2 = data1 + i; \
747 for (j = k; j < h; ++j) { \
748 opj_t1_dec_sigpass_step_mqc(t1, flagsp, data2, oneplushalf, j - k, flags_stride); \
755 static void opj_t1_dec_sigpass_mqc_64x64(
759 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
762 static void opj_t1_dec_sigpass_mqc_generic(
766 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2);
769 static void opj_t1_dec_sigpass_mqc_vsc(
773 OPJ_INT32 one, half, oneplushalf, vsc;
775 OPJ_INT32 *data1 = t1->data;
776 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
779 oneplushalf = one | half;
780 for (k = 0; k < (t1->h & ~3U); k += 4U) {
781 for (i = 0; i < t1->w; ++i) {
782 OPJ_INT32 *data2 = data1 + i;
783 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 0U);
785 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 1U);
787 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 2U);
789 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 1, 3U);
796 for (i = 0; i < t1->w; ++i) {
797 OPJ_INT32 *data2 = data1 + i;
798 for (j = k; j < t1->h; ++j) {
799 vsc = (j == t1->h - 1) ? 1 : 0;
800 opj_t1_dec_sigpass_step_mqc_vsc(
815 Encode refinement pass step
817 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
829 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
831 OPJ_UINT32 const shift_flags =
833 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
835 (*flagsp >> (ci * 3U));
837 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
838 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
839 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
841 v = opj_int_abs(*datap) & one ? 1 : 0;
843 fprintf(stderr, " ctxt=%d\n", ctxt);
845 opj_mqc_setcurctx(mqc, ctxt);
846 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
847 opj_mqc_bypass_enc(mqc, v);
849 opj_mqc_encode(mqc, v);
851 *flagsp |= T1_MU_THIS << (ci * 3U);
856 static INLINE void opj_t1_dec_refpass_step_raw(
865 opj_raw_t *raw = &(t1->raw); /* RAW component */
867 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
868 (T1_SIGMA_THIS << (ci * 3U))) {
869 v = opj_raw_decode(raw);
870 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
871 *flagsp |= T1_MU_THIS << (ci * 3U);
875 static INLINE void opj_t1_dec_refpass_step_mqc(
884 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
886 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
887 (T1_SIGMA_THIS << (ci * 3U))) {
888 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(*flagsp >> (ci * 3U));
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 INLINE void opj_t1_dec_refpass_step_mqc_vsc(
906 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
908 OPJ_UINT32 const shift_flags =
910 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
912 (*flagsp >> (ci * 3U));
915 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
916 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
917 opj_mqc_setcurctx(mqc, ctxt);
918 v = opj_mqc_decode(mqc);
919 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
920 *flagsp |= T1_MU_THIS << (ci * 3U);
924 static void opj_t1_enc_refpass(
932 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
933 opj_flag_t* f = &T1_FLAGS(0, 0);
934 const OPJ_UINT32 extra = 2U;
938 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
940 for (k = 0; k < (t1->h & ~3U); k += 4) {
942 fprintf(stderr, " k=%d\n", k);
944 for (i = 0; i < t1->w; ++i) {
946 fprintf(stderr, " i=%d\n", i);
948 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
949 /* none significant */
953 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
954 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
955 /* all processed by sigpass */
960 opj_t1_enc_refpass_step(
963 &t1->data[((k + 0) * t1->data_stride) + i],
969 opj_t1_enc_refpass_step(
972 &t1->data[((k + 1) * t1->data_stride) + i],
978 opj_t1_enc_refpass_step(
981 &t1->data[((k + 2) * t1->data_stride) + i],
987 opj_t1_enc_refpass_step(
990 &t1->data[((k + 3) * t1->data_stride) + i],
995 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1003 #ifdef DEBUG_ENC_REF
1004 fprintf(stderr, " k=%d\n", k);
1006 for (i = 0; i < t1->w; ++i) {
1007 #ifdef DEBUG_ENC_REF
1008 fprintf(stderr, " i=%d\n", i);
1010 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1011 /* none significant */
1015 for (j = k; j < t1->h; ++j) {
1016 opj_t1_enc_refpass_step(
1019 &t1->data[(j * t1->data_stride) + i],
1025 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1033 static void opj_t1_dec_refpass_raw(
1037 OPJ_INT32 one, poshalf;
1039 OPJ_INT32 *data1 = t1->data;
1040 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1043 for (k = 0; k < (t1->h & ~3U); k += 4) {
1044 for (i = 0; i < t1->w; ++i) {
1045 OPJ_INT32 *data2 = data1 + i;
1046 opj_t1_dec_refpass_step_raw(
1053 opj_t1_dec_refpass_step_raw(
1060 opj_t1_dec_refpass_step_raw(
1067 opj_t1_dec_refpass_step_raw(
1076 data1 += t1->w << 2;
1080 for (i = 0; i < t1->w; ++i) {
1081 OPJ_INT32 *data2 = data1 + i;
1082 for (j = k; j < t1->h; ++j) {
1083 opj_t1_dec_refpass_step_raw(
1096 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1098 OPJ_INT32 one, poshalf; \
1099 OPJ_UINT32 i, j, k; \
1100 OPJ_INT32 *data1 = t1->data; \
1101 opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1103 poshalf = one >> 1; \
1104 for (k = 0; k < (h & ~3u); k += 4) { \
1105 for (i = 0; i < w; ++i) { \
1106 OPJ_INT32 *data2 = data1 + i; \
1107 if( *flagsp != 0 ) { \
1108 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 0U); \
1110 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 1U); \
1112 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 2U); \
1114 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, 3U); \
1122 for (i = 0; i < w; ++i) { \
1123 OPJ_INT32 *data2 = data1 + i; \
1124 for (j = k; j < h; ++j) { \
1125 opj_t1_dec_refpass_step_mqc(t1, flagsp, data2, poshalf, j - k); \
1132 static void opj_t1_dec_refpass_mqc_64x64(
1136 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1139 static void opj_t1_dec_refpass_mqc_generic(
1143 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1146 static void opj_t1_dec_refpass_mqc_vsc(
1150 OPJ_INT32 one, poshalf;
1153 OPJ_INT32 *data1 = t1->data;
1154 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1157 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1158 for (i = 0; i < t1->w; ++i) {
1159 OPJ_INT32 *data2 = data1 + i;
1160 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1163 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1166 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1169 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1174 data1 += t1->w << 2;
1177 for (i = 0; i < t1->w; ++i) {
1178 OPJ_INT32 *data2 = data1 + i;
1179 for (j = k; j < t1->h; ++j) {
1180 vsc = (j == t1->h - 1) ? 1 : 0;
1181 opj_t1_dec_refpass_step_mqc_vsc(
1195 Encode clean-up pass step
1197 static void opj_t1_enc_clnpass_step(
1211 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1213 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1214 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1216 if ((*flagsp & check) == check) {
1218 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1219 } else if (runlen == 1) {
1220 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1221 } else if (runlen == 2) {
1222 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1223 } else if (runlen == 3) {
1224 *flagsp &= ~(T1_PI_3);
1229 for (ci = runlen; ci < lim; ++ci) {
1231 OPJ_UINT32 vsc_mask;
1234 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1235 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1237 flags = *flagsp & vsc_mask;
1239 if ((agg != 0) && (ci == runlen)) {
1243 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1244 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1245 #ifdef DEBUG_ENC_CLN
1246 printf(" ctxt1=%d\n", ctxt1);
1248 opj_mqc_setcurctx(mqc, ctxt1);
1249 v = opj_int_abs(*datap) & one ? 1 : 0;
1250 opj_mqc_encode(mqc, v);
1252 OPJ_UINT32 ctxt2, spb;
1255 /* Note: using flags instead of *flagsp & vsc_mask result */
1256 /* in slow down. Probably because of register pressure */
1257 lu = opj_t1_getctxtno_sc_or_spb_index(
1259 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1261 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1263 ctxt2 = opj_t1_getctxno_sc(lu);
1264 #ifdef DEBUG_ENC_CLN
1265 printf(" ctxt2=%d\n", ctxt2);
1267 opj_mqc_setcurctx(mqc, ctxt2);
1269 v = *datap < 0 ? 1 : 0;
1270 spb = opj_t1_getspb(lu);
1271 #ifdef DEBUG_ENC_CLN
1272 printf(" spb=%d\n", spb);
1274 opj_mqc_encode(mqc, v ^ spb);
1275 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1278 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1279 datap += t1->data_stride;
1284 static void opj_t1_dec_clnpass_step_partial(
1288 OPJ_INT32 oneplushalf,
1292 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1294 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1295 *flagsp, flagsp[-1], flagsp[1],
1297 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1298 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1299 *datap = v ? -oneplushalf : oneplushalf;
1300 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1301 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1304 static void opj_t1_dec_clnpass_step(
1308 OPJ_INT32 oneplushalf,
1313 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1314 if (!(*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1315 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1316 opj_mqc_setcurctx(mqc, ctxt1);
1317 if (opj_mqc_decode(mqc)) {
1318 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1319 *flagsp, flagsp[-1], flagsp[1],
1321 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1322 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1323 *datap = v ? -oneplushalf : oneplushalf;
1324 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1327 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1330 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1334 OPJ_INT32 oneplushalf,
1336 OPJ_UINT32 flags_stride)
1340 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1342 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, *flagsp >> (ci * 3U));
1343 opj_mqc_setcurctx(mqc, ctxt1);
1344 if (opj_mqc_decode(mqc)) {
1345 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
1346 *flagsp, flagsp[-1], flagsp[1],
1348 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1349 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1350 *datap = v ? -oneplushalf : oneplushalf;
1351 opj_t1_update_flags(flagsp, ci, v, flags_stride);
1355 static void opj_t1_dec_clnpass_step_vsc(
1359 OPJ_INT32 oneplushalf,
1366 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1368 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1371 OPJ_UINT32 flags = *flagsp & vsc_mask;
1375 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1376 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1377 opj_mqc_setcurctx(mqc, ctxt1);
1378 if (opj_mqc_decode(mqc)) {
1381 lu = opj_t1_getctxtno_sc_or_spb_index(
1382 *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1383 flagsp[1] & vsc_mask,
1385 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1386 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(lu);
1387 *datap = v ? -oneplushalf : oneplushalf;
1388 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1391 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1394 static void opj_t1_enc_clnpass(
1401 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1402 OPJ_UINT32 agg, runlen;
1404 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1406 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1407 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1411 #ifdef DEBUG_ENC_CLN
1412 printf("enc_clnpass: bpno=%d\n", bpno);
1414 for (k = 0; k < (t1->h & ~3U); k += 4) {
1415 #ifdef DEBUG_ENC_CLN
1416 printf(" k=%d\n", k);
1418 for (i = 0; i < t1->w; ++i) {
1419 #ifdef DEBUG_ENC_CLN
1420 printf(" i=%d\n", i);
1422 agg = !(T1_FLAGS(i, k) & agg_mask);
1423 #ifdef DEBUG_ENC_CLN
1424 printf(" agg=%d\n", agg);
1427 for (runlen = 0; runlen < 4; ++runlen) {
1428 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1432 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1433 opj_mqc_encode(mqc, runlen != 4);
1437 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1438 opj_mqc_encode(mqc, runlen >> 1);
1439 opj_mqc_encode(mqc, runlen & 1);
1443 opj_t1_enc_clnpass_step(
1446 &t1->data[((k + runlen) * t1->data_stride) + i],
1459 #ifdef DEBUG_ENC_CLN
1460 printf(" k=%d\n", k);
1462 for (i = 0; i < t1->w; ++i) {
1463 #ifdef DEBUG_ENC_CLN
1464 printf(" i=%d\n", i);
1465 printf(" agg=%d\n", agg);
1467 opj_t1_enc_clnpass_step(
1470 &t1->data[((k + runlen) * t1->data_stride) + i],
1482 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1484 OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1485 OPJ_UINT32 runlen; \
1486 OPJ_UINT32 i, j, k; \
1487 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1489 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */ \
1493 oneplushalf = one | half; \
1494 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1495 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1496 const OPJ_UINT32 agg_mask = ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1497 for (k = 0; k < h; k += 4) { \
1498 for (i = 0; i < w; ++i) { \
1499 opj_flag_t *flagsp2 = flagsp1 + i; \
1501 agg = !(*flagsp2 & agg_mask); \
1506 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1507 if (!opj_mqc_decode(mqc)) { \
1510 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1511 runlen = opj_mqc_decode(mqc); \
1512 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1516 for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1517 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1518 opj_t1_dec_clnpass_step_vsc( \
1521 &t1->data[(j * w) + i], \
1523 agg && (j == k + runlen), \
1526 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1528 flagsp1 += flags_stride; \
1531 OPJ_INT32 *data1 = t1->data; \
1532 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1533 for (k = 0; k < (h & ~3u); k += 4) { \
1534 for (i = 0; i < w; ++i) { \
1535 OPJ_INT32 *data2 = data1 + i; \
1536 opj_flag_t *flagsp2 = flagsp1 + i; \
1537 if (*flagsp2 == 0) { \
1538 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1539 if (!opj_mqc_decode(mqc)) { \
1542 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1543 runlen = opj_mqc_decode(mqc); \
1544 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1545 data2 += runlen * w; \
1546 for (j = runlen; j < 4; ++j) { \
1547 if (j == runlen) { \
1548 opj_t1_dec_clnpass_step_partial(t1, flagsp2, data2, oneplushalf, j); \
1550 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j); \
1555 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (0 * 3U)))) {\
1556 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 0U, flags_stride); \
1559 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (1 * 3U)))) {\
1560 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 1U, flags_stride); \
1563 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (2 * 3U)))) {\
1564 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 2U, flags_stride); \
1567 if (!(*flagsp2 & ((T1_SIGMA_THIS | T1_PI_THIS) << (3 * 3U)))) {\
1568 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flagsp2, data2, oneplushalf, 3U, flags_stride); \
1572 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1575 flagsp1 += flags_stride; \
1577 for (i = 0; i < w; ++i) { \
1578 OPJ_INT32 *data2 = data1 + i; \
1579 opj_flag_t *flagsp2 = flagsp1 + i; \
1580 for (j = k; j < h; ++j) { \
1581 opj_t1_dec_clnpass_step(t1, flagsp2, data2, oneplushalf, j - k); \
1584 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1590 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1591 v = opj_mqc_decode(mqc); \
1592 v = (v << 1) | opj_mqc_decode(mqc); \
1593 v = (v << 1) | opj_mqc_decode(mqc); \
1594 v = (v << 1) | opj_mqc_decode(mqc); \
1597 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1603 static void opj_t1_dec_clnpass_64x64(
1608 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1611 static void opj_t1_dec_clnpass_generic(
1616 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1621 /** mod fixed_quality */
1622 static OPJ_FLOAT64 opj_t1_getwmsedec(
1629 OPJ_FLOAT64 stepsize,
1630 OPJ_UINT32 numcomps,
1631 const OPJ_FLOAT64 * mct_norms,
1632 OPJ_UINT32 mct_numcomps)
1634 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1635 OPJ_ARG_NOT_USED(numcomps);
1637 if (mct_norms && (compno < mct_numcomps)) {
1638 w1 = mct_norms[compno];
1642 w2 = opj_dwt_getnorm(level, orient);
1643 } else { /* if (qmfbid == 0) */
1644 w2 = opj_dwt_getnorm_real(level, orient);
1647 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1648 wmsedec *= wmsedec * nmsedec / 8192.0;
1653 static OPJ_BOOL opj_t1_allocate_buffers(
1659 OPJ_UINT32 flags_stride;
1661 /* encoder uses tile buffer, so no need to allocate */
1665 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1666 /* Overflow check */
1667 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1668 /* FIXME event manager error callback */
1672 datasize = (size_t)w * h;
1674 /* Overflow check */
1675 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1676 /* FIXME event manager error callback */
1680 if (datasize > (size_t)t1->datasize) {
1681 opj_aligned_free(t1->data);
1682 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1684 /* FIXME event manager error callback */
1687 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1688 /* TODO remove this if t1->datasize type changes to size_t */
1689 /* Overflow check */
1690 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1691 /* FIXME event manager error callback */
1695 t1->datasize = (OPJ_UINT32)datasize;
1697 /* memset first arg is declared to never be null by gcc */
1698 if (t1->data != NULL) {
1699 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1703 /* Overflow check */
1704 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1705 /* FIXME event manager error callback */
1708 flags_stride = w + 2U; /* can't be 0U */
1710 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1711 /* Overflow check */
1712 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1713 /* FIXME event manager error callback */
1717 flagssize = (h + 3U) / 4U + 2U;
1719 /* Overflow check */
1720 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1721 /* FIXME event manager error callback */
1724 flagssize *= (size_t)flags_stride;
1729 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1731 if (flagssize > (size_t)t1->flagssize) {
1732 /* Overflow check */
1733 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1734 /* FIXME event manager error callback */
1737 opj_aligned_free(t1->flags);
1738 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1741 /* FIXME event manager error callback */
1744 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1745 /* TODO remove this if t1->flagssize type changes to size_t */
1746 /* Overflow check */
1747 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1748 /* FIXME event manager error callback */
1753 t1->flagssize = (OPJ_UINT32)flagssize;
1755 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1758 for (x = 0; x < flags_stride; ++x) {
1759 /* magic value to hopefully stop any passes being interested in this entry */
1760 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1763 p = &t1->flags[((flags_height + 1) * flags_stride)];
1764 for (x = 0; x < flags_stride; ++x) {
1765 /* magic value to hopefully stop any passes being interested in this entry */
1766 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1771 p = &t1->flags[((flags_height) * flags_stride)];
1773 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1774 } else if (h % 4 == 2) {
1775 v |= T1_PI_2 | T1_PI_3;
1776 } else if (h % 4 == 3) {
1779 for (x = 0; x < flags_stride; ++x) {
1791 /* ----------------------------------------------------------------------- */
1793 /* ----------------------------------------------------------------------- */
1795 * Creates a new Tier 1 handle
1796 * and initializes the look-up tables of the Tier-1 coder/decoder
1797 * @return a new T1 handle if successful, returns NULL otherwise
1799 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1801 opj_t1_t *l_t1 = 00;
1803 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1808 l_t1->encoder = isEncoder;
1815 * Destroys a previously created T1 handle
1817 * @param p_t1 Tier 1 handle to destroy
1819 void opj_t1_destroy(opj_t1_t *p_t1)
1825 /* encoder uses tile buffer, so no need to free */
1826 if (!p_t1->encoder && p_t1->data) {
1827 opj_aligned_free(p_t1->data);
1832 opj_aligned_free(p_t1->flags);
1841 opj_tcd_cblk_dec_t* cblk;
1842 opj_tcd_band_t* band;
1843 opj_tcd_tilecomp_t* tilec;
1845 volatile OPJ_BOOL* pret;
1846 } opj_t1_cblk_decode_processing_job_t;
1848 static void opj_t1_destroy_wrapper(void* t1)
1850 opj_t1_destroy((opj_t1_t*) t1);
1853 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1855 opj_tcd_cblk_dec_t* cblk;
1856 opj_tcd_band_t* band;
1857 opj_tcd_tilecomp_t* tilec;
1859 OPJ_INT32* OPJ_RESTRICT datap;
1860 OPJ_UINT32 cblk_w, cblk_h;
1863 opj_t1_cblk_decode_processing_job_t* job;
1868 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1874 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1876 if (!*(job->pret)) {
1881 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1883 t1 = opj_t1_create(OPJ_FALSE);
1884 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1887 if (OPJ_FALSE == opj_t1_decode_cblk(
1891 (OPJ_UINT32)tccp->roishift,
1893 *(job->pret) = OPJ_FALSE;
1898 x = cblk->x0 - band->x0;
1899 y = cblk->y0 - band->y0;
1900 if (band->bandno & 1) {
1901 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1902 x += pres->x1 - pres->x0;
1904 if (band->bandno & 2) {
1905 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1906 y += pres->y1 - pres->y0;
1913 if (tccp->roishift) {
1914 OPJ_INT32 thresh = 1 << tccp->roishift;
1915 for (j = 0; j < cblk_h; ++j) {
1916 for (i = 0; i < cblk_w; ++i) {
1917 OPJ_INT32 val = datap[(j * cblk_w) + i];
1918 OPJ_INT32 mag = abs(val);
1919 if (mag >= thresh) {
1920 mag >>= tccp->roishift;
1921 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1926 if (tccp->qmfbid == 1) {
1927 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1929 for (j = 0; j < cblk_h; ++j) {
1931 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1932 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1933 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1934 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1935 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1936 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1937 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1938 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1939 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1941 for (; i < cblk_w; ++i) {
1942 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1943 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1946 } else { /* if (tccp->qmfbid == 0) */
1947 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1948 tile_w + (OPJ_UINT32)x];
1949 for (j = 0; j < cblk_h; ++j) {
1950 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1951 for (i = 0; i < cblk_w; ++i) {
1952 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1965 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1966 volatile OPJ_BOOL* pret,
1967 opj_tcd_tilecomp_t* tilec,
1971 OPJ_UINT32 resno, bandno, precno, cblkno;
1973 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1974 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1976 for (bandno = 0; bandno < res->numbands; ++bandno) {
1977 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1979 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1980 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1982 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1983 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1984 opj_t1_cblk_decode_processing_job_t* job;
1986 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1987 sizeof(opj_t1_cblk_decode_processing_job_t));
1998 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2011 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2012 opj_tcd_cblk_dec_t* cblk,
2014 OPJ_UINT32 roishift,
2017 opj_raw_t *raw = &(t1->raw); /* RAW component */
2018 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2020 OPJ_INT32 bpno_plus_one;
2021 OPJ_UINT32 passtype;
2022 OPJ_UINT32 segno, passno;
2023 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2025 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2027 if (!opj_t1_allocate_buffers(
2029 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2030 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2034 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2037 opj_mqc_resetstates(mqc);
2038 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2039 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2040 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2042 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2043 opj_tcd_seg_t *seg = &cblk->segs[segno];
2046 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2047 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2048 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2049 if (seg->data == 00) {
2052 if (type == T1_TYPE_RAW) {
2053 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2055 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2061 if (t1->w == 64 && t1->h == 64) {
2062 for (passno = 0; (passno < seg->real_num_passes) &&
2063 (bpno_plus_one >= 1); ++passno) {
2066 if (type == T1_TYPE_RAW) {
2067 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2069 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2070 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2072 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2077 if (type == T1_TYPE_RAW) {
2078 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2080 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2081 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2083 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2088 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2092 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2093 opj_mqc_resetstates(mqc);
2094 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2095 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2096 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2098 if (++passtype == 3) {
2104 for (passno = 0; (passno < seg->real_num_passes) &&
2105 (bpno_plus_one >= 1); ++passno) {
2108 if (type == T1_TYPE_RAW) {
2109 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2111 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2112 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2114 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2119 if (type == T1_TYPE_RAW) {
2120 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2122 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2123 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2125 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2130 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2134 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2135 opj_mqc_resetstates(mqc);
2136 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2137 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2138 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2140 if (++passtype == 3) {
2153 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2154 opj_tcd_tile_t *tile,
2156 const OPJ_FLOAT64 * mct_norms,
2157 OPJ_UINT32 mct_numcomps
2160 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2162 tile->distotile = 0; /* fixed_quality */
2164 for (compno = 0; compno < tile->numcomps; ++compno) {
2165 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2166 opj_tccp_t* tccp = &tcp->tccps[compno];
2167 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2169 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2170 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2172 for (bandno = 0; bandno < res->numbands; ++bandno) {
2173 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2174 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2176 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2177 opj_tcd_precinct_t *prc = &band->precincts[precno];
2179 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2180 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2181 OPJ_INT32* OPJ_RESTRICT tiledp;
2184 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2186 OPJ_INT32 x = cblk->x0 - band->x0;
2187 OPJ_INT32 y = cblk->y0 - band->y0;
2188 if (band->bandno & 1) {
2189 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2190 x += pres->x1 - pres->x0;
2192 if (band->bandno & 2) {
2193 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2194 y += pres->y1 - pres->y0;
2197 if (!opj_t1_allocate_buffers(
2199 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2200 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2206 tileLineAdvance = tile_w - cblk_w;
2208 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2210 t1->data_stride = tile_w;
2211 if (tccp->qmfbid == 1) {
2212 for (j = 0; j < cblk_h; ++j) {
2213 for (i = 0; i < cblk_w; ++i) {
2214 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2217 tileIndex += tileLineAdvance;
2219 } else { /* if (tccp->qmfbid == 0) */
2220 for (j = 0; j < cblk_h; ++j) {
2221 for (i = 0; i < cblk_w; ++i) {
2222 OPJ_INT32 tmp = tiledp[tileIndex];
2229 tileIndex += tileLineAdvance;
2238 tilec->numresolutions - 1 - resno,
2255 /** mod fixed_quality */
2256 static void opj_t1_encode_cblk(opj_t1_t *t1,
2257 opj_tcd_cblk_enc_t* cblk,
2262 OPJ_FLOAT64 stepsize,
2264 OPJ_UINT32 numcomps,
2265 opj_tcd_tile_t * tile,
2266 const OPJ_FLOAT64 * mct_norms,
2267 OPJ_UINT32 mct_numcomps)
2269 OPJ_FLOAT64 cumwmsedec = 0.0;
2271 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2275 OPJ_UINT32 passtype;
2276 OPJ_INT32 nmsedec = 0;
2279 OPJ_BYTE type = T1_TYPE_MQ;
2280 OPJ_FLOAT64 tempwmsedec;
2282 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2285 for (i = 0; i < t1->w; ++i) {
2286 for (j = 0; j < t1->h; ++j) {
2287 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2288 max = opj_int_max(max, tmp);
2292 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2293 T1_NMSEDEC_FRACBITS) : 0;
2295 bpno = (OPJ_INT32)(cblk->numbps - 1);
2298 opj_mqc_resetstates(mqc);
2299 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2300 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2301 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2302 opj_mqc_init_enc(mqc, cblk->data);
2304 for (passno = 0; bpno >= 0; ++passno) {
2305 opj_tcd_pass_t *pass = &cblk->passes[passno];
2306 OPJ_UINT32 correction = 3;
2307 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2308 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2312 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2315 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2318 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2319 /* code switch SEGMARK (i.e. SEGSYM) */
2320 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2321 opj_mqc_segmark_enc(mqc);
2327 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2328 stepsize, numcomps, mct_norms, mct_numcomps) ;
2329 cumwmsedec += tempwmsedec;
2330 tile->distotile += tempwmsedec;
2332 /* Code switch "RESTART" (i.e. TERMALL) */
2333 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2335 if (type == T1_TYPE_RAW) {
2338 /* correction = mqc_bypass_flush_enc(); */
2339 } else { /* correction = mqc_restart_enc(); */
2345 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2346 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2347 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2348 if (type == T1_TYPE_RAW) {
2351 /* correction = mqc_bypass_flush_enc(); */
2352 } else { /* correction = mqc_restart_enc(); */
2362 if (++passtype == 3) {
2367 if (pass->term && bpno > 0) {
2368 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2369 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2370 if (type == T1_TYPE_RAW) {
2371 opj_mqc_bypass_init_enc(mqc);
2373 opj_mqc_restart_init_enc(mqc);
2377 pass->distortiondec = cumwmsedec;
2378 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2380 /* Code-switch "RESET" */
2381 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2382 opj_mqc_reset_enc(mqc);
2386 /* Code switch "ERTERM" (i.e. PTERM) */
2387 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2388 opj_mqc_erterm_enc(mqc);
2389 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2393 cblk->totalpasses = passno;
2395 for (passno = 0; passno < cblk->totalpasses; passno++) {
2396 opj_tcd_pass_t *pass = &cblk->passes[passno];
2397 if (pass->rate > opj_mqc_numbytes(mqc)) {
2398 pass->rate = opj_mqc_numbytes(mqc);
2400 /*Preventing generation of FF as last data byte of a pass*/
2401 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2404 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);