2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
15 * Copyright (c) 2012, Carl Hetherington
16 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
44 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
47 #define T1_FLAGS(x, y) (t1->flags[x + 1 + ((y / 4) + 1) * (t1->w+2)])
49 #define opj_t1_setcurctx(curctx, ctxno) curctx = &(mqc)->ctxs[(OPJ_UINT32)(ctxno)]
51 /** @name Local static functions */
54 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
55 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
56 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
57 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
58 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
59 OPJ_UINT32 s, OPJ_UINT32 stride);
63 Decode significant pass
66 static INLINE void opj_t1_dec_sigpass_step_raw(
70 OPJ_INT32 oneplushalf,
73 static INLINE void opj_t1_dec_sigpass_step_mqc(
77 OPJ_INT32 oneplushalf,
79 OPJ_UINT32 flags_stride);
80 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
84 OPJ_INT32 oneplushalf,
90 Encode significant pass
92 static void opj_t1_enc_sigpass(opj_t1_t *t1,
99 Decode significant pass
101 static void opj_t1_dec_sigpass_raw(
105 static void opj_t1_dec_sigpass_mqc_vsc(
112 Encode refinement pass
114 static void opj_t1_enc_refpass(opj_t1_t *t1,
121 Decode refinement pass
123 static void opj_t1_dec_refpass_raw(
126 static void opj_t1_dec_refpass_mqc_vsc(
132 Decode refinement pass
135 static INLINE void opj_t1_dec_refpass_step_raw(
141 static INLINE void opj_t1_dec_refpass_step_mqc(
147 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
160 static void opj_t1_dec_clnpass_step(
164 OPJ_INT32 oneplushalf,
166 static void opj_t1_dec_clnpass_step_vsc(
170 OPJ_INT32 oneplushalf,
177 static void opj_t1_enc_clnpass(
183 static OPJ_FLOAT64 opj_t1_getwmsedec(
190 OPJ_FLOAT64 stepsize,
192 const OPJ_FLOAT64 * mct_norms,
193 OPJ_UINT32 mct_numcomps);
195 static void opj_t1_encode_cblk(opj_t1_t *t1,
196 opj_tcd_cblk_enc_t* cblk,
201 OPJ_FLOAT64 stepsize,
204 opj_tcd_tile_t * tile,
205 const OPJ_FLOAT64 * mct_norms,
206 OPJ_UINT32 mct_numcomps);
211 @param cblk Code-block coding parameters
213 @param roishift Region of interest shifting value
214 @param cblksty Code-block style
216 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
217 opj_tcd_cblk_dec_t* cblk,
222 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
230 /* ----------------------------------------------------------------------- */
232 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
234 return mqc->lut_ctxno_zc_orient[(f & T1_SIGMA_NEIGHBOURS)];
237 static INLINE OPJ_UINT32 opj_t1_getctxtno_sc_or_spb_index(OPJ_UINT32 fX,
243 0 pfX T1_CHI_THIS T1_LUT_SGN_W
244 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
245 2 nfX T1_CHI_THIS T1_LUT_SGN_E
246 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
247 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
248 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
249 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
250 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
253 OPJ_UINT32 lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
256 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
257 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
259 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
261 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
263 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
267 static INLINE OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 lu)
269 return lut_ctxno_sc[lu];
272 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
274 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
275 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
279 static INLINE OPJ_BYTE opj_t1_getspb(OPJ_UINT32 lu)
284 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
287 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
290 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
293 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
296 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
299 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
302 #define opj_t1_update_flags_macro(flags, flagsp, ci, s, stride ) \
305 flagsp[-1] |= T1_SIGMA_5 << (3U * ci); \
307 /* mark target as significant */ \
308 flags |= ((s << T1_CHI_1_I) | T1_SIGMA_4) << (3U * ci); \
311 flagsp[1] |= T1_SIGMA_3 << (3U * ci); \
313 /* north-west, north, north-east */ \
315 opj_flag_t* north = flagsp - (stride); \
316 *north |= (s << T1_CHI_5_I) | T1_SIGMA_16; \
317 north[-1] |= T1_SIGMA_17; \
318 north[1] |= T1_SIGMA_15; \
321 /* south-west, south, south-east */ \
323 opj_flag_t* south = flagsp + (stride); \
324 *south |= (s << T1_CHI_0_I) | T1_SIGMA_1; \
325 south[-1] |= T1_SIGMA_2; \
326 south[1] |= T1_SIGMA_0; \
331 static INLINE void opj_t1_update_flags(opj_flag_t *flagsp, OPJ_UINT32 ci,
332 OPJ_UINT32 s, OPJ_UINT32 stride)
334 opj_t1_update_flags_macro(*flagsp, flagsp, ci, s, stride);
338 Encode significant pass
340 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
353 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
355 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
356 T1_CHI_S) << (ci * 3U)) : ~0U;
357 OPJ_UINT32 const flags = *flagsp & vsc_mask;
359 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
360 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
361 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
362 v = opj_int_abs(*datap) & one ? 1 : 0;
364 fprintf(stderr, " ctxt1=%d\n", ctxt1);
366 opj_mqc_setcurctx(mqc, ctxt1);
367 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
368 opj_mqc_bypass_enc(mqc, v);
370 opj_mqc_encode(mqc, v);
373 /* Note: using flags instead of *flagsp & vsc_mask result */
374 /* in slow down. Probably because of register pressure */
375 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
377 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
379 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
380 v = *datap < 0 ? 1 : 0;
381 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
384 fprintf(stderr, " ctxt2=%d\n", ctxt2);
386 opj_mqc_setcurctx(mqc, ctxt2);
387 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
388 opj_mqc_bypass_enc(mqc, v);
390 OPJ_UINT32 spb = opj_t1_getspb(lu);
392 fprintf(stderr, " spb=%d\n", spb);
394 opj_mqc_encode(mqc, v ^ spb);
396 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
398 *flagsp |= T1_PI_THIS << (ci * 3U);
402 static INLINE void opj_t1_dec_sigpass_step_raw(
406 OPJ_INT32 oneplushalf,
411 opj_raw_t *raw = &(t1->raw); /* RAW component */
413 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
414 T1_CHI_S) << (ci * 3U)) : ~0U;
415 OPJ_UINT32 const flags = *flagsp & vsc_mask;
417 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
418 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
419 if (opj_raw_decode(raw)) {
420 v = opj_raw_decode(raw);
421 *datap = v ? -oneplushalf : oneplushalf;
422 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
424 *flagsp |= T1_PI_THIS << (ci * 3U);
428 #define opj_t1_dec_sigpass_step_mqc_macro(flags, flagsp, flags_stride, data, \
429 data_stride, ci, mqc, curctx, \
430 v, a, c, ct, oneplushalf) \
432 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U && \
433 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) { \
434 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
435 opj_t1_setcurctx(curctx, ctxt1); \
436 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
438 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
440 flagsp[-1], flagsp[1], \
442 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu); \
443 OPJ_UINT32 spb = opj_t1_getspb(lu); \
444 opj_t1_setcurctx(curctx, ctxt2); \
445 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
447 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
448 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride); \
450 flags |= 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_t1_dec_sigpass_step_mqc_macro(*flagsp, flagsp, flags_stride, datap,
466 0, ci, mqc, mqc->curctx,
467 v, mqc->a, mqc->c, mqc->ct, oneplushalf);
470 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
474 OPJ_INT32 oneplushalf,
480 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
482 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
483 T1_CHI_S) << (ci * 3U)) : ~0U;
484 OPJ_UINT32 const flags = *flagsp & vsc_mask;
486 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
487 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
488 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index(
490 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
492 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
493 opj_mqc_setcurctx(mqc, ctxt1);
494 opj_mqc_decode(v, mqc);
496 OPJ_UINT32 ctxt2 = opj_t1_getctxno_sc(lu);
497 OPJ_UINT32 spb = opj_t1_getspb(lu);
498 opj_mqc_setcurctx(mqc, ctxt2);
499 opj_mqc_decode(v, mqc);
501 *datap = v ? -oneplushalf : oneplushalf;
502 opj_t1_update_flags(flagsp, ci, v, t1->w + 2);
504 *flagsp |= T1_PI_THIS << (ci * 3U);
509 static void opj_t1_enc_sigpass(opj_t1_t *t1,
517 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
518 opj_flag_t* f = &T1_FLAGS(0, 0);
519 OPJ_UINT32 const extra = 2;
523 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
525 for (k = 0; k < (t1->h & ~3U); k += 4) {
527 fprintf(stderr, " k=%d\n", k);
529 for (i = 0; i < t1->w; ++i) {
531 fprintf(stderr, " i=%d\n", i);
534 /* Nothing to do for any of the 4 data points */
538 opj_t1_enc_sigpass_step(
541 &t1->data[((k + 0) * t1->data_stride) + i],
547 opj_t1_enc_sigpass_step(
550 &t1->data[((k + 1) * t1->data_stride) + i],
556 opj_t1_enc_sigpass_step(
559 &t1->data[((k + 2) * t1->data_stride) + i],
565 opj_t1_enc_sigpass_step(
568 &t1->data[((k + 3) * t1->data_stride) + i],
573 3, cblksty & J2K_CCP_CBLKSTY_VSC);
582 fprintf(stderr, " k=%d\n", k);
584 for (i = 0; i < t1->w; ++i) {
586 fprintf(stderr, " i=%d\n", i);
589 /* Nothing to do for any of the 4 data points */
593 for (j = k; j < t1->h; ++j) {
594 opj_t1_enc_sigpass_step(
597 &t1->data[(j * t1->data_stride) + i],
603 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
610 static void opj_t1_dec_sigpass_raw(
615 OPJ_INT32 one, half, oneplushalf;
617 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
620 oneplushalf = one | half;
621 if ((cblksty & J2K_CCP_CBLKSTY_VSC)) {
622 OPJ_INT32 *data1 = t1->data;
623 for (k = 0; k < t1->h; k += 4) {
624 for (i = 0; i < t1->w; ++i) {
625 OPJ_INT32* data2 = data1 + i;
626 for (j = k; j < k + 4 && j < t1->h; ++j) {
627 OPJ_INT32 vsc = (j == k + 3 || j == t1->h - 1) ? 1 : 0;
628 opj_t1_dec_sigpass_step_raw(
643 OPJ_INT32 *data1 = t1->data;
644 for (k = 0; k < (t1->h & ~3U); k += 4) {
645 for (i = 0; i < t1->w; ++i) {
646 OPJ_INT32* data2 = data1 + i;
647 opj_t1_dec_sigpass_step_raw(
655 opj_t1_dec_sigpass_step_raw(
663 opj_t1_dec_sigpass_step_raw(
671 opj_t1_dec_sigpass_step_raw(
685 for (i = 0; i < t1->w; ++i) {
686 OPJ_INT32* data2 = data1 + i;
687 for (j = 0; j < t1->h - k; ++j) {
688 opj_t1_dec_sigpass_step_raw(
703 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
705 OPJ_INT32 one, half, oneplushalf; \
706 OPJ_UINT32 i, j, k; \
707 register OPJ_INT32 *data = t1->data; \
708 register opj_flag_t *flagsp = &t1->flags[(flags_stride) + 1]; \
709 const OPJ_UINT32 l_w = w; \
710 opj_mqc_t* mqc = &(t1->mqc); \
711 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
712 register OPJ_UINT32 v; \
715 oneplushalf = one | half; \
716 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
717 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
718 opj_flag_t flags = *flagsp; \
720 opj_t1_dec_sigpass_step_mqc_macro( \
721 flags, flagsp, flags_stride, data, \
722 l_w, 0, mqc, curctx, v, a, c, ct, oneplushalf); \
723 opj_t1_dec_sigpass_step_mqc_macro( \
724 flags, flagsp, flags_stride, data, \
725 l_w, 1, mqc, curctx, v, a, c, ct, oneplushalf); \
726 opj_t1_dec_sigpass_step_mqc_macro( \
727 flags, flagsp, flags_stride, data, \
728 l_w, 2, mqc, curctx, v, a, c, ct, oneplushalf); \
729 opj_t1_dec_sigpass_step_mqc_macro( \
730 flags, flagsp, flags_stride, data, \
731 l_w, 3, mqc, curctx, v, a, c, ct, oneplushalf); \
736 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
738 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
739 for (j = 0; j < h - k; ++j) { \
740 opj_t1_dec_sigpass_step_mqc(t1, flagsp, \
741 data + j * l_w, oneplushalf, j, flags_stride); \
747 static void opj_t1_dec_sigpass_mqc_64x64(
751 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
754 static void opj_t1_dec_sigpass_mqc_generic(
758 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2);
761 static void opj_t1_dec_sigpass_mqc_vsc(
765 OPJ_INT32 one, half, oneplushalf, vsc;
767 OPJ_INT32 *data1 = t1->data;
768 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
771 oneplushalf = one | half;
772 for (k = 0; k < (t1->h & ~3U); k += 4U) {
773 for (i = 0; i < t1->w; ++i) {
774 OPJ_INT32 *data2 = data1 + i;
775 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 0U);
777 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 1U);
779 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 0, 2U);
781 opj_t1_dec_sigpass_step_mqc_vsc(t1, flagsp, data2, oneplushalf, 1, 3U);
788 for (i = 0; i < t1->w; ++i) {
789 OPJ_INT32 *data2 = data1 + i;
790 for (j = k; j < t1->h; ++j) {
791 vsc = (j == t1->h - 1) ? 1 : 0;
792 opj_t1_dec_sigpass_step_mqc_vsc(
807 Encode refinement pass step
809 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
821 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
823 OPJ_UINT32 const shift_flags =
825 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
827 (*flagsp >> (ci * 3U));
829 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
830 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
831 *nmsedec += opj_t1_getnmsedec_ref((OPJ_UINT32)opj_int_abs(*datap),
833 v = opj_int_abs(*datap) & one ? 1 : 0;
835 fprintf(stderr, " ctxt=%d\n", ctxt);
837 opj_mqc_setcurctx(mqc, ctxt);
838 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
839 opj_mqc_bypass_enc(mqc, v);
841 opj_mqc_encode(mqc, v);
843 *flagsp |= T1_MU_THIS << (ci * 3U);
848 static INLINE void opj_t1_dec_refpass_step_raw(
857 opj_raw_t *raw = &(t1->raw); /* RAW component */
859 if ((*flagsp & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) ==
860 (T1_SIGMA_THIS << (ci * 3U))) {
861 v = opj_raw_decode(raw);
862 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
863 *flagsp |= T1_MU_THIS << (ci * 3U);
867 #define opj_t1_dec_refpass_step_mqc_macro(flags, data, data_stride, ci, \
868 mqc, curctx, v, a, c, ct, poshalf) \
870 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == \
871 (T1_SIGMA_THIS << (ci * 3U))) { \
872 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(flags >> (ci * 3U)); \
873 opj_t1_setcurctx(curctx, ctxt); \
874 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
875 data[ci*data_stride] += (v ^ (data[ci*data_stride] < 0)) ? poshalf : -poshalf; \
876 flags |= T1_MU_THIS << (ci * 3U); \
880 static INLINE void opj_t1_dec_refpass_step_mqc(
889 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
890 opj_t1_dec_refpass_step_mqc_macro(*flagsp, datap, 0, ci,
891 mqc, mqc->curctx, v, mqc->a, mqc->c,
895 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
905 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
907 OPJ_UINT32 const shift_flags =
909 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
911 (*flagsp >> (ci * 3U));
914 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
915 OPJ_UINT32 ctxt = opj_t1_getctxno_mag(shift_flags);
916 opj_mqc_setcurctx(mqc, ctxt);
917 opj_mqc_decode(v, mqc);
918 *datap += (v ^ (*datap < 0)) ? poshalf : -poshalf;
919 *flagsp |= T1_MU_THIS << (ci * 3U);
923 static void opj_t1_enc_refpass(
931 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
932 opj_flag_t* f = &T1_FLAGS(0, 0);
933 const OPJ_UINT32 extra = 2U;
937 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
939 for (k = 0; k < (t1->h & ~3U); k += 4) {
941 fprintf(stderr, " k=%d\n", k);
943 for (i = 0; i < t1->w; ++i) {
945 fprintf(stderr, " i=%d\n", i);
947 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
948 /* none significant */
952 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
953 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
954 /* all processed by sigpass */
959 opj_t1_enc_refpass_step(
962 &t1->data[((k + 0) * t1->data_stride) + i],
968 opj_t1_enc_refpass_step(
971 &t1->data[((k + 1) * t1->data_stride) + i],
977 opj_t1_enc_refpass_step(
980 &t1->data[((k + 2) * t1->data_stride) + i],
986 opj_t1_enc_refpass_step(
989 &t1->data[((k + 3) * t1->data_stride) + i],
994 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1002 #ifdef DEBUG_ENC_REF
1003 fprintf(stderr, " k=%d\n", k);
1005 for (i = 0; i < t1->w; ++i) {
1006 #ifdef DEBUG_ENC_REF
1007 fprintf(stderr, " i=%d\n", i);
1009 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1010 /* none significant */
1014 for (j = k; j < t1->h; ++j) {
1015 opj_t1_enc_refpass_step(
1018 &t1->data[(j * t1->data_stride) + i],
1024 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1032 static void opj_t1_dec_refpass_raw(
1036 OPJ_INT32 one, poshalf;
1038 OPJ_INT32 *data1 = t1->data;
1039 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1042 for (k = 0; k < (t1->h & ~3U); k += 4) {
1043 for (i = 0; i < t1->w; ++i) {
1044 OPJ_INT32 *data2 = data1 + i;
1045 opj_t1_dec_refpass_step_raw(
1052 opj_t1_dec_refpass_step_raw(
1059 opj_t1_dec_refpass_step_raw(
1066 opj_t1_dec_refpass_step_raw(
1075 data1 += t1->w << 2;
1079 for (i = 0; i < t1->w; ++i) {
1080 OPJ_INT32 *data2 = data1 + i;
1081 for (j = k; j < t1->h; ++j) {
1082 opj_t1_dec_refpass_step_raw(
1095 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1097 OPJ_INT32 one, poshalf; \
1098 OPJ_UINT32 i, j, k; \
1099 register OPJ_INT32 *data = t1->data; \
1100 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1101 const OPJ_UINT32 l_w = w; \
1102 opj_mqc_t* mqc = &(t1->mqc); \
1103 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1104 register OPJ_UINT32 v; \
1106 poshalf = one >> 1; \
1107 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1108 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1109 opj_flag_t flags = *flagsp; \
1110 if( flags != 0 ) { \
1111 opj_t1_dec_refpass_step_mqc_macro( \
1112 flags, data, l_w, 0, \
1113 mqc, curctx, v, a, c, ct, poshalf); \
1114 opj_t1_dec_refpass_step_mqc_macro( \
1115 flags, data, l_w, 1, \
1116 mqc, curctx, v, a, c, ct, poshalf); \
1117 opj_t1_dec_refpass_step_mqc_macro( \
1118 flags, data, l_w, 2, \
1119 mqc, curctx, v, a, c, ct, poshalf); \
1120 opj_t1_dec_refpass_step_mqc_macro( \
1121 flags, data, l_w, 3, \
1122 mqc, curctx, v, a, c, ct, poshalf); \
1127 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1129 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1130 for (j = 0; j < h - k; ++j) { \
1131 opj_t1_dec_refpass_step_mqc(t1, flagsp, data + j * l_w, poshalf, j); \
1137 static void opj_t1_dec_refpass_mqc_64x64(
1141 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1144 static void opj_t1_dec_refpass_mqc_generic(
1148 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->w + 2U);
1151 static void opj_t1_dec_refpass_mqc_vsc(
1155 OPJ_INT32 one, poshalf;
1158 OPJ_INT32 *data1 = t1->data;
1159 opj_flag_t *flagsp = &T1_FLAGS(0, 0);
1162 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1163 for (i = 0; i < t1->w; ++i) {
1164 OPJ_INT32 *data2 = data1 + i;
1165 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1168 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1171 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1174 opj_t1_dec_refpass_step_mqc_vsc(t1, flagsp, data2, poshalf,
1179 data1 += t1->w << 2;
1182 for (i = 0; i < t1->w; ++i) {
1183 OPJ_INT32 *data2 = data1 + i;
1184 for (j = k; j < t1->h; ++j) {
1185 vsc = (j == t1->h - 1) ? 1 : 0;
1186 opj_t1_dec_refpass_step_mqc_vsc(
1200 Encode clean-up pass step
1202 static void opj_t1_enc_clnpass_step(
1216 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1218 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1219 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1221 if ((*flagsp & check) == check) {
1223 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1224 } else if (runlen == 1) {
1225 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1226 } else if (runlen == 2) {
1227 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1228 } else if (runlen == 3) {
1229 *flagsp &= ~(T1_PI_3);
1234 for (ci = runlen; ci < lim; ++ci) {
1236 OPJ_UINT32 vsc_mask;
1239 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1240 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1242 flags = *flagsp & vsc_mask;
1244 if ((agg != 0) && (ci == runlen)) {
1248 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1249 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1250 #ifdef DEBUG_ENC_CLN
1251 printf(" ctxt1=%d\n", ctxt1);
1253 opj_mqc_setcurctx(mqc, ctxt1);
1254 v = opj_int_abs(*datap) & one ? 1 : 0;
1255 opj_mqc_encode(mqc, v);
1257 OPJ_UINT32 ctxt2, spb;
1260 /* Note: using flags instead of *flagsp & vsc_mask result */
1261 /* in slow down. Probably because of register pressure */
1262 lu = opj_t1_getctxtno_sc_or_spb_index(
1264 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1266 *nmsedec += opj_t1_getnmsedec_sig((OPJ_UINT32)opj_int_abs(*datap),
1268 ctxt2 = opj_t1_getctxno_sc(lu);
1269 #ifdef DEBUG_ENC_CLN
1270 printf(" ctxt2=%d\n", ctxt2);
1272 opj_mqc_setcurctx(mqc, ctxt2);
1274 v = *datap < 0 ? 1 : 0;
1275 spb = opj_t1_getspb(lu);
1276 #ifdef DEBUG_ENC_CLN
1277 printf(" spb=%d\n", spb);
1279 opj_mqc_encode(mqc, v ^ spb);
1280 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1283 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1284 datap += t1->data_stride;
1288 #define opj_t1_dec_clnpass_step_macro(check_flags, partial, \
1289 flags, flagsp, flags_stride, data, \
1290 data_stride, ci, mqc, curctx, \
1291 v, a, c, ct, oneplushalf) \
1293 if ( !check_flags || !(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {\
1296 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U)); \
1297 opj_t1_setcurctx(curctx, ctxt1); \
1298 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1303 OPJ_UINT32 lu = opj_t1_getctxtno_sc_or_spb_index( \
1304 flags, flagsp[-1], flagsp[1], \
1306 opj_t1_setcurctx(curctx, opj_t1_getctxno_sc(lu)); \
1307 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1308 v = v ^ opj_t1_getspb(lu); \
1309 data[ci*data_stride] = v ? -oneplushalf : oneplushalf; \
1310 opj_t1_update_flags_macro(flags, flagsp, ci, v, flags_stride); \
1316 static void opj_t1_dec_clnpass_step(
1320 OPJ_INT32 oneplushalf,
1325 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1326 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE,
1327 *flagsp, flagsp, t1->w + 2U, datap,
1328 0, ci, mqc, mqc->curctx,
1329 v, mqc->a, mqc->c, mqc->ct, oneplushalf);
1332 static void opj_t1_dec_clnpass_step_vsc(
1336 OPJ_INT32 oneplushalf,
1343 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1345 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
1348 OPJ_UINT32 flags = *flagsp & vsc_mask;
1352 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1353 OPJ_UINT32 ctxt1 = opj_t1_getctxno_zc(mqc, flags >> (ci * 3U));
1354 opj_mqc_setcurctx(mqc, ctxt1);
1355 opj_mqc_decode(v, mqc);
1359 lu = opj_t1_getctxtno_sc_or_spb_index(
1360 *flagsp & vsc_mask, flagsp[-1] & vsc_mask,
1361 flagsp[1] & vsc_mask,
1363 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(lu));
1364 opj_mqc_decode(v, mqc);
1365 v = v ^ opj_t1_getspb(lu);
1366 *datap = v ? -oneplushalf : oneplushalf;
1367 opj_t1_update_flags(flagsp, ci, v, t1->w + 2U);
1370 /* *flagsp &= ~(T1_PI_THIS << (3U * ci)); */
1373 static void opj_t1_enc_clnpass(
1380 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1381 OPJ_UINT32 agg, runlen;
1383 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
1385 const OPJ_UINT32 agg_mask = (cblksty & J2K_CCP_CBLKSTY_VSC) ?
1386 ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5) :
1390 #ifdef DEBUG_ENC_CLN
1391 printf("enc_clnpass: bpno=%d\n", bpno);
1393 for (k = 0; k < (t1->h & ~3U); k += 4) {
1394 #ifdef DEBUG_ENC_CLN
1395 printf(" k=%d\n", k);
1397 for (i = 0; i < t1->w; ++i) {
1398 #ifdef DEBUG_ENC_CLN
1399 printf(" i=%d\n", i);
1401 agg = !(T1_FLAGS(i, k) & agg_mask);
1402 #ifdef DEBUG_ENC_CLN
1403 printf(" agg=%d\n", agg);
1406 for (runlen = 0; runlen < 4; ++runlen) {
1407 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1411 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1412 opj_mqc_encode(mqc, runlen != 4);
1416 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1417 opj_mqc_encode(mqc, runlen >> 1);
1418 opj_mqc_encode(mqc, runlen & 1);
1422 opj_t1_enc_clnpass_step(
1425 &t1->data[((k + runlen) * t1->data_stride) + i],
1438 #ifdef DEBUG_ENC_CLN
1439 printf(" k=%d\n", k);
1441 for (i = 0; i < t1->w; ++i) {
1442 #ifdef DEBUG_ENC_CLN
1443 printf(" i=%d\n", i);
1444 printf(" agg=%d\n", agg);
1446 opj_t1_enc_clnpass_step(
1449 &t1->data[((k + runlen) * t1->data_stride) + i],
1461 #define opj_t1_dec_clnpass_internal(t1, bpno, cblksty, w, h, flags_stride) \
1463 OPJ_INT32 one, half, oneplushalf, agg, vsc; \
1464 OPJ_UINT32 runlen, v; \
1465 OPJ_UINT32 i, j, k; \
1466 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1467 const OPJ_UINT32 l_w = w; \
1468 opj_mqc_t* mqc = &(t1->mqc); \
1471 oneplushalf = one | half; \
1472 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1473 opj_flag_t *flagsp1 = &t1->flags[flags_stride + 1]; \
1474 const OPJ_UINT32 agg_mask = ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 | T1_CHI_5); \
1475 for (k = 0; k < h; k += 4) { \
1476 for (i = 0; i < w; ++i) { \
1477 opj_flag_t *flagsp2 = flagsp1 + i; \
1479 agg = !(*flagsp2 & agg_mask); \
1484 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1485 opj_mqc_decode(v, mqc); \
1489 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1490 opj_mqc_decode(runlen, mqc); \
1491 opj_mqc_decode(v, mqc); \
1492 runlen = (runlen << 1) | v; \
1496 for (j = k + runlen; j < k + 4 && j < h; ++j) { \
1497 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1498 opj_t1_dec_clnpass_step_vsc( \
1501 &t1->data[(j * w) + i], \
1503 agg && (j == k + runlen), \
1506 *flagsp2 &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1508 flagsp1 += flags_stride; \
1511 DOWNLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1512 register OPJ_UINT32 v; \
1513 register OPJ_INT32 *data = t1->data; \
1514 register opj_flag_t *flagsp = &t1->flags[flags_stride + 1]; \
1515 for (k = 0; k < (h & ~3u); k += 4, data += 3*l_w, flagsp += 2) { \
1516 for (i = 0; i < l_w; ++i, ++data, ++flagsp) { \
1517 opj_flag_t flags = *flagsp; \
1519 OPJ_UINT32 partial = OPJ_TRUE; \
1520 opj_t1_setcurctx(curctx, T1_CTXNO_AGG); \
1521 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1525 opj_t1_setcurctx(curctx, T1_CTXNO_UNI); \
1526 opj_mqc_decode_macro(runlen, mqc, curctx, a, c, ct); \
1527 opj_mqc_decode_macro(v, mqc, curctx, a, c, ct); \
1528 runlen = (runlen << 1) | v; \
1531 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, OPJ_TRUE,\
1532 flags, flagsp, flags_stride, data, \
1533 l_w, 0, mqc, curctx, \
1534 v, a, c, ct, oneplushalf); \
1535 partial = OPJ_FALSE; \
1538 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1539 flags, flagsp, flags_stride, data, \
1540 l_w, 1, mqc, curctx, \
1541 v, a, c, ct, oneplushalf); \
1542 partial = OPJ_FALSE; \
1545 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1546 flags, flagsp, flags_stride, data, \
1547 l_w, 2, mqc, curctx, \
1548 v, a, c, ct, oneplushalf); \
1549 partial = OPJ_FALSE; \
1552 opj_t1_dec_clnpass_step_macro(OPJ_FALSE, partial,\
1553 flags, flagsp, flags_stride, data, \
1554 l_w, 3, mqc, curctx, \
1555 v, a, c, ct, oneplushalf); \
1559 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1560 flags, flagsp, flags_stride, data, \
1561 l_w, 0, mqc, curctx, \
1562 v, a, c, ct, oneplushalf); \
1563 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1564 flags, flagsp, flags_stride, data, \
1565 l_w, 1, mqc, curctx, \
1566 v, a, c, ct, oneplushalf); \
1567 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1568 flags, flagsp, flags_stride, data, \
1569 l_w, 2, mqc, curctx, \
1570 v, a, c, ct, oneplushalf); \
1571 opj_t1_dec_clnpass_step_macro(OPJ_TRUE, OPJ_FALSE, \
1572 flags, flagsp, flags_stride, data, \
1573 l_w, 3, mqc, curctx, \
1574 v, a, c, ct, oneplushalf); \
1576 *flagsp = flags & ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1579 UPLOAD_MQC_VARIABLES(mqc, curctx, c, a, ct); \
1581 for (i = 0; i < l_w; ++i, ++flagsp, ++data) { \
1582 for (j = 0; j < h - k; ++j) { \
1583 opj_t1_dec_clnpass_step(t1, flagsp, data + j * l_w, oneplushalf, j); \
1585 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3); \
1592 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1593 opj_mqc_decode(v, mqc); \
1594 opj_mqc_decode(v2, mqc); \
1595 v = (v << 1) | v2; \
1596 opj_mqc_decode(v2, mqc); \
1597 v = (v << 1) | v2; \
1598 opj_mqc_decode(v2, mqc); \
1599 v = (v << 1) | v2; \
1602 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1608 static void opj_t1_dec_clnpass_64x64(
1613 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, 64, 64, 66);
1616 static void opj_t1_dec_clnpass_generic(
1621 opj_t1_dec_clnpass_internal(t1, bpno, cblksty, t1->w, t1->h,
1626 /** mod fixed_quality */
1627 static OPJ_FLOAT64 opj_t1_getwmsedec(
1634 OPJ_FLOAT64 stepsize,
1635 OPJ_UINT32 numcomps,
1636 const OPJ_FLOAT64 * mct_norms,
1637 OPJ_UINT32 mct_numcomps)
1639 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1640 OPJ_ARG_NOT_USED(numcomps);
1642 if (mct_norms && (compno < mct_numcomps)) {
1643 w1 = mct_norms[compno];
1647 w2 = opj_dwt_getnorm(level, orient);
1648 } else { /* if (qmfbid == 0) */
1649 w2 = opj_dwt_getnorm_real(level, orient);
1652 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1653 wmsedec *= wmsedec * nmsedec / 8192.0;
1658 static OPJ_BOOL opj_t1_allocate_buffers(
1664 OPJ_UINT32 flags_stride;
1666 /* encoder uses tile buffer, so no need to allocate */
1670 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1671 /* Overflow check */
1672 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1673 /* FIXME event manager error callback */
1677 datasize = (size_t)w * h;
1679 /* Overflow check */
1680 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1681 /* FIXME event manager error callback */
1685 if (datasize > (size_t)t1->datasize) {
1686 opj_aligned_free(t1->data);
1687 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1689 /* FIXME event manager error callback */
1692 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1693 /* TODO remove this if t1->datasize type changes to size_t */
1694 /* Overflow check */
1695 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1696 /* FIXME event manager error callback */
1700 t1->datasize = (OPJ_UINT32)datasize;
1702 /* memset first arg is declared to never be null by gcc */
1703 if (t1->data != NULL) {
1704 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1708 /* Overflow check */
1709 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1710 /* FIXME event manager error callback */
1713 flags_stride = w + 2U; /* can't be 0U */
1715 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1716 /* Overflow check */
1717 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1718 /* FIXME event manager error callback */
1722 flagssize = (h + 3U) / 4U + 2U;
1724 /* Overflow check */
1725 if (flagssize > (SIZE_MAX / (size_t)flags_stride)) {
1726 /* FIXME event manager error callback */
1729 flagssize *= (size_t)flags_stride;
1734 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1736 if (flagssize > (size_t)t1->flagssize) {
1737 /* Overflow check */
1738 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1739 /* FIXME event manager error callback */
1742 opj_aligned_free(t1->flags);
1743 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(
1746 /* FIXME event manager error callback */
1749 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1750 /* TODO remove this if t1->flagssize type changes to size_t */
1751 /* Overflow check */
1752 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1753 /* FIXME event manager error callback */
1758 t1->flagssize = (OPJ_UINT32)flagssize;
1760 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1763 for (x = 0; x < flags_stride; ++x) {
1764 /* magic value to hopefully stop any passes being interested in this entry */
1765 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1768 p = &t1->flags[((flags_height + 1) * flags_stride)];
1769 for (x = 0; x < flags_stride; ++x) {
1770 /* magic value to hopefully stop any passes being interested in this entry */
1771 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1776 p = &t1->flags[((flags_height) * flags_stride)];
1778 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1779 } else if (h % 4 == 2) {
1780 v |= T1_PI_2 | T1_PI_3;
1781 } else if (h % 4 == 3) {
1784 for (x = 0; x < flags_stride; ++x) {
1796 /* ----------------------------------------------------------------------- */
1798 /* ----------------------------------------------------------------------- */
1800 * Creates a new Tier 1 handle
1801 * and initializes the look-up tables of the Tier-1 coder/decoder
1802 * @return a new T1 handle if successful, returns NULL otherwise
1804 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
1806 opj_t1_t *l_t1 = 00;
1808 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
1813 l_t1->encoder = isEncoder;
1820 * Destroys a previously created T1 handle
1822 * @param p_t1 Tier 1 handle to destroy
1824 void opj_t1_destroy(opj_t1_t *p_t1)
1830 /* encoder uses tile buffer, so no need to free */
1831 if (!p_t1->encoder && p_t1->data) {
1832 opj_aligned_free(p_t1->data);
1837 opj_aligned_free(p_t1->flags);
1846 opj_tcd_cblk_dec_t* cblk;
1847 opj_tcd_band_t* band;
1848 opj_tcd_tilecomp_t* tilec;
1850 volatile OPJ_BOOL* pret;
1851 } opj_t1_cblk_decode_processing_job_t;
1853 static void opj_t1_destroy_wrapper(void* t1)
1855 opj_t1_destroy((opj_t1_t*) t1);
1858 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
1860 opj_tcd_cblk_dec_t* cblk;
1861 opj_tcd_band_t* band;
1862 opj_tcd_tilecomp_t* tilec;
1864 OPJ_INT32* OPJ_RESTRICT datap;
1865 OPJ_UINT32 cblk_w, cblk_h;
1868 opj_t1_cblk_decode_processing_job_t* job;
1873 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
1879 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
1881 if (!*(job->pret)) {
1886 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
1888 t1 = opj_t1_create(OPJ_FALSE);
1889 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
1892 if (OPJ_FALSE == opj_t1_decode_cblk(
1896 (OPJ_UINT32)tccp->roishift,
1898 *(job->pret) = OPJ_FALSE;
1903 x = cblk->x0 - band->x0;
1904 y = cblk->y0 - band->y0;
1905 if (band->bandno & 1) {
1906 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1907 x += pres->x1 - pres->x0;
1909 if (band->bandno & 2) {
1910 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
1911 y += pres->y1 - pres->y0;
1918 if (tccp->roishift) {
1919 OPJ_INT32 thresh = 1 << tccp->roishift;
1920 for (j = 0; j < cblk_h; ++j) {
1921 for (i = 0; i < cblk_w; ++i) {
1922 OPJ_INT32 val = datap[(j * cblk_w) + i];
1923 OPJ_INT32 mag = abs(val);
1924 if (mag >= thresh) {
1925 mag >>= tccp->roishift;
1926 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
1931 if (tccp->qmfbid == 1) {
1932 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
1934 for (j = 0; j < cblk_h; ++j) {
1936 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
1937 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
1938 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
1939 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
1940 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
1941 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
1942 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
1943 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
1944 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
1946 for (; i < cblk_w; ++i) {
1947 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
1948 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
1951 } else { /* if (tccp->qmfbid == 0) */
1952 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
1953 tile_w + (OPJ_UINT32)x];
1954 for (j = 0; j < cblk_h; ++j) {
1955 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
1956 for (i = 0; i < cblk_w; ++i) {
1957 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
1970 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
1971 volatile OPJ_BOOL* pret,
1972 opj_tcd_tilecomp_t* tilec,
1976 OPJ_UINT32 resno, bandno, precno, cblkno;
1978 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
1979 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
1981 for (bandno = 0; bandno < res->numbands; ++bandno) {
1982 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
1984 for (precno = 0; precno < res->pw * res->ph; ++precno) {
1985 opj_tcd_precinct_t* precinct = &band->precincts[precno];
1987 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
1988 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
1989 opj_t1_cblk_decode_processing_job_t* job;
1991 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
1992 sizeof(opj_t1_cblk_decode_processing_job_t));
2003 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2016 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2017 opj_tcd_cblk_dec_t* cblk,
2019 OPJ_UINT32 roishift,
2022 opj_raw_t *raw = &(t1->raw); /* RAW component */
2023 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2025 OPJ_INT32 bpno_plus_one;
2026 OPJ_UINT32 passtype;
2027 OPJ_UINT32 segno, passno;
2028 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2030 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2032 if (!opj_t1_allocate_buffers(
2034 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2035 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2039 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2042 opj_mqc_resetstates(mqc);
2043 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2044 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2045 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2047 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2048 opj_tcd_seg_t *seg = &cblk->segs[segno];
2051 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2052 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2053 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2054 if (seg->data == 00) {
2057 if (type == T1_TYPE_RAW) {
2058 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2060 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2066 if (t1->w == 64 && t1->h == 64) {
2067 for (passno = 0; (passno < seg->real_num_passes) &&
2068 (bpno_plus_one >= 1); ++passno) {
2071 if (type == T1_TYPE_RAW) {
2072 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2074 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2075 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2077 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2082 if (type == T1_TYPE_RAW) {
2083 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2085 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2086 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2088 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2093 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2097 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2098 opj_mqc_resetstates(mqc);
2099 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2100 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2101 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2103 if (++passtype == 3) {
2109 for (passno = 0; (passno < seg->real_num_passes) &&
2110 (bpno_plus_one >= 1); ++passno) {
2113 if (type == T1_TYPE_RAW) {
2114 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2116 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2117 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2119 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2124 if (type == T1_TYPE_RAW) {
2125 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2127 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2128 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2130 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2135 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2139 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2140 opj_mqc_resetstates(mqc);
2141 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2142 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2143 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2145 if (++passtype == 3) {
2158 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2159 opj_tcd_tile_t *tile,
2161 const OPJ_FLOAT64 * mct_norms,
2162 OPJ_UINT32 mct_numcomps
2165 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2167 tile->distotile = 0; /* fixed_quality */
2169 for (compno = 0; compno < tile->numcomps; ++compno) {
2170 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2171 opj_tccp_t* tccp = &tcp->tccps[compno];
2172 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2174 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2175 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2177 for (bandno = 0; bandno < res->numbands; ++bandno) {
2178 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2179 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2181 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2182 opj_tcd_precinct_t *prc = &band->precincts[precno];
2184 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2185 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2186 OPJ_INT32* OPJ_RESTRICT tiledp;
2189 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2191 OPJ_INT32 x = cblk->x0 - band->x0;
2192 OPJ_INT32 y = cblk->y0 - band->y0;
2193 if (band->bandno & 1) {
2194 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2195 x += pres->x1 - pres->x0;
2197 if (band->bandno & 2) {
2198 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2199 y += pres->y1 - pres->y0;
2202 if (!opj_t1_allocate_buffers(
2204 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2205 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2211 tileLineAdvance = tile_w - cblk_w;
2213 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2215 t1->data_stride = tile_w;
2216 if (tccp->qmfbid == 1) {
2217 for (j = 0; j < cblk_h; ++j) {
2218 for (i = 0; i < cblk_w; ++i) {
2219 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2222 tileIndex += tileLineAdvance;
2224 } else { /* if (tccp->qmfbid == 0) */
2225 for (j = 0; j < cblk_h; ++j) {
2226 for (i = 0; i < cblk_w; ++i) {
2227 OPJ_INT32 tmp = tiledp[tileIndex];
2234 tileIndex += tileLineAdvance;
2243 tilec->numresolutions - 1 - resno,
2260 /** mod fixed_quality */
2261 static void opj_t1_encode_cblk(opj_t1_t *t1,
2262 opj_tcd_cblk_enc_t* cblk,
2267 OPJ_FLOAT64 stepsize,
2269 OPJ_UINT32 numcomps,
2270 opj_tcd_tile_t * tile,
2271 const OPJ_FLOAT64 * mct_norms,
2272 OPJ_UINT32 mct_numcomps)
2274 OPJ_FLOAT64 cumwmsedec = 0.0;
2276 opj_mqc_t *mqc = &(t1->mqc); /* MQC component */
2280 OPJ_UINT32 passtype;
2281 OPJ_INT32 nmsedec = 0;
2284 OPJ_BYTE type = T1_TYPE_MQ;
2285 OPJ_FLOAT64 tempwmsedec;
2287 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + (orient << 9);
2290 for (i = 0; i < t1->w; ++i) {
2291 for (j = 0; j < t1->h; ++j) {
2292 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2293 max = opj_int_max(max, tmp);
2297 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2298 T1_NMSEDEC_FRACBITS) : 0;
2300 bpno = (OPJ_INT32)(cblk->numbps - 1);
2303 opj_mqc_resetstates(mqc);
2304 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2305 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2306 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2307 opj_mqc_init_enc(mqc, cblk->data);
2309 for (passno = 0; bpno >= 0; ++passno) {
2310 opj_tcd_pass_t *pass = &cblk->passes[passno];
2311 OPJ_UINT32 correction = 3;
2312 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2313 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2317 opj_t1_enc_sigpass(t1, bpno, &nmsedec, type, cblksty);
2320 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2323 opj_t1_enc_clnpass(t1, bpno, &nmsedec, cblksty);
2324 /* code switch SEGMARK (i.e. SEGSYM) */
2325 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2326 opj_mqc_segmark_enc(mqc);
2332 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2333 stepsize, numcomps, mct_norms, mct_numcomps) ;
2334 cumwmsedec += tempwmsedec;
2335 tile->distotile += tempwmsedec;
2337 /* Code switch "RESTART" (i.e. TERMALL) */
2338 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2340 if (type == T1_TYPE_RAW) {
2343 /* correction = mqc_bypass_flush_enc(); */
2344 } else { /* correction = mqc_restart_enc(); */
2350 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2351 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2352 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2353 if (type == T1_TYPE_RAW) {
2356 /* correction = mqc_bypass_flush_enc(); */
2357 } else { /* correction = mqc_restart_enc(); */
2367 if (++passtype == 3) {
2372 if (pass->term && bpno > 0) {
2373 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2374 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2375 if (type == T1_TYPE_RAW) {
2376 opj_mqc_bypass_init_enc(mqc);
2378 opj_mqc_restart_init_enc(mqc);
2382 pass->distortiondec = cumwmsedec;
2383 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2385 /* Code-switch "RESET" */
2386 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2387 opj_mqc_reset_enc(mqc);
2391 /* Code switch "ERTERM" (i.e. PTERM) */
2392 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2393 opj_mqc_erterm_enc(mqc);
2394 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2398 cblk->totalpasses = passno;
2400 for (passno = 0; passno < cblk->totalpasses; passno++) {
2401 opj_tcd_pass_t *pass = &cblk->passes[passno];
2402 if (pass->rate > opj_mqc_numbytes(mqc)) {
2403 pass->rate = opj_mqc_numbytes(mqc);
2405 /*Preventing generation of FF as last data byte of a pass*/
2406 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2409 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);