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 /* #define CONSISTENCY_CHECK */
46 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
49 #define ENC_FLAGS(x, y) (t1->enc_flags[x + 1 + ((y / 4) + 1) * t1->flags_stride])
51 /** @name Local static functions */
54 static INLINE OPJ_BYTE opj_t1_enc_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient);
55 static INLINE OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f);
56 static OPJ_BYTE opj_t1_enc_getctxno_sc(OPJ_UINT32 fX, OPJ_UINT32 pfX,
57 OPJ_UINT32 nfX, OPJ_UINT32 ci);
58 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f);
59 static INLINE OPJ_UINT32 opj_t1_enc_getctxno_mag(OPJ_UINT32 f);
60 static INLINE OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f);
61 static OPJ_BYTE opj_t1_enc_getspb(OPJ_UINT32 fX, OPJ_UINT32 pfX, OPJ_UINT32 nfX,
63 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f);
64 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos);
65 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos);
66 static INLINE void opj_t1_enc_updateflags(opj_flag_enc_t *flagsp, OPJ_UINT32 ci,
67 OPJ_UINT32 s, OPJ_UINT32 stride);
68 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
73 Decode significant pass
76 static INLINE void opj_t1_dec_sigpass_step_raw(
79 opj_colflag_t* colflagsp,
81 OPJ_INT32 oneplushalf,
84 static INLINE void opj_t1_dec_sigpass_step_mqc(
87 opj_colflag_t* colflagsp,
89 OPJ_INT32 oneplushalf,
91 OPJ_UINT32 flags_stride);
92 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
95 opj_colflag_t* colflagsp,
97 OPJ_INT32 oneplushalf,
103 Encode significant pass
105 static void opj_t1_enc_sigpass(opj_t1_t *t1,
113 Decode significant pass
115 static void opj_t1_dec_sigpass_raw(
119 static void opj_t1_dec_sigpass_mqc_vsc(
126 Encode refinement pass
128 static void opj_t1_enc_refpass(opj_t1_t *t1,
135 Decode refinement pass
137 static void opj_t1_dec_refpass_raw(
140 static void opj_t1_dec_refpass_mqc_vsc(
146 Decode refinement pass
149 static INLINE void opj_t1_dec_refpass_step_raw(
151 opj_colflag_t *colflagsp,
156 static INLINE void opj_t1_dec_refpass_step_mqc(
159 opj_colflag_t *colflagsp,
164 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
167 opj_colflag_t *colflagsp,
178 static void opj_t1_dec_clnpass_step_partial(
181 opj_colflag_t *colflagsp,
183 OPJ_INT32 oneplushalf,
185 static void opj_t1_dec_clnpass_step(
188 opj_colflag_t *colflagsp,
190 OPJ_INT32 oneplushalf,
192 static void opj_t1_dec_clnpass_step_vsc(
195 opj_colflag_t *colflagsp,
197 OPJ_INT32 oneplushalf,
204 static void opj_t1_enc_clnpass(
211 static OPJ_FLOAT64 opj_t1_getwmsedec(
218 OPJ_FLOAT64 stepsize,
220 const OPJ_FLOAT64 * mct_norms,
221 OPJ_UINT32 mct_numcomps);
223 static void opj_t1_encode_cblk(opj_t1_t *t1,
224 opj_tcd_cblk_enc_t* cblk,
229 OPJ_FLOAT64 stepsize,
232 opj_tcd_tile_t * tile,
233 const OPJ_FLOAT64 * mct_norms,
234 OPJ_UINT32 mct_numcomps);
239 @param cblk Code-block coding parameters
241 @param roishift Region of interest shifting value
242 @param cblksty Code-block style
244 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
245 opj_tcd_cblk_dec_t* cblk,
250 static OPJ_BOOL opj_t1_allocate_buffers(opj_t1_t *t1,
258 /* ----------------------------------------------------------------------- */
260 static INLINE OPJ_BYTE opj_t1_enc_getctxno_zc(OPJ_UINT32 f, OPJ_UINT32 orient)
262 return lut_enc_ctxno_zc[(orient << 9) | (f & T1_SIGMA_NEIGHBOURS)];
265 static OPJ_BYTE opj_t1_getctxno_zc(opj_mqc_t *mqc, OPJ_UINT32 f)
267 return mqc->lut_ctxno_zc_orient[(f & T1_SIG_OTH)];
271 static OPJ_BYTE opj_t1_enc_getctxno_sc(OPJ_UINT32 fX, OPJ_UINT32 pfX,
272 OPJ_UINT32 nfX, OPJ_UINT32 ci)
275 0 pfX T1_CHI_THIS T1_LUT_CTXNO_SGN_W
276 1 tfX T1_SIGMA_1 T1_LUT_CTXNO_SIG_N
277 2 nfX T1_CHI_THIS T1_LUT_CTXNO_SGN_E
278 3 tfX T1_SIGMA_3 T1_LUT_CTXNO_SIG_W
279 4 fX T1_CHI_(THIS - 1) T1_LUT_CTXNO_SGN_N
280 5 tfX T1_SIGMA_5 T1_LUT_CTXNO_SIG_E
281 6 fX T1_CHI_(THIS + 1) T1_LUT_CTXNO_SGN_S
282 7 tfX T1_SIGMA_7 T1_LUT_CTXNO_SIG_S
285 OPJ_UINT32 lu = (fX >> (ci * 3)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
288 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
289 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
291 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
293 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
295 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
297 return lut_enc_ctxno_sc[lu];
300 static OPJ_BYTE opj_t1_getctxno_sc(OPJ_UINT32 f)
302 return lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
305 static INLINE OPJ_UINT32 opj_t1_enc_getctxno_mag(OPJ_UINT32 f)
307 OPJ_UINT32 tmp = (f & T1_SIGMA_NEIGHBOURS) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
308 OPJ_UINT32 tmp2 = (f & T1_MU_0) ? T1_CTXNO_MAG + 2 : tmp;
312 static OPJ_UINT32 opj_t1_getctxno_mag(OPJ_UINT32 f)
314 OPJ_UINT32 tmp1 = (f & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
315 OPJ_UINT32 tmp2 = (f & T1_REFINE) ? T1_CTXNO_MAG + 2 : tmp1;
319 static OPJ_BYTE opj_t1_enc_getspb(OPJ_UINT32 fX, OPJ_UINT32 pfX, OPJ_UINT32 nfX,
323 0 pfX T1_CHI_THIS T1_LUT_SGN_W
324 1 tfX T1_SIGMA_1 T1_LUT_SIG_N
325 2 nfX T1_CHI_THIS T1_LUT_SGN_E
326 3 tfX T1_SIGMA_3 T1_LUT_SIG_W
327 4 fX T1_CHI_(THIS - 1) T1_LUT_SGN_N
328 5 tfX T1_SIGMA_5 T1_LUT_SIG_E
329 6 fX T1_CHI_(THIS + 1) T1_LUT_SGN_S
330 7 tfX T1_SIGMA_7 T1_LUT_SIG_S
333 int lu = (fX >> (ci * 3U)) & (T1_SIGMA_1 | T1_SIGMA_3 | T1_SIGMA_5 |
336 lu |= (pfX >> (T1_CHI_THIS_I + (ci * 3U))) & (1U << 0);
337 lu |= (nfX >> (T1_CHI_THIS_I - 2U + (ci * 3U))) & (1U << 2);
339 lu |= (fX >> (T1_CHI_0_I - 4U)) & (1U << 4);
341 lu |= (fX >> (T1_CHI_1_I - 4U + ((ci - 1U) * 3U))) & (1U << 4);
343 lu |= (fX >> (T1_CHI_2_I - 6U + (ci * 3U))) & (1U << 6);
345 return lut_enc_spb[lu];
348 static OPJ_BYTE opj_t1_getspb(OPJ_UINT32 f)
350 return lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
353 static OPJ_INT16 opj_t1_getnmsedec_sig(OPJ_UINT32 x, OPJ_UINT32 bitpos)
356 return lut_nmsedec_sig[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
359 return lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
362 static OPJ_INT16 opj_t1_getnmsedec_ref(OPJ_UINT32 x, OPJ_UINT32 bitpos)
365 return lut_nmsedec_ref[(x >> (bitpos)) & ((1 << T1_NMSEDEC_BITS) - 1)];
368 return lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
372 static INLINE void opj_t1_enc_updateflags(opj_flag_enc_t *flagsp, OPJ_UINT32 ci,
373 OPJ_UINT32 s, OPJ_UINT32 stride)
375 /* set up to point to the north and south data points' flags words, if required */
376 opj_flag_enc_t* north;
377 opj_flag_enc_t* south;
379 /* mark target as significant */
380 *flagsp |= T1_SIGMA_4 << (3U * ci);
382 /* north-west, north, north-east */
384 north = flagsp - stride;
385 *north |= T1_SIGMA_16;
386 north[-1] |= T1_SIGMA_17;
387 north[1] |= T1_SIGMA_15;
390 /* south-west, south, south-east */
392 south = flagsp + stride;
393 *south |= T1_SIGMA_1;
394 south[-1] |= T1_SIGMA_2;
395 south[1] |= T1_SIGMA_0;
399 flagsp[-1] |= T1_SIGMA_5 << (3U * ci);
402 flagsp[1] |= T1_SIGMA_3 << (3U * ci);
427 static INLINE void opj_t1_updateflags(opj_flag_t *flagsp, OPJ_UINT32 s,
430 opj_flag_t *np = flagsp - stride;
431 opj_flag_t *sp = flagsp + stride;
433 /* We strongly rely on (T1_SGN_N == 0x0100) == (T1_SIG_N == 0x0010) << 4 */
434 /* and T1_SIG_E == T1_SIG_N << 1, T1_SIG_W == T1_SIG_N << 2 and T1_SIG_S == T1_SIG_N << 2 */
435 /* and T1_SGN_E == T1_SGN_N << 1, T1_SGN_W == T1_SGN_N << 2 and T1_SGN_S == T1_SGN_N << 2 */
437 unsigned int flag_N = T1_SIG_N | ((unsigned int)T1_SIG_N << (4U * s));
440 np[0] |= (opj_flag_t)(flag_N << 2U);
443 flagsp[-1] |= (opj_flag_t)(flag_N << 1U);
445 flagsp[1] |= (opj_flag_t)(flag_N << 3U);
448 sp[0] = (opj_flag_t)(sp[0] | flag_N);
452 static INLINE void opj_t1_updateflagscolflags(opj_flag_t *flagsp,
453 opj_colflag_t *colflagsp, OPJ_UINT32 s, OPJ_UINT32 stride, OPJ_UINT32 row)
455 opj_t1_updateflags(flagsp, s, stride);
457 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
458 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
459 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
460 (T1_COLFLAG_RBS * row)) |
461 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
462 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
463 (T1_COLFLAG_RBS * row)) |
464 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
465 *(colflagsp - stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
466 *(colflagsp - stride) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
467 *(colflagsp - stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_3);
468 } else if (row == 3U) {
469 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
470 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
471 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
472 (T1_COLFLAG_RBS * row)) |
473 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))));
474 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
475 (T1_COLFLAG_RBS * row)) |
476 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1))));
477 *(colflagsp + stride - 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
478 *(colflagsp + stride) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
479 *(colflagsp + stride + 1) |= (T1_COLFLAG_SIG_OTHER_ROW_0);
481 *(colflagsp - 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
482 (T1_COLFLAG_RBS * row)) |
483 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
484 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
485 *colflagsp |= (opj_colflag_t)((T1_COLFLAG_SIG_ROW_0 << (T1_COLFLAG_RBS * row)) |
486 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
487 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
488 *(colflagsp + 1) |= (opj_colflag_t)((T1_COLFLAG_SIG_OTHER_ROW_0 <<
489 (T1_COLFLAG_RBS * row)) |
490 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row - 1U))) |
491 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * (row + 1U))));
496 Encode significant pass
498 static INLINE void opj_t1_enc_sigpass_step(opj_t1_t *t1,
499 opj_flag_enc_t *flagsp,
512 opj_mqc_t *mqc = t1->mqc; /* MQC component */
514 OPJ_UINT32 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE |
515 T1_CHI_S) << (ci * 3U)) : ~0U;
516 OPJ_UINT32 const flags = *flagsp & vsc_mask;
518 if ((flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U))) == 0U &&
519 (flags & (T1_SIGMA_NEIGHBOURS << (ci * 3U))) != 0U) {
520 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U),
522 v = opj_int_abs(*datap) & one ? 1 : 0;
524 fprintf(stderr, " ctxt1=%d\n", ctxt1);
526 opj_mqc_setcurctx(mqc, ctxt1); /* ESSAI */
527 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
528 opj_mqc_bypass_enc(mqc, v);
530 opj_mqc_encode(mqc, v);
533 OPJ_UINT32 ctxt2 = opj_t1_enc_getctxno_sc(*flagsp & vsc_mask,
534 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
536 v = *datap < 0 ? 1 : 0;
537 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno);
539 fprintf(stderr, " ctxt2=%d\n", ctxt2);
541 opj_mqc_setcurctx(mqc, ctxt2); /* ESSAI */
542 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
543 opj_mqc_bypass_enc(mqc, v);
545 OPJ_UINT32 spb = opj_t1_enc_getspb(*flagsp & vsc_mask,
546 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask, ci);
548 fprintf(stderr, " spb=%d\n", spb);
550 opj_mqc_encode(mqc, v ^ spb);
552 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
554 *flagsp |= T1_PI_THIS << (ci * 3U);
558 static INLINE void opj_t1_dec_sigpass_step_raw(
561 opj_colflag_t* colflagsp,
563 OPJ_INT32 oneplushalf,
568 opj_raw_t *raw = t1->raw; /* RAW component */
570 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
572 if ((flag & T1_SIG_OTH) &&
573 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
574 (T1_COLFLAG_RBS * row)))) {
575 if (opj_raw_decode(raw)) {
576 v = opj_raw_decode(raw); /* ESSAI */
577 *datap = v ? -oneplushalf : oneplushalf;
578 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
580 #ifdef CONSISTENCY_CHECK
583 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
584 (T1_COLFLAG_RBS * row)));
588 static INLINE void opj_t1_dec_sigpass_step_mqc(
591 opj_colflag_t* colflagsp,
593 OPJ_INT32 oneplushalf,
595 OPJ_UINT32 flags_stride)
599 opj_mqc_t *mqc = t1->mqc; /* MQC component */
600 #ifdef CONSISTENCY_CHECK
601 assert(((*flagsp & T1_SIG_OTH) && !(*flagsp & (T1_SIG | T1_VISIT))) ==
602 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
603 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
604 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))));
606 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 |
607 T1_COLFLAG_SIG_OTHER_ROW_0) << (T1_COLFLAG_RBS * row))) ==
608 (T1_COLFLAG_SIG_OTHER_ROW_0 << (T1_COLFLAG_RBS * row))) {
610 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
611 if (opj_mqc_decode(mqc)) {
612 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
613 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
614 *datap = v ? -oneplushalf : oneplushalf;
615 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
617 #ifdef CONSISTENCY_CHECK
620 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
621 (T1_COLFLAG_RBS * row)));
623 } /* VSC and BYPASS by Antonin */
625 static INLINE void opj_t1_dec_sigpass_step_mqc_vsc(
628 opj_colflag_t* colflagsp,
630 OPJ_INT32 oneplushalf,
636 opj_mqc_t *mqc = t1->mqc; /* MQC component */
638 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
640 if ((flag & T1_SIG_OTH) &&
641 !(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
642 (T1_COLFLAG_RBS * row)))) {
643 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
644 if (opj_mqc_decode(mqc)) {
645 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
646 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
647 *datap = v ? -oneplushalf : oneplushalf;
648 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
650 #ifdef CONSISTENCY_CHECK
653 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_VISIT_ROW_0 <<
654 (T1_COLFLAG_RBS * row)));
656 } /* VSC and BYPASS by Antonin */
659 static void opj_t1_enc_sigpass(opj_t1_t *t1,
668 OPJ_INT32 const one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
669 opj_flag_enc_t* f = &ENC_FLAGS(0, 0);
670 OPJ_UINT32 const extra = t1->flags_stride - t1->w;
674 fprintf(stderr, "enc_sigpass: bpno=%d\n", bpno);
676 for (k = 0; k < (t1->h & ~3U); k += 4) {
678 fprintf(stderr, " k=%d\n", k);
680 for (i = 0; i < t1->w; ++i) {
682 fprintf(stderr, " i=%d\n", i);
685 /* Nothing to do for any of the 4 data points */
689 opj_t1_enc_sigpass_step(
692 &t1->data[((k + 0) * t1->data_stride) + i],
699 opj_t1_enc_sigpass_step(
702 &t1->data[((k + 1) * t1->data_stride) + i],
709 opj_t1_enc_sigpass_step(
712 &t1->data[((k + 2) * t1->data_stride) + i],
719 opj_t1_enc_sigpass_step(
722 &t1->data[((k + 3) * t1->data_stride) + i],
728 3, cblksty & J2K_CCP_CBLKSTY_VSC);
737 fprintf(stderr, " k=%d\n", k);
739 for (i = 0; i < t1->w; ++i) {
741 fprintf(stderr, " i=%d\n", i);
744 /* Nothing to do for any of the 4 data points */
748 for (j = k; j < t1->h; ++j) {
749 opj_t1_enc_sigpass_step(
752 &t1->data[(j * t1->data_stride) + i],
759 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
766 static void opj_t1_dec_sigpass_raw(
771 OPJ_INT32 one, half, oneplushalf, vsc;
773 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
776 oneplushalf = one | half;
777 for (k = 0; k < t1->h; k += 4) {
778 for (i = 0; i < t1->w; ++i) {
779 opj_colflag_t *colflags2 = colflags1 + i;
780 for (j = k; j < k + 4 && j < t1->h; ++j) {
781 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 ||
782 j == t1->h - 1)) ? 1 : 0;
783 opj_t1_dec_sigpass_step_raw(
785 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
787 &t1->data[(j * t1->w) + i],
793 colflags1 += t1->flags_stride;
795 } /* VSC and BYPASS by Antonin */
797 #define opj_t1_dec_sigpass_mqc_internal(t1, bpno, w, h, flags_stride) \
799 OPJ_INT32 one, half, oneplushalf; \
800 OPJ_UINT32 i, j, k; \
801 OPJ_INT32 *data1 = t1->data; \
802 opj_flag_t *flags1 = &t1->flags[1]; \
803 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
806 oneplushalf = one | half; \
807 for (k = 0; k < (h & ~3u); k += 4) { \
808 for (i = 0; i < w; ++i) { \
809 OPJ_INT32 *data2 = data1 + i; \
810 opj_flag_t *flags2 = flags1 + i; \
811 opj_colflag_t *colflags2 = colflags1 + i; \
812 if( *colflags2 == 0 ) continue; \
813 flags2 += flags_stride; \
814 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
816 flags2 += flags_stride; \
817 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
819 flags2 += flags_stride; \
820 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
822 flags2 += flags_stride; \
823 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
827 flags1 += flags_stride << 2; \
828 colflags1 += flags_stride; \
830 for (i = 0; i < w; ++i) { \
831 OPJ_INT32 *data2 = data1 + i; \
832 opj_flag_t *flags2 = flags1 + i; \
833 opj_colflag_t *colflags2 = colflags1 + i; \
834 for (j = k; j < h; ++j) { \
835 flags2 += flags_stride; \
836 opj_t1_dec_sigpass_step_mqc(t1, flags2, colflags2, data2, oneplushalf, j - k, flags_stride); \
842 static void opj_t1_dec_sigpass_mqc_64x64(
846 opj_t1_dec_sigpass_mqc_internal(t1, bpno, 64, 64, 66);
849 static void opj_t1_dec_sigpass_mqc_generic(
853 opj_t1_dec_sigpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
856 /* VSC and BYPASS by Antonin */
857 static void opj_t1_dec_sigpass_mqc_vsc(
861 OPJ_INT32 one, half, oneplushalf, vsc;
863 OPJ_INT32 *data1 = t1->data;
864 opj_flag_t *flags1 = &t1->flags[1];
865 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
868 oneplushalf = one | half;
869 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3); k += 4U) {
870 for (i = 0; i < t1->w; ++i) {
871 OPJ_INT32 *data2 = data1 + i;
872 opj_flag_t *flags2 = flags1 + i;
873 opj_colflag_t *colflags2 = colflags1 + i;
874 flags2 += t1->flags_stride;
875 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
878 flags2 += t1->flags_stride;
879 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
882 flags2 += t1->flags_stride;
883 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 0,
886 flags2 += t1->flags_stride;
887 opj_t1_dec_sigpass_step_mqc_vsc(t1, flags2, colflags2, data2, oneplushalf, 1,
892 flags1 += t1->flags_stride << 2;
893 colflags1 += t1->flags_stride;
895 for (i = 0; i < t1->w; ++i) {
896 opj_colflag_t *colflags2 = colflags1 + i;
897 for (j = k; j < t1->h; ++j) {
898 vsc = (j == t1->h - 1) ? 1 : 0;
899 opj_t1_dec_sigpass_step_mqc_vsc(
901 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
903 &t1->data[(j * t1->w) + i],
909 } /* VSC and BYPASS by Antonin */
913 Encode refinement pass step
915 static INLINE void opj_t1_enc_refpass_step(opj_t1_t *t1,
916 opj_flag_enc_t *flagsp,
928 opj_mqc_t *mqc = t1->mqc; /* MQC component */
930 OPJ_UINT32 const shift_flags =
932 ((*flagsp >> (ci * 3U)) & ~(T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S))
934 (*flagsp >> (ci * 3U));
936 if ((shift_flags & (T1_SIGMA_THIS | T1_PI_THIS)) == T1_SIGMA_THIS) {
937 OPJ_UINT32 ctxt = opj_t1_enc_getctxno_mag(shift_flags);
938 *nmsedec += opj_t1_getnmsedec_ref(opj_int_abs(*datap), bpno);
939 v = opj_int_abs(*datap) & one ? 1 : 0;
941 fprintf(stderr, " ctxt=%d\n", ctxt);
943 opj_mqc_setcurctx(mqc, ctxt); /* ESSAI */
944 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
945 opj_mqc_bypass_enc(mqc, v);
947 opj_mqc_encode(mqc, v);
949 *flagsp |= T1_MU_THIS << (ci * 3U);
954 static INLINE void opj_t1_dec_refpass_step_raw(
956 opj_colflag_t *colflagsp,
964 opj_raw_t *raw = t1->raw; /* RAW component */
966 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
967 (T1_COLFLAG_RBS * row))) ==
968 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
969 v = (OPJ_INT32)opj_raw_decode(raw);
970 t = v ? poshalf : neghalf;
971 *datap += *datap < 0 ? -t : t;
972 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
973 (T1_COLFLAG_RBS * row)));
975 } /* VSC and BYPASS by Antonin */
977 static INLINE void opj_t1_dec_refpass_step_mqc(
979 #ifdef CONSISTENCY_CHECK
982 opj_flag_t *flagsp_unused,
984 opj_colflag_t *colflagsp,
992 opj_mqc_t *mqc = t1->mqc; /* MQC component */
993 #ifdef CONSISTENCY_CHECK
994 assert(((*flagsp & (T1_SIG | T1_VISIT)) == T1_SIG) ==
995 ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
996 (T1_COLFLAG_RBS * row))) == ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS *
1001 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1002 (T1_COLFLAG_RBS * row))) ==
1003 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
1004 OPJ_UINT32 tmp1 = (*colflagsp & (T1_COLFLAG_SIG_OTHER_ROW_0 <<
1005 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
1006 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
1007 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2 : tmp1;
1008 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
1009 v = opj_mqc_decode(mqc);
1010 t = v ? poshalf : neghalf;
1011 *datap += *datap < 0 ? -t : t;
1012 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
1013 (T1_COLFLAG_RBS * row)));
1015 } /* VSC and BYPASS by Antonin */
1017 static INLINE void opj_t1_dec_refpass_step_mqc_vsc(
1020 opj_colflag_t *colflagsp,
1030 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1032 if ((*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1033 (T1_COLFLAG_RBS * row))) ==
1034 ((T1_COLFLAG_SIG_ROW_0) << (T1_COLFLAG_RBS * row))) {
1035 OPJ_UINT32 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
1036 T1_SGN_S))) : (*flagsp);
1037 OPJ_UINT32 tmp1 = (flag & T1_SIG_OTH) ? T1_CTXNO_MAG + 1 : T1_CTXNO_MAG;
1038 OPJ_UINT32 tmp2 = (*colflagsp & (T1_COLFLAG_REFINE_ROW_0 <<
1039 (T1_COLFLAG_RBS * row))) ? T1_CTXNO_MAG + 2U : tmp1;
1040 opj_mqc_setcurctx(mqc, tmp2); /* ESSAI */
1041 v = (OPJ_UINT32)opj_mqc_decode(mqc);
1042 t = v ? poshalf : neghalf;
1043 *datap += *datap < 0 ? -t : t;
1044 *colflagsp = (opj_colflag_t)(*colflagsp | (T1_COLFLAG_REFINE_ROW_0 <<
1045 (T1_COLFLAG_RBS * row)));
1047 } /* VSC and BYPASS by Antonin */
1049 static void opj_t1_enc_refpass(
1057 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1058 opj_flag_enc_t* f = &ENC_FLAGS(0, 0);
1059 const OPJ_UINT32 extra = t1->flags_stride - t1->w;
1062 #ifdef DEBUG_ENC_REF
1063 fprintf(stderr, "enc_refpass: bpno=%d\n", bpno);
1065 for (k = 0; k < (t1->h & ~3U); k += 4) {
1066 #ifdef DEBUG_ENC_REF
1067 fprintf(stderr, " k=%d\n", k);
1069 for (i = 0; i < t1->w; ++i) {
1070 #ifdef DEBUG_ENC_REF
1071 fprintf(stderr, " i=%d\n", i);
1073 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1074 /* none significant */
1078 if ((*f & (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) ==
1079 (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3)) {
1080 /* all processed by sigpass */
1085 opj_t1_enc_refpass_step(
1088 &t1->data[((k + 0) * t1->data_stride) + i],
1094 opj_t1_enc_refpass_step(
1097 &t1->data[((k + 1) * t1->data_stride) + i],
1103 opj_t1_enc_refpass_step(
1106 &t1->data[((k + 2) * t1->data_stride) + i],
1112 opj_t1_enc_refpass_step(
1115 &t1->data[((k + 3) * t1->data_stride) + i],
1120 3, cblksty & J2K_CCP_CBLKSTY_VSC);
1128 #ifdef DEBUG_ENC_REF
1129 fprintf(stderr, " k=%d\n", k);
1131 for (i = 0; i < t1->w; ++i) {
1132 #ifdef DEBUG_ENC_REF
1133 fprintf(stderr, " i=%d\n", i);
1135 if ((*f & (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13)) == 0) {
1136 /* none significant */
1140 for (j = k; j < t1->h; ++j) {
1141 opj_t1_enc_refpass_step(
1144 &t1->data[(j * t1->data_stride) + i],
1150 (j == t1->h - 1 && (cblksty & J2K_CCP_CBLKSTY_VSC) != 0));
1158 static void opj_t1_dec_refpass_raw(
1162 OPJ_INT32 one, poshalf, neghalf;
1164 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
1167 neghalf = bpno > 0 ? -poshalf : -1;
1168 for (k = 0; k < t1->h; k += 4) {
1169 for (i = 0; i < t1->w; ++i) {
1170 opj_colflag_t *colflags2 = colflags1 + i;
1171 for (j = k; j < k + 4 && j < t1->h; ++j) {
1172 opj_t1_dec_refpass_step_raw(
1175 &t1->data[(j * t1->w) + i],
1180 colflags1 += t1->flags_stride;
1182 } /* VSC and BYPASS by Antonin */
1184 #define opj_t1_dec_refpass_mqc_internal(t1, bpno, w, h, flags_stride) \
1186 OPJ_INT32 one, poshalf, neghalf; \
1187 OPJ_UINT32 i, j, k; \
1188 OPJ_INT32 *data1 = t1->data; \
1189 opj_flag_t *flags1 = &t1->flags[1]; \
1190 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1192 poshalf = one >> 1; \
1193 neghalf = bpno > 0 ? -poshalf : -1; \
1194 for (k = 0; k < (h & ~3u); k += 4) { \
1195 for (i = 0; i < w; ++i) { \
1196 OPJ_INT32 *data2 = data1 + i; \
1197 opj_flag_t *flags2 = flags1 + i; \
1198 opj_colflag_t *colflags2 = colflags1 + i; \
1199 if( *colflags2 == 0 ) continue; \
1200 flags2 += flags_stride; \
1201 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 0U); \
1203 flags2 += flags_stride; \
1204 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 1U); \
1206 flags2 += flags_stride; \
1207 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 2U); \
1209 flags2 += flags_stride; \
1210 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, 3U); \
1214 flags1 += flags_stride << 2; \
1215 colflags1 += flags_stride; \
1217 for (i = 0; i < w; ++i) { \
1218 OPJ_INT32 *data2 = data1 + i; \
1219 opj_flag_t *flags2 = flags1 + i; \
1220 opj_colflag_t *colflags2 = colflags1 + i; \
1221 for (j = k; j < h; ++j) { \
1222 flags2 += flags_stride; \
1223 opj_t1_dec_refpass_step_mqc(t1, flags2, colflags2, data2, poshalf, neghalf, j - k); \
1229 static void opj_t1_dec_refpass_mqc_64x64(
1233 opj_t1_dec_refpass_mqc_internal(t1, bpno, 64, 64, 66);
1236 static void opj_t1_dec_refpass_mqc_generic(
1240 opj_t1_dec_refpass_mqc_internal(t1, bpno, t1->w, t1->h, t1->flags_stride);
1243 /* VSC and BYPASS by Antonin */
1244 static void opj_t1_dec_refpass_mqc_vsc(
1248 OPJ_INT32 one, poshalf, neghalf;
1251 OPJ_INT32 *data1 = t1->data;
1252 opj_flag_t *flags1 = &t1->flags[1];
1253 opj_colflag_t *colflags1 = &t1->colflags[t1->flags_stride + 1];
1256 neghalf = bpno > 0 ? -poshalf : -1;
1257 for (k = 0; k < (t1->h & ~(OPJ_UINT32)3U); k += 4U) {
1258 for (i = 0; i < t1->w; ++i) {
1259 OPJ_INT32 *data2 = data1 + i;
1260 opj_flag_t *flags2 = flags1 + i;
1261 opj_colflag_t *colflags2 = colflags1 + i;
1262 flags2 += t1->flags_stride;
1263 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1266 flags2 += t1->flags_stride;
1267 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1270 flags2 += t1->flags_stride;
1271 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1274 flags2 += t1->flags_stride;
1275 opj_t1_dec_refpass_step_mqc_vsc(t1, flags2, colflags2, data2, poshalf, neghalf,
1279 data1 += t1->w << 2;
1280 flags1 += t1->flags_stride << 2;
1281 colflags1 += t1->flags_stride;
1283 for (i = 0; i < t1->w; ++i) {
1284 opj_colflag_t *colflags2 = colflags1 + i;
1285 for (j = k; j < t1->h; ++j) {
1286 vsc = (j == t1->h - 1) ? 1 : 0;
1287 opj_t1_dec_refpass_step_mqc_vsc(
1289 &t1->flags[((j + 1) * t1->flags_stride) + i + 1],
1291 &t1->data[(j * t1->w) + i],
1297 } /* VSC and BYPASS by Antonin */
1300 Encode clean-up pass step
1302 static void opj_t1_enc_clnpass_step(
1304 opj_flag_enc_t *flagsp,
1318 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1321 if ((cblksty & J2K_CCP_CBLKSTY_VSC) == 0) {
1322 const OPJ_UINT32 check = (T1_SIGMA_4 | T1_SIGMA_7 | T1_SIGMA_10 | T1_SIGMA_13 |
1323 T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1325 if ((*flagsp & check) == check) {
1327 *flagsp &= ~(T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1328 } else if (runlen == 1) {
1329 *flagsp &= ~(T1_PI_1 | T1_PI_2 | T1_PI_3);
1330 } else if (runlen == 2) {
1331 *flagsp &= ~(T1_PI_2 | T1_PI_3);
1332 } else if (runlen == 3) {
1333 *flagsp &= ~(T1_PI_3);
1339 lim = 4U < (t1->h - y) ? 4U : (t1->h - y);
1340 for (ci = runlen; ci < lim; ++ci) {
1342 OPJ_UINT32 vsc_mask;
1343 opj_flag_enc_t flags;
1345 vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (ci == lim - 1)) ? 1 : 0;
1346 vsc_mask = vsc ? ~((T1_SIGMA_SW | T1_SIGMA_S | T1_SIGMA_SE | T1_CHI_S) <<
1349 if ((agg != 0) && (ci == runlen)) {
1353 flags = *flagsp & vsc_mask;
1355 if (!(flags & ((T1_SIGMA_THIS | T1_PI_THIS) << (ci * 3U)))) {
1356 OPJ_UINT32 ctxt1 = opj_t1_enc_getctxno_zc(flags >> (ci * 3U), orient);
1357 #ifdef DEBUG_ENC_CLN
1358 printf(" ctxt1=%d\n", ctxt1);
1360 opj_mqc_setcurctx(mqc, ctxt1);
1361 v = opj_int_abs(*datap) & one ? 1 : 0;
1362 opj_mqc_encode(mqc, v);
1364 OPJ_UINT32 ctxt2, spb;
1366 *nmsedec += opj_t1_getnmsedec_sig(opj_int_abs(*datap), bpno);
1367 ctxt2 = opj_t1_enc_getctxno_sc(*flagsp & vsc_mask,
1368 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask,
1370 #ifdef DEBUG_ENC_CLN
1371 printf(" ctxt2=%d\n", ctxt2);
1373 opj_mqc_setcurctx(mqc, ctxt2);
1375 v = *datap < 0 ? 1 : 0;
1376 spb = opj_t1_enc_getspb(*flagsp & vsc_mask,
1377 flagsp[-1] & vsc_mask, flagsp[1] & vsc_mask, ci);
1378 #ifdef DEBUG_ENC_CLN
1379 printf(" spb=%d\n", spb);
1381 opj_mqc_encode(mqc, v ^ spb);
1382 opj_t1_enc_updateflags(flagsp, ci, v, t1->flags_stride);
1385 *flagsp &= ~(T1_PI_THIS << (3U * ci));
1386 datap += t1->data_stride;
1391 static void opj_t1_dec_clnpass_step_partial(
1394 opj_colflag_t *colflagsp,
1396 OPJ_INT32 oneplushalf,
1400 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1403 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1404 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1405 *datap = v ? -oneplushalf : oneplushalf;
1406 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1408 #ifdef CONSISTENCY_CHECK
1409 *flagsp &= ~T1_VISIT;
1411 } /* VSC and BYPASS by Antonin */
1413 static void opj_t1_dec_clnpass_step(
1416 opj_colflag_t *colflagsp,
1418 OPJ_INT32 oneplushalf,
1423 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1424 #ifdef CONSISTENCY_CHECK
1425 assert((!(*flagsp & (T1_SIG | T1_VISIT))) == (!(*colflagsp & ((
1426 T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) << (4 * row)))));
1428 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1431 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1432 if (opj_mqc_decode(mqc)) {
1433 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1434 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1435 *datap = v ? -oneplushalf : oneplushalf;
1436 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, t1->flags_stride,
1440 #ifdef CONSISTENCY_CHECK
1441 *flagsp &= ~T1_VISIT;
1443 } /* VSC and BYPASS by Antonin */
1445 static void opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(
1448 opj_colflag_t *colflagsp,
1450 OPJ_INT32 oneplushalf,
1452 OPJ_UINT32 flags_stride)
1457 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1460 /*if (!(flag & (T1_SIG | T1_VISIT)))*/
1462 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, (OPJ_UINT32)flag));
1463 if (opj_mqc_decode(mqc)) {
1464 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc((OPJ_UINT32)flag));
1465 v = opj_mqc_decode(mqc) ^ opj_t1_getspb((OPJ_UINT32)flag);
1466 *datap = v ? -oneplushalf : oneplushalf;
1467 opj_t1_updateflagscolflags(flagsp, colflagsp, (OPJ_UINT32)v, flags_stride, row);
1470 /*flagsp &= ~T1_VISIT;*/
1473 static void opj_t1_dec_clnpass_step_vsc(
1476 opj_colflag_t *colflagsp,
1478 OPJ_INT32 oneplushalf,
1485 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1487 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
1492 if (!(*colflagsp & ((T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0) <<
1493 (T1_COLFLAG_RBS * row)))) {
1494 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(mqc, flag));
1495 if (opj_mqc_decode(mqc)) {
1497 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
1498 v = (OPJ_UINT32)opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
1499 *datap = v ? -oneplushalf : oneplushalf;
1500 opj_t1_updateflagscolflags(flagsp, colflagsp, v, t1->flags_stride, row);
1503 #ifdef CONSISTENCY_CHECK
1504 *flagsp &= ~T1_VISIT;
1508 #define MACRO_t1_enc_flags(x,y) t1->enc_flags[((x)*(t1->flags_stride))+(y)]
1510 static void opj_t1_enc_clnpass(
1518 const OPJ_INT32 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
1519 OPJ_UINT32 agg, runlen;
1521 opj_mqc_t *mqc = t1->mqc; /* MQC component */
1524 #ifdef DEBUG_ENC_CLN
1525 printf("enc_clnpass: bpno=%d\n", bpno);
1527 for (k = 0; k < t1->h; k += 4) {
1528 #ifdef DEBUG_ENC_CLN
1529 printf(" k=%d\n", k);
1531 for (i = 0; i < t1->w; ++i) {
1532 #ifdef DEBUG_ENC_CLN
1533 printf(" i=%d\n", i);
1535 if (k + 3 < t1->h) {
1536 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
1537 agg = !(ENC_FLAGS(i, k) & ~(T1_SIGMA_15 | T1_SIGMA_16 | T1_SIGMA_17 |
1540 agg = !ENC_FLAGS(i, k);
1545 #ifdef DEBUG_ENC_CLN
1546 printf(" agg=%d\n", agg);
1549 for (runlen = 0; runlen < 4; ++runlen) {
1550 if (opj_int_abs(t1->data[((k + runlen)*t1->data_stride) + i]) & one) {
1554 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG);
1555 opj_mqc_encode(mqc, runlen != 4);
1559 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI);
1560 opj_mqc_encode(mqc, runlen >> 1);
1561 opj_mqc_encode(mqc, runlen & 1);
1565 opj_t1_enc_clnpass_step(
1568 &t1->data[((k + runlen) * t1->data_stride) + i],
1583 #define MACRO_t1_flags_internal(x,y,flags_stride) t1->flags[((x)*(flags_stride))+(y)]
1585 #define opj_t1_dec_clnpass_internal(consistency_check, t1, bpno, cblksty, w, h, flags_stride) \
1587 OPJ_INT32 one, half, oneplushalf, agg, runlen, vsc; \
1588 OPJ_UINT32 i, j, k; \
1589 OPJ_INT32 segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM; \
1591 opj_mqc_t *mqc = t1->mqc; /* MQC component */ \
1595 oneplushalf = one | half; \
1596 if (cblksty & J2K_CCP_CBLKSTY_VSC) { \
1597 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1598 for (k = 0; k < h; k += 4) { \
1599 for (i = 0; i < w; ++i) { \
1600 opj_colflag_t *colflags2 = colflags1 + i; \
1602 agg = !((*colflags2 & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1603 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1604 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1605 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3)) || \
1606 ((MACRO_t1_flags_internal(1 + k + 3,1 + i,flags_stride) \
1607 & ((~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) & (T1_SIG_OTH)))); \
1612 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1613 if (!opj_mqc_decode(mqc)) { \
1616 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1617 runlen = opj_mqc_decode(mqc); \
1618 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1622 for (j = k + (OPJ_UINT32)runlen; j < k + 4 && j < h; ++j) { \
1623 vsc = (j == k + 3 || j == h - 1) ? 1 : 0; \
1624 opj_t1_dec_clnpass_step_vsc( \
1626 &t1->flags[((j+1) * flags_stride) + i + 1], \
1628 &t1->data[(j * w) + i], \
1630 agg && (j == k + (OPJ_UINT32)runlen), \
1633 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1635 colflags1 += flags_stride; \
1638 OPJ_INT32 *data1 = t1->data; \
1639 opj_flag_t *flags1 = &t1->flags[1]; \
1640 opj_colflag_t *colflags1 = &t1->colflags[flags_stride + 1]; \
1641 for (k = 0; k < (h & ~3u); k += 4) { \
1642 for (i = 0; i < w; ++i) { \
1643 OPJ_INT32 *data2 = data1 + i; \
1644 opj_flag_t *flags2 = flags1 + i; \
1645 opj_colflag_t *colflags2 = colflags1 + i; \
1646 opj_colflag_t colflags = *colflags2; \
1647 agg = !(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_SIG_OTHER_ROW_0 | \
1648 T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_SIG_OTHER_ROW_1 | \
1649 T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_SIG_OTHER_ROW_2 | \
1650 T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3 | T1_COLFLAG_SIG_OTHER_ROW_3)); \
1651 if( consistency_check ) { \
1652 assert( agg == !((MACRO_t1_flags_internal(1 + k, 1 + i,flags_stride) | \
1653 MACRO_t1_flags_internal(1 + k + 1, 1 + i,flags_stride) | \
1654 MACRO_t1_flags_internal(1 + k + 2, 1 + i,flags_stride) | \
1655 MACRO_t1_flags_internal(1 + k + 3, 1 + i,flags_stride)) & (T1_SIG | T1_VISIT | T1_SIG_OTH)) ); \
1658 opj_mqc_setcurctx(mqc, T1_CTXNO_AGG); \
1659 if (!opj_mqc_decode(mqc)) { \
1662 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1663 runlen = opj_mqc_decode(mqc); \
1664 runlen = (runlen << 1) | opj_mqc_decode(mqc); \
1665 flags2 += (OPJ_UINT32)runlen * flags_stride; \
1666 data2 += (OPJ_UINT32)runlen * w; \
1667 for (j = (OPJ_UINT32)runlen; j < 4; ++j) { \
1668 flags2 += flags_stride; \
1669 if (j == (OPJ_UINT32)runlen) { \
1670 opj_t1_dec_clnpass_step_partial(t1, flags2, colflags2, data2, oneplushalf, j); \
1672 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j); \
1677 flags2 += flags_stride; \
1678 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1679 if (!(colflags & (T1_COLFLAG_SIG_ROW_0 | T1_COLFLAG_VISIT_ROW_0))) {\
1680 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 0U, flags_stride); \
1682 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1684 flags2 += flags_stride; \
1685 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1686 if (!(colflags & (T1_COLFLAG_SIG_ROW_1 | T1_COLFLAG_VISIT_ROW_1))) {\
1687 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 1U, flags_stride); \
1689 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1691 flags2 += flags_stride; \
1692 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1693 if (!(colflags & (T1_COLFLAG_SIG_ROW_2 | T1_COLFLAG_VISIT_ROW_2))) {\
1694 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 2U, flags_stride); \
1696 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1698 flags2 += flags_stride; \
1699 if( consistency_check ) { assert( (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) == (!(*flags2 & (T1_SIG | T1_VISIT))) ); } \
1700 if (!(colflags & (T1_COLFLAG_SIG_ROW_3 | T1_COLFLAG_VISIT_ROW_3))) {\
1701 opj_t1_dec_clnpass_step_only_if_flag_not_sig_visit(t1, flags2, colflags2, data2, oneplushalf, 3U, flags_stride); \
1703 if( consistency_check ) *flags2 &= (opj_flag_t)~T1_VISIT; \
1706 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1709 flags1 += flags_stride << 2; \
1710 colflags1 += flags_stride; \
1712 for (i = 0; i < w; ++i) { \
1713 OPJ_INT32 *data2 = data1 + i; \
1714 opj_flag_t *flags2 = flags1 + i; \
1715 opj_colflag_t *colflags2 = colflags1 + i; \
1716 for (j = k; j < h; ++j) { \
1717 flags2 += flags_stride; \
1718 opj_t1_dec_clnpass_step(t1, flags2, colflags2, data2, oneplushalf, j - k); \
1721 *colflags2 &= (opj_colflag_t)~(T1_COLFLAG_VISIT_ROW_0 | T1_COLFLAG_VISIT_ROW_1 | T1_COLFLAG_VISIT_ROW_2 | T1_COLFLAG_VISIT_ROW_3); \
1727 opj_mqc_setcurctx(mqc, T1_CTXNO_UNI); \
1728 v = opj_mqc_decode(mqc); \
1729 v = (v << 1) | opj_mqc_decode(mqc); \
1730 v = (v << 1) | opj_mqc_decode(mqc); \
1731 v = (v << 1) | opj_mqc_decode(mqc); \
1734 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v); \
1738 } /* VSC and BYPASS by Antonin */
1740 static void opj_t1_dec_clnpass_64x64(
1745 #ifdef CONSISTENCY_CHECK
1746 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, 64, 64, 66);
1748 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, 64, 64, 66);
1752 static void opj_t1_dec_clnpass_generic(
1757 #ifdef CONSISTENCY_CHECK
1758 opj_t1_dec_clnpass_internal(OPJ_TRUE, t1, bpno, cblksty, t1->w, t1->h,
1761 opj_t1_dec_clnpass_internal(OPJ_FALSE, t1, bpno, cblksty, t1->w, t1->h,
1767 /** mod fixed_quality */
1768 static OPJ_FLOAT64 opj_t1_getwmsedec(
1775 OPJ_FLOAT64 stepsize,
1776 OPJ_UINT32 numcomps,
1777 const OPJ_FLOAT64 * mct_norms,
1778 OPJ_UINT32 mct_numcomps)
1780 OPJ_FLOAT64 w1 = 1, w2, wmsedec;
1781 OPJ_ARG_NOT_USED(numcomps);
1783 if (mct_norms && (compno < mct_numcomps)) {
1784 w1 = mct_norms[compno];
1788 w2 = opj_dwt_getnorm(level, orient);
1789 } else { /* if (qmfbid == 0) */
1790 w2 = opj_dwt_getnorm_real(level, orient);
1793 wmsedec = w1 * w2 * stepsize * (1 << bpno);
1794 wmsedec *= wmsedec * nmsedec / 8192.0;
1799 static OPJ_BOOL opj_t1_allocate_buffers(
1806 /* encoder uses tile buffer, so no need to allocate */
1810 #if (SIZE_MAX / 0xFFFFFFFFU) < 0xFFFFFFFFU /* UINT32_MAX */
1811 /* Overflow check */
1812 if ((w > 0U) && ((size_t)h > (SIZE_MAX / (size_t)w))) {
1813 /* FIXME event manager error callback */
1817 datasize = (size_t)w * h;
1819 /* Overflow check */
1820 if (datasize > (SIZE_MAX / sizeof(OPJ_INT32))) {
1821 /* FIXME event manager error callback */
1825 if (datasize > (size_t)t1->datasize) {
1826 opj_aligned_free(t1->data);
1827 t1->data = (OPJ_INT32*) opj_aligned_malloc(datasize * sizeof(OPJ_INT32));
1829 /* FIXME event manager error callback */
1832 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1833 /* TODO remove this if t1->datasize type changes to size_t */
1834 /* Overflow check */
1835 if (datasize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1836 /* FIXME event manager error callback */
1840 t1->datasize = (OPJ_UINT32)datasize;
1842 /* memset first arg is declared to never be null by gcc */
1843 if (t1->data != NULL) {
1844 memset(t1->data, 0, datasize * sizeof(OPJ_INT32));
1848 /* Overflow check */
1849 if (w > (0xFFFFFFFFU /* UINT32_MAX */ - 2U)) {
1850 /* FIXME event manager error callback */
1853 t1->flags_stride = w + 2U; /* can't be 0U */
1855 #if (SIZE_MAX - 3U) < 0xFFFFFFFFU /* UINT32_MAX */
1856 /* Overflow check */
1857 if (h > (0xFFFFFFFFU /* UINT32_MAX */ - 3U)) {
1858 /* FIXME event manager error callback */
1863 flagssize = (size_t)h + 2U;
1865 flagssize = (h + 3U) / 4U + 2U;
1868 /* Overflow check */
1869 if (flagssize > (SIZE_MAX / (size_t)t1->flags_stride)) {
1870 /* FIXME event manager error callback */
1873 flagssize *= (size_t)t1->flags_stride;
1877 if (flagssize > (size_t)t1->flagssize) {
1878 /* Overflow check */
1879 if (flagssize > (SIZE_MAX / sizeof(opj_flag_t))) {
1880 /* FIXME event manager error callback */
1883 opj_aligned_free(t1->flags);
1884 t1->flags = (opj_flag_t*) opj_aligned_malloc(flagssize * sizeof(opj_flag_t));
1886 /* FIXME event manager error callback */
1889 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1890 /* TODO remove this if t1->flagssize type changes to size_t */
1891 /* Overflow check */
1892 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1893 /* FIXME event manager error callback */
1898 t1->flagssize = (OPJ_UINT32)flagssize;
1900 memset(t1->flags, 0, flagssize * sizeof(opj_flag_t));
1902 size_t colflags_size = ((((size_t)h + 3U) / 4U) +
1903 2U); /* Can't overflow, h checked against UINT32_MAX - 3U */
1905 /* Overflow check */
1906 if (colflags_size > (SIZE_MAX / (size_t)t1->flags_stride)) {
1907 /* FIXME event manager error callback */
1910 colflags_size *= (size_t)t1->flags_stride;
1912 if (colflags_size > (size_t)t1->colflags_size) {
1913 /* Overflow check */
1914 if ((size_t)colflags_size > (SIZE_MAX / sizeof(opj_colflag_t))) {
1915 /* FIXME event manager error callback */
1918 opj_aligned_free(t1->colflags);
1919 t1->colflags = (opj_colflag_t*) opj_aligned_malloc(colflags_size * sizeof(
1921 if (!t1->colflags) {
1922 /* FIXME event manager error callback */
1925 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1926 /* TODO remove this if t1->colflags_size type changes to size_t */
1927 /* Overflow check */
1928 if (colflags_size > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1929 /* FIXME event manager error callback */
1933 t1->colflags_size = (OPJ_UINT32)colflags_size;
1935 memset(t1->colflags, 0, colflags_size * sizeof(opj_colflag_t));
1940 OPJ_UINT32 flags_height = (h + 3U) / 4U;
1942 if (flagssize > (size_t)t1->flagssize) {
1943 /* Overflow check */
1944 if (flagssize > (SIZE_MAX / sizeof(opj_flag_enc_t))) {
1945 /* FIXME event manager error callback */
1948 opj_aligned_free(t1->enc_flags);
1949 t1->enc_flags = (opj_flag_enc_t*) opj_aligned_malloc(flagssize * sizeof(
1951 if (!t1->enc_flags) {
1952 /* FIXME event manager error callback */
1955 #if SIZE_MAX > 0xFFFFFFFFU /* UINT32_MAX */
1956 /* TODO remove this if t1->flagssize type changes to size_t */
1957 /* Overflow check */
1958 if (flagssize > (size_t)0xFFFFFFFFU /* UINT32_MAX */) {
1959 /* FIXME event manager error callback */
1964 t1->flagssize = (OPJ_UINT32)flagssize;
1966 memset(t1->enc_flags, 0, flagssize * sizeof(opj_flag_enc_t));
1968 p = &t1->enc_flags[0];
1969 for (x = 0; x < t1->flags_stride; ++x) {
1970 /* magic value to hopefully stop any passes being interested in this entry */
1971 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1974 p = &t1->enc_flags[((flags_height + 1) * t1->flags_stride)];
1975 for (x = 0; x < t1->flags_stride; ++x) {
1976 /* magic value to hopefully stop any passes being interested in this entry */
1977 *p++ = (T1_PI_0 | T1_PI_1 | T1_PI_2 | T1_PI_3);
1982 p = &t1->enc_flags[((flags_height) * t1->flags_stride)];
1984 v |= T1_PI_1 | T1_PI_2 | T1_PI_3;
1985 } else if (h % 4 == 2) {
1986 v |= T1_PI_2 | T1_PI_3;
1987 } else if (h % 4 == 3) {
1990 for (x = 0; x < t1->flags_stride; ++x) {
2002 /* ----------------------------------------------------------------------- */
2004 /* ----------------------------------------------------------------------- */
2006 * Creates a new Tier 1 handle
2007 * and initializes the look-up tables of the Tier-1 coder/decoder
2008 * @return a new T1 handle if successful, returns NULL otherwise
2010 opj_t1_t* opj_t1_create(OPJ_BOOL isEncoder)
2012 opj_t1_t *l_t1 = 00;
2014 l_t1 = (opj_t1_t*) opj_calloc(1, sizeof(opj_t1_t));
2019 /* create MQC and RAW handles */
2020 l_t1->mqc = opj_mqc_create();
2022 opj_t1_destroy(l_t1);
2026 l_t1->raw = opj_raw_create();
2028 opj_t1_destroy(l_t1);
2031 l_t1->encoder = isEncoder;
2038 * Destroys a previously created T1 handle
2040 * @param p_t1 Tier 1 handle to destroy
2042 void opj_t1_destroy(opj_t1_t *p_t1)
2048 /* destroy MQC and RAW handles */
2049 opj_mqc_destroy(p_t1->mqc);
2051 opj_raw_destroy(p_t1->raw);
2054 /* encoder uses tile buffer, so no need to free */
2055 if (!p_t1->encoder && p_t1->data) {
2056 opj_aligned_free(p_t1->data);
2061 opj_aligned_free(p_t1->flags);
2065 if (p_t1->colflags) {
2066 opj_aligned_free(p_t1->colflags);
2067 p_t1->colflags = 00;
2070 if (p_t1->enc_flags) {
2071 opj_aligned_free(p_t1->enc_flags);
2072 p_t1->enc_flags = 00;
2079 opj_tcd_cblk_dec_t* cblk;
2080 opj_tcd_band_t* band;
2081 opj_tcd_tilecomp_t* tilec;
2083 volatile OPJ_BOOL* pret;
2084 } opj_t1_cblk_decode_processing_job_t;
2086 static void opj_t1_destroy_wrapper(void* t1)
2088 opj_t1_destroy((opj_t1_t*) t1);
2091 static void opj_t1_clbl_decode_processor(void* user_data, opj_tls_t* tls)
2093 opj_tcd_cblk_dec_t* cblk;
2094 opj_tcd_band_t* band;
2095 opj_tcd_tilecomp_t* tilec;
2097 OPJ_INT32* OPJ_RESTRICT datap;
2098 OPJ_UINT32 cblk_w, cblk_h;
2101 opj_t1_cblk_decode_processing_job_t* job;
2106 job = (opj_t1_cblk_decode_processing_job_t*) user_data;
2112 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2114 if (!*(job->pret)) {
2119 t1 = (opj_t1_t*) opj_tls_get(tls, OPJ_TLS_KEY_T1);
2121 t1 = opj_t1_create(OPJ_FALSE);
2122 opj_tls_set(tls, OPJ_TLS_KEY_T1, t1, opj_t1_destroy_wrapper);
2125 if (OPJ_FALSE == opj_t1_decode_cblk(
2129 (OPJ_UINT32)tccp->roishift,
2131 *(job->pret) = OPJ_FALSE;
2136 x = cblk->x0 - band->x0;
2137 y = cblk->y0 - band->y0;
2138 if (band->bandno & 1) {
2139 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2140 x += pres->x1 - pres->x0;
2142 if (band->bandno & 2) {
2143 opj_tcd_resolution_t* pres = &tilec->resolutions[resno - 1];
2144 y += pres->y1 - pres->y0;
2151 if (tccp->roishift) {
2152 OPJ_INT32 thresh = 1 << tccp->roishift;
2153 for (j = 0; j < cblk_h; ++j) {
2154 for (i = 0; i < cblk_w; ++i) {
2155 OPJ_INT32 val = datap[(j * cblk_w) + i];
2156 OPJ_INT32 mag = abs(val);
2157 if (mag >= thresh) {
2158 mag >>= tccp->roishift;
2159 datap[(j * cblk_w) + i] = val < 0 ? -mag : mag;
2164 if (tccp->qmfbid == 1) {
2165 OPJ_INT32* OPJ_RESTRICT tiledp = &tilec->data[(OPJ_UINT32)y * tile_w +
2167 for (j = 0; j < cblk_h; ++j) {
2169 for (; i < (cblk_w & ~(OPJ_UINT32)3U); i += 4U) {
2170 OPJ_INT32 tmp0 = datap[(j * cblk_w) + i + 0U];
2171 OPJ_INT32 tmp1 = datap[(j * cblk_w) + i + 1U];
2172 OPJ_INT32 tmp2 = datap[(j * cblk_w) + i + 2U];
2173 OPJ_INT32 tmp3 = datap[(j * cblk_w) + i + 3U];
2174 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 0U] = tmp0 / 2;
2175 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 1U] = tmp1 / 2;
2176 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 2U] = tmp2 / 2;
2177 ((OPJ_INT32*)tiledp)[(j * tile_w) + i + 3U] = tmp3 / 2;
2179 for (; i < cblk_w; ++i) {
2180 OPJ_INT32 tmp = datap[(j * cblk_w) + i];
2181 ((OPJ_INT32*)tiledp)[(j * tile_w) + i] = tmp / 2;
2184 } else { /* if (tccp->qmfbid == 0) */
2185 OPJ_FLOAT32* OPJ_RESTRICT tiledp = (OPJ_FLOAT32*) &tilec->data[(OPJ_UINT32)y *
2186 tile_w + (OPJ_UINT32)x];
2187 for (j = 0; j < cblk_h; ++j) {
2188 OPJ_FLOAT32* OPJ_RESTRICT tiledp2 = tiledp;
2189 for (i = 0; i < cblk_w; ++i) {
2190 OPJ_FLOAT32 tmp = (OPJ_FLOAT32) * datap * band->stepsize;
2203 void opj_t1_decode_cblks(opj_thread_pool_t* tp,
2204 volatile OPJ_BOOL* pret,
2205 opj_tcd_tilecomp_t* tilec,
2209 OPJ_UINT32 resno, bandno, precno, cblkno;
2211 for (resno = 0; resno < tilec->minimum_num_resolutions; ++resno) {
2212 opj_tcd_resolution_t* res = &tilec->resolutions[resno];
2214 for (bandno = 0; bandno < res->numbands; ++bandno) {
2215 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2217 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2218 opj_tcd_precinct_t* precinct = &band->precincts[precno];
2220 for (cblkno = 0; cblkno < precinct->cw * precinct->ch; ++cblkno) {
2221 opj_tcd_cblk_dec_t* cblk = &precinct->cblks.dec[cblkno];
2222 opj_t1_cblk_decode_processing_job_t* job;
2224 job = (opj_t1_cblk_decode_processing_job_t*) opj_calloc(1,
2225 sizeof(opj_t1_cblk_decode_processing_job_t));
2236 opj_thread_pool_submit_job(tp, opj_t1_clbl_decode_processor, job);
2249 static OPJ_BOOL opj_t1_decode_cblk(opj_t1_t *t1,
2250 opj_tcd_cblk_dec_t* cblk,
2252 OPJ_UINT32 roishift,
2255 opj_raw_t *raw = t1->raw; /* RAW component */
2256 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2258 OPJ_INT32 bpno_plus_one;
2259 OPJ_UINT32 passtype;
2260 OPJ_UINT32 segno, passno;
2261 OPJ_BYTE type = T1_TYPE_MQ; /* BYPASS mode */
2263 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2265 if (!opj_t1_allocate_buffers(
2267 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2268 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2272 bpno_plus_one = (OPJ_INT32)(roishift + cblk->numbps);
2275 opj_mqc_resetstates(mqc);
2276 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2277 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2278 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2280 for (segno = 0; segno < cblk->real_num_segs; ++segno) {
2281 opj_tcd_seg_t *seg = &cblk->segs[segno];
2284 type = ((bpno_plus_one <= ((OPJ_INT32)(cblk->numbps)) - 4) && (passtype < 2) &&
2285 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2286 /* FIXME: slviewer gets here with a null pointer. Why? Partially downloaded and/or corrupt textures? */
2287 if (seg->data == 00) {
2290 if (type == T1_TYPE_RAW) {
2291 opj_raw_init_dec(raw, (*seg->data) + seg->dataindex, seg->len);
2293 if (OPJ_FALSE == opj_mqc_init_dec(mqc, (*seg->data) + seg->dataindex,
2299 if (t1->w == 64 && t1->h == 64) {
2300 for (passno = 0; (passno < seg->real_num_passes) &&
2301 (bpno_plus_one >= 1); ++passno) {
2304 if (type == T1_TYPE_RAW) {
2305 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2307 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2308 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2310 opj_t1_dec_sigpass_mqc_64x64(t1, bpno_plus_one);
2315 if (type == T1_TYPE_RAW) {
2316 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2318 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2319 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2321 opj_t1_dec_refpass_mqc_64x64(t1, bpno_plus_one);
2326 opj_t1_dec_clnpass_64x64(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2330 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2331 opj_mqc_resetstates(mqc);
2332 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2333 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2334 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2336 if (++passtype == 3) {
2342 for (passno = 0; (passno < seg->real_num_passes) &&
2343 (bpno_plus_one >= 1); ++passno) {
2346 if (type == T1_TYPE_RAW) {
2347 opj_t1_dec_sigpass_raw(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2349 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2350 opj_t1_dec_sigpass_mqc_vsc(t1, bpno_plus_one);
2352 opj_t1_dec_sigpass_mqc_generic(t1, bpno_plus_one);
2357 if (type == T1_TYPE_RAW) {
2358 opj_t1_dec_refpass_raw(t1, bpno_plus_one);
2360 if (cblksty & J2K_CCP_CBLKSTY_VSC) {
2361 opj_t1_dec_refpass_mqc_vsc(t1, bpno_plus_one);
2363 opj_t1_dec_refpass_mqc_generic(t1, bpno_plus_one);
2368 opj_t1_dec_clnpass_generic(t1, bpno_plus_one, (OPJ_INT32)cblksty);
2372 if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
2373 opj_mqc_resetstates(mqc);
2374 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2375 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2376 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2378 if (++passtype == 3) {
2391 OPJ_BOOL opj_t1_encode_cblks(opj_t1_t *t1,
2392 opj_tcd_tile_t *tile,
2394 const OPJ_FLOAT64 * mct_norms,
2395 OPJ_UINT32 mct_numcomps
2398 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
2400 tile->distotile = 0; /* fixed_quality */
2402 for (compno = 0; compno < tile->numcomps; ++compno) {
2403 opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
2404 opj_tccp_t* tccp = &tcp->tccps[compno];
2405 OPJ_UINT32 tile_w = (OPJ_UINT32)(tilec->x1 - tilec->x0);
2407 for (resno = 0; resno < tilec->numresolutions; ++resno) {
2408 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
2410 for (bandno = 0; bandno < res->numbands; ++bandno) {
2411 opj_tcd_band_t* OPJ_RESTRICT band = &res->bands[bandno];
2412 OPJ_INT32 bandconst = 8192 * 8192 / ((OPJ_INT32) floor(band->stepsize * 8192));
2414 for (precno = 0; precno < res->pw * res->ph; ++precno) {
2415 opj_tcd_precinct_t *prc = &band->precincts[precno];
2417 for (cblkno = 0; cblkno < prc->cw * prc->ch; ++cblkno) {
2418 opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
2419 OPJ_INT32* OPJ_RESTRICT tiledp;
2422 OPJ_UINT32 i, j, tileIndex = 0, tileLineAdvance;
2424 OPJ_INT32 x = cblk->x0 - band->x0;
2425 OPJ_INT32 y = cblk->y0 - band->y0;
2426 if (band->bandno & 1) {
2427 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2428 x += pres->x1 - pres->x0;
2430 if (band->bandno & 2) {
2431 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
2432 y += pres->y1 - pres->y0;
2435 if (!opj_t1_allocate_buffers(
2437 (OPJ_UINT32)(cblk->x1 - cblk->x0),
2438 (OPJ_UINT32)(cblk->y1 - cblk->y0))) {
2444 tileLineAdvance = tile_w - cblk_w;
2446 tiledp = &tilec->data[(OPJ_UINT32)y * tile_w + (OPJ_UINT32)x];
2448 t1->data_stride = tile_w;
2449 if (tccp->qmfbid == 1) {
2450 for (j = 0; j < cblk_h; ++j) {
2451 for (i = 0; i < cblk_w; ++i) {
2452 tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
2455 tileIndex += tileLineAdvance;
2457 } else { /* if (tccp->qmfbid == 0) */
2458 for (j = 0; j < cblk_h; ++j) {
2459 for (i = 0; i < cblk_w; ++i) {
2460 OPJ_INT32 tmp = tiledp[tileIndex];
2467 tileIndex += tileLineAdvance;
2476 tilec->numresolutions - 1 - resno,
2493 /** mod fixed_quality */
2494 static void opj_t1_encode_cblk(opj_t1_t *t1,
2495 opj_tcd_cblk_enc_t* cblk,
2500 OPJ_FLOAT64 stepsize,
2502 OPJ_UINT32 numcomps,
2503 opj_tcd_tile_t * tile,
2504 const OPJ_FLOAT64 * mct_norms,
2505 OPJ_UINT32 mct_numcomps)
2507 OPJ_FLOAT64 cumwmsedec = 0.0;
2509 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2513 OPJ_UINT32 passtype;
2514 OPJ_INT32 nmsedec = 0;
2517 OPJ_BYTE type = T1_TYPE_MQ;
2518 OPJ_FLOAT64 tempwmsedec;
2520 mqc->lut_ctxno_zc_orient = lut_ctxno_zc + orient * 256;
2523 for (i = 0; i < t1->w; ++i) {
2524 for (j = 0; j < t1->h; ++j) {
2525 OPJ_INT32 tmp = abs(t1->data[i + j * t1->data_stride]);
2526 max = opj_int_max(max, tmp);
2530 cblk->numbps = max ? (OPJ_UINT32)((opj_int_floorlog2(max) + 1) -
2531 T1_NMSEDEC_FRACBITS) : 0;
2533 bpno = (OPJ_INT32)(cblk->numbps - 1);
2536 opj_mqc_resetstates(mqc);
2537 opj_mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
2538 opj_mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
2539 opj_mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
2540 opj_mqc_init_enc(mqc, cblk->data);
2542 for (passno = 0; bpno >= 0; ++passno) {
2543 opj_tcd_pass_t *pass = &cblk->passes[passno];
2544 OPJ_UINT32 correction = 3;
2545 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2546 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2550 opj_t1_enc_sigpass(t1, bpno, orient, &nmsedec, type, cblksty);
2553 opj_t1_enc_refpass(t1, bpno, &nmsedec, type, cblksty);
2556 opj_t1_enc_clnpass(t1, bpno, orient, &nmsedec, cblksty);
2557 /* code switch SEGMARK (i.e. SEGSYM) */
2558 if (cblksty & J2K_CCP_CBLKSTY_SEGSYM) {
2559 opj_mqc_segmark_enc(mqc);
2565 tempwmsedec = opj_t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
2566 stepsize, numcomps, mct_norms, mct_numcomps) ;
2567 cumwmsedec += tempwmsedec;
2568 tile->distotile += tempwmsedec;
2570 /* Code switch "RESTART" (i.e. TERMALL) */
2571 if ((cblksty & J2K_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
2573 if (type == T1_TYPE_RAW) {
2576 /* correction = mqc_bypass_flush_enc(); */
2577 } else { /* correction = mqc_restart_enc(); */
2583 if (((bpno < ((OPJ_INT32)(cblk->numbps) - 4) && (passtype > 0))
2584 || ((bpno == ((OPJ_INT32)cblk->numbps - 4)) && (passtype == 2))) &&
2585 (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2586 if (type == T1_TYPE_RAW) {
2589 /* correction = mqc_bypass_flush_enc(); */
2590 } else { /* correction = mqc_restart_enc(); */
2600 if (++passtype == 3) {
2605 if (pass->term && bpno > 0) {
2606 type = ((bpno < ((OPJ_INT32)(cblk->numbps) - 4)) && (passtype < 2) &&
2607 (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
2608 if (type == T1_TYPE_RAW) {
2609 opj_mqc_bypass_init_enc(mqc);
2611 opj_mqc_restart_init_enc(mqc);
2615 pass->distortiondec = cumwmsedec;
2616 pass->rate = opj_mqc_numbytes(mqc) + correction; /* FIXME */
2618 /* Code-switch "RESET" */
2619 if (cblksty & J2K_CCP_CBLKSTY_RESET) {
2620 opj_mqc_reset_enc(mqc);
2624 /* Code switch "ERTERM" (i.e. PTERM) */
2625 if (cblksty & J2K_CCP_CBLKSTY_PTERM) {
2626 opj_mqc_erterm_enc(mqc);
2627 } else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY)) {
2631 cblk->totalpasses = passno;
2633 for (passno = 0; passno < cblk->totalpasses; passno++) {
2634 opj_tcd_pass_t *pass = &cblk->passes[passno];
2635 if (pass->rate > opj_mqc_numbytes(mqc)) {
2636 pass->rate = opj_mqc_numbytes(mqc);
2638 /*Preventing generation of FF as last data byte of a pass*/
2639 if ((pass->rate > 1) && (cblk->data[pass->rate - 1] == 0xFF)) {
2642 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
2647 static void opj_t1_dec_refpass_step(opj_t1_t *t1,
2658 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2659 opj_raw_t *raw = t1->raw; /* RAW component */
2661 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2663 if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
2664 opj_mqc_setcurctx(mqc, opj_t1_getctxno_mag(flag)); /* ESSAI */
2665 if (type == T1_TYPE_RAW) {
2666 v = opj_raw_decode(raw);
2668 v = opj_mqc_decode(mqc);
2670 t = v ? poshalf : neghalf;
2671 *datap += *datap < 0 ? -t : t;
2672 *flagsp |= T1_REFINE;
2674 } /* VSC and BYPASS by Antonin */
2680 static void opj_t1_dec_sigpass_step(opj_t1_t *t1,
2684 OPJ_INT32 oneplushalf,
2690 opj_raw_t *raw = t1->raw; /* RAW component */
2691 opj_mqc_t *mqc = t1->mqc; /* MQC component */
2693 flag = vsc ? ((*flagsp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) :
2695 if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
2696 if (type == T1_TYPE_RAW) {
2697 if (opj_raw_decode(raw)) {
2698 v = opj_raw_decode(raw); /* ESSAI */
2699 *datap = v ? -oneplushalf : oneplushalf;
2700 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2703 opj_mqc_setcurctx(mqc, opj_t1_getctxno_zc(flag, orient));
2704 if (opj_mqc_decode(mqc)) {
2705 opj_mqc_setcurctx(mqc, opj_t1_getctxno_sc(flag));
2706 v = opj_mqc_decode(mqc) ^ opj_t1_getspb(flag);
2707 *datap = v ? -oneplushalf : oneplushalf;
2708 opj_t1_updateflags(flagsp, v, t1->flags_stride);
2711 *flagsp |= T1_VISIT;
2713 } /* VSC and BYPASS by Antonin */