0001: /*
0002: * $RCSfile: StdEntropyDecoder.java,v $
0003: * $Revision: 1.1 $
0004: * $Date: 2005/02/11 05:02:07 $
0005: * $State: Exp $
0006: *
0007: * Class: StdEntropyDecoder
0008: *
0009: * Description: Entropy decoding engine of stripes in code-blocks
0010: *
0011: *
0012: *
0013: * COPYRIGHT:
0014: *
0015: * This software module was originally developed by Raphaël Grosbois and
0016: * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
0017: * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
0018: * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
0019: * Centre France S.A) in the course of development of the JPEG2000
0020: * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
0021: * software module is an implementation of a part of the JPEG 2000
0022: * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
0023: * Systems AB and Canon Research Centre France S.A (collectively JJ2000
0024: * Partners) agree not to assert against ISO/IEC and users of the JPEG
0025: * 2000 Standard (Users) any of their rights under the copyright, not
0026: * including other intellectual property rights, for this software module
0027: * with respect to the usage by ISO/IEC and Users of this software module
0028: * or modifications thereof for use in hardware or software products
0029: * claiming conformance to the JPEG 2000 Standard. Those intending to use
0030: * this software module in hardware or software products are advised that
0031: * their use may infringe existing patents. The original developers of
0032: * this software module, JJ2000 Partners and ISO/IEC assume no liability
0033: * for use of this software module or modifications thereof. No license
0034: * or right to this software module is granted for non JPEG 2000 Standard
0035: * conforming products. JJ2000 Partners have full right to use this
0036: * software module for his/her own purpose, assign or donate this
0037: * software module to any third party and to inhibit third parties from
0038: * using this software module for non JPEG 2000 Standard conforming
0039: * products. This copyright notice must be included in all copies or
0040: * derivative works of this software module.
0041: *
0042: * Copyright (c) 1999/2000 JJ2000 Partners.
0043: * */
0044: package jj2000.j2k.entropy.decoder;
0045:
0046: import java.awt.Point;
0047:
0048: import jj2000.j2k.wavelet.synthesis.*;
0049: import jj2000.j2k.wavelet.*;
0050: import jj2000.j2k.entropy.*;
0051: import jj2000.j2k.decoder.*;
0052: import jj2000.j2k.image.*;
0053: import jj2000.j2k.util.*;
0054: import jj2000.j2k.io.*;
0055: import jj2000.j2k.*;
0056:
0057: /**
0058: * This class implements the JPEG 2000 entropy decoder, which codes stripes in
0059: * code-blocks. This entropy decoding engine decodes one code-block at a time.
0060: *
0061: * The code-block are rectangular, with dimensions which must be powers of
0062: * 2. Each dimension has to be no smaller than 4 and no larger than 256. The
0063: * product of the two dimensions (i.e. area of the code-block) may not exceed
0064: * 4096.
0065: *
0066: * Context 0 of the MQ-coder is used as the uniform one (uniform, non-adaptive
0067: * probability distribution). Context 1 is used for RLC coding. Contexts 2-10
0068: * are used for zero-coding (ZC), contexts 11-15 are used for sign-coding (SC)
0069: * and contexts 16-18 are used for magnitude-refinement (MR).
0070: *
0071: * <P>This implementation also provides some timing features. They can be
0072: * enabled by setting the 'DO_TIMING' constant of this class to true and
0073: * recompiling. The timing uses the 'System.currentTimeMillis()' Java API
0074: * call, which returns wall clock time, not the actual CPU time used. The
0075: * timing results will be printed on the message output. Since the times
0076: * reported are wall clock times and not CPU usage times they can not be added
0077: * to find the total used time (i.e. some time might be counted in several
0078: * places). When timing is disabled ('DO_TIMING' is false) there is no penalty
0079: * if the compiler performs some basic optimizations. Even if not the penalty
0080: * should be negligeable.
0081: * */
0082: public class StdEntropyDecoder extends EntropyDecoder implements
0083: StdEntropyCoderOptions {
0084:
0085: /** Whether to collect timing information or not: false. Used as a compile
0086: * time directive. */
0087: private final static boolean DO_TIMING = false;
0088:
0089: /** The cumulative wall time for the entropy coding engine, for each
0090: * component. */
0091: private long time[];
0092:
0093: /** The bit based input for arithmetic coding bypass (i.e. raw) coding */
0094: private ByteToBitInput bin;
0095:
0096: /** The MQ decoder to use. It has in as the underlying source of coded
0097: * data. */
0098: private MQDecoder mq;
0099:
0100: /** The decoder spec */
0101: private DecoderSpecs decSpec;
0102:
0103: /** The options that are turned on, as flag bits. The options are
0104: * 'OPT_TERM_PASS', 'OPT_RESET_MQ', 'OPT_VERT_STR_CAUSAL', 'OPT_BYPASS' and
0105: * 'OPT_SEG_SYMBOLS' as defined in the StdEntropyCoderOptions interface
0106: *
0107: * @see StdEntropyCoderOptions
0108: **/
0109: private int options;
0110:
0111: /** Flag to indicate if we should try to detect errors or just ignore any
0112: * error resilient information */
0113: private final boolean doer;
0114:
0115: /** Flag to indicate if we should be verbose about bit stream errors
0116: detected with the error resilience options */
0117: private final boolean verber;
0118:
0119: /** Number of bits used for the Zero Coding lookup table */
0120: private static final int ZC_LUT_BITS = 8;
0121:
0122: /** Zero Coding context lookup tables for the LH global orientation */
0123: private static final int ZC_LUT_LH[] = new int[1 << ZC_LUT_BITS];
0124:
0125: /** Zero Coding context lookup tables for the HL global orientation */
0126: private static final int ZC_LUT_HL[] = new int[1 << ZC_LUT_BITS];
0127:
0128: /** Zero Coding context lookup tables for the HH global orientation */
0129: private static final int ZC_LUT_HH[] = new int[1 << ZC_LUT_BITS];
0130:
0131: /** Number of bits used for the Sign Coding lookup table */
0132: private static final int SC_LUT_BITS = 9;
0133:
0134: /** Sign Coding context lookup table. The index into the table is a 9 bit
0135: * index, which correspond the the value in the 'state' array shifted by
0136: * 'SC_SHIFT'. Bits 8-5 are the signs of the horizontal-left,
0137: * horizontal-right, vertical-up and vertical-down neighbors,
0138: * respectively. Bit 4 is not used (0 or 1 makes no difference). Bits 3-0
0139: * are the significance of the horizontal-left, horizontal-right,
0140: * vertical-up and vertical-down neighbors, respectively. The least 4 bits
0141: * of the value in the lookup table define the context number and the sign
0142: * bit defines the "sign predictor". */
0143: private static final int SC_LUT[] = new int[1 << SC_LUT_BITS];
0144:
0145: /** The mask to obtain the context index from the 'SC_LUT' */
0146: private static final int SC_LUT_MASK = (1 << 4) - 1;
0147:
0148: /** The shift to obtain the sign predictor from the 'SC_LUT'. It must be
0149: * an unsigned shift. */
0150: private static final int SC_SPRED_SHIFT = 31;
0151:
0152: /** The sign bit for int data */
0153: private static final int INT_SIGN_BIT = 1 << 31;
0154:
0155: /** The number of bits used for the Magnitude Refinement lookup table */
0156: private static final int MR_LUT_BITS = 9;
0157:
0158: /** Magnitude Refinement context lookup table */
0159: private static final int MR_LUT[] = new int[1 << MR_LUT_BITS];
0160:
0161: /** The number of contexts used */
0162: private static final int NUM_CTXTS = 19;
0163:
0164: /** The RLC context */
0165: private static final int RLC_CTXT = 1;
0166:
0167: /** The UNIFORM context (with a uniform probability distribution which
0168: * does not adapt) */
0169: private static final int UNIF_CTXT = 0;
0170:
0171: /** The initial states for the MQ coder */
0172: private static final int MQ_INIT[] = { 46, 3, 4, 0, 0, 0, 0, 0, 0,
0173: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
0174:
0175: /** The 4 symbol segmentation marker (decimal 10, which is binary sequence
0176: 1010) */
0177: private static final int SEG_SYMBOL = 10;
0178:
0179: /**
0180: * The state array for entropy coding. Each element of the state array
0181: * stores the state of two coefficients. The lower 16 bits store the state
0182: * of a coefficient in row 'i' and column 'j', while the upper 16 bits
0183: * store the state of a coefficient in row 'i+1' and column 'j'. The 'i'
0184: * row is either the first or the third row of a stripe. This packing of
0185: * the states into 32 bit words allows a faster scan of all coefficients
0186: * on each coding pass and diminished the amount of data transferred. The
0187: * size of the state array is increased by 1 on each side (top, bottom,
0188: * left, right) to handle boundary conditions without any special logic.
0189: *
0190: * <P>The state of a coefficient is stored in the following way in the
0191: * lower 16 bits, where bit 0 is the least significant bit. Bit 15 is the
0192: * significance of a coefficient (0 if non-significant, 1 otherwise). Bit
0193: * 14 is the visited state (i.e. if a coefficient has been coded in the
0194: * significance propagation pass of the current bit-plane). Bit 13 is the
0195: * "non zero-context" state (i.e. if one of the eight immediate neighbors
0196: * is significant it is 1, otherwise is 0). Bits 12 to 9 store the sign of
0197: * the already significant left, right, up and down neighbors (1 for
0198: * negative, 0 for positive or not yet significant). Bit 8 indicates if
0199: * the magnitude refinement has already been applied to the
0200: * coefficient. Bits 7 to 4 store the significance of the left, right, up
0201: * and down neighbors (1 for significant, 0 for non significant). Bits 3
0202: * to 0 store the significance of the diagonal coefficients (up-left,
0203: * up-right, down-left and down-right; 1 for significant, 0 for non
0204: * significant).
0205: *
0206: * <P>The upper 16 bits the state is stored as in the lower 16 bits,
0207: * but with the bits shifted up by 16.
0208: *
0209: * <P>The lower 16 bits are referred to as "row 1" ("R1") while the upper
0210: * 16 bits are referred to as "row 2" ("R2").
0211: * */
0212: private final int state[];
0213:
0214: /** The separation between the upper and lower bits in the state array: 16
0215: * */
0216: private static final int STATE_SEP = 16;
0217:
0218: /** The flag bit for the significance in the state array, for row 1. */
0219: private static final int STATE_SIG_R1 = 1 << 15;
0220:
0221: /** The flag bit for the "visited" bit in the state array, for row 1. */
0222: private static final int STATE_VISITED_R1 = 1 << 14;
0223:
0224: /** The flag bit for the "not zero context" bit in the state array, for
0225: * row 1. This bit is always the OR of bits STATE_H_L_R1, STATE_H_R_R1,
0226: * STATE_V_U_R1, STATE_V_D_R1, STATE_D_UL_R1, STATE_D_UR_R1, STATE_D_DL_R1
0227: * and STATE_D_DR_R1. */
0228: private static final int STATE_NZ_CTXT_R1 = 1 << 13;
0229:
0230: /** The flag bit for the horizontal-left sign in the state array, for row
0231: * 1. This bit can only be set if the STATE_H_L_R1 is also set. */
0232: private static final int STATE_H_L_SIGN_R1 = 1 << 12;
0233:
0234: /** The flag bit for the horizontal-right sign in the state array, for
0235: * row 1. This bit can only be set if the STATE_H_R_R1 is also set. */
0236: private static final int STATE_H_R_SIGN_R1 = 1 << 11;
0237:
0238: /** The flag bit for the vertical-up sign in the state array, for row
0239: * 1. This bit can only be set if the STATE_V_U_R1 is also set. */
0240: private static final int STATE_V_U_SIGN_R1 = 1 << 10;
0241:
0242: /** The flag bit for the vertical-down sign in the state array, for row
0243: * 1. This bit can only be set if the STATE_V_D_R1 is also set. */
0244: private static final int STATE_V_D_SIGN_R1 = 1 << 9;
0245:
0246: /** The flag bit for the previous MR primitive applied in the state array,
0247: for row 1. */
0248: private static final int STATE_PREV_MR_R1 = 1 << 8;
0249:
0250: /** The flag bit for the horizontal-left significance in the state array,
0251: for row 1. */
0252: private static final int STATE_H_L_R1 = 1 << 7;
0253:
0254: /** The flag bit for the horizontal-right significance in the state array,
0255: for row 1. */
0256: private static final int STATE_H_R_R1 = 1 << 6;
0257:
0258: /** The flag bit for the vertical-up significance in the state array, for
0259: row 1. */
0260: private static final int STATE_V_U_R1 = 1 << 5;
0261:
0262: /** The flag bit for the vertical-down significance in the state array,
0263: for row 1. */
0264: private static final int STATE_V_D_R1 = 1 << 4;
0265:
0266: /** The flag bit for the diagonal up-left significance in the state array,
0267: for row 1. */
0268: private static final int STATE_D_UL_R1 = 1 << 3;
0269:
0270: /** The flag bit for the diagonal up-right significance in the state
0271: array, for row 1.*/
0272: private static final int STATE_D_UR_R1 = 1 << 2;
0273:
0274: /** The flag bit for the diagonal down-left significance in the state
0275: array, for row 1. */
0276: private static final int STATE_D_DL_R1 = 1 << 1;
0277:
0278: /** The flag bit for the diagonal down-right significance in the state
0279: array , for row 1.*/
0280: private static final int STATE_D_DR_R1 = 1;
0281:
0282: /** The flag bit for the significance in the state array, for row 2. */
0283: private static final int STATE_SIG_R2 = STATE_SIG_R1 << STATE_SEP;
0284:
0285: /** The flag bit for the "visited" bit in the state array, for row 2. */
0286: private static final int STATE_VISITED_R2 = STATE_VISITED_R1 << STATE_SEP;
0287:
0288: /** The flag bit for the "not zero context" bit in the state array, for
0289: * row 2. This bit is always the OR of bits STATE_H_L_R2, STATE_H_R_R2,
0290: * STATE_V_U_R2, STATE_V_D_R2, STATE_D_UL_R2, STATE_D_UR_R2, STATE_D_DL_R2
0291: * and STATE_D_DR_R2. */
0292: private static final int STATE_NZ_CTXT_R2 = STATE_NZ_CTXT_R1 << STATE_SEP;
0293:
0294: /** The flag bit for the horizontal-left sign in the state array, for row
0295: * 2. This bit can only be set if the STATE_H_L_R2 is also set. */
0296: private static final int STATE_H_L_SIGN_R2 = STATE_H_L_SIGN_R1 << STATE_SEP;
0297:
0298: /** The flag bit for the horizontal-right sign in the state array, for
0299: * row 2. This bit can only be set if the STATE_H_R_R2 is also set. */
0300: private static final int STATE_H_R_SIGN_R2 = STATE_H_R_SIGN_R1 << STATE_SEP;
0301:
0302: /** The flag bit for the vertical-up sign in the state array, for row
0303: * 2. This bit can only be set if the STATE_V_U_R2 is also set. */
0304: private static final int STATE_V_U_SIGN_R2 = STATE_V_U_SIGN_R1 << STATE_SEP;
0305:
0306: /** The flag bit for the vertical-down sign in the state array, for row
0307: * 2. This bit can only be set if the STATE_V_D_R2 is also set. */
0308: private static final int STATE_V_D_SIGN_R2 = STATE_V_D_SIGN_R1 << STATE_SEP;
0309:
0310: /** The flag bit for the previous MR primitive applied in the state array,
0311: for row 2. */
0312: private static final int STATE_PREV_MR_R2 = STATE_PREV_MR_R1 << STATE_SEP;
0313:
0314: /** The flag bit for the horizontal-left significance in the state array,
0315: for row 2. */
0316: private static final int STATE_H_L_R2 = STATE_H_L_R1 << STATE_SEP;
0317:
0318: /** The flag bit for the horizontal-right significance in the state array,
0319: for row 2. */
0320: private static final int STATE_H_R_R2 = STATE_H_R_R1 << STATE_SEP;
0321:
0322: /** The flag bit for the vertical-up significance in the state array, for
0323: row 2. */
0324: private static final int STATE_V_U_R2 = STATE_V_U_R1 << STATE_SEP;
0325:
0326: /** The flag bit for the vertical-down significance in the state array,
0327: for row 2. */
0328: private static final int STATE_V_D_R2 = STATE_V_D_R1 << STATE_SEP;
0329:
0330: /** The flag bit for the diagonal up-left significance in the state array,
0331: for row 2. */
0332: private static final int STATE_D_UL_R2 = STATE_D_UL_R1 << STATE_SEP;
0333:
0334: /** The flag bit for the diagonal up-right significance in the state
0335: array, for row 2.*/
0336: private static final int STATE_D_UR_R2 = STATE_D_UR_R1 << STATE_SEP;
0337:
0338: /** The flag bit for the diagonal down-left significance in the state
0339: array, for row 2. */
0340: private static final int STATE_D_DL_R2 = STATE_D_DL_R1 << STATE_SEP;
0341:
0342: /** The flag bit for the diagonal down-right significance in the state
0343: array , for row 2.*/
0344: private static final int STATE_D_DR_R2 = STATE_D_DR_R1 << STATE_SEP;
0345:
0346: /** The mask to isolate the significance bits for row 1 and 2 of the state
0347: * array. */
0348: private static final int SIG_MASK_R1R2 = STATE_SIG_R1
0349: | STATE_SIG_R2;
0350:
0351: /** The mask to isolate the visited bits for row 1 and 2 of the state
0352: * array. */
0353: private static final int VSTD_MASK_R1R2 = STATE_VISITED_R1
0354: | STATE_VISITED_R2;
0355:
0356: /** The mask to isolate the bits necessary to identify RLC coding state
0357: * (significant, visited and non-zero context, for row 1 and 2). */
0358: private static final int RLC_MASK_R1R2 = STATE_SIG_R1
0359: | STATE_SIG_R2 | STATE_VISITED_R1 | STATE_VISITED_R2
0360: | STATE_NZ_CTXT_R1 | STATE_NZ_CTXT_R2;
0361:
0362: /** The mask to obtain the ZC_LUT index from the 'state' information */
0363: // This is needed because of the STATE_V_D_SIGN, STATE_V_U_SIGN,
0364: // STATE_H_R_SIGN, and STATE_H_L_SIGN bits.
0365: private static final int ZC_MASK = (1 << 8) - 1;
0366:
0367: /** The shift to obtain the SC index to 'SC_LUT' from the 'state'
0368: * information, for row 1. */
0369: private static final int SC_SHIFT_R1 = 4;
0370:
0371: /** The shift to obtain the SC index to 'SC_LUT' from the state
0372: * information, for row 2. */
0373: private static final int SC_SHIFT_R2 = SC_SHIFT_R1 + STATE_SEP;
0374:
0375: /** The bit mask to isolate the state bits relative to the sign coding
0376: * lookup table ('SC_LUT'). */
0377: private static final int SC_MASK = (1 << SC_LUT_BITS) - 1;
0378:
0379: /** The mask to obtain the MR index to 'MR_LUT' from the 'state'
0380: * information. It is to be applied after the 'MR_SHIFT' */
0381: private static final int MR_MASK = (1 << 9) - 1;
0382:
0383: /** The source code-block to entropy code (avoids reallocation for each
0384: code-block). */
0385: private DecLyrdCBlk srcblk;
0386:
0387: /** The maximum number of bit planes to decode for any code-block */
0388: private int mQuit;
0389:
0390: /** Static initializer: initializes all the lookup tables. */
0391: static {
0392: int i, j;
0393: double val, deltaMSE;
0394: int inter_sc_lut[];
0395: int ds, us, rs, ls;
0396: int dsgn, usgn, rsgn, lsgn;
0397: int h, v;
0398:
0399: // Initialize the zero coding lookup tables
0400:
0401: // LH
0402:
0403: // - No neighbors significant
0404: ZC_LUT_LH[0] = 2;
0405: // - No horizontal or vertical neighbors significant
0406: for (i = 1; i < 16; i++) { // Two or more diagonal coeffs significant
0407: ZC_LUT_LH[i] = 4;
0408: }
0409: for (i = 0; i < 4; i++) { // Only one diagonal coeff significant
0410: ZC_LUT_LH[1 << i] = 3;
0411: }
0412: // - No horizontal neighbors significant, diagonal irrelevant
0413: for (i = 0; i < 16; i++) {
0414: // Only one vertical coeff significant
0415: ZC_LUT_LH[STATE_V_U_R1 | i] = 5;
0416: ZC_LUT_LH[STATE_V_D_R1 | i] = 5;
0417: // The two vertical coeffs significant
0418: ZC_LUT_LH[STATE_V_U_R1 | STATE_V_D_R1 | i] = 6;
0419: }
0420: // - One horiz. neighbor significant, diagonal/vertical non-significant
0421: ZC_LUT_LH[STATE_H_L_R1] = 7;
0422: ZC_LUT_LH[STATE_H_R_R1] = 7;
0423: // - One horiz. significant, no vertical significant, one or more
0424: // diagonal significant
0425: for (i = 1; i < 16; i++) {
0426: ZC_LUT_LH[STATE_H_L_R1 | i] = 8;
0427: ZC_LUT_LH[STATE_H_R_R1 | i] = 8;
0428: }
0429: // - One horiz. significant, one or more vertical significant,
0430: // diagonal irrelevant
0431: for (i = 1; i < 4; i++) {
0432: for (j = 0; j < 16; j++) {
0433: ZC_LUT_LH[STATE_H_L_R1 | (i << 4) | j] = 9;
0434: ZC_LUT_LH[STATE_H_R_R1 | (i << 4) | j] = 9;
0435: }
0436: }
0437: // - Two horiz. significant, others irrelevant
0438: for (i = 0; i < 64; i++) {
0439: ZC_LUT_LH[STATE_H_L_R1 | STATE_H_R_R1 | i] = 10;
0440: }
0441:
0442: // HL
0443:
0444: // - No neighbors significant
0445: ZC_LUT_HL[0] = 2;
0446: // - No horizontal or vertical neighbors significant
0447: for (i = 1; i < 16; i++) { // Two or more diagonal coeffs significant
0448: ZC_LUT_HL[i] = 4;
0449: }
0450: for (i = 0; i < 4; i++) { // Only one diagonal coeff significant
0451: ZC_LUT_HL[1 << i] = 3;
0452: }
0453: // - No vertical significant, diagonal irrelevant
0454: for (i = 0; i < 16; i++) {
0455: // One horiz. significant
0456: ZC_LUT_HL[STATE_H_L_R1 | i] = 5;
0457: ZC_LUT_HL[STATE_H_R_R1 | i] = 5;
0458: // Two horiz. significant
0459: ZC_LUT_HL[STATE_H_L_R1 | STATE_H_R_R1 | i] = 6;
0460: }
0461: // - One vert. significant, diagonal/horizontal non-significant
0462: ZC_LUT_HL[STATE_V_U_R1] = 7;
0463: ZC_LUT_HL[STATE_V_D_R1] = 7;
0464: // - One vert. significant, horizontal non-significant, one or more
0465: // diag. significant
0466: for (i = 1; i < 16; i++) {
0467: ZC_LUT_HL[STATE_V_U_R1 | i] = 8;
0468: ZC_LUT_HL[STATE_V_D_R1 | i] = 8;
0469: }
0470: // - One vertical significant, one or more horizontal significant,
0471: // diagonal irrelevant
0472: for (i = 1; i < 4; i++) {
0473: for (j = 0; j < 16; j++) {
0474: ZC_LUT_HL[(i << 6) | STATE_V_U_R1 | j] = 9;
0475: ZC_LUT_HL[(i << 6) | STATE_V_D_R1 | j] = 9;
0476: }
0477: }
0478: // - Two vertical significant, others irrelevant
0479: for (i = 0; i < 4; i++) {
0480: for (j = 0; j < 16; j++) {
0481: ZC_LUT_HL[(i << 6) | STATE_V_U_R1 | STATE_V_D_R1 | j] = 10;
0482: }
0483: }
0484:
0485: // HH
0486: int[] twoBits = { 3, 5, 6, 9, 10, 12 }; // Figures (between 0 and 15)
0487: // countaning 2 and only 2 bits on in its binary representation.
0488:
0489: int[] oneBit = { 1, 2, 4, 8 }; // Figures (between 0 and 15)
0490: // countaning 1 and only 1 bit on in its binary representation.
0491:
0492: int[] twoLeast = { 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15 }; // Figures
0493: // (between 0 and 15) countaining, at least, 2 bits on in its
0494: // binary representation.
0495:
0496: int[] threeLeast = { 7, 11, 13, 14, 15 }; // Figures
0497: // (between 0 and 15) countaining, at least, 3 bits on in its
0498: // binary representation.
0499:
0500: // - None significant
0501: ZC_LUT_HH[0] = 2;
0502:
0503: // - One horizontal+vertical significant, none diagonal
0504: for (i = 0; i < oneBit.length; i++)
0505: ZC_LUT_HH[oneBit[i] << 4] = 3;
0506:
0507: // - Two or more horizontal+vertical significant, diagonal non-signif
0508: for (i = 0; i < twoLeast.length; i++)
0509: ZC_LUT_HH[twoLeast[i] << 4] = 4;
0510:
0511: // - One diagonal significant, horiz./vert. non-significant
0512: for (i = 0; i < oneBit.length; i++)
0513: ZC_LUT_HH[oneBit[i]] = 5;
0514:
0515: // - One diagonal significant, one horiz.+vert. significant
0516: for (i = 0; i < oneBit.length; i++)
0517: for (j = 0; j < oneBit.length; j++)
0518: ZC_LUT_HH[(oneBit[i] << 4) | oneBit[j]] = 6;
0519:
0520: // - One diag signif, two or more horiz+vert signif
0521: for (i = 0; i < twoLeast.length; i++)
0522: for (j = 0; j < oneBit.length; j++)
0523: ZC_LUT_HH[(twoLeast[i] << 4) | oneBit[j]] = 7;
0524:
0525: // - Two diagonal significant, none horiz+vert significant
0526: for (i = 0; i < twoBits.length; i++)
0527: ZC_LUT_HH[twoBits[i]] = 8;
0528:
0529: // - Two diagonal significant, one or more horiz+vert significant
0530: for (j = 0; j < twoBits.length; j++)
0531: for (i = 1; i < 16; i++)
0532: ZC_LUT_HH[(i << 4) | twoBits[j]] = 9;
0533:
0534: // - Three or more diagonal significant, horiz+vert irrelevant
0535: for (i = 0; i < 16; i++)
0536: for (j = 0; j < threeLeast.length; j++)
0537: ZC_LUT_HH[(i << 4) | threeLeast[j]] = 10;
0538:
0539: // Initialize the SC lookup tables
0540:
0541: // Use an intermediate sign code lookup table that is similar to the
0542: // one in the VM text, in that it depends on the 'h' and 'v'
0543: // quantities. The index into this table is a 6 bit index, the top 3
0544: // bits are (h+1) and the low 3 bits (v+1).
0545: inter_sc_lut = new int[36];
0546: inter_sc_lut[(2 << 3) | 2] = 15;
0547: inter_sc_lut[(2 << 3) | 1] = 14;
0548: inter_sc_lut[(2 << 3) | 0] = 13;
0549: inter_sc_lut[(1 << 3) | 2] = 12;
0550: inter_sc_lut[(1 << 3) | 1] = 11;
0551: inter_sc_lut[(1 << 3) | 0] = 12 | INT_SIGN_BIT;
0552: inter_sc_lut[(0 << 3) | 2] = 13 | INT_SIGN_BIT;
0553: inter_sc_lut[(0 << 3) | 1] = 14 | INT_SIGN_BIT;
0554: inter_sc_lut[(0 << 3) | 0] = 15 | INT_SIGN_BIT;
0555:
0556: // Using the intermediate sign code lookup table create the final
0557: // one. The index into this table is a 9 bit index, the low 4 bits are
0558: // the significance of the 4 horizontal/vertical neighbors, while the
0559: // top 4 bits are the signs of those neighbors. The bit in the middle
0560: // is ignored. This index arrangement matches the state bits in the
0561: // 'state' array, thus direct addressing of the table can be done from
0562: // the sate information.
0563: for (i = 0; i < (1 << SC_LUT_BITS) - 1; i++) {
0564: ds = i & 0x01; // significance of down neighbor
0565: us = (i >> 1) & 0x01; // significance of up neighbor
0566: rs = (i >> 2) & 0x01; // significance of right neighbor
0567: ls = (i >> 3) & 0x01; // significance of left neighbor
0568: dsgn = (i >> 5) & 0x01; // sign of down neighbor
0569: usgn = (i >> 6) & 0x01; // sign of up neighbor
0570: rsgn = (i >> 7) & 0x01; // sign of right neighbor
0571: lsgn = (i >> 8) & 0x01; // sign of left neighbor
0572: // Calculate 'h' and 'v' as in VM text
0573: h = ls * (1 - 2 * lsgn) + rs * (1 - 2 * rsgn);
0574: h = (h >= -1) ? h : -1;
0575: h = (h <= 1) ? h : 1;
0576: v = us * (1 - 2 * usgn) + ds * (1 - 2 * dsgn);
0577: v = (v >= -1) ? v : -1;
0578: v = (v <= 1) ? v : 1;
0579: // Get context and sign predictor from 'inter_sc_lut'
0580: SC_LUT[i] = inter_sc_lut[(h + 1) << 3 | (v + 1)];
0581: }
0582: inter_sc_lut = null;
0583:
0584: // Initialize the MR lookup tables
0585:
0586: // None significant, prev MR off
0587: MR_LUT[0] = 16;
0588: // One or more significant, prev MR off
0589: for (i = 1; i < (1 << (MR_LUT_BITS - 1)); i++) {
0590: MR_LUT[i] = 17;
0591: }
0592: // Previous MR on, significance irrelevant
0593: for (; i < (1 << MR_LUT_BITS); i++) {
0594: MR_LUT[i] = 18;
0595: }
0596: }
0597:
0598: /**
0599: * Instantiates a new entropy decoder engine, with the specified source of
0600: * data, nominal block width and height.
0601: *
0602: * @param src The source of data
0603: *
0604: * @param opt The options to use for this encoder. It is a mix of the
0605: * 'OPT_TERM_PASS', 'OPT_RESET_MQ', 'OPT_VERT_STR_CAUSAL', 'OPT_BYPASS' and
0606: * 'OPT_SEG_SYMBOLS' option flags.
0607: *
0608: * @param doer If true error detection will be performed, if any error
0609: * detection features have been enabled.
0610: *
0611: * @param verber This flag indicates if the entropy decoder should be
0612: * verbose about bit stream errors that are detected and concealed.
0613: * */
0614: public StdEntropyDecoder(CodedCBlkDataSrcDec src,
0615: DecoderSpecs decSpec, boolean doer, boolean verber,
0616: int mQuit) {
0617: super (src);
0618:
0619: this .decSpec = decSpec;
0620: this .doer = doer;
0621: this .verber = verber;
0622: this .mQuit = mQuit;
0623:
0624: // If we do timing create necessary structures
0625: if (DO_TIMING) {
0626: time = new long[src.getNumComps()];
0627: // If we are timing make sure that 'finalize' gets called.
0628: System.runFinalizersOnExit(true);
0629: }
0630:
0631: // Initialize internal variables
0632: state = new int[(decSpec.cblks.getMaxCBlkWidth() + 2)
0633: * ((decSpec.cblks.getMaxCBlkHeight() + 1) / 2 + 2)];
0634: }
0635:
0636: /**
0637: * Prints the timing information, if collected, and calls 'finalize' on
0638: * the super class.
0639: * */
0640: public void finalize() throws Throwable {
0641: if (DO_TIMING) {
0642: int c;
0643: StringBuffer sb;
0644:
0645: sb = new StringBuffer(
0646: "StdEntropyDecoder decompression wall "
0647: + "clock time:");
0648: for (c = 0; c < time.length; c++) {
0649: sb.append("\n component ");
0650: sb.append(c);
0651: sb.append(": ");
0652: sb.append(time[c]);
0653: sb.append(" ms");
0654: }
0655: FacilityManager.getMsgLogger().printmsg(MsgLogger.INFO,
0656: sb.toString());
0657: }
0658: super .finalize();
0659: }
0660:
0661: /**
0662: * Returns the specified code-block in the current tile for the specified
0663: * component, as a copy (see below).
0664: *
0665: * <P>The returned code-block may be progressive, which is indicated by
0666: * the 'progressive' variable of the returned 'DataBlk' object. If a
0667: * code-block is progressive it means that in a later request to this
0668: * method for the same code-block it is possible to retrieve data which is
0669: * a better approximation, since meanwhile more data to decode for the
0670: * code-block could have been received. If the code-block is not
0671: * progressive then later calls to this method for the same code-block
0672: * will return the exact same data values.
0673: *
0674: * <P>The data returned by this method is always a copy of the internal
0675: * data of this object, if any, and it can be modified "in place" without
0676: * any problems after being returned. The 'offset' of the returned data is
0677: * 0, and the 'scanw' is the same as the code-block width. See the
0678: * 'DataBlk' class.
0679: *
0680: * <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object
0681: * contain the coordinates of the top-left corner of the block, with
0682: * respect to the tile, not the subband.
0683: *
0684: * @param c The component for which to return the next code-block.
0685: *
0686: * @param m The vertical index of the code-block to return, in the
0687: * specified subband.
0688: *
0689: * @param n The horizontal index of the code-block to return, in the
0690: * specified subband.
0691: *
0692: * @param sb The subband in which the code-block to return is.
0693: *
0694: * @param cblk If non-null this object will be used to return the new
0695: * code-block. If null a new one will be allocated and returned. If the
0696: * "data" array of the object is non-null it will be reused, if possible,
0697: * to return the data.
0698: *
0699: * @return The next code-block in the current tile for component 'n', or
0700: * null if all code-blocks for the current tile have been returned.
0701: *
0702: * @see DataBlk
0703: * */
0704: public DataBlk getCodeBlock(int c, int m, int n, SubbandSyn sb,
0705: DataBlk cblk) {
0706: long stime = 0L; // Start time for timed sections
0707: int zc_lut[]; // The ZC lookup table to use
0708: int out_data[]; // The outupt data buffer
0709: int npasses; // The number of coding passes to perform
0710: int curbp; // The current magnitude bit-plane (starts at 30)
0711: boolean error; // Error indicator
0712: int tslen; // Length of first terminated segment
0713: int tsidx; // Index of current terminated segment
0714: ByteInputBuffer in = null;
0715:
0716: boolean isterm;
0717:
0718: // Get the code-block to decode
0719: srcblk = src.getCodeBlock(c, m, n, sb, 1, -1, srcblk);
0720: if (DO_TIMING)
0721: stime = System.currentTimeMillis();
0722:
0723: // Retrieve options from decSpec
0724: options = ((Integer) decSpec.ecopts.getTileCompVal(tIdx, c))
0725: .intValue();
0726:
0727: // Reset state
0728: ArrayUtil.intArraySet(state, 0);
0729:
0730: // Initialize output code-block
0731: if (cblk == null) {
0732: cblk = new DataBlkInt();
0733: }
0734: cblk.progressive = srcblk.prog;
0735: cblk.ulx = srcblk.ulx;
0736: cblk.uly = srcblk.uly;
0737: cblk.w = srcblk.w;
0738: cblk.h = srcblk.h;
0739: cblk.offset = 0;
0740: cblk.scanw = cblk.w;
0741: out_data = (int[]) cblk.getData();
0742:
0743: if (out_data == null || out_data.length < srcblk.w * srcblk.h) {
0744: out_data = new int[srcblk.w * srcblk.h];
0745: cblk.setData(out_data);
0746: } else {
0747: // Set data values to 0
0748: ArrayUtil.intArraySet(out_data, 0);
0749: }
0750:
0751: if (srcblk.nl <= 0 || srcblk.nTrunc <= 0) {
0752: // 0 layers => no data to decode => return all 0s
0753: return cblk;
0754: }
0755:
0756: // Get the length of the first terminated segment
0757: tslen = (srcblk.tsLengths == null) ? srcblk.dl
0758: : srcblk.tsLengths[0];
0759: tsidx = 0;
0760: // Initialize for decoding
0761: npasses = srcblk.nTrunc;
0762: if (mq == null) {
0763: in = new ByteInputBuffer(srcblk.data, 0, tslen);
0764: mq = new MQDecoder(in, NUM_CTXTS, MQ_INIT);
0765: } else {
0766: // We always start by an MQ segment
0767: mq.nextSegment(srcblk.data, 0, tslen);
0768: mq.resetCtxts();
0769: }
0770: error = false;
0771:
0772: if ((options & OPT_BYPASS) != 0) {
0773: if (bin == null) {
0774: if (in == null)
0775: in = mq.getByteInputBuffer();
0776: bin = new ByteToBitInput(in);
0777: }
0778: }
0779:
0780: // Choose correct ZC lookup table for global orientation
0781: switch (sb.orientation) {
0782: case Subband.WT_ORIENT_HL:
0783: zc_lut = ZC_LUT_HL;
0784: break;
0785: case Subband.WT_ORIENT_LH:
0786: case Subband.WT_ORIENT_LL:
0787: zc_lut = ZC_LUT_LH;
0788: break;
0789: case Subband.WT_ORIENT_HH:
0790: zc_lut = ZC_LUT_HH;
0791: break;
0792: default:
0793: throw new Error("JJ2000 internal error");
0794: }
0795:
0796: // NOTE: we don't currently detect which is the last magnitude
0797: // bit-plane so that 'isterm' is true for the last pass of it. Doing so
0798: // would aid marginally in error detection with the predictable error
0799: // resilient MQ termination. However, determining which is the last
0800: // magnitude bit-plane is quite hard (due to ROI, quantization, etc.)
0801: // and in any case the predictable error resilient termination used
0802: // without the arithmetic coding bypass and/or regular termination
0803: // modes is almost useless.
0804:
0805: // Loop on bit-planes and passes
0806:
0807: curbp = 30 - srcblk.skipMSBP;
0808:
0809: // Check for maximum number of bitplanes quit condition
0810: if (mQuit != -1 && (mQuit * 3 - 2) < npasses) {
0811: npasses = mQuit * 3 - 2;
0812: }
0813:
0814: // First bit-plane has only the cleanup pass
0815: if (curbp >= 0 && npasses > 0) {
0816: isterm = (options & OPT_TERM_PASS) != 0
0817: || ((options & OPT_BYPASS) != 0 && (31 - NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP) >= curbp);
0818: error = cleanuppass(cblk, mq, curbp, state, zc_lut, isterm);
0819: npasses--;
0820: if (!error || !doer)
0821: curbp--;
0822: }
0823:
0824: // Other bit-planes have the three coding passes
0825: if (!error || !doer) {
0826: while (curbp >= 0 && npasses > 0) {
0827:
0828: if ((options & OPT_BYPASS) != 0
0829: && (curbp < 31 - NUM_NON_BYPASS_MS_BP
0830: - srcblk.skipMSBP)) {
0831: // Use bypass decoding mode (only all bit-planes
0832: // after the first 4 bit-planes).
0833:
0834: // Here starts a new raw segment
0835: bin.setByteArray(null, -1,
0836: srcblk.tsLengths[++tsidx]);
0837: isterm = (options & OPT_TERM_PASS) != 0;
0838: error = rawSigProgPass(cblk, bin, curbp, state,
0839: isterm);
0840: npasses--;
0841: if (npasses <= 0 || (error && doer))
0842: break;
0843:
0844: if ((options & OPT_TERM_PASS) != 0) {
0845: // Start a new raw segment
0846: bin.setByteArray(null, -1,
0847: srcblk.tsLengths[++tsidx]);
0848: }
0849: isterm = (options & OPT_TERM_PASS) != 0
0850: || ((options & OPT_BYPASS) != 0 && (31
0851: - NUM_NON_BYPASS_MS_BP
0852: - srcblk.skipMSBP > curbp));
0853: error = rawMagRefPass(cblk, bin, curbp, state,
0854: isterm);
0855: } else {// Do not use bypass decoding mode
0856: if ((options & OPT_TERM_PASS) != 0) {
0857: // Here starts a new MQ segment
0858: mq.nextSegment(null, -1,
0859: srcblk.tsLengths[++tsidx]);
0860: }
0861: isterm = (options & OPT_TERM_PASS) != 0;
0862: error = sigProgPass(cblk, mq, curbp, state, zc_lut,
0863: isterm);
0864: npasses--;
0865: if (npasses <= 0 || (error && doer))
0866: break;
0867:
0868: if ((options & OPT_TERM_PASS) != 0) {
0869: // Here starts a new MQ segment
0870: mq.nextSegment(null, -1,
0871: srcblk.tsLengths[++tsidx]);
0872: }
0873: isterm = (options & OPT_TERM_PASS) != 0
0874: || ((options & OPT_BYPASS) != 0 && (31
0875: - NUM_NON_BYPASS_MS_BP
0876: - srcblk.skipMSBP > curbp));
0877: error = magRefPass(cblk, mq, curbp, state, isterm);
0878: }
0879:
0880: npasses--;
0881: if (npasses <= 0 || (error && doer))
0882: break;
0883:
0884: if ((options & OPT_TERM_PASS) != 0
0885: || ((options & OPT_BYPASS) != 0 && (curbp < 31
0886: - NUM_NON_BYPASS_MS_BP
0887: - srcblk.skipMSBP))) {
0888: // Here starts a new MQ segment
0889: mq.nextSegment(null, -1, srcblk.tsLengths[++tsidx]);
0890: }
0891: isterm = (options & OPT_TERM_PASS) != 0
0892: || ((options & OPT_BYPASS) != 0 && (31 - NUM_NON_BYPASS_MS_BP - srcblk.skipMSBP) >= curbp);
0893: error = cleanuppass(cblk, mq, curbp, state, zc_lut,
0894: isterm);
0895: npasses--;
0896: if (error)
0897: break;
0898: // Goto next bit-plane
0899: curbp--;
0900: }
0901: }
0902:
0903: // If an error ocurred conceal it
0904: if (error && doer) {
0905: if (verber) {
0906: FacilityManager.getMsgLogger().printmsg(
0907: MsgLogger.WARNING,
0908: "Error detected at bit-plane " + curbp
0909: + " in code-block (" + m + "," + n
0910: + "), sb_idx " + sb.sbandIdx
0911: + ", res. level " + sb.resLvl
0912: + ". Concealing...");
0913: }
0914:
0915: conceal(cblk, curbp);
0916: }
0917:
0918: if (DO_TIMING)
0919: time[c] += System.currentTimeMillis() - stime;
0920:
0921: // Return decoded block
0922: return cblk;
0923: }
0924:
0925: /**
0926: * Returns the specified code-block in the current tile for the specified
0927: * component (as a reference or copy).
0928: *
0929: * <P>The returned code-block may be progressive, which is indicated by
0930: * the 'progressive' variable of the returned 'DataBlk'
0931: * object. If a code-block is progressive it means that in a later request
0932: * to this method for the same code-block it is possible to retrieve data
0933: * which is a better approximation, since meanwhile more data to decode
0934: * for the code-block could have been received. If the code-block is not
0935: * progressive then later calls to this method for the same code-block
0936: * will return the exact same data values.
0937: *
0938: * <P>The data returned by this method can be the data in the internal
0939: * buffer of this object, if any, and thus can not be modified by the
0940: * caller. The 'offset' and 'scanw' of the returned data can be
0941: * arbitrary. See the 'DataBlk' class.
0942: *
0943: * <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object
0944: * contain the coordinates of the top-left corner of the block, with
0945: * respect to the tile, not the subband.
0946: *
0947: * @param c The component for which to return the next code-block.
0948: *
0949: * @param m The vertical index of the code-block to return, in the
0950: * specified subband.
0951: *
0952: * @param n The horizontal index of the code-block to return, in the
0953: * specified subband.
0954: *
0955: * @param sb The subband in which the code-block to return is.
0956: *
0957: * @param cblk If non-null this object will be used to return the new
0958: * code-block. If null a new one will be allocated and returned. If the
0959: * "data" array of the object is non-null it will be reused, if possible,
0960: * to return the data.
0961: *
0962: * @return The next code-block in the current tile for component 'n', or
0963: * null if all code-blocks for the current tile have been returned.
0964: *
0965: * @see DataBlk
0966: * */
0967: public DataBlk getInternCodeBlock(int c, int m, int n,
0968: SubbandSyn sb, DataBlk cblk) {
0969: return getCodeBlock(c, m, n, sb, cblk);
0970: }
0971:
0972: /**
0973: * Performs the significance propagation pass on the specified data and
0974: * bit-plane. It decodes all insignificant samples which have, at least,
0975: * one of its immediate eight neighbors already significant, using the ZC
0976: * and SC primitives as needed. It toggles the "visited" state bit to 1
0977: * for all those samples.
0978: *
0979: * <P>This method also checks for segmentation markers if those are
0980: * present and returns true if an error is detected, or false
0981: * otherwise. If an error is detected it means that the bit stream contains
0982: * some erroneous bit that have led to the decoding of incorrect
0983: * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If
0984: * 'true' is returned the 'conceal' method should be called and no more
0985: * passes should be decoded for this code-block's bit stream.
0986: *
0987: * @param cblk The code-block data to decode
0988: *
0989: * @param mq The MQ-coder to use
0990: *
0991: * @param bp The bit-plane to decode
0992: *
0993: * @param state The state information for the code-block
0994: *
0995: * @param zc_lut The ZC lookup table to use in ZC.
0996: *
0997: * @param isterm If this pass has been terminated. If the pass has been
0998: * terminated it can be used to check error resilience.
0999: *
1000: * @return True if an error was detected in the bit stream, false otherwise.
1001: * */
1002: private boolean sigProgPass(DataBlk cblk, MQDecoder mq, int bp,
1003: int state[], int zc_lut[], boolean isterm) {
1004: int j, sj; // The state index for line and stripe
1005: int k, sk; // The data index for line and stripe
1006: int dscanw; // The data scan-width
1007: int sscanw; // The state scan-width
1008: int jstep; // Stripe to stripe step for 'sj'
1009: int kstep; // Stripe to stripe step for 'sk'
1010: int stopsk; // The loop limit on the variable sk
1011: int csj; // Local copy (i.e. cached) of 'state[j]'
1012: int setmask; // The mask to set current and lower bit-planes to 1/2
1013: // approximation
1014: int sym; // The symbol to code
1015: int ctxt; // The context to use
1016: int data[]; // The data buffer
1017: int s; // The stripe index
1018: boolean causal; // Flag to indicate if stripe-causal context
1019: // formation is to be used
1020: int nstripes; // The number of stripes in the code-block
1021: int sheight; // Height of the current stripe
1022: int off_ul, off_ur, off_dr, off_dl; // offsets
1023: boolean error; // The error condition
1024:
1025: // Initialize local variables
1026: dscanw = cblk.scanw;
1027: sscanw = cblk.w + 2;
1028: jstep = sscanw * STRIPE_HEIGHT / 2 - cblk.w;
1029: kstep = dscanw * STRIPE_HEIGHT - cblk.w;
1030: setmask = (3 << bp) >> 1;
1031: data = (int[]) cblk.getData();
1032: nstripes = (cblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
1033: causal = (options & OPT_VERT_STR_CAUSAL) != 0;
1034:
1035: // Pre-calculate offsets in 'state' for diagonal neighbors
1036: off_ul = -sscanw - 1; // up-left
1037: off_ur = -sscanw + 1; // up-right
1038: off_dr = sscanw + 1; // down-right
1039: off_dl = sscanw - 1; // down-left
1040:
1041: // Decode stripe by stripe
1042: sk = cblk.offset;
1043: sj = sscanw + 1;
1044: for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
1045: sheight = (s != 0) ? STRIPE_HEIGHT : cblk.h
1046: - (nstripes - 1) * STRIPE_HEIGHT;
1047: stopsk = sk + cblk.w;
1048: // Scan by set of 1 stripe column at a time
1049: for (; sk < stopsk; sk++, sj++) {
1050: // Do half top of column
1051: j = sj;
1052: csj = state[j];
1053: // If any of the two samples is not significant and has a
1054: // non-zero context (i.e. some neighbor is significant) we can
1055: // not skip them
1056: if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
1057: k = sk;
1058: // Scan first row
1059: if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
1060: // Use zero coding
1061: if (mq.decodeSymbol(zc_lut[csj & ZC_MASK]) != 0) {
1062: // Became significant
1063: // Use sign coding
1064: ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
1065: & SC_MASK];
1066: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
1067: ^ (ctxt >>> SC_SPRED_SHIFT);
1068: // Update data
1069: data[k] = (sym << 31) | setmask;
1070: // Update state information (significant bit,
1071: // visited bit, neighbor significant bit of
1072: // neighbors, non zero context of neighbors, sign
1073: // of neighbors)
1074: if (!causal) {
1075: // If in causal mode do not change contexts of
1076: // previous stripe.
1077: state[j + off_ul] |= STATE_NZ_CTXT_R2
1078: | STATE_D_DR_R2;
1079: state[j + off_ur] |= STATE_NZ_CTXT_R2
1080: | STATE_D_DL_R2;
1081: }
1082: // Update sign state information of neighbors
1083: if (sym != 0) {
1084: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1085: | STATE_NZ_CTXT_R2
1086: | STATE_V_U_R2
1087: | STATE_V_U_SIGN_R2;
1088: if (!causal) {
1089: // If in causal mode do not change
1090: // contexts of previous stripe.
1091: state[j - sscanw] |= STATE_NZ_CTXT_R2
1092: | STATE_V_D_R2
1093: | STATE_V_D_SIGN_R2;
1094: }
1095: state[j + 1] |= STATE_NZ_CTXT_R1
1096: | STATE_NZ_CTXT_R2
1097: | STATE_H_L_R1
1098: | STATE_H_L_SIGN_R1
1099: | STATE_D_UL_R2;
1100: state[j - 1] |= STATE_NZ_CTXT_R1
1101: | STATE_NZ_CTXT_R2
1102: | STATE_H_R_R1
1103: | STATE_H_R_SIGN_R1
1104: | STATE_D_UR_R2;
1105: } else {
1106: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1107: | STATE_NZ_CTXT_R2
1108: | STATE_V_U_R2;
1109: if (!causal) {
1110: // If in causal mode do not change
1111: // contexts of previous stripe.
1112: state[j - sscanw] |= STATE_NZ_CTXT_R2
1113: | STATE_V_D_R2;
1114: }
1115: state[j + 1] |= STATE_NZ_CTXT_R1
1116: | STATE_NZ_CTXT_R2
1117: | STATE_H_L_R1 | STATE_D_UL_R2;
1118: state[j - 1] |= STATE_NZ_CTXT_R1
1119: | STATE_NZ_CTXT_R2
1120: | STATE_H_R_R1 | STATE_D_UR_R2;
1121: }
1122: } else {
1123: csj |= STATE_VISITED_R1;
1124: }
1125: }
1126: if (sheight < 2) {
1127: state[j] = csj;
1128: continue;
1129: }
1130: // Scan second row
1131: if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
1132: k += dscanw;
1133: // Use zero coding
1134: if (mq.decodeSymbol(zc_lut[(csj >>> STATE_SEP)
1135: & ZC_MASK]) != 0) {
1136: // Became significant
1137: // Use sign coding
1138: ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
1139: & SC_MASK];
1140: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
1141: ^ (ctxt >>> SC_SPRED_SHIFT);
1142: // Update data
1143: data[k] = (sym << 31) | setmask;
1144: // Update state information (significant bit,
1145: // visited bit, neighbor significant bit of
1146: // neighbors, non zero context of neighbors, sign
1147: // of neighbors)
1148: state[j + off_dl] |= STATE_NZ_CTXT_R1
1149: | STATE_D_UR_R1;
1150: state[j + off_dr] |= STATE_NZ_CTXT_R1
1151: | STATE_D_UL_R1;
1152: // Update sign state information of neighbors
1153: if (sym != 0) {
1154: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1155: | STATE_NZ_CTXT_R1
1156: | STATE_V_D_R1
1157: | STATE_V_D_SIGN_R1;
1158: state[j + sscanw] |= STATE_NZ_CTXT_R1
1159: | STATE_V_U_R1
1160: | STATE_V_U_SIGN_R1;
1161: state[j + 1] |= STATE_NZ_CTXT_R1
1162: | STATE_NZ_CTXT_R2
1163: | STATE_D_DL_R1 | STATE_H_L_R2
1164: | STATE_H_L_SIGN_R2;
1165: state[j - 1] |= STATE_NZ_CTXT_R1
1166: | STATE_NZ_CTXT_R2
1167: | STATE_D_DR_R1 | STATE_H_R_R2
1168: | STATE_H_R_SIGN_R2;
1169: } else {
1170: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1171: | STATE_NZ_CTXT_R1
1172: | STATE_V_D_R1;
1173: state[j + sscanw] |= STATE_NZ_CTXT_R1
1174: | STATE_V_U_R1;
1175: state[j + 1] |= STATE_NZ_CTXT_R1
1176: | STATE_NZ_CTXT_R2
1177: | STATE_D_DL_R1 | STATE_H_L_R2;
1178: state[j - 1] |= STATE_NZ_CTXT_R1
1179: | STATE_NZ_CTXT_R2
1180: | STATE_D_DR_R1 | STATE_H_R_R2;
1181: }
1182: } else {
1183: csj |= STATE_VISITED_R2;
1184: }
1185: }
1186: state[j] = csj;
1187: }
1188: // Do half bottom of column
1189: if (sheight < 3)
1190: continue;
1191: j += sscanw;
1192: csj = state[j];
1193: // If any of the two samples is not significant and has a
1194: // non-zero context (i.e. some neighbor is significant) we can
1195: // not skip them
1196: if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
1197: k = sk + (dscanw << 1);
1198: // Scan first row
1199: if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
1200: // Use zero coding
1201: if (mq.decodeSymbol(zc_lut[csj & ZC_MASK]) != 0) {
1202: // Became significant
1203: // Use sign coding
1204: ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
1205: & SC_MASK];
1206: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
1207: ^ (ctxt >>> SC_SPRED_SHIFT);
1208: // Update data
1209: data[k] = (sym << 31) | setmask;
1210: // Update state information (significant bit,
1211: // visited bit, neighbor significant bit of
1212: // neighbors, non zero context of neighbors, sign
1213: // of neighbors)
1214: state[j + off_ul] |= STATE_NZ_CTXT_R2
1215: | STATE_D_DR_R2;
1216: state[j + off_ur] |= STATE_NZ_CTXT_R2
1217: | STATE_D_DL_R2;
1218: // Update sign state information of neighbors
1219: if (sym != 0) {
1220: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1221: | STATE_NZ_CTXT_R2
1222: | STATE_V_U_R2
1223: | STATE_V_U_SIGN_R2;
1224: state[j - sscanw] |= STATE_NZ_CTXT_R2
1225: | STATE_V_D_R2
1226: | STATE_V_D_SIGN_R2;
1227: state[j + 1] |= STATE_NZ_CTXT_R1
1228: | STATE_NZ_CTXT_R2
1229: | STATE_H_L_R1
1230: | STATE_H_L_SIGN_R1
1231: | STATE_D_UL_R2;
1232: state[j - 1] |= STATE_NZ_CTXT_R1
1233: | STATE_NZ_CTXT_R2
1234: | STATE_H_R_R1
1235: | STATE_H_R_SIGN_R1
1236: | STATE_D_UR_R2;
1237: } else {
1238: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1239: | STATE_NZ_CTXT_R2
1240: | STATE_V_U_R2;
1241: state[j - sscanw] |= STATE_NZ_CTXT_R2
1242: | STATE_V_D_R2;
1243: state[j + 1] |= STATE_NZ_CTXT_R1
1244: | STATE_NZ_CTXT_R2
1245: | STATE_H_L_R1 | STATE_D_UL_R2;
1246: state[j - 1] |= STATE_NZ_CTXT_R1
1247: | STATE_NZ_CTXT_R2
1248: | STATE_H_R_R1 | STATE_D_UR_R2;
1249: }
1250: } else {
1251: csj |= STATE_VISITED_R1;
1252: }
1253: }
1254: if (sheight < 4) {
1255: state[j] = csj;
1256: continue;
1257: }
1258: // Scan second row
1259: if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
1260: k += dscanw;
1261: // Use zero coding
1262: if (mq.decodeSymbol(zc_lut[(csj >>> STATE_SEP)
1263: & ZC_MASK]) != 0) {
1264: // Became significant
1265: // Use sign coding
1266: ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
1267: & SC_MASK];
1268: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
1269: ^ (ctxt >>> SC_SPRED_SHIFT);
1270: // Update data
1271: data[k] = (sym << 31) | setmask;
1272: // Update state information (significant bit,
1273: // visited bit, neighbor significant bit of
1274: // neighbors, non zero context of neighbors, sign
1275: // of neighbors)
1276: state[j + off_dl] |= STATE_NZ_CTXT_R1
1277: | STATE_D_UR_R1;
1278: state[j + off_dr] |= STATE_NZ_CTXT_R1
1279: | STATE_D_UL_R1;
1280: // Update sign state information of neighbors
1281: if (sym != 0) {
1282: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1283: | STATE_NZ_CTXT_R1
1284: | STATE_V_D_R1
1285: | STATE_V_D_SIGN_R1;
1286: state[j + sscanw] |= STATE_NZ_CTXT_R1
1287: | STATE_V_U_R1
1288: | STATE_V_U_SIGN_R1;
1289: state[j + 1] |= STATE_NZ_CTXT_R1
1290: | STATE_NZ_CTXT_R2
1291: | STATE_D_DL_R1 | STATE_H_L_R2
1292: | STATE_H_L_SIGN_R2;
1293: state[j - 1] |= STATE_NZ_CTXT_R1
1294: | STATE_NZ_CTXT_R2
1295: | STATE_D_DR_R1 | STATE_H_R_R2
1296: | STATE_H_R_SIGN_R2;
1297: } else {
1298: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1299: | STATE_NZ_CTXT_R1
1300: | STATE_V_D_R1;
1301: state[j + sscanw] |= STATE_NZ_CTXT_R1
1302: | STATE_V_U_R1;
1303: state[j + 1] |= STATE_NZ_CTXT_R1
1304: | STATE_NZ_CTXT_R2
1305: | STATE_D_DL_R1 | STATE_H_L_R2;
1306: state[j - 1] |= STATE_NZ_CTXT_R1
1307: | STATE_NZ_CTXT_R2
1308: | STATE_D_DR_R1 | STATE_H_R_R2;
1309: }
1310: } else {
1311: csj |= STATE_VISITED_R2;
1312: }
1313: }
1314: state[j] = csj;
1315: }
1316: }
1317: }
1318:
1319: error = false;
1320:
1321: // Check the error resilient termination
1322: if (isterm && (options & OPT_PRED_TERM) != 0) {
1323: error = mq.checkPredTerm();
1324: }
1325:
1326: // Reset the MQ context states if we need to
1327: if ((options & OPT_RESET_MQ) != 0) {
1328: mq.resetCtxts();
1329: }
1330:
1331: // Return error condition
1332: return error;
1333: }
1334:
1335: /**
1336: * Performs the significance propagation pass on the specified data and
1337: * bit-plane. It decodes all insignificant samples which have, at least, one
1338: * of its immediate eight neighbors already significant, using the ZC and
1339: * SC primitives as needed. It toggles the "visited" state bit to 1 for
1340: * all those samples.
1341: *
1342: * <P>This method bypasses the arithmetic coder and reads "raw" symbols
1343: * from the bit stream.
1344: *
1345: * <P>This method also checks for segmentation markers if those are
1346: * present and returns true if an error is detected, or false
1347: * otherwise. If an error is detected it measn that the bit stream contains
1348: * some erroneous bit that have led to the decoding of incorrect
1349: * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If
1350: * 'true' is returned the 'conceal' method should be called and no more
1351: * passes should be decoded for this code-block's bit stream.
1352: *
1353: * @param cblk The code-block data to decode
1354: *
1355: * @param bin The raw bit based input
1356: *
1357: * @param bp The bit-plane to decode
1358: *
1359: * @param state The state information for the code-block
1360: *
1361: * @param isterm If this pass has been terminated. If the pass has been
1362: * terminated it can be used to check error resilience.
1363: *
1364: * @return True if an error was detected in the bit stream, false otherwise.
1365: * */
1366: private boolean rawSigProgPass(DataBlk cblk, ByteToBitInput bin,
1367: int bp, int state[], boolean isterm) {
1368: int j, sj; // The state index for line and stripe
1369: int k, sk; // The data index for line and stripe
1370: int dscanw; // The data scan-width
1371: int sscanw; // The state scan-width
1372: int jstep; // Stripe to stripe step for 'sj'
1373: int kstep; // Stripe to stripe step for 'sk'
1374: int stopsk; // The loop limit on the variable sk
1375: int csj; // Local copy (i.e. cached) of 'state[j]'
1376: int setmask; // The mask to set current and lower bit-planes to 1/2
1377: // approximation
1378: int sym; // The symbol to code
1379: int data[]; // The data buffer
1380: int s; // The stripe index
1381: boolean causal; // Flag to indicate if stripe-causal context
1382: // formation is to be used
1383: int nstripes; // The number of stripes in the code-block
1384: int sheight; // Height of the current stripe
1385: int off_ul, off_ur, off_dr, off_dl; // offsets
1386: boolean error; // The error condition
1387:
1388: // Initialize local variables
1389: dscanw = cblk.scanw;
1390: sscanw = cblk.w + 2;
1391: jstep = sscanw * STRIPE_HEIGHT / 2 - cblk.w;
1392: kstep = dscanw * STRIPE_HEIGHT - cblk.w;
1393: setmask = (3 << bp) >> 1;
1394: data = (int[]) cblk.getData();
1395: nstripes = (cblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
1396: causal = (options & OPT_VERT_STR_CAUSAL) != 0;
1397:
1398: // Pre-calculate offsets in 'state' for diagonal neighbors
1399: off_ul = -sscanw - 1; // up-left
1400: off_ur = -sscanw + 1; // up-right
1401: off_dr = sscanw + 1; // down-right
1402: off_dl = sscanw - 1; // down-left
1403:
1404: // Decode stripe by stripe
1405: sk = cblk.offset;
1406: sj = sscanw + 1;
1407: for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
1408: sheight = (s != 0) ? STRIPE_HEIGHT : cblk.h
1409: - (nstripes - 1) * STRIPE_HEIGHT;
1410: stopsk = sk + cblk.w;
1411: // Scan by set of 1 stripe column at a time
1412: for (; sk < stopsk; sk++, sj++) {
1413: // Do half top of column
1414: j = sj;
1415: csj = state[j];
1416: // If any of the two samples is not significant and has a
1417: // non-zero context (i.e. some neighbor is significant) we can
1418: // not skip them
1419: if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
1420: k = sk;
1421: // Scan first row
1422: if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
1423: // Use zero coding
1424: if (bin.readBit() != 0) {
1425: // Became significant
1426: // Use sign coding
1427: sym = bin.readBit();
1428: // Update data
1429: data[k] = (sym << 31) | setmask;
1430: // Update state information (significant bit,
1431: // visited bit, neighbor significant bit of
1432: // neighbors, non zero context of neighbors, sign
1433: // of neighbors)
1434: if (!causal) {
1435: // If in causal mode do not change contexts of
1436: // previous stripe.
1437: state[j + off_ul] |= STATE_NZ_CTXT_R2
1438: | STATE_D_DR_R2;
1439: state[j + off_ur] |= STATE_NZ_CTXT_R2
1440: | STATE_D_DL_R2;
1441: }
1442: // Update sign state information of neighbors
1443: if (sym != 0) {
1444: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1445: | STATE_NZ_CTXT_R2
1446: | STATE_V_U_R2
1447: | STATE_V_U_SIGN_R2;
1448: if (!causal) {
1449: // If in causal mode do not change
1450: // contexts of previous stripe.
1451: state[j - sscanw] |= STATE_NZ_CTXT_R2
1452: | STATE_V_D_R2
1453: | STATE_V_D_SIGN_R2;
1454: }
1455: state[j + 1] |= STATE_NZ_CTXT_R1
1456: | STATE_NZ_CTXT_R2
1457: | STATE_H_L_R1
1458: | STATE_H_L_SIGN_R1
1459: | STATE_D_UL_R2;
1460: state[j - 1] |= STATE_NZ_CTXT_R1
1461: | STATE_NZ_CTXT_R2
1462: | STATE_H_R_R1
1463: | STATE_H_R_SIGN_R1
1464: | STATE_D_UR_R2;
1465: } else {
1466: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1467: | STATE_NZ_CTXT_R2
1468: | STATE_V_U_R2;
1469: if (!causal) {
1470: // If in causal mode do not change
1471: // contexts of previous stripe.
1472: state[j - sscanw] |= STATE_NZ_CTXT_R2
1473: | STATE_V_D_R2;
1474: }
1475: state[j + 1] |= STATE_NZ_CTXT_R1
1476: | STATE_NZ_CTXT_R2
1477: | STATE_H_L_R1 | STATE_D_UL_R2;
1478: state[j - 1] |= STATE_NZ_CTXT_R1
1479: | STATE_NZ_CTXT_R2
1480: | STATE_H_R_R1 | STATE_D_UR_R2;
1481: }
1482: } else {
1483: csj |= STATE_VISITED_R1;
1484: }
1485: }
1486: if (sheight < 2) {
1487: state[j] = csj;
1488: continue;
1489: }
1490: if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
1491: k += dscanw;
1492: // Use zero coding
1493: if (bin.readBit() != 0) {
1494: // Became significant
1495: // Use sign coding
1496: sym = bin.readBit();
1497: // Update data
1498: data[k] = (sym << 31) | setmask;
1499: // Update state information (significant bit,
1500: // visited bit, neighbor significant bit of
1501: // neighbors, non zero context of neighbors, sign
1502: // of neighbors)
1503: state[j + off_dl] |= STATE_NZ_CTXT_R1
1504: | STATE_D_UR_R1;
1505: state[j + off_dr] |= STATE_NZ_CTXT_R1
1506: | STATE_D_UL_R1;
1507: // Update sign state information of neighbors
1508: if (sym != 0) {
1509: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1510: | STATE_NZ_CTXT_R1
1511: | STATE_V_D_R1
1512: | STATE_V_D_SIGN_R1;
1513: state[j + sscanw] |= STATE_NZ_CTXT_R1
1514: | STATE_V_U_R1
1515: | STATE_V_U_SIGN_R1;
1516: state[j + 1] |= STATE_NZ_CTXT_R1
1517: | STATE_NZ_CTXT_R2
1518: | STATE_D_DL_R1 | STATE_H_L_R2
1519: | STATE_H_L_SIGN_R2;
1520: state[j - 1] |= STATE_NZ_CTXT_R1
1521: | STATE_NZ_CTXT_R2
1522: | STATE_D_DR_R1 | STATE_H_R_R2
1523: | STATE_H_R_SIGN_R2;
1524: } else {
1525: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1526: | STATE_NZ_CTXT_R1
1527: | STATE_V_D_R1;
1528: state[j + sscanw] |= STATE_NZ_CTXT_R1
1529: | STATE_V_U_R1;
1530: state[j + 1] |= STATE_NZ_CTXT_R1
1531: | STATE_NZ_CTXT_R2
1532: | STATE_D_DL_R1 | STATE_H_L_R2;
1533: state[j - 1] |= STATE_NZ_CTXT_R1
1534: | STATE_NZ_CTXT_R2
1535: | STATE_D_DR_R1 | STATE_H_R_R2;
1536: }
1537: } else {
1538: csj |= STATE_VISITED_R2;
1539: }
1540: }
1541: state[j] = csj;
1542: }
1543: // Do half bottom of column
1544: if (sheight < 3)
1545: continue;
1546: j += sscanw;
1547: csj = state[j];
1548: // If any of the two samples is not significant and has a
1549: // non-zero context (i.e. some neighbor is significant) we can
1550: // not skip them
1551: if ((((~csj) & (csj << 2)) & SIG_MASK_R1R2) != 0) {
1552: k = sk + (dscanw << 1);
1553: // Scan first row
1554: if ((csj & (STATE_SIG_R1 | STATE_NZ_CTXT_R1)) == STATE_NZ_CTXT_R1) {
1555: // Use zero coding
1556: if (bin.readBit() != 0) {
1557: // Became significant
1558: // Use sign coding
1559: sym = bin.readBit();
1560: // Update data
1561: data[k] = (sym << 31) | setmask;
1562: // Update state information (significant bit,
1563: // visited bit, neighbor significant bit of
1564: // neighbors, non zero context of neighbors, sign
1565: // of neighbors)
1566: state[j + off_ul] |= STATE_NZ_CTXT_R2
1567: | STATE_D_DR_R2;
1568: state[j + off_ur] |= STATE_NZ_CTXT_R2
1569: | STATE_D_DL_R2;
1570: // Update sign state information of neighbors
1571: if (sym != 0) {
1572: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1573: | STATE_NZ_CTXT_R2
1574: | STATE_V_U_R2
1575: | STATE_V_U_SIGN_R2;
1576: state[j - sscanw] |= STATE_NZ_CTXT_R2
1577: | STATE_V_D_R2
1578: | STATE_V_D_SIGN_R2;
1579: state[j + 1] |= STATE_NZ_CTXT_R1
1580: | STATE_NZ_CTXT_R2
1581: | STATE_H_L_R1
1582: | STATE_H_L_SIGN_R1
1583: | STATE_D_UL_R2;
1584: state[j - 1] |= STATE_NZ_CTXT_R1
1585: | STATE_NZ_CTXT_R2
1586: | STATE_H_R_R1
1587: | STATE_H_R_SIGN_R1
1588: | STATE_D_UR_R2;
1589: } else {
1590: csj |= STATE_SIG_R1 | STATE_VISITED_R1
1591: | STATE_NZ_CTXT_R2
1592: | STATE_V_U_R2;
1593: state[j - sscanw] |= STATE_NZ_CTXT_R2
1594: | STATE_V_D_R2;
1595: state[j + 1] |= STATE_NZ_CTXT_R1
1596: | STATE_NZ_CTXT_R2
1597: | STATE_H_L_R1 | STATE_D_UL_R2;
1598: state[j - 1] |= STATE_NZ_CTXT_R1
1599: | STATE_NZ_CTXT_R2
1600: | STATE_H_R_R1 | STATE_D_UR_R2;
1601: }
1602: } else {
1603: csj |= STATE_VISITED_R1;
1604: }
1605: }
1606: if (sheight < 4) {
1607: state[j] = csj;
1608: continue;
1609: }
1610: // Scan second row
1611: if ((csj & (STATE_SIG_R2 | STATE_NZ_CTXT_R2)) == STATE_NZ_CTXT_R2) {
1612: k += dscanw;
1613: // Use zero coding
1614: if (bin.readBit() != 0) {
1615: // Became significant
1616: // Use sign coding
1617: sym = bin.readBit();
1618: // Update data
1619: data[k] = (sym << 31) | setmask;
1620: // Update state information (significant bit,
1621: // visited bit, neighbor significant bit of
1622: // neighbors, non zero context of neighbors, sign
1623: // of neighbors)
1624: state[j + off_dl] |= STATE_NZ_CTXT_R1
1625: | STATE_D_UR_R1;
1626: state[j + off_dr] |= STATE_NZ_CTXT_R1
1627: | STATE_D_UL_R1;
1628: // Update sign state information of neighbors
1629: if (sym != 0) {
1630: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1631: | STATE_NZ_CTXT_R1
1632: | STATE_V_D_R1
1633: | STATE_V_D_SIGN_R1;
1634: state[j + sscanw] |= STATE_NZ_CTXT_R1
1635: | STATE_V_U_R1
1636: | STATE_V_U_SIGN_R1;
1637: state[j + 1] |= STATE_NZ_CTXT_R1
1638: | STATE_NZ_CTXT_R2
1639: | STATE_D_DL_R1 | STATE_H_L_R2
1640: | STATE_H_L_SIGN_R2;
1641: state[j - 1] |= STATE_NZ_CTXT_R1
1642: | STATE_NZ_CTXT_R2
1643: | STATE_D_DR_R1 | STATE_H_R_R2
1644: | STATE_H_R_SIGN_R2;
1645: } else {
1646: csj |= STATE_SIG_R2 | STATE_VISITED_R2
1647: | STATE_NZ_CTXT_R1
1648: | STATE_V_D_R1;
1649: state[j + sscanw] |= STATE_NZ_CTXT_R1
1650: | STATE_V_U_R1;
1651: state[j + 1] |= STATE_NZ_CTXT_R1
1652: | STATE_NZ_CTXT_R2
1653: | STATE_D_DL_R1 | STATE_H_L_R2;
1654: state[j - 1] |= STATE_NZ_CTXT_R1
1655: | STATE_NZ_CTXT_R2
1656: | STATE_D_DR_R1 | STATE_H_R_R2;
1657: }
1658: } else {
1659: csj |= STATE_VISITED_R2;
1660: }
1661: }
1662: state[j] = csj;
1663: }
1664: }
1665: }
1666:
1667: error = false;
1668:
1669: // Check the byte padding if the pass is terminated
1670: if (isterm) {
1671: error = bin.checkBytePadding();
1672: }
1673:
1674: // Return error condition
1675: return error;
1676: }
1677:
1678: /**
1679: * Performs the magnitude refinement pass on the specified data and
1680: * bit-plane. It decodes the samples which are significant and which do not
1681: * have the "visited" state bit turned on, using the MR primitive. The
1682: * "visited" state bit is not mofified for any samples.
1683: *
1684: * <P>This method also checks for segmentation markers if those are
1685: * present and returns true if an error is detected, or false
1686: * otherwise. If an error is detected it means that the bit stream contains
1687: * some erroneous bit that have led to the decoding of incorrect
1688: * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If
1689: * 'true' is returned the 'conceal' method should be called and no more
1690: * passes should be decoded for this code-block's bit stream.
1691: *
1692: * @param cblk The code-block data to decode
1693: *
1694: * @param mq The MQ-decoder to use
1695: *
1696: * @param bp The bit-plane to decode
1697: *
1698: * @param state The state information for the code-block
1699: *
1700: * @param isterm If this pass has been terminated. If the pass has been
1701: * terminated it can be used to check error resilience.
1702: *
1703: * @return True if an error was detected in the bit stream, false otherwise.
1704: * */
1705: private boolean magRefPass(DataBlk cblk, MQDecoder mq, int bp,
1706: int state[], boolean isterm) {
1707: int j, sj; // The state index for line and stripe
1708: int k, sk; // The data index for line and stripe
1709: int dscanw; // The data scan-width
1710: int sscanw; // The state scan-width
1711: int jstep; // Stripe to stripe step for 'sj'
1712: int kstep; // Stripe to stripe step for 'sk'
1713: int stopsk; // The loop limit on the variable sk
1714: int csj; // Local copy (i.e. cached) of 'state[j]'
1715: int setmask; // The mask to set lower bit-planes to 1/2 approximation
1716: int resetmask; // The mask to reset approximation bit-planes
1717: int sym; // The symbol to decode
1718: int data[]; // The data buffer
1719: int s; // The stripe index
1720: int nstripes; // The number of stripes in the code-block
1721: int sheight; // Height of the current stripe
1722: boolean error; // The error condition
1723:
1724: // Initialize local variables
1725: dscanw = cblk.scanw;
1726: sscanw = cblk.w + 2;
1727: jstep = sscanw * STRIPE_HEIGHT / 2 - cblk.w;
1728: kstep = dscanw * STRIPE_HEIGHT - cblk.w;
1729: setmask = (1 << bp) >> 1;
1730: resetmask = (-1) << (bp + 1);
1731: data = (int[]) cblk.getData();
1732: nstripes = (cblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
1733:
1734: // Decode stripe by stripe
1735: sk = cblk.offset;
1736: sj = sscanw + 1;
1737: for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
1738: sheight = (s != 0) ? STRIPE_HEIGHT : cblk.h
1739: - (nstripes - 1) * STRIPE_HEIGHT;
1740: stopsk = sk + cblk.w;
1741: // Scan by set of 1 stripe column at a time
1742: for (; sk < stopsk; sk++, sj++) {
1743: // Do half top of column
1744: j = sj;
1745: csj = state[j];
1746: // If any of the two samples is significant and not yet
1747: // visited in the current bit-plane we can not skip them
1748: if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
1749: k = sk;
1750: // Scan first row
1751: if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
1752: // Use MR primitive
1753: sym = mq.decodeSymbol(MR_LUT[csj & MR_MASK]);
1754: // Update the data
1755: data[k] &= resetmask;
1756: data[k] |= (sym << bp) | setmask;
1757: // Update the STATE_PREV_MR bit
1758: csj |= STATE_PREV_MR_R1;
1759: }
1760: if (sheight < 2) {
1761: state[j] = csj;
1762: continue;
1763: }
1764: // Scan second row
1765: if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
1766: k += dscanw;
1767: // Use MR primitive
1768: sym = mq
1769: .decodeSymbol(MR_LUT[(csj >>> STATE_SEP)
1770: & MR_MASK]);
1771: // Update the data
1772: data[k] &= resetmask;
1773: data[k] |= (sym << bp) | setmask;
1774: // Update the STATE_PREV_MR bit
1775: csj |= STATE_PREV_MR_R2;
1776: }
1777: state[j] = csj;
1778: }
1779: // Do half bottom of column
1780: if (sheight < 3)
1781: continue;
1782: j += sscanw;
1783: csj = state[j];
1784: // If any of the two samples is significant and not yet
1785: // visited in the current bit-plane we can not skip them
1786: if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
1787: k = sk + (dscanw << 1);
1788: // Scan first row
1789: if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
1790: // Use MR primitive
1791: sym = mq.decodeSymbol(MR_LUT[csj & MR_MASK]);
1792: // Update the data
1793: data[k] &= resetmask;
1794: data[k] |= (sym << bp) | setmask;
1795: // Update the STATE_PREV_MR bit
1796: csj |= STATE_PREV_MR_R1;
1797: }
1798: if (sheight < 4) {
1799: state[j] = csj;
1800: continue;
1801: }
1802: // Scan second row
1803: if ((state[j] & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
1804: k += dscanw;
1805: // Use MR primitive
1806: sym = mq
1807: .decodeSymbol(MR_LUT[(csj >>> STATE_SEP)
1808: & MR_MASK]);
1809: // Update the data
1810: data[k] &= resetmask;
1811: data[k] |= (sym << bp) | setmask;
1812: // Update the STATE_PREV_MR bit
1813: csj |= STATE_PREV_MR_R2;
1814: }
1815: state[j] = csj;
1816: }
1817: }
1818: }
1819:
1820: error = false;
1821:
1822: // Check the error resilient termination
1823: if (isterm && (options & OPT_PRED_TERM) != 0) {
1824: error = mq.checkPredTerm();
1825: }
1826:
1827: // Reset the MQ context states if we need to
1828: if ((options & OPT_RESET_MQ) != 0) {
1829: mq.resetCtxts();
1830: }
1831:
1832: // Return error condition
1833: return error;
1834: }
1835:
1836: /**
1837: * Performs the magnitude refinement pass on the specified data and
1838: * bit-plane. It decodes the samples which are significant and which do not
1839: * have the "visited" state bit turned on, using the MR primitive. The
1840: * "visited" state bit is not mofified for any samples.
1841: *
1842: * <P>This method bypasses the arithmetic coder and reads "raw" symbols
1843: * from the bit stream.
1844: *
1845: * <P>This method also checks for segmentation markers if those are
1846: * present and returns true if an error is detected, or false
1847: * otherwise. If an error is detected it measn that the bit stream contains
1848: * some erroneous bit that have led to the decoding of incorrect
1849: * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If
1850: * 'true' is returned the 'conceal' method should be called and no more
1851: * passes should be decoded for this code-block's bit stream.
1852: *
1853: * @param cblk The code-block data to decode
1854: *
1855: * @param bin The raw bit based input
1856: *
1857: * @param bp The bit-plane to decode
1858: *
1859: * @param state The state information for the code-block
1860: *
1861: * @param isterm If this pass has been terminated. If the pass has been
1862: * terminated it can be used to check error resilience.
1863: *
1864: * @return True if an error was detected in the bit stream, false otherwise.
1865: * */
1866: private boolean rawMagRefPass(DataBlk cblk, ByteToBitInput bin,
1867: int bp, int state[], boolean isterm) {
1868: int j, sj; // The state index for line and stripe
1869: int k, sk; // The data index for line and stripe
1870: int dscanw; // The data scan-width
1871: int sscanw; // The state scan-width
1872: int jstep; // Stripe to stripe step for 'sj'
1873: int kstep; // Stripe to stripe step for 'sk'
1874: int stopsk; // The loop limit on the variable sk
1875: int csj; // Local copy (i.e. cached) of 'state[j]'
1876: int setmask; // The mask to set lower bit-planes to 1/2 approximation
1877: int resetmask; // The mask to reset approximation bit-planes
1878: int sym; // The symbol to decode
1879: int data[]; // The data buffer
1880: int s; // The stripe index
1881: int nstripes; // The number of stripes in the code-block
1882: int sheight; // Height of the current stripe
1883: boolean error; // The error condition
1884:
1885: // Initialize local variables
1886: dscanw = cblk.scanw;
1887: sscanw = cblk.w + 2;
1888: jstep = sscanw * STRIPE_HEIGHT / 2 - cblk.w;
1889: kstep = dscanw * STRIPE_HEIGHT - cblk.w;
1890: setmask = (1 << bp) >> 1;
1891: resetmask = (-1) << (bp + 1);
1892: data = (int[]) cblk.getData();
1893: nstripes = (cblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
1894:
1895: // Decode stripe by stripe
1896: sk = cblk.offset;
1897: sj = sscanw + 1;
1898: for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
1899: sheight = (s != 0) ? STRIPE_HEIGHT : cblk.h
1900: - (nstripes - 1) * STRIPE_HEIGHT;
1901: stopsk = sk + cblk.w;
1902: // Scan by set of 1 stripe column at a time
1903: for (; sk < stopsk; sk++, sj++) {
1904: // Do half top of column
1905: j = sj;
1906: csj = state[j];
1907: // If any of the two samples is significant and not yet
1908: // visited in the current bit-plane we can not skip them
1909: if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
1910: k = sk;
1911: // Scan first row
1912: if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
1913: // Read raw bit (no MR primative)
1914: sym = bin.readBit();
1915: // Update the data
1916: data[k] &= resetmask;
1917: data[k] |= (sym << bp) | setmask;
1918: // No need to set STATE_PREV_MR_R1 since all magnitude
1919: // refinement passes to follow are "raw"
1920: }
1921: if (sheight < 2)
1922: continue;
1923: // Scan second row
1924: if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
1925: k += dscanw;
1926: // Read raw bit (no MR primative)
1927: sym = bin.readBit();
1928: // Update the data
1929: data[k] &= resetmask;
1930: data[k] |= (sym << bp) | setmask;
1931: // No need to set STATE_PREV_MR_R1 since all magnitude
1932: // refinement passes to follow are "raw"
1933: }
1934: }
1935: // Do half bottom of column
1936: if (sheight < 3)
1937: continue;
1938: j += sscanw;
1939: csj = state[j];
1940: // If any of the two samples is significant and not yet
1941: // visited in the current bit-plane we can not skip them
1942: if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) {
1943: k = sk + (dscanw << 1);
1944: // Scan first row
1945: if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == STATE_SIG_R1) {
1946: // Read raw bit (no MR primative)
1947: sym = bin.readBit();
1948: // Update the data
1949: data[k] &= resetmask;
1950: data[k] |= (sym << bp) | setmask;
1951: // No need to set STATE_PREV_MR_R1 since all magnitude
1952: // refinement passes to follow are "raw"
1953: }
1954: if (sheight < 4)
1955: continue;
1956: // Scan second row
1957: if ((state[j] & (STATE_SIG_R2 | STATE_VISITED_R2)) == STATE_SIG_R2) {
1958: k += dscanw;
1959: // Read raw bit (no MR primative)
1960: sym = bin.readBit();
1961: // Update the data
1962: data[k] &= resetmask;
1963: data[k] |= (sym << bp) | setmask;
1964: // No need to set STATE_PREV_MR_R1 since all magnitude
1965: // refinement passes to follow are "raw"
1966: }
1967: }
1968: }
1969: }
1970:
1971: error = false;
1972:
1973: // Check the byte padding if the pass is terminated
1974: if (isterm && (options & OPT_PRED_TERM) != 0) {
1975: error = bin.checkBytePadding();
1976: }
1977:
1978: // Return error condition
1979: return error;
1980: }
1981:
1982: /**
1983: * Performs the cleanup pass on the specified data and bit-plane. It
1984: * decodes all insignificant samples which have its "visited" state bit
1985: * off, using the ZC, SC, and RLC primitives. It toggles the "visited"
1986: * state bit to 0 (off) for all samples in the code-block.
1987: *
1988: * <P>This method also checks for segmentation markers if those are
1989: * present and returns true if an error is detected, or false
1990: * otherwise. If an error is detected it measn that the bit stream
1991: * contains some erroneous bit that have led to the decoding of incorrect
1992: * data. This data affects the whole last decoded bit-plane
1993: * (i.e. 'bp'). If 'true' is returned the 'conceal' method should be
1994: * called and no more passes should be decoded for this code-block's bit
1995: * stream.
1996: *
1997: * @param cblk The code-block data to code
1998: *
1999: * @param mq The MQ-coder to use
2000: *
2001: * @param bp The bit-plane to decode
2002: *
2003: * @param state The state information for the code-block
2004: *
2005: * @param zc_lut The ZC lookup table to use in ZC.
2006: *
2007: * @param isterm If this pass has been terminated. If the pass has been
2008: * terminated it can be used to check error resilience.
2009: *
2010: * @return True if an error was detected in the bit stream, false
2011: * otherwise.
2012: * */
2013: private boolean cleanuppass(DataBlk cblk, MQDecoder mq, int bp,
2014: int state[], int zc_lut[], boolean isterm) {
2015: int j, sj; // The state index for line and stripe
2016: int k, sk; // The data index for line and stripe
2017: int dscanw; // The data scan-width
2018: int sscanw; // The state scan-width
2019: int jstep; // Stripe to stripe step for 'sj'
2020: int kstep; // Stripe to stripe step for 'sk'
2021: int stopsk; // The loop limit on the variable sk
2022: int csj; // Local copy (i.e. cached) of 'state[j]'
2023: int setmask; // The mask to set current and lower bit-planes to 1/2
2024: // approximation
2025: int sym; // The decoded symbol
2026: int rlclen; // Length of RLC
2027: int ctxt; // The context to use
2028: int data[]; // The data buffer
2029: int s; // The stripe index
2030: boolean causal; // Flag to indicate if stripe-causal context
2031: // formation is to be used
2032: int nstripes; // The number of stripes in the code-block
2033: int sheight; // Height of the current stripe
2034: int off_ul, off_ur, off_dr, off_dl; // offsets
2035: boolean error; // The error condition
2036:
2037: // Initialize local variables
2038: dscanw = cblk.scanw;
2039: sscanw = cblk.w + 2;
2040: jstep = sscanw * STRIPE_HEIGHT / 2 - cblk.w;
2041: kstep = dscanw * STRIPE_HEIGHT - cblk.w;
2042: setmask = (3 << bp) >> 1;
2043: data = (int[]) cblk.getData();
2044: nstripes = (cblk.h + STRIPE_HEIGHT - 1) / STRIPE_HEIGHT;
2045: causal = (options & OPT_VERT_STR_CAUSAL) != 0;
2046:
2047: // Pre-calculate offsets in 'state' for diagonal neighbors
2048: off_ul = -sscanw - 1; // up-left
2049: off_ur = -sscanw + 1; // up-right
2050: off_dr = sscanw + 1; // down-right
2051: off_dl = sscanw - 1; // down-left
2052:
2053: // Decode stripe by stripe
2054: sk = cblk.offset;
2055: sj = sscanw + 1;
2056: for (s = nstripes - 1; s >= 0; s--, sk += kstep, sj += jstep) {
2057: sheight = (s != 0) ? STRIPE_HEIGHT : cblk.h
2058: - (nstripes - 1) * STRIPE_HEIGHT;
2059: stopsk = sk + cblk.w;
2060: // Scan by set of 1 stripe column at a time
2061: for (; sk < stopsk; sk++, sj++) {
2062: // Start column
2063: j = sj;
2064: csj = state[j];
2065: top_half: {
2066: // Check for RLC: if all samples are not significant, not
2067: // visited and do not have a non-zero context, and column is
2068: // full height, we do RLC.
2069: if (csj == 0 && state[j + sscanw] == 0
2070: && sheight == STRIPE_HEIGHT) {
2071: if (mq.decodeSymbol(RLC_CTXT) != 0) {
2072: // run-length is significant, decode length
2073: rlclen = mq.decodeSymbol(UNIF_CTXT) << 1;
2074: rlclen |= mq.decodeSymbol(UNIF_CTXT);
2075: // Set 'k' and 'j' accordingly
2076: k = sk + rlclen * dscanw;
2077: if (rlclen > 1) {
2078: j += sscanw;
2079: csj = state[j];
2080: }
2081: } else { // RLC is insignificant
2082: // Goto next column
2083: continue;
2084: }
2085: // We just decoded the length of a significant RLC
2086: // and a sample became significant
2087: // Use sign coding
2088: if ((rlclen & 0x01) == 0) {
2089: // Sample that became significant is first row of
2090: // its column half
2091: ctxt = SC_LUT[(csj >> SC_SHIFT_R1)
2092: & SC_MASK];
2093: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
2094: ^ (ctxt >>> SC_SPRED_SHIFT);
2095: // Update the data
2096: data[k] = (sym << 31) | setmask;
2097: // Update state information (significant bit,
2098: // visited bit, neighbor significant bit of
2099: // neighbors, non zero context of neighbors, sign
2100: // of neighbors)
2101: if (rlclen != 0 || !causal) {
2102: // If in causal mode do not change
2103: // contexts of previous stripe.
2104: state[j + off_ul] |= STATE_NZ_CTXT_R2
2105: | STATE_D_DR_R2;
2106: state[j + off_ur] |= STATE_NZ_CTXT_R2
2107: | STATE_D_DL_R2;
2108: }
2109: // Update sign state information of neighbors
2110: if (sym != 0) {
2111: csj |= STATE_SIG_R1 | STATE_VISITED_R1
2112: | STATE_NZ_CTXT_R2
2113: | STATE_V_U_R2
2114: | STATE_V_U_SIGN_R2;
2115: if (rlclen != 0 || !causal) {
2116: // If in causal mode do not change
2117: // contexts of previous stripe.
2118: state[j - sscanw] |= STATE_NZ_CTXT_R2
2119: | STATE_V_D_R2
2120: | STATE_V_D_SIGN_R2;
2121: }
2122: state[j + 1] |= STATE_NZ_CTXT_R1
2123: | STATE_NZ_CTXT_R2
2124: | STATE_H_L_R1
2125: | STATE_H_L_SIGN_R1
2126: | STATE_D_UL_R2;
2127: state[j - 1] |= STATE_NZ_CTXT_R1
2128: | STATE_NZ_CTXT_R2
2129: | STATE_H_R_R1
2130: | STATE_H_R_SIGN_R1
2131: | STATE_D_UR_R2;
2132: } else {
2133: csj |= STATE_SIG_R1 | STATE_VISITED_R1
2134: | STATE_NZ_CTXT_R2
2135: | STATE_V_U_R2;
2136: if (rlclen != 0 || !causal) {
2137: // If in causal mode do not change
2138: // contexts of previous stripe.
2139: state[j - sscanw] |= STATE_NZ_CTXT_R2
2140: | STATE_V_D_R2;
2141: }
2142: state[j + 1] |= STATE_NZ_CTXT_R1
2143: | STATE_NZ_CTXT_R2
2144: | STATE_H_L_R1 | STATE_D_UL_R2;
2145: state[j - 1] |= STATE_NZ_CTXT_R1
2146: | STATE_NZ_CTXT_R2
2147: | STATE_H_R_R1 | STATE_D_UR_R2;
2148: }
2149: // Changes to csj are saved later
2150: if ((rlclen >> 1) != 0) {
2151: // Sample that became significant is in
2152: // bottom half of column => jump to bottom
2153: // half
2154: break top_half;
2155: }
2156: // Otherwise sample that became significant is in
2157: // top half of column => continue on top half
2158: } else {
2159: // Sample that became significant is second row of
2160: // its column half
2161: ctxt = SC_LUT[(csj >> SC_SHIFT_R2)
2162: & SC_MASK];
2163: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
2164: ^ (ctxt >>> SC_SPRED_SHIFT);
2165: // Update the data
2166: data[k] = (sym << 31) | setmask;
2167: // Update state information (significant bit,
2168: // neighbor significant bit of neighbors,
2169: // non zero context of neighbors, sign of neighbors)
2170: state[j + off_dl] |= STATE_NZ_CTXT_R1
2171: | STATE_D_UR_R1;
2172: state[j + off_dr] |= STATE_NZ_CTXT_R1
2173: | STATE_D_UL_R1;
2174: // Update sign state information of neighbors
2175: if (sym != 0) {
2176: csj |= STATE_SIG_R2 | STATE_NZ_CTXT_R1
2177: | STATE_V_D_R1
2178: | STATE_V_D_SIGN_R1;
2179: state[j + sscanw] |= STATE_NZ_CTXT_R1
2180: | STATE_V_U_R1
2181: | STATE_V_U_SIGN_R1;
2182: state[j + 1] |= STATE_NZ_CTXT_R1
2183: | STATE_NZ_CTXT_R2
2184: | STATE_D_DL_R1 | STATE_H_L_R2
2185: | STATE_H_L_SIGN_R2;
2186: state[j - 1] |= STATE_NZ_CTXT_R1
2187: | STATE_NZ_CTXT_R2
2188: | STATE_D_DR_R1 | STATE_H_R_R2
2189: | STATE_H_R_SIGN_R2;
2190: } else {
2191: csj |= STATE_SIG_R2 | STATE_NZ_CTXT_R1
2192: | STATE_V_D_R1;
2193: state[j + sscanw] |= STATE_NZ_CTXT_R1
2194: | STATE_V_U_R1;
2195: state[j + 1] |= STATE_NZ_CTXT_R1
2196: | STATE_NZ_CTXT_R2
2197: | STATE_D_DL_R1 | STATE_H_L_R2;
2198: state[j - 1] |= STATE_NZ_CTXT_R1
2199: | STATE_NZ_CTXT_R2
2200: | STATE_D_DR_R1 | STATE_H_R_R2;
2201: }
2202: // Save changes to csj
2203: state[j] = csj;
2204: if ((rlclen >> 1) != 0) {
2205: // Sample that became significant is in bottom
2206: // half of column => we're done with this
2207: // column
2208: continue;
2209: }
2210: // Otherwise sample that became significant is in
2211: // top half of column => we're done with top
2212: // column
2213: j += sscanw;
2214: csj = state[j];
2215: break top_half;
2216: }
2217: }
2218: // Do half top of column
2219: // If any of the two samples is not significant and has
2220: // not been visited in the current bit-plane we can not
2221: // skip them
2222: if ((((csj >> 1) | csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) {
2223: k = sk;
2224: // Scan first row
2225: if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == 0) {
2226: // Use zero coding
2227: if (mq.decodeSymbol(zc_lut[csj & ZC_MASK]) != 0) {
2228: // Became significant
2229: // Use sign coding
2230: ctxt = SC_LUT[(csj >>> SC_SHIFT_R1)
2231: & SC_MASK];
2232: sym = mq.decodeSymbol(ctxt
2233: & SC_LUT_MASK)
2234: ^ (ctxt >>> SC_SPRED_SHIFT);
2235: // Update the data
2236: data[k] = (sym << 31) | setmask;
2237: // Update state information (significant bit,
2238: // visited bit, neighbor significant bit of
2239: // neighbors, non zero context of neighbors,
2240: // sign of neighbors)
2241: if (!causal) {
2242: // If in causal mode do not change
2243: // contexts of previous stripe.
2244: state[j + off_ul] |= STATE_NZ_CTXT_R2
2245: | STATE_D_DR_R2;
2246: state[j + off_ur] |= STATE_NZ_CTXT_R2
2247: | STATE_D_DL_R2;
2248: }
2249: // Update sign state information of neighbors
2250: if (sym != 0) {
2251: csj |= STATE_SIG_R1
2252: | STATE_VISITED_R1
2253: | STATE_NZ_CTXT_R2
2254: | STATE_V_U_R2
2255: | STATE_V_U_SIGN_R2;
2256: if (!causal) {
2257: // If in causal mode do not change
2258: // contexts of previous stripe.
2259: state[j - sscanw] |= STATE_NZ_CTXT_R2
2260: | STATE_V_D_R2
2261: | STATE_V_D_SIGN_R2;
2262: }
2263: state[j + 1] |= STATE_NZ_CTXT_R1
2264: | STATE_NZ_CTXT_R2
2265: | STATE_H_L_R1
2266: | STATE_H_L_SIGN_R1
2267: | STATE_D_UL_R2;
2268: state[j - 1] |= STATE_NZ_CTXT_R1
2269: | STATE_NZ_CTXT_R2
2270: | STATE_H_R_R1
2271: | STATE_H_R_SIGN_R1
2272: | STATE_D_UR_R2;
2273: } else {
2274: csj |= STATE_SIG_R1
2275: | STATE_VISITED_R1
2276: | STATE_NZ_CTXT_R2
2277: | STATE_V_U_R2;
2278: if (!causal) {
2279: // If in causal mode do not change
2280: // contexts of previous stripe.
2281: state[j - sscanw] |= STATE_NZ_CTXT_R2
2282: | STATE_V_D_R2;
2283: }
2284: state[j + 1] |= STATE_NZ_CTXT_R1
2285: | STATE_NZ_CTXT_R2
2286: | STATE_H_L_R1
2287: | STATE_D_UL_R2;
2288: state[j - 1] |= STATE_NZ_CTXT_R1
2289: | STATE_NZ_CTXT_R2
2290: | STATE_H_R_R1
2291: | STATE_D_UR_R2;
2292: }
2293: }
2294: }
2295: if (sheight < 2) {
2296: csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
2297: state[j] = csj;
2298: continue;
2299: }
2300: // Scan second row
2301: if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == 0) {
2302: k += dscanw;
2303: // Use zero coding
2304: if (mq
2305: .decodeSymbol(zc_lut[(csj >>> STATE_SEP)
2306: & ZC_MASK]) != 0) {
2307: // Became significant
2308: // Use sign coding
2309: ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
2310: & SC_MASK];
2311: sym = mq.decodeSymbol(ctxt
2312: & SC_LUT_MASK)
2313: ^ (ctxt >>> SC_SPRED_SHIFT);
2314: // Update the data
2315: data[k] = (sym << 31) | setmask;
2316: // Update state information (significant bit,
2317: // visited bit, neighbor significant bit of
2318: // neighbors, non zero context of neighbors,
2319: // sign of neighbors)
2320: state[j + off_dl] |= STATE_NZ_CTXT_R1
2321: | STATE_D_UR_R1;
2322: state[j + off_dr] |= STATE_NZ_CTXT_R1
2323: | STATE_D_UL_R1;
2324: // Update sign state information of neighbors
2325: if (sym != 0) {
2326: csj |= STATE_SIG_R2
2327: | STATE_VISITED_R2
2328: | STATE_NZ_CTXT_R1
2329: | STATE_V_D_R1
2330: | STATE_V_D_SIGN_R1;
2331: state[j + sscanw] |= STATE_NZ_CTXT_R1
2332: | STATE_V_U_R1
2333: | STATE_V_U_SIGN_R1;
2334: state[j + 1] |= STATE_NZ_CTXT_R1
2335: | STATE_NZ_CTXT_R2
2336: | STATE_D_DL_R1
2337: | STATE_H_L_R2
2338: | STATE_H_L_SIGN_R2;
2339: state[j - 1] |= STATE_NZ_CTXT_R1
2340: | STATE_NZ_CTXT_R2
2341: | STATE_D_DR_R1
2342: | STATE_H_R_R2
2343: | STATE_H_R_SIGN_R2;
2344: } else {
2345: csj |= STATE_SIG_R2
2346: | STATE_VISITED_R2
2347: | STATE_NZ_CTXT_R1
2348: | STATE_V_D_R1;
2349: state[j + sscanw] |= STATE_NZ_CTXT_R1
2350: | STATE_V_U_R1;
2351: state[j + 1] |= STATE_NZ_CTXT_R1
2352: | STATE_NZ_CTXT_R2
2353: | STATE_D_DL_R1
2354: | STATE_H_L_R2;
2355: state[j - 1] |= STATE_NZ_CTXT_R1
2356: | STATE_NZ_CTXT_R2
2357: | STATE_D_DR_R1
2358: | STATE_H_R_R2;
2359: }
2360: }
2361: }
2362: }
2363: csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
2364: state[j] = csj;
2365: // Do half bottom of column
2366: if (sheight < 3)
2367: continue;
2368: j += sscanw;
2369: csj = state[j];
2370: } // end of 'top_half' block
2371: // If any of the two samples is not significant and has
2372: // not been visited in the current bit-plane we can not
2373: // skip them
2374: if ((((csj >> 1) | csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) {
2375: k = sk + (dscanw << 1);
2376: // Scan first row
2377: if ((csj & (STATE_SIG_R1 | STATE_VISITED_R1)) == 0) {
2378: // Use zero coding
2379: if (mq.decodeSymbol(zc_lut[csj & ZC_MASK]) != 0) {
2380: // Became significant
2381: // Use sign coding
2382: ctxt = SC_LUT[(csj >> SC_SHIFT_R1)
2383: & SC_MASK];
2384: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
2385: ^ (ctxt >>> SC_SPRED_SHIFT);
2386: // Update the data
2387: data[k] = (sym << 31) | setmask;
2388: // Update state information (significant bit,
2389: // visited bit, neighbor significant bit of
2390: // neighbors, non zero context of neighbors,
2391: // sign of neighbors)
2392: state[j + off_ul] |= STATE_NZ_CTXT_R2
2393: | STATE_D_DR_R2;
2394: state[j + off_ur] |= STATE_NZ_CTXT_R2
2395: | STATE_D_DL_R2;
2396: // Update sign state information of neighbors
2397: if (sym != 0) {
2398: csj |= STATE_SIG_R1 | STATE_VISITED_R1
2399: | STATE_NZ_CTXT_R2
2400: | STATE_V_U_R2
2401: | STATE_V_U_SIGN_R2;
2402: state[j - sscanw] |= STATE_NZ_CTXT_R2
2403: | STATE_V_D_R2
2404: | STATE_V_D_SIGN_R2;
2405: state[j + 1] |= STATE_NZ_CTXT_R1
2406: | STATE_NZ_CTXT_R2
2407: | STATE_H_L_R1
2408: | STATE_H_L_SIGN_R1
2409: | STATE_D_UL_R2;
2410: state[j - 1] |= STATE_NZ_CTXT_R1
2411: | STATE_NZ_CTXT_R2
2412: | STATE_H_R_R1
2413: | STATE_H_R_SIGN_R1
2414: | STATE_D_UR_R2;
2415: } else {
2416: csj |= STATE_SIG_R1 | STATE_VISITED_R1
2417: | STATE_NZ_CTXT_R2
2418: | STATE_V_U_R2;
2419: state[j - sscanw] |= STATE_NZ_CTXT_R2
2420: | STATE_V_D_R2;
2421: state[j + 1] |= STATE_NZ_CTXT_R1
2422: | STATE_NZ_CTXT_R2
2423: | STATE_H_L_R1 | STATE_D_UL_R2;
2424: state[j - 1] |= STATE_NZ_CTXT_R1
2425: | STATE_NZ_CTXT_R2
2426: | STATE_H_R_R1 | STATE_D_UR_R2;
2427: }
2428: }
2429: }
2430: if (sheight < 4) {
2431: csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
2432: state[j] = csj;
2433: continue;
2434: }
2435: // Scan second row
2436: if ((csj & (STATE_SIG_R2 | STATE_VISITED_R2)) == 0) {
2437: k += dscanw;
2438: // Use zero coding
2439: if (mq.decodeSymbol(zc_lut[(csj >>> STATE_SEP)
2440: & ZC_MASK]) != 0) {
2441: // Became significant
2442: // Use sign coding
2443: ctxt = SC_LUT[(csj >>> SC_SHIFT_R2)
2444: & SC_MASK];
2445: sym = mq.decodeSymbol(ctxt & SC_LUT_MASK)
2446: ^ (ctxt >>> SC_SPRED_SHIFT);
2447: // Update the data
2448: data[k] = (sym << 31) | setmask;
2449: // Update state information (significant bit,
2450: // visited bit, neighbor significant bit of
2451: // neighbors, non zero context of neighbors,
2452: // sign of neighbors)
2453: state[j + off_dl] |= STATE_NZ_CTXT_R1
2454: | STATE_D_UR_R1;
2455: state[j + off_dr] |= STATE_NZ_CTXT_R1
2456: | STATE_D_UL_R1;
2457: // Update sign state information of neighbors
2458: if (sym != 0) {
2459: csj |= STATE_SIG_R2 | STATE_VISITED_R2
2460: | STATE_NZ_CTXT_R1
2461: | STATE_V_D_R1
2462: | STATE_V_D_SIGN_R1;
2463: state[j + sscanw] |= STATE_NZ_CTXT_R1
2464: | STATE_V_U_R1
2465: | STATE_V_U_SIGN_R1;
2466: state[j + 1] |= STATE_NZ_CTXT_R1
2467: | STATE_NZ_CTXT_R2
2468: | STATE_D_DL_R1 | STATE_H_L_R2
2469: | STATE_H_L_SIGN_R2;
2470: state[j - 1] |= STATE_NZ_CTXT_R1
2471: | STATE_NZ_CTXT_R2
2472: | STATE_D_DR_R1 | STATE_H_R_R2
2473: | STATE_H_R_SIGN_R2;
2474: } else {
2475: csj |= STATE_SIG_R2 | STATE_VISITED_R2
2476: | STATE_NZ_CTXT_R1
2477: | STATE_V_D_R1;
2478: state[j + sscanw] |= STATE_NZ_CTXT_R1
2479: | STATE_V_U_R1;
2480: state[j + 1] |= STATE_NZ_CTXT_R1
2481: | STATE_NZ_CTXT_R2
2482: | STATE_D_DL_R1 | STATE_H_L_R2;
2483: state[j - 1] |= STATE_NZ_CTXT_R1
2484: | STATE_NZ_CTXT_R2
2485: | STATE_D_DR_R1 | STATE_H_R_R2;
2486: }
2487: }
2488: }
2489: }
2490: csj &= ~(STATE_VISITED_R1 | STATE_VISITED_R2);
2491: state[j] = csj;
2492: }
2493: }
2494:
2495: // Decode segment marker if we need to
2496: if ((options & OPT_SEG_SYMBOLS) != 0) {
2497: sym = mq.decodeSymbol(UNIF_CTXT) << 3;
2498: sym |= mq.decodeSymbol(UNIF_CTXT) << 2;
2499: sym |= mq.decodeSymbol(UNIF_CTXT) << 1;
2500: sym |= mq.decodeSymbol(UNIF_CTXT);
2501: // Set error condition accordingly
2502: error = sym != SEG_SYMBOL;
2503: } else { // We can not detect any errors
2504: error = false;
2505: }
2506:
2507: // Check the error resilient termination
2508: if (isterm && (options & OPT_PRED_TERM) != 0) {
2509: error = mq.checkPredTerm();
2510: }
2511:
2512: // Reset the MQ context states if we need to
2513: if ((options & OPT_RESET_MQ) != 0) {
2514: mq.resetCtxts();
2515: }
2516:
2517: // Return error condition
2518: return error;
2519: }
2520:
2521: /**
2522: * Conceals decoding errors detected in the last bit-plane. The
2523: * concealement resets the state of the decoded data to what it was before
2524: * the decoding of bit-plane 'bp' started. No more data should be decoded
2525: * after this method is called for this code-block's data to which it is
2526: * applied.
2527: *
2528: * @param cblk The code-block's data
2529: *
2530: * @param bp The last decoded bit-plane (which contains errors).
2531: * */
2532: private void conceal(DataBlk cblk, int bp) {
2533: int l; // line index
2534: int k; // array index
2535: int kmax; // 'k' limit
2536: int dk; // Value of data[k]
2537: int data[]; // the data array
2538: int setmask; // Bitmask to set approximation to 1/2 of
2539: // known interval on significant data
2540: int resetmask; // Bitmask to erase all the data from
2541: // bit-plane 'bp'
2542:
2543: // Initialize masks
2544: setmask = 1 << bp;
2545: resetmask = (-1) << (bp);
2546:
2547: // Get the data array
2548: data = (int[]) cblk.getData();
2549:
2550: // Visit each sample, apply the reset mask to it and add an
2551: // approximation if significant.
2552: for (l = cblk.h - 1, k = cblk.offset; l >= 0; l--) {
2553: for (kmax = k + cblk.w; k < kmax; k++) {
2554: dk = data[k];
2555: if ((dk & resetmask & 0x7FFFFFFF) != 0) {
2556: // Something was decoded in previous bit-planes => set the
2557: // approximation for previous bit-plane
2558: data[k] = (dk & resetmask) | setmask;
2559: } else {
2560: // Was insignificant in previous bit-planes = set to zero
2561: data[k] = 0;
2562: }
2563: }
2564: k += cblk.scanw - cblk.w;
2565: }
2566:
2567: }
2568: }
|