001/* 002 * $RCSfile: StdEntropyDecoder.java,v $ 003 * $Revision: 1.1 $ 004 * $Date: 2005/02/11 05:02:07 $ 005 * $State: Exp $ 006 * 007 * Class: StdEntropyDecoder 008 * 009 * Description: Entropy decoding engine of stripes in code-blocks 010 * 011 * 012 * 013 * COPYRIGHT: 014 * 015 * This software module was originally developed by Raphaël Grosbois and 016 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel 017 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David 018 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research 019 * Centre France S.A) in the course of development of the JPEG2000 020 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This 021 * software module is an implementation of a part of the JPEG 2000 022 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio 023 * Systems AB and Canon Research Centre France S.A (collectively JJ2000 024 * Partners) agree not to assert against ISO/IEC and users of the JPEG 025 * 2000 Standard (Users) any of their rights under the copyright, not 026 * including other intellectual property rights, for this software module 027 * with respect to the usage by ISO/IEC and Users of this software module 028 * or modifications thereof for use in hardware or software products 029 * claiming conformance to the JPEG 2000 Standard. Those intending to use 030 * this software module in hardware or software products are advised that 031 * their use may infringe existing patents. The original developers of 032 * this software module, JJ2000 Partners and ISO/IEC assume no liability 033 * for use of this software module or modifications thereof. No license 034 * or right to this software module is granted for non JPEG 2000 Standard 035 * conforming products. JJ2000 Partners have full right to use this 036 * software module for his/her own purpose, assign or donate this 037 * software module to any third party and to inhibit third parties from 038 * using this software module for non JPEG 2000 Standard conforming 039 * products. This copyright notice must be included in all copies or 040 * derivative works of this software module. 041 * 042 * Copyright (c) 1999/2000 JJ2000 Partners. 043 * */ 044package jj2000.j2k.entropy.decoder; 045import jj2000.j2k.decoder.DecoderSpecs; 046import jj2000.j2k.entropy.StdEntropyCoderOptions; 047import jj2000.j2k.image.DataBlk; 048import jj2000.j2k.image.DataBlkInt; 049import jj2000.j2k.util.ArrayUtil; 050import jj2000.j2k.util.FacilityManager; 051import jj2000.j2k.util.MsgLogger; 052import jj2000.j2k.wavelet.Subband; 053import jj2000.j2k.wavelet.synthesis.SubbandSyn; 054 055/** 056 * This class implements the JPEG 2000 entropy decoder, which codes stripes in 057 * code-blocks. This entropy decoding engine decodes one code-block at a time. 058 * 059 * The code-block are rectangular, with dimensions which must be powers of 060 * 2. Each dimension has to be no smaller than 4 and no larger than 256. The 061 * product of the two dimensions (i.e. area of the code-block) may not exceed 062 * 4096. 063 * 064 * Context 0 of the MQ-coder is used as the uniform one (uniform, non-adaptive 065 * probability distribution). Context 1 is used for RLC coding. Contexts 2-10 066 * are used for zero-coding (ZC), contexts 11-15 are used for sign-coding (SC) 067 * and contexts 16-18 are used for magnitude-refinement (MR). 068 * 069 * <P>This implementation also provides some timing features. They can be 070 * enabled by setting the 'DO_TIMING' constant of this class to true and 071 * recompiling. The timing uses the 'System.currentTimeMillis()' Java API 072 * call, which returns wall clock time, not the actual CPU time used. The 073 * timing results will be printed on the message output. Since the times 074 * reported are wall clock times and not CPU usage times they can not be added 075 * to find the total used time (i.e. some time might be counted in several 076 * places). When timing is disabled ('DO_TIMING' is false) there is no penalty 077 * if the compiler performs some basic optimizations. Even if not the penalty 078 * should be negligeable. 079 * */ 080public class StdEntropyDecoder extends EntropyDecoder 081 implements StdEntropyCoderOptions { 082 083 /** Whether to collect timing information or not: false. Used as a compile 084 * time directive. */ 085 private final static boolean DO_TIMING = false; 086 087 /** The cumulative wall time for the entropy coding engine, for each 088 * component. */ 089 private long time[]; 090 091 /** The bit based input for arithmetic coding bypass (i.e. raw) coding */ 092 private ByteToBitInput bin; 093 094 /** The MQ decoder to use. It has in as the underlying source of coded 095 * data. */ 096 private MQDecoder mq; 097 098 /** The decoder spec */ 099 private DecoderSpecs decSpec; 100 101 /** The options that are turned on, as flag bits. The options are 102 * 'OPT_TERM_PASS', 'OPT_RESET_MQ', 'OPT_VERT_STR_CAUSAL', 'OPT_BYPASS' and 103 * 'OPT_SEG_SYMBOLS' as defined in the StdEntropyCoderOptions interface 104 * 105 * @see StdEntropyCoderOptions 106 **/ 107 private int options; 108 109 /** Flag to indicate if we should try to detect errors or just ignore any 110 * error resilient information */ 111 private final boolean doer; 112 113 /** Flag to indicate if we should be verbose about bit stream errors 114 detected with the error resilience options */ 115 private final boolean verber; 116 117 /** Number of bits used for the Zero Coding lookup table */ 118 private static final int ZC_LUT_BITS = 8; 119 120 /** Zero Coding context lookup tables for the LH global orientation */ 121 private static final int ZC_LUT_LH[] = new int[1<<ZC_LUT_BITS]; 122 123 /** Zero Coding context lookup tables for the HL global orientation */ 124 private static final int ZC_LUT_HL[] = new int[1<<ZC_LUT_BITS]; 125 126 /** Zero Coding context lookup tables for the HH global orientation */ 127 private static final int ZC_LUT_HH[] = new int[1<<ZC_LUT_BITS]; 128 129 /** Number of bits used for the Sign Coding lookup table */ 130 private static final int SC_LUT_BITS = 9; 131 132 /** Sign Coding context lookup table. The index into the table is a 9 bit 133 * index, which correspond the the value in the 'state' array shifted by 134 * 'SC_SHIFT'. Bits 8-5 are the signs of the horizontal-left, 135 * horizontal-right, vertical-up and vertical-down neighbors, 136 * respectively. Bit 4 is not used (0 or 1 makes no difference). Bits 3-0 137 * are the significance of the horizontal-left, horizontal-right, 138 * vertical-up and vertical-down neighbors, respectively. The least 4 bits 139 * of the value in the lookup table define the context number and the sign 140 * bit defines the "sign predictor". */ 141 private static final int SC_LUT[] = new int[1<<SC_LUT_BITS]; 142 143 /** The mask to obtain the context index from the 'SC_LUT' */ 144 private static final int SC_LUT_MASK = (1<<4)-1; 145 146 /** The shift to obtain the sign predictor from the 'SC_LUT'. It must be 147 * an unsigned shift. */ 148 private static final int SC_SPRED_SHIFT = 31; 149 150 /** The sign bit for int data */ 151 private static final int INT_SIGN_BIT = 1<<31; 152 153 /** The number of bits used for the Magnitude Refinement lookup table */ 154 private static final int MR_LUT_BITS = 9; 155 156 /** Magnitude Refinement context lookup table */ 157 private static final int MR_LUT[] = new int[1<<MR_LUT_BITS]; 158 159 /** The number of contexts used */ 160 private static final int NUM_CTXTS = 19; 161 162 /** The RLC context */ 163 private static final int RLC_CTXT = 1; 164 165 /** The UNIFORM context (with a uniform probability distribution which 166 * does not adapt) */ 167 private static final int UNIF_CTXT = 0; 168 169 /** The initial states for the MQ coder */ 170 private static final int MQ_INIT[] = {46, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 171 0, 0, 0, 0, 0, 0, 0, 0}; 172 173 /** The 4 symbol segmentation marker (decimal 10, which is binary sequence 174 1010) */ 175 private static final int SEG_SYMBOL = 10; 176 177 /** 178 * The state array for entropy coding. Each element of the state array 179 * stores the state of two coefficients. The lower 16 bits store the state 180 * of a coefficient in row 'i' and column 'j', while the upper 16 bits 181 * store the state of a coefficient in row 'i+1' and column 'j'. The 'i' 182 * row is either the first or the third row of a stripe. This packing of 183 * the states into 32 bit words allows a faster scan of all coefficients 184 * on each coding pass and diminished the amount of data transferred. The 185 * size of the state array is increased by 1 on each side (top, bottom, 186 * left, right) to handle boundary conditions without any special logic. 187 * 188 * <P>The state of a coefficient is stored in the following way in the 189 * lower 16 bits, where bit 0 is the least significant bit. Bit 15 is the 190 * significance of a coefficient (0 if non-significant, 1 otherwise). Bit 191 * 14 is the visited state (i.e. if a coefficient has been coded in the 192 * significance propagation pass of the current bit-plane). Bit 13 is the 193 * "non zero-context" state (i.e. if one of the eight immediate neighbors 194 * is significant it is 1, otherwise is 0). Bits 12 to 9 store the sign of 195 * the already significant left, right, up and down neighbors (1 for 196 * negative, 0 for positive or not yet significant). Bit 8 indicates if 197 * the magnitude refinement has already been applied to the 198 * coefficient. Bits 7 to 4 store the significance of the left, right, up 199 * and down neighbors (1 for significant, 0 for non significant). Bits 3 200 * to 0 store the significance of the diagonal coefficients (up-left, 201 * up-right, down-left and down-right; 1 for significant, 0 for non 202 * significant). 203 * 204 * <P>The upper 16 bits the state is stored as in the lower 16 bits, 205 * but with the bits shifted up by 16. 206 * 207 * <P>The lower 16 bits are referred to as "row 1" ("R1") while the upper 208 * 16 bits are referred to as "row 2" ("R2"). 209 * */ 210 private final int state[]; 211 212 /** The separation between the upper and lower bits in the state array: 16 213 * */ 214 private static final int STATE_SEP = 16; 215 216 /** The flag bit for the significance in the state array, for row 1. */ 217 private static final int STATE_SIG_R1 = 1<<15; 218 219 /** The flag bit for the "visited" bit in the state array, for row 1. */ 220 private static final int STATE_VISITED_R1 = 1<<14; 221 222 /** The flag bit for the "not zero context" bit in the state array, for 223 * row 1. This bit is always the OR of bits STATE_H_L_R1, STATE_H_R_R1, 224 * STATE_V_U_R1, STATE_V_D_R1, STATE_D_UL_R1, STATE_D_UR_R1, STATE_D_DL_R1 225 * and STATE_D_DR_R1. */ 226 private static final int STATE_NZ_CTXT_R1 = 1<<13; 227 228 /** The flag bit for the horizontal-left sign in the state array, for row 229 * 1. This bit can only be set if the STATE_H_L_R1 is also set. */ 230 private static final int STATE_H_L_SIGN_R1 = 1<<12; 231 232 /** The flag bit for the horizontal-right sign in the state array, for 233 * row 1. This bit can only be set if the STATE_H_R_R1 is also set. */ 234 private static final int STATE_H_R_SIGN_R1 = 1<<11; 235 236 /** The flag bit for the vertical-up sign in the state array, for row 237 * 1. This bit can only be set if the STATE_V_U_R1 is also set. */ 238 private static final int STATE_V_U_SIGN_R1 = 1<<10; 239 240 /** The flag bit for the vertical-down sign in the state array, for row 241 * 1. This bit can only be set if the STATE_V_D_R1 is also set. */ 242 private static final int STATE_V_D_SIGN_R1 = 1<<9; 243 244 /** The flag bit for the previous MR primitive applied in the state array, 245 for row 1. */ 246 private static final int STATE_PREV_MR_R1 = 1<<8; 247 248 /** The flag bit for the horizontal-left significance in the state array, 249 for row 1. */ 250 private static final int STATE_H_L_R1 = 1<<7; 251 252 /** The flag bit for the horizontal-right significance in the state array, 253 for row 1. */ 254 private static final int STATE_H_R_R1 = 1<<6; 255 256 /** The flag bit for the vertical-up significance in the state array, for 257 row 1. */ 258 private static final int STATE_V_U_R1 = 1<<5; 259 260 /** The flag bit for the vertical-down significance in the state array, 261 for row 1. */ 262 private static final int STATE_V_D_R1 = 1<<4; 263 264 /** The flag bit for the diagonal up-left significance in the state array, 265 for row 1. */ 266 private static final int STATE_D_UL_R1 = 1<<3; 267 268 /** The flag bit for the diagonal up-right significance in the state 269 array, for row 1.*/ 270 private static final int STATE_D_UR_R1 = 1<<2; 271 272 /** The flag bit for the diagonal down-left significance in the state 273 array, for row 1. */ 274 private static final int STATE_D_DL_R1 = 1<<1; 275 276 /** The flag bit for the diagonal down-right significance in the state 277 array , for row 1.*/ 278 private static final int STATE_D_DR_R1 = 1; 279 280 /** The flag bit for the significance in the state array, for row 2. */ 281 private static final int STATE_SIG_R2 = STATE_SIG_R1<<STATE_SEP; 282 283 /** The flag bit for the "visited" bit in the state array, for row 2. */ 284 private static final int STATE_VISITED_R2 = STATE_VISITED_R1<<STATE_SEP; 285 286 /** The flag bit for the "not zero context" bit in the state array, for 287 * row 2. This bit is always the OR of bits STATE_H_L_R2, STATE_H_R_R2, 288 * STATE_V_U_R2, STATE_V_D_R2, STATE_D_UL_R2, STATE_D_UR_R2, STATE_D_DL_R2 289 * and STATE_D_DR_R2. */ 290 private static final int STATE_NZ_CTXT_R2 = STATE_NZ_CTXT_R1<<STATE_SEP; 291 292 /** The flag bit for the horizontal-left sign in the state array, for row 293 * 2. This bit can only be set if the STATE_H_L_R2 is also set. */ 294 private static final int STATE_H_L_SIGN_R2 = STATE_H_L_SIGN_R1<<STATE_SEP; 295 296 /** The flag bit for the horizontal-right sign in the state array, for 297 * row 2. This bit can only be set if the STATE_H_R_R2 is also set. */ 298 private static final int STATE_H_R_SIGN_R2 = STATE_H_R_SIGN_R1<<STATE_SEP; 299 300 /** The flag bit for the vertical-up sign in the state array, for row 301 * 2. This bit can only be set if the STATE_V_U_R2 is also set. */ 302 private static final int STATE_V_U_SIGN_R2 = STATE_V_U_SIGN_R1<<STATE_SEP; 303 304 /** The flag bit for the vertical-down sign in the state array, for row 305 * 2. This bit can only be set if the STATE_V_D_R2 is also set. */ 306 private static final int STATE_V_D_SIGN_R2 = STATE_V_D_SIGN_R1<<STATE_SEP; 307 308 /** The flag bit for the previous MR primitive applied in the state array, 309 for row 2. */ 310 private static final int STATE_PREV_MR_R2 = STATE_PREV_MR_R1<<STATE_SEP; 311 312 /** The flag bit for the horizontal-left significance in the state array, 313 for row 2. */ 314 private static final int STATE_H_L_R2 = STATE_H_L_R1<<STATE_SEP; 315 316 /** The flag bit for the horizontal-right significance in the state array, 317 for row 2. */ 318 private static final int STATE_H_R_R2 = STATE_H_R_R1<<STATE_SEP; 319 320 /** The flag bit for the vertical-up significance in the state array, for 321 row 2. */ 322 private static final int STATE_V_U_R2 = STATE_V_U_R1<<STATE_SEP; 323 324 /** The flag bit for the vertical-down significance in the state array, 325 for row 2. */ 326 private static final int STATE_V_D_R2 = STATE_V_D_R1<<STATE_SEP; 327 328 /** The flag bit for the diagonal up-left significance in the state array, 329 for row 2. */ 330 private static final int STATE_D_UL_R2 = STATE_D_UL_R1<<STATE_SEP; 331 332 /** The flag bit for the diagonal up-right significance in the state 333 array, for row 2.*/ 334 private static final int STATE_D_UR_R2 = STATE_D_UR_R1<<STATE_SEP; 335 336 /** The flag bit for the diagonal down-left significance in the state 337 array, for row 2. */ 338 private static final int STATE_D_DL_R2 = STATE_D_DL_R1<<STATE_SEP; 339 340 /** The flag bit for the diagonal down-right significance in the state 341 array , for row 2.*/ 342 private static final int STATE_D_DR_R2 = STATE_D_DR_R1<<STATE_SEP; 343 344 /** The mask to isolate the significance bits for row 1 and 2 of the state 345 * array. */ 346 private static final int SIG_MASK_R1R2 = STATE_SIG_R1|STATE_SIG_R2; 347 348 /** The mask to isolate the visited bits for row 1 and 2 of the state 349 * array. */ 350 private static final int VSTD_MASK_R1R2 = STATE_VISITED_R1|STATE_VISITED_R2; 351 352 /** The mask to isolate the bits necessary to identify RLC coding state 353 * (significant, visited and non-zero context, for row 1 and 2). */ 354 private static final int RLC_MASK_R1R2 = 355 STATE_SIG_R1|STATE_SIG_R2| 356 STATE_VISITED_R1|STATE_VISITED_R2| 357 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2; 358 359 /** The mask to obtain the ZC_LUT index from the 'state' information */ 360 // This is needed because of the STATE_V_D_SIGN, STATE_V_U_SIGN, 361 // STATE_H_R_SIGN, and STATE_H_L_SIGN bits. 362 private static final int ZC_MASK = (1<<8)-1; 363 364 /** The shift to obtain the SC index to 'SC_LUT' from the 'state' 365 * information, for row 1. */ 366 private static final int SC_SHIFT_R1 = 4; 367 368 /** The shift to obtain the SC index to 'SC_LUT' from the state 369 * information, for row 2. */ 370 private static final int SC_SHIFT_R2 = SC_SHIFT_R1+STATE_SEP; 371 372 /** The bit mask to isolate the state bits relative to the sign coding 373 * lookup table ('SC_LUT'). */ 374 private static final int SC_MASK = (1<<SC_LUT_BITS)-1; 375 376 /** The mask to obtain the MR index to 'MR_LUT' from the 'state' 377 * information. It is to be applied after the 'MR_SHIFT' */ 378 private static final int MR_MASK = (1<<9)-1; 379 380 /** The source code-block to entropy code (avoids reallocation for each 381 code-block). */ 382 private DecLyrdCBlk srcblk; 383 384 /** The maximum number of bit planes to decode for any code-block */ 385 private int mQuit; 386 387 /** Static initializer: initializes all the lookup tables. */ 388 static { 389 int i,j; 390 double val, deltaMSE; 391 int inter_sc_lut[]; 392 int ds,us,rs,ls; 393 int dsgn,usgn,rsgn,lsgn; 394 int h,v; 395 396 // Initialize the zero coding lookup tables 397 398 // LH 399 400 // - No neighbors significant 401 ZC_LUT_LH[0] = 2; 402 // - No horizontal or vertical neighbors significant 403 for (i=1; i<16; i++) { // Two or more diagonal coeffs significant 404 ZC_LUT_LH[i] = 4; 405 } 406 for (i=0; i<4; i++) { // Only one diagonal coeff significant 407 ZC_LUT_LH[1<<i] = 3; 408 } 409 // - No horizontal neighbors significant, diagonal irrelevant 410 for (i=0; i<16; i++) { 411 // Only one vertical coeff significant 412 ZC_LUT_LH[STATE_V_U_R1 | i] = 5; 413 ZC_LUT_LH[STATE_V_D_R1 | i] = 5; 414 // The two vertical coeffs significant 415 ZC_LUT_LH[STATE_V_U_R1 | STATE_V_D_R1 | i] = 6; 416 } 417 // - One horiz. neighbor significant, diagonal/vertical non-significant 418 ZC_LUT_LH[STATE_H_L_R1] = 7; 419 ZC_LUT_LH[STATE_H_R_R1] = 7; 420 // - One horiz. significant, no vertical significant, one or more 421 // diagonal significant 422 for (i=1; i<16; i++) { 423 ZC_LUT_LH[STATE_H_L_R1 | i] = 8; 424 ZC_LUT_LH[STATE_H_R_R1 | i] = 8; 425 } 426 // - One horiz. significant, one or more vertical significant, 427 // diagonal irrelevant 428 for (i=1; i<4; i++) { 429 for (j=0; j<16; j++) { 430 ZC_LUT_LH[STATE_H_L_R1 | (i<<4) | j] = 9; 431 ZC_LUT_LH[STATE_H_R_R1 | (i<<4) | j] = 9; 432 } 433 } 434 // - Two horiz. significant, others irrelevant 435 for (i=0; i<64; i++) { 436 ZC_LUT_LH[STATE_H_L_R1 | STATE_H_R_R1 | i] = 10; 437 } 438 439 // HL 440 441 // - No neighbors significant 442 ZC_LUT_HL[0] = 2; 443 // - No horizontal or vertical neighbors significant 444 for (i=1; i<16; i++) { // Two or more diagonal coeffs significant 445 ZC_LUT_HL[i] = 4; 446 } 447 for (i=0; i<4; i++) { // Only one diagonal coeff significant 448 ZC_LUT_HL[1<<i] = 3; 449 } 450 // - No vertical significant, diagonal irrelevant 451 for (i=0; i<16; i++) { 452 // One horiz. significant 453 ZC_LUT_HL[STATE_H_L_R1 | i] = 5; 454 ZC_LUT_HL[STATE_H_R_R1 | i] = 5; 455 // Two horiz. significant 456 ZC_LUT_HL[STATE_H_L_R1 | STATE_H_R_R1 | i] = 6; 457 } 458 // - One vert. significant, diagonal/horizontal non-significant 459 ZC_LUT_HL[STATE_V_U_R1] = 7; 460 ZC_LUT_HL[STATE_V_D_R1] = 7; 461 // - One vert. significant, horizontal non-significant, one or more 462 // diag. significant 463 for (i=1; i<16; i++) { 464 ZC_LUT_HL[STATE_V_U_R1 | i] = 8; 465 ZC_LUT_HL[STATE_V_D_R1 | i] = 8; 466 } 467 // - One vertical significant, one or more horizontal significant, 468 // diagonal irrelevant 469 for (i=1; i<4; i++) { 470 for (j=0; j<16; j++) { 471 ZC_LUT_HL[(i<<6) | STATE_V_U_R1 | j] = 9; 472 ZC_LUT_HL[(i<<6) | STATE_V_D_R1 | j] = 9; 473 } 474 } 475 // - Two vertical significant, others irrelevant 476 for (i=0; i<4; i++) { 477 for (j=0; j<16; j++) { 478 ZC_LUT_HL[(i<<6) | STATE_V_U_R1 | STATE_V_D_R1 | j] = 10; 479 } 480 } 481 482 // HH 483 int[] twoBits = {3,5,6,9,10,12}; // Figures (between 0 and 15) 484 // countaning 2 and only 2 bits on in its binary representation. 485 486 int[] oneBit = {1,2,4,8}; // Figures (between 0 and 15) 487 // countaning 1 and only 1 bit on in its binary representation. 488 489 int[] twoLeast = {3,5,6,7,9,10,11,12,13,14,15}; // Figures 490 // (between 0 and 15) countaining, at least, 2 bits on in its 491 // binary representation. 492 493 int[] threeLeast = {7,11,13,14,15}; // Figures 494 // (between 0 and 15) countaining, at least, 3 bits on in its 495 // binary representation. 496 497 // - None significant 498 ZC_LUT_HH[0] = 2; 499 500 // - One horizontal+vertical significant, none diagonal 501 for(i=0; i<oneBit.length; i++) 502 ZC_LUT_HH[ oneBit[i]<<4 ] = 3; 503 504 // - Two or more horizontal+vertical significant, diagonal non-signif 505 for(i=0; i<twoLeast.length; i++) 506 ZC_LUT_HH[ twoLeast[i]<<4 ] = 4; 507 508 // - One diagonal significant, horiz./vert. non-significant 509 for(i=0; i<oneBit.length; i++) 510 ZC_LUT_HH[ oneBit[i] ] = 5; 511 512 // - One diagonal significant, one horiz.+vert. significant 513 for(i=0; i<oneBit.length; i++) 514 for(j=0; j<oneBit.length; j++) 515 ZC_LUT_HH[ (oneBit[i]<<4) | oneBit[j] ] = 6; 516 517 // - One diag signif, two or more horiz+vert signif 518 for(i=0; i<twoLeast.length; i++) 519 for(j=0; j<oneBit.length; j++) 520 ZC_LUT_HH[ (twoLeast[i]<<4) | oneBit[j] ] = 7; 521 522 // - Two diagonal significant, none horiz+vert significant 523 for(i=0; i<twoBits.length; i++) 524 ZC_LUT_HH[ twoBits[i] ] = 8; 525 526 // - Two diagonal significant, one or more horiz+vert significant 527 for(j=0; j<twoBits.length; j++) 528 for(i=1; i<16; i++) 529 ZC_LUT_HH[ (i<<4) | twoBits[j] ] = 9; 530 531 // - Three or more diagonal significant, horiz+vert irrelevant 532 for(i=0; i<16; i++) 533 for(j=0; j<threeLeast.length; j++) 534 ZC_LUT_HH[ (i<<4) | threeLeast[j] ] = 10; 535 536 537 // Initialize the SC lookup tables 538 539 // Use an intermediate sign code lookup table that is similar to the 540 // one in the VM text, in that it depends on the 'h' and 'v' 541 // quantities. The index into this table is a 6 bit index, the top 3 542 // bits are (h+1) and the low 3 bits (v+1). 543 inter_sc_lut = new int[36]; 544 inter_sc_lut[(2<<3)|2] = 15; 545 inter_sc_lut[(2<<3)|1] = 14; 546 inter_sc_lut[(2<<3)|0] = 13; 547 inter_sc_lut[(1<<3)|2] = 12; 548 inter_sc_lut[(1<<3)|1] = 11; 549 inter_sc_lut[(1<<3)|0] = 12 | INT_SIGN_BIT; 550 inter_sc_lut[(0<<3)|2] = 13 | INT_SIGN_BIT; 551 inter_sc_lut[(0<<3)|1] = 14 | INT_SIGN_BIT; 552 inter_sc_lut[(0<<3)|0] = 15 | INT_SIGN_BIT; 553 554 // Using the intermediate sign code lookup table create the final 555 // one. The index into this table is a 9 bit index, the low 4 bits are 556 // the significance of the 4 horizontal/vertical neighbors, while the 557 // top 4 bits are the signs of those neighbors. The bit in the middle 558 // is ignored. This index arrangement matches the state bits in the 559 // 'state' array, thus direct addressing of the table can be done from 560 // the sate information. 561 for (i=0; i<(1<<SC_LUT_BITS)-1; i++) { 562 ds = i & 0x01; // significance of down neighbor 563 us = (i >> 1) & 0x01; // significance of up neighbor 564 rs = (i >> 2) & 0x01; // significance of right neighbor 565 ls = (i >> 3) & 0x01; // significance of left neighbor 566 dsgn = (i >> 5) & 0x01; // sign of down neighbor 567 usgn = (i >> 6) & 0x01; // sign of up neighbor 568 rsgn = (i >> 7) & 0x01; // sign of right neighbor 569 lsgn = (i >> 8) & 0x01; // sign of left neighbor 570 // Calculate 'h' and 'v' as in VM text 571 h = ls*(1-2*lsgn)+rs*(1-2*rsgn); 572 h = (h >= -1) ? h : -1; 573 h = (h <= 1) ? h : 1; 574 v = us*(1-2*usgn)+ds*(1-2*dsgn); 575 v = (v >= -1) ? v : -1; 576 v = (v <= 1) ? v : 1; 577 // Get context and sign predictor from 'inter_sc_lut' 578 SC_LUT[i] = inter_sc_lut[(h+1)<<3|(v+1)]; 579 } 580 inter_sc_lut = null; 581 582 // Initialize the MR lookup tables 583 584 // None significant, prev MR off 585 MR_LUT[0] = 16; 586 // One or more significant, prev MR off 587 for (i=1; i<(1<<(MR_LUT_BITS-1)); i++) { 588 MR_LUT[i] = 17; 589 } 590 // Previous MR on, significance irrelevant 591 for (; i<(1<<MR_LUT_BITS); i++) { 592 MR_LUT[i] = 18; 593 } 594 } 595 596 /** 597 * Instantiates a new entropy decoder engine, with the specified source of 598 * data, nominal block width and height. 599 * 600 * @param src The source of data 601 * 602 * @param opt The options to use for this encoder. It is a mix of the 603 * 'OPT_TERM_PASS', 'OPT_RESET_MQ', 'OPT_VERT_STR_CAUSAL', 'OPT_BYPASS' and 604 * 'OPT_SEG_SYMBOLS' option flags. 605 * 606 * @param doer If true error detection will be performed, if any error 607 * detection features have been enabled. 608 * 609 * @param verber This flag indicates if the entropy decoder should be 610 * verbose about bit stream errors that are detected and concealed. 611 * */ 612 public StdEntropyDecoder(CodedCBlkDataSrcDec src, DecoderSpecs decSpec, 613 boolean doer, boolean verber, int mQuit) { 614 super(src); 615 616 this.decSpec = decSpec; 617 this.doer = doer; 618 this.verber = verber; 619 this.mQuit = mQuit; 620 621 // If we do timing create necessary structures 622 if (DO_TIMING) { 623 time = new long[src.getNumComps()]; 624 // If we are timing make sure that 'finalize' gets called. 625 System.runFinalizersOnExit(true); 626 } 627 628 // Initialize internal variables 629 state = new int[(decSpec.cblks.getMaxCBlkWidth()+2) * 630 ((decSpec.cblks.getMaxCBlkHeight()+1)/2+2)]; 631 } 632 633 /** 634 * Prints the timing information, if collected, and calls 'finalize' on 635 * the super class. 636 * */ 637 public void finalize() throws Throwable { 638 if (DO_TIMING) { 639 int c; 640 StringBuffer sb; 641 642 sb = new StringBuffer("StdEntropyDecoder decompression wall "+ 643 "clock time:"); 644 for (c=0; c<time.length; c++) { 645 sb.append("\n component "); 646 sb.append(c); 647 sb.append(": "); 648 sb.append(time[c]); 649 sb.append(" ms"); 650 } 651 FacilityManager.getMsgLogger(). 652 printmsg(MsgLogger.INFO,sb.toString()); 653 } 654 super.finalize(); 655 } 656 657 /** 658 * Returns the specified code-block in the current tile for the specified 659 * component, as a copy (see below). 660 * 661 * <P>The returned code-block may be progressive, which is indicated by 662 * the 'progressive' variable of the returned 'DataBlk' object. If a 663 * code-block is progressive it means that in a later request to this 664 * method for the same code-block it is possible to retrieve data which is 665 * a better approximation, since meanwhile more data to decode for the 666 * code-block could have been received. If the code-block is not 667 * progressive then later calls to this method for the same code-block 668 * will return the exact same data values. 669 * 670 * <P>The data returned by this method is always a copy of the internal 671 * data of this object, if any, and it can be modified "in place" without 672 * any problems after being returned. The 'offset' of the returned data is 673 * 0, and the 'scanw' is the same as the code-block width. See the 674 * 'DataBlk' class. 675 * 676 * <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object 677 * contain the coordinates of the top-left corner of the block, with 678 * respect to the tile, not the subband. 679 * 680 * @param c The component for which to return the next code-block. 681 * 682 * @param m The vertical index of the code-block to return, in the 683 * specified subband. 684 * 685 * @param n The horizontal index of the code-block to return, in the 686 * specified subband. 687 * 688 * @param sb The subband in which the code-block to return is. 689 * 690 * @param cblk If non-null this object will be used to return the new 691 * code-block. If null a new one will be allocated and returned. If the 692 * "data" array of the object is non-null it will be reused, if possible, 693 * to return the data. 694 * 695 * @return The next code-block in the current tile for component 'n', or 696 * null if all code-blocks for the current tile have been returned. 697 * 698 * @see DataBlk 699 * */ 700 public DataBlk getCodeBlock(int c, int m, int n, SubbandSyn sb, 701 DataBlk cblk) { 702 long stime = 0L; // Start time for timed sections 703 int zc_lut[]; // The ZC lookup table to use 704 int out_data[]; // The outupt data buffer 705 int npasses; // The number of coding passes to perform 706 int curbp; // The current magnitude bit-plane (starts at 30) 707 boolean error; // Error indicator 708 int tslen; // Length of first terminated segment 709 int tsidx; // Index of current terminated segment 710 ByteInputBuffer in = null; 711 712 boolean isterm; 713 714 // Get the code-block to decode 715 srcblk = src.getCodeBlock(c,m,n,sb,1,-1,srcblk); 716 if (DO_TIMING) stime = System.currentTimeMillis(); 717 718 // Retrieve options from decSpec 719 options = ((Integer)decSpec.ecopts. 720 getTileCompVal(tIdx,c)).intValue(); 721 722 // Reset state 723 ArrayUtil.intArraySet(state,0); 724 725 // Initialize output code-block 726 if (cblk==null) { 727 cblk = new DataBlkInt(); 728 } 729 cblk.progressive = srcblk.prog; 730 cblk.ulx = srcblk.ulx; 731 cblk.uly = srcblk.uly; 732 cblk.w = srcblk.w; 733 cblk.h = srcblk.h; 734 cblk.offset = 0; 735 cblk.scanw = cblk.w; 736 out_data = (int[])cblk.getData(); 737 738 if (out_data == null || out_data.length < srcblk.w*srcblk.h) { 739 out_data = new int[srcblk.w*srcblk.h]; 740 cblk.setData(out_data); 741 } else { 742 // Set data values to 0 743 ArrayUtil.intArraySet(out_data,0); 744 } 745 746 if (srcblk.nl <= 0 || srcblk.nTrunc <= 0) { 747 // 0 layers => no data to decode => return all 0s 748 return cblk; 749 } 750 751 // Get the length of the first terminated segment 752 tslen = (srcblk.tsLengths == null) ? srcblk.dl : srcblk.tsLengths[0]; 753 tsidx = 0; 754 // Initialize for decoding 755 npasses = srcblk.nTrunc; 756 if (mq == null) { 757 in = new ByteInputBuffer(srcblk.data,0,tslen); 758 mq = new MQDecoder(in ,NUM_CTXTS,MQ_INIT); 759 } 760 else { 761 // We always start by an MQ segment 762 mq.nextSegment(srcblk.data,0,tslen); 763 mq.resetCtxts(); 764 } 765 error = false; 766 767 if ((options & OPT_BYPASS) != 0) { 768 if(bin==null){ 769 if (in == null) in = mq.getByteInputBuffer(); 770 bin = new ByteToBitInput(in); 771 } 772 } 773 774 // Choose correct ZC lookup table for global orientation 775 switch (sb.orientation) { 776 case Subband.WT_ORIENT_HL: 777 zc_lut = ZC_LUT_HL; 778 break; 779 case Subband.WT_ORIENT_LH: 780 case Subband.WT_ORIENT_LL: 781 zc_lut = ZC_LUT_LH; 782 break; 783 case Subband.WT_ORIENT_HH: 784 zc_lut = ZC_LUT_HH; 785 break; 786 default: 787 throw new Error("JJ2000 internal error"); 788 } 789 790 // NOTE: we don't currently detect which is the last magnitude 791 // bit-plane so that 'isterm' is true for the last pass of it. Doing so 792 // would aid marginally in error detection with the predictable error 793 // resilient MQ termination. However, determining which is the last 794 // magnitude bit-plane is quite hard (due to ROI, quantization, etc.) 795 // and in any case the predictable error resilient termination used 796 // without the arithmetic coding bypass and/or regular termination 797 // modes is almost useless. 798 799 // Loop on bit-planes and passes 800 801 curbp = 30-srcblk.skipMSBP; 802 803 // Check for maximum number of bitplanes quit condition 804 if(mQuit != -1 && (mQuit*3-2) < npasses){ 805 npasses = mQuit*3-2; 806 } 807 808 // First bit-plane has only the cleanup pass 809 if (curbp >= 0 && npasses > 0) { 810 isterm = (options & OPT_TERM_PASS) != 0 || 811 ((options & OPT_BYPASS) != 0 && 812 (31-NUM_NON_BYPASS_MS_BP-srcblk.skipMSBP)>=curbp); 813 error = cleanuppass(cblk,mq,curbp,state,zc_lut,isterm); 814 npasses--; 815 if (!error || !doer) curbp--; 816 } 817 818 // Other bit-planes have the three coding passes 819 if (!error || !doer) { 820 while (curbp >= 0 && npasses > 0) { 821 822 if((options & OPT_BYPASS) != 0 && 823 (curbp < 31-NUM_NON_BYPASS_MS_BP-srcblk.skipMSBP)){ 824 // Use bypass decoding mode (only all bit-planes 825 // after the first 4 bit-planes). 826 827 // Here starts a new raw segment 828 bin.setByteArray(null,-1,srcblk.tsLengths[++tsidx]); 829 isterm = (options & OPT_TERM_PASS) != 0; 830 error = rawSigProgPass(cblk,bin,curbp,state,isterm); 831 npasses--; 832 if (npasses <= 0 || (error && doer)) break; 833 834 if ((options & OPT_TERM_PASS) != 0) { 835 // Start a new raw segment 836 bin.setByteArray(null,-1,srcblk.tsLengths[++tsidx]); 837 } 838 isterm = (options & OPT_TERM_PASS) != 0 || 839 ((options & OPT_BYPASS) != 0 && 840 (31-NUM_NON_BYPASS_MS_BP-srcblk.skipMSBP>curbp)); 841 error = rawMagRefPass(cblk,bin,curbp,state,isterm); 842 } 843 else {// Do not use bypass decoding mode 844 if ((options & OPT_TERM_PASS) != 0) { 845 // Here starts a new MQ segment 846 mq.nextSegment(null,-1,srcblk.tsLengths[++tsidx]); 847 } 848 isterm = (options & OPT_TERM_PASS) != 0; 849 error = sigProgPass(cblk,mq,curbp,state,zc_lut,isterm); 850 npasses--; 851 if (npasses <= 0 || (error && doer)) break; 852 853 if ((options & OPT_TERM_PASS) != 0) { 854 // Here starts a new MQ segment 855 mq.nextSegment(null,-1,srcblk.tsLengths[++tsidx]); 856 } 857 isterm = (options & OPT_TERM_PASS) != 0 || 858 ((options & OPT_BYPASS) != 0 && 859 (31-NUM_NON_BYPASS_MS_BP-srcblk.skipMSBP>curbp)); 860 error = magRefPass(cblk,mq,curbp,state,isterm); 861 } 862 863 npasses--; 864 if (npasses <= 0 || (error && doer)) break; 865 866 if ((options & OPT_TERM_PASS) != 0 || 867 ((options & OPT_BYPASS) != 0 && 868 (curbp < 31-NUM_NON_BYPASS_MS_BP-srcblk.skipMSBP))) { 869 // Here starts a new MQ segment 870 mq.nextSegment(null,-1,srcblk.tsLengths[++tsidx]); 871 } 872 isterm = (options & OPT_TERM_PASS) != 0 || 873 ((options & OPT_BYPASS) != 0 && 874 (31-NUM_NON_BYPASS_MS_BP-srcblk.skipMSBP)>=curbp); 875 error = cleanuppass(cblk,mq,curbp,state,zc_lut,isterm); 876 npasses--; 877 if (error) break; 878 // Goto next bit-plane 879 curbp--; 880 } 881 } 882 883 // If an error ocurred conceal it 884 if (error && doer) { 885 if (verber) { 886 FacilityManager.getMsgLogger(). 887 printmsg(MsgLogger.WARNING, 888 "Error detected at bit-plane "+curbp+ 889 " in code-block ("+m+","+n+"), sb_idx "+ 890 sb.sbandIdx+", res. level "+sb.resLvl+ 891 ". Concealing..."); 892 } 893 894 conceal(cblk,curbp); 895 } 896 897 if (DO_TIMING) time[c] += System.currentTimeMillis()-stime; 898 899 // Return decoded block 900 return cblk; 901 } 902 903 /** 904 * Returns the specified code-block in the current tile for the specified 905 * component (as a reference or copy). 906 * 907 * <P>The returned code-block may be progressive, which is indicated by 908 * the 'progressive' variable of the returned 'DataBlk' 909 * object. If a code-block is progressive it means that in a later request 910 * to this method for the same code-block it is possible to retrieve data 911 * which is a better approximation, since meanwhile more data to decode 912 * for the code-block could have been received. If the code-block is not 913 * progressive then later calls to this method for the same code-block 914 * will return the exact same data values. 915 * 916 * <P>The data returned by this method can be the data in the internal 917 * buffer of this object, if any, and thus can not be modified by the 918 * caller. The 'offset' and 'scanw' of the returned data can be 919 * arbitrary. See the 'DataBlk' class. 920 * 921 * <P>The 'ulx' and 'uly' members of the returned 'DataBlk' object 922 * contain the coordinates of the top-left corner of the block, with 923 * respect to the tile, not the subband. 924 * 925 * @param c The component for which to return the next code-block. 926 * 927 * @param m The vertical index of the code-block to return, in the 928 * specified subband. 929 * 930 * @param n The horizontal index of the code-block to return, in the 931 * specified subband. 932 * 933 * @param sb The subband in which the code-block to return is. 934 * 935 * @param cblk If non-null this object will be used to return the new 936 * code-block. If null a new one will be allocated and returned. If the 937 * "data" array of the object is non-null it will be reused, if possible, 938 * to return the data. 939 * 940 * @return The next code-block in the current tile for component 'n', or 941 * null if all code-blocks for the current tile have been returned. 942 * 943 * @see DataBlk 944 * */ 945 public DataBlk getInternCodeBlock(int c, int m, int n, SubbandSyn sb, 946 DataBlk cblk) { 947 return getCodeBlock(c,m,n,sb,cblk); 948 } 949 950 /** 951 * Performs the significance propagation pass on the specified data and 952 * bit-plane. It decodes all insignificant samples which have, at least, 953 * one of its immediate eight neighbors already significant, using the ZC 954 * and SC primitives as needed. It toggles the "visited" state bit to 1 955 * for all those samples. 956 * 957 * <P>This method also checks for segmentation markers if those are 958 * present and returns true if an error is detected, or false 959 * otherwise. If an error is detected it means that the bit stream contains 960 * some erroneous bit that have led to the decoding of incorrect 961 * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If 962 * 'true' is returned the 'conceal' method should be called and no more 963 * passes should be decoded for this code-block's bit stream. 964 * 965 * @param cblk The code-block data to decode 966 * 967 * @param mq The MQ-coder to use 968 * 969 * @param bp The bit-plane to decode 970 * 971 * @param state The state information for the code-block 972 * 973 * @param zc_lut The ZC lookup table to use in ZC. 974 * 975 * @param isterm If this pass has been terminated. If the pass has been 976 * terminated it can be used to check error resilience. 977 * 978 * @return True if an error was detected in the bit stream, false otherwise. 979 * */ 980 private boolean sigProgPass(DataBlk cblk, MQDecoder mq, int bp, 981 int state[], int zc_lut[], boolean isterm) { 982 int j,sj; // The state index for line and stripe 983 int k,sk; // The data index for line and stripe 984 int dscanw; // The data scan-width 985 int sscanw; // The state scan-width 986 int jstep; // Stripe to stripe step for 'sj' 987 int kstep; // Stripe to stripe step for 'sk' 988 int stopsk; // The loop limit on the variable sk 989 int csj; // Local copy (i.e. cached) of 'state[j]' 990 int setmask; // The mask to set current and lower bit-planes to 1/2 991 // approximation 992 int sym; // The symbol to code 993 int ctxt; // The context to use 994 int data[]; // The data buffer 995 int s; // The stripe index 996 boolean causal; // Flag to indicate if stripe-causal context 997 // formation is to be used 998 int nstripes; // The number of stripes in the code-block 999 int sheight; // Height of the current stripe 1000 int off_ul,off_ur,off_dr,off_dl; // offsets 1001 boolean error; // The error condition 1002 1003 // Initialize local variables 1004 dscanw = cblk.scanw; 1005 sscanw = cblk.w+2; 1006 jstep = sscanw*STRIPE_HEIGHT/2-cblk.w; 1007 kstep = dscanw*STRIPE_HEIGHT-cblk.w; 1008 setmask = (3<<bp)>>1; 1009 data = (int[]) cblk.getData(); 1010 nstripes = (cblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 1011 causal = (options & OPT_VERT_STR_CAUSAL) != 0; 1012 1013 // Pre-calculate offsets in 'state' for diagonal neighbors 1014 off_ul = -sscanw-1; // up-left 1015 off_ur = -sscanw+1; // up-right 1016 off_dr = sscanw+1; // down-right 1017 off_dl = sscanw-1; // down-left 1018 1019 // Decode stripe by stripe 1020 sk = cblk.offset; 1021 sj = sscanw+1; 1022 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 1023 sheight = (s != 0) ? STRIPE_HEIGHT : 1024 cblk.h-(nstripes-1)*STRIPE_HEIGHT; 1025 stopsk = sk+cblk.w; 1026 // Scan by set of 1 stripe column at a time 1027 for (; sk < stopsk; sk++, sj++) { 1028 // Do half top of column 1029 j = sj; 1030 csj = state[j]; 1031 // If any of the two samples is not significant and has a 1032 // non-zero context (i.e. some neighbor is significant) we can 1033 // not skip them 1034 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 1035 k = sk; 1036 // Scan first row 1037 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 1038 STATE_NZ_CTXT_R1) { 1039 // Use zero coding 1040 if (mq.decodeSymbol(zc_lut[csj&ZC_MASK]) != 0) { 1041 // Became significant 1042 // Use sign coding 1043 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 1044 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 1045 (ctxt>>>SC_SPRED_SHIFT); 1046 // Update data 1047 data[k] = (sym<<31) | setmask; 1048 // Update state information (significant bit, 1049 // visited bit, neighbor significant bit of 1050 // neighbors, non zero context of neighbors, sign 1051 // of neighbors) 1052 if (!causal) { 1053 // If in causal mode do not change contexts of 1054 // previous stripe. 1055 state[j+off_ul] |= 1056 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 1057 state[j+off_ur] |= 1058 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 1059 } 1060 // Update sign state information of neighbors 1061 if (sym != 0) { 1062 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1063 STATE_NZ_CTXT_R2| 1064 STATE_V_U_R2|STATE_V_U_SIGN_R2; 1065 if (!causal) { 1066 // If in causal mode do not change 1067 // contexts of previous stripe. 1068 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1069 STATE_V_D_R2|STATE_V_D_SIGN_R2; 1070 } 1071 state[j+1] |= 1072 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1073 STATE_H_L_R1|STATE_H_L_SIGN_R1| 1074 STATE_D_UL_R2; 1075 state[j-1] |= 1076 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1077 STATE_H_R_R1|STATE_H_R_SIGN_R1| 1078 STATE_D_UR_R2; 1079 } 1080 else { 1081 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1082 STATE_NZ_CTXT_R2|STATE_V_U_R2; 1083 if (!causal) { 1084 // If in causal mode do not change 1085 // contexts of previous stripe. 1086 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1087 STATE_V_D_R2; 1088 } 1089 state[j+1] |= 1090 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1091 STATE_H_L_R1|STATE_D_UL_R2; 1092 state[j-1] |= 1093 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1094 STATE_H_R_R1|STATE_D_UR_R2; 1095 } 1096 } 1097 else { 1098 csj |= STATE_VISITED_R1; 1099 } 1100 } 1101 if (sheight < 2) { 1102 state[j] = csj; 1103 continue; 1104 } 1105 // Scan second row 1106 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 1107 STATE_NZ_CTXT_R2) { 1108 k += dscanw; 1109 // Use zero coding 1110 if (mq.decodeSymbol(zc_lut[(csj>>>STATE_SEP)& 1111 ZC_MASK]) != 0) { 1112 // Became significant 1113 // Use sign coding 1114 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 1115 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 1116 (ctxt>>>SC_SPRED_SHIFT); 1117 // Update data 1118 data[k] = (sym<<31) | setmask; 1119 // Update state information (significant bit, 1120 // visited bit, neighbor significant bit of 1121 // neighbors, non zero context of neighbors, sign 1122 // of neighbors) 1123 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 1124 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 1125 // Update sign state information of neighbors 1126 if (sym != 0) { 1127 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1128 STATE_NZ_CTXT_R1| 1129 STATE_V_D_R1|STATE_V_D_SIGN_R1; 1130 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1131 STATE_V_U_R1|STATE_V_U_SIGN_R1; 1132 state[j+1] |= 1133 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1134 STATE_D_DL_R1| 1135 STATE_H_L_R2|STATE_H_L_SIGN_R2; 1136 state[j-1] |= 1137 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1138 STATE_D_DR_R1| 1139 STATE_H_R_R2|STATE_H_R_SIGN_R2; 1140 } 1141 else { 1142 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1143 STATE_NZ_CTXT_R1|STATE_V_D_R1; 1144 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1145 STATE_V_U_R1; 1146 state[j+1] |= 1147 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1148 STATE_D_DL_R1|STATE_H_L_R2; 1149 state[j-1] |= 1150 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1151 STATE_D_DR_R1|STATE_H_R_R2; 1152 } 1153 } 1154 else { 1155 csj |= STATE_VISITED_R2; 1156 } 1157 } 1158 state[j] = csj; 1159 } 1160 // Do half bottom of column 1161 if (sheight < 3) continue; 1162 j += sscanw; 1163 csj = state[j]; 1164 // If any of the two samples is not significant and has a 1165 // non-zero context (i.e. some neighbor is significant) we can 1166 // not skip them 1167 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 1168 k = sk+(dscanw<<1); 1169 // Scan first row 1170 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 1171 STATE_NZ_CTXT_R1) { 1172 // Use zero coding 1173 if (mq.decodeSymbol(zc_lut[csj&ZC_MASK]) != 0) { 1174 // Became significant 1175 // Use sign coding 1176 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 1177 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 1178 (ctxt>>>SC_SPRED_SHIFT); 1179 // Update data 1180 data[k] = (sym<<31) | setmask; 1181 // Update state information (significant bit, 1182 // visited bit, neighbor significant bit of 1183 // neighbors, non zero context of neighbors, sign 1184 // of neighbors) 1185 state[j+off_ul] |= 1186 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 1187 state[j+off_ur] |= 1188 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 1189 // Update sign state information of neighbors 1190 if (sym != 0) { 1191 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1192 STATE_NZ_CTXT_R2| 1193 STATE_V_U_R2|STATE_V_U_SIGN_R2; 1194 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1195 STATE_V_D_R2|STATE_V_D_SIGN_R2; 1196 state[j+1] |= 1197 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1198 STATE_H_L_R1|STATE_H_L_SIGN_R1| 1199 STATE_D_UL_R2; 1200 state[j-1] |= 1201 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1202 STATE_H_R_R1|STATE_H_R_SIGN_R1| 1203 STATE_D_UR_R2; 1204 } 1205 else { 1206 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1207 STATE_NZ_CTXT_R2|STATE_V_U_R2; 1208 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1209 STATE_V_D_R2; 1210 state[j+1] |= 1211 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1212 STATE_H_L_R1|STATE_D_UL_R2; 1213 state[j-1] |= 1214 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1215 STATE_H_R_R1|STATE_D_UR_R2; 1216 } 1217 } 1218 else { 1219 csj |= STATE_VISITED_R1; 1220 } 1221 } 1222 if (sheight < 4) { 1223 state[j] = csj; 1224 continue; 1225 } 1226 // Scan second row 1227 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 1228 STATE_NZ_CTXT_R2) { 1229 k += dscanw; 1230 // Use zero coding 1231 if (mq.decodeSymbol(zc_lut[(csj>>>STATE_SEP)& 1232 ZC_MASK]) != 0) { 1233 // Became significant 1234 // Use sign coding 1235 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 1236 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 1237 (ctxt>>>SC_SPRED_SHIFT); 1238 // Update data 1239 data[k] = (sym<<31) | setmask; 1240 // Update state information (significant bit, 1241 // visited bit, neighbor significant bit of 1242 // neighbors, non zero context of neighbors, sign 1243 // of neighbors) 1244 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 1245 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 1246 // Update sign state information of neighbors 1247 if (sym != 0) { 1248 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1249 STATE_NZ_CTXT_R1| 1250 STATE_V_D_R1|STATE_V_D_SIGN_R1; 1251 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1252 STATE_V_U_R1|STATE_V_U_SIGN_R1; 1253 state[j+1] |= 1254 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1255 STATE_D_DL_R1| 1256 STATE_H_L_R2|STATE_H_L_SIGN_R2; 1257 state[j-1] |= 1258 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1259 STATE_D_DR_R1| 1260 STATE_H_R_R2|STATE_H_R_SIGN_R2; 1261 } 1262 else { 1263 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1264 STATE_NZ_CTXT_R1|STATE_V_D_R1; 1265 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1266 STATE_V_U_R1; 1267 state[j+1] |= 1268 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1269 STATE_D_DL_R1|STATE_H_L_R2; 1270 state[j-1] |= 1271 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1272 STATE_D_DR_R1|STATE_H_R_R2; 1273 } 1274 } 1275 else { 1276 csj |= STATE_VISITED_R2; 1277 } 1278 } 1279 state[j] = csj; 1280 } 1281 } 1282 } 1283 1284 error = false; 1285 1286 // Check the error resilient termination 1287 if (isterm && (options & OPT_PRED_TERM) != 0) { 1288 error = mq.checkPredTerm(); 1289 } 1290 1291 // Reset the MQ context states if we need to 1292 if ((options & OPT_RESET_MQ) != 0) { 1293 mq.resetCtxts(); 1294 } 1295 1296 // Return error condition 1297 return error; 1298 } 1299 1300 /** 1301 * Performs the significance propagation pass on the specified data and 1302 * bit-plane. It decodes all insignificant samples which have, at least, one 1303 * of its immediate eight neighbors already significant, using the ZC and 1304 * SC primitives as needed. It toggles the "visited" state bit to 1 for 1305 * all those samples. 1306 * 1307 * <P>This method bypasses the arithmetic coder and reads "raw" symbols 1308 * from the bit stream. 1309 * 1310 * <P>This method also checks for segmentation markers if those are 1311 * present and returns true if an error is detected, or false 1312 * otherwise. If an error is detected it measn that the bit stream contains 1313 * some erroneous bit that have led to the decoding of incorrect 1314 * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If 1315 * 'true' is returned the 'conceal' method should be called and no more 1316 * passes should be decoded for this code-block's bit stream. 1317 * 1318 * @param cblk The code-block data to decode 1319 * 1320 * @param bin The raw bit based input 1321 * 1322 * @param bp The bit-plane to decode 1323 * 1324 * @param state The state information for the code-block 1325 * 1326 * @param isterm If this pass has been terminated. If the pass has been 1327 * terminated it can be used to check error resilience. 1328 * 1329 * @return True if an error was detected in the bit stream, false otherwise. 1330 * */ 1331 private boolean rawSigProgPass(DataBlk cblk, ByteToBitInput bin, int bp, 1332 int state[], boolean isterm) { 1333 int j,sj; // The state index for line and stripe 1334 int k,sk; // The data index for line and stripe 1335 int dscanw; // The data scan-width 1336 int sscanw; // The state scan-width 1337 int jstep; // Stripe to stripe step for 'sj' 1338 int kstep; // Stripe to stripe step for 'sk' 1339 int stopsk; // The loop limit on the variable sk 1340 int csj; // Local copy (i.e. cached) of 'state[j]' 1341 int setmask; // The mask to set current and lower bit-planes to 1/2 1342 // approximation 1343 int sym; // The symbol to code 1344 int data[]; // The data buffer 1345 int s; // The stripe index 1346 boolean causal; // Flag to indicate if stripe-causal context 1347 // formation is to be used 1348 int nstripes; // The number of stripes in the code-block 1349 int sheight; // Height of the current stripe 1350 int off_ul,off_ur,off_dr,off_dl; // offsets 1351 boolean error; // The error condition 1352 1353 // Initialize local variables 1354 dscanw = cblk.scanw; 1355 sscanw = cblk.w+2; 1356 jstep = sscanw*STRIPE_HEIGHT/2-cblk.w; 1357 kstep = dscanw*STRIPE_HEIGHT-cblk.w; 1358 setmask = (3<<bp)>>1; 1359 data = (int[]) cblk.getData(); 1360 nstripes = (cblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 1361 causal = (options & OPT_VERT_STR_CAUSAL) != 0; 1362 1363 // Pre-calculate offsets in 'state' for diagonal neighbors 1364 off_ul = -sscanw-1; // up-left 1365 off_ur = -sscanw+1; // up-right 1366 off_dr = sscanw+1; // down-right 1367 off_dl = sscanw-1; // down-left 1368 1369 // Decode stripe by stripe 1370 sk = cblk.offset; 1371 sj = sscanw+1; 1372 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 1373 sheight = (s != 0) ? STRIPE_HEIGHT : 1374 cblk.h-(nstripes-1)*STRIPE_HEIGHT; 1375 stopsk = sk+cblk.w; 1376 // Scan by set of 1 stripe column at a time 1377 for (; sk < stopsk; sk++, sj++) { 1378 // Do half top of column 1379 j = sj; 1380 csj = state[j]; 1381 // If any of the two samples is not significant and has a 1382 // non-zero context (i.e. some neighbor is significant) we can 1383 // not skip them 1384 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 1385 k = sk; 1386 // Scan first row 1387 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 1388 STATE_NZ_CTXT_R1) { 1389 // Use zero coding 1390 if (bin.readBit() != 0) { 1391 // Became significant 1392 // Use sign coding 1393 sym = bin.readBit(); 1394 // Update data 1395 data[k] = (sym<<31) | setmask; 1396 // Update state information (significant bit, 1397 // visited bit, neighbor significant bit of 1398 // neighbors, non zero context of neighbors, sign 1399 // of neighbors) 1400 if (!causal) { 1401 // If in causal mode do not change contexts of 1402 // previous stripe. 1403 state[j+off_ul] |= 1404 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 1405 state[j+off_ur] |= 1406 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 1407 } 1408 // Update sign state information of neighbors 1409 if (sym != 0) { 1410 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1411 STATE_NZ_CTXT_R2| 1412 STATE_V_U_R2|STATE_V_U_SIGN_R2; 1413 if (!causal) { 1414 // If in causal mode do not change 1415 // contexts of previous stripe. 1416 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1417 STATE_V_D_R2|STATE_V_D_SIGN_R2; 1418 } 1419 state[j+1] |= 1420 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1421 STATE_H_L_R1|STATE_H_L_SIGN_R1| 1422 STATE_D_UL_R2; 1423 state[j-1] |= 1424 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1425 STATE_H_R_R1|STATE_H_R_SIGN_R1| 1426 STATE_D_UR_R2; 1427 } 1428 else { 1429 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1430 STATE_NZ_CTXT_R2|STATE_V_U_R2; 1431 if (!causal) { 1432 // If in causal mode do not change 1433 // contexts of previous stripe. 1434 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1435 STATE_V_D_R2; 1436 } 1437 state[j+1] |= 1438 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1439 STATE_H_L_R1|STATE_D_UL_R2; 1440 state[j-1] |= 1441 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1442 STATE_H_R_R1|STATE_D_UR_R2; 1443 } 1444 } 1445 else { 1446 csj |= STATE_VISITED_R1; 1447 } 1448 } 1449 if (sheight < 2) { 1450 state[j] = csj; 1451 continue; 1452 } 1453 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 1454 STATE_NZ_CTXT_R2) { 1455 k += dscanw; 1456 // Use zero coding 1457 if (bin.readBit() != 0) { 1458 // Became significant 1459 // Use sign coding 1460 sym = bin.readBit(); 1461 // Update data 1462 data[k] = (sym<<31) | setmask; 1463 // Update state information (significant bit, 1464 // visited bit, neighbor significant bit of 1465 // neighbors, non zero context of neighbors, sign 1466 // of neighbors) 1467 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 1468 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 1469 // Update sign state information of neighbors 1470 if (sym != 0) { 1471 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1472 STATE_NZ_CTXT_R1| 1473 STATE_V_D_R1|STATE_V_D_SIGN_R1; 1474 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1475 STATE_V_U_R1|STATE_V_U_SIGN_R1; 1476 state[j+1] |= 1477 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1478 STATE_D_DL_R1| 1479 STATE_H_L_R2|STATE_H_L_SIGN_R2; 1480 state[j-1] |= 1481 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1482 STATE_D_DR_R1| 1483 STATE_H_R_R2|STATE_H_R_SIGN_R2; 1484 } 1485 else { 1486 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1487 STATE_NZ_CTXT_R1|STATE_V_D_R1; 1488 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1489 STATE_V_U_R1; 1490 state[j+1] |= 1491 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1492 STATE_D_DL_R1|STATE_H_L_R2; 1493 state[j-1] |= 1494 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1495 STATE_D_DR_R1|STATE_H_R_R2; 1496 } 1497 } 1498 else { 1499 csj |= STATE_VISITED_R2; 1500 } 1501 } 1502 state[j] = csj; 1503 } 1504 // Do half bottom of column 1505 if (sheight < 3) continue; 1506 j += sscanw; 1507 csj = state[j]; 1508 // If any of the two samples is not significant and has a 1509 // non-zero context (i.e. some neighbor is significant) we can 1510 // not skip them 1511 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 1512 k = sk+(dscanw<<1); 1513 // Scan first row 1514 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 1515 STATE_NZ_CTXT_R1) { 1516 // Use zero coding 1517 if (bin.readBit() != 0) { 1518 // Became significant 1519 // Use sign coding 1520 sym = bin.readBit(); 1521 // Update data 1522 data[k] = (sym<<31) | setmask; 1523 // Update state information (significant bit, 1524 // visited bit, neighbor significant bit of 1525 // neighbors, non zero context of neighbors, sign 1526 // of neighbors) 1527 state[j+off_ul] |= 1528 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 1529 state[j+off_ur] |= 1530 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 1531 // Update sign state information of neighbors 1532 if (sym != 0) { 1533 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1534 STATE_NZ_CTXT_R2| 1535 STATE_V_U_R2|STATE_V_U_SIGN_R2; 1536 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1537 STATE_V_D_R2|STATE_V_D_SIGN_R2; 1538 state[j+1] |= 1539 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1540 STATE_H_L_R1|STATE_H_L_SIGN_R1| 1541 STATE_D_UL_R2; 1542 state[j-1] |= 1543 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1544 STATE_H_R_R1|STATE_H_R_SIGN_R1| 1545 STATE_D_UR_R2; 1546 } 1547 else { 1548 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1549 STATE_NZ_CTXT_R2|STATE_V_U_R2; 1550 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1551 STATE_V_D_R2; 1552 state[j+1] |= 1553 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1554 STATE_H_L_R1|STATE_D_UL_R2; 1555 state[j-1] |= 1556 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1557 STATE_H_R_R1|STATE_D_UR_R2; 1558 } 1559 } 1560 else { 1561 csj |= STATE_VISITED_R1; 1562 } 1563 } 1564 if (sheight < 4) { 1565 state[j] = csj; 1566 continue; 1567 } 1568 // Scan second row 1569 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 1570 STATE_NZ_CTXT_R2) { 1571 k += dscanw; 1572 // Use zero coding 1573 if (bin.readBit() != 0) { 1574 // Became significant 1575 // Use sign coding 1576 sym = bin.readBit(); 1577 // Update data 1578 data[k] = (sym<<31) | setmask; 1579 // Update state information (significant bit, 1580 // visited bit, neighbor significant bit of 1581 // neighbors, non zero context of neighbors, sign 1582 // of neighbors) 1583 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 1584 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 1585 // Update sign state information of neighbors 1586 if (sym != 0) { 1587 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1588 STATE_NZ_CTXT_R1| 1589 STATE_V_D_R1|STATE_V_D_SIGN_R1; 1590 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1591 STATE_V_U_R1|STATE_V_U_SIGN_R1; 1592 state[j+1] |= 1593 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1594 STATE_D_DL_R1| 1595 STATE_H_L_R2|STATE_H_L_SIGN_R2; 1596 state[j-1] |= 1597 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1598 STATE_D_DR_R1| 1599 STATE_H_R_R2|STATE_H_R_SIGN_R2; 1600 } 1601 else { 1602 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1603 STATE_NZ_CTXT_R1|STATE_V_D_R1; 1604 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1605 STATE_V_U_R1; 1606 state[j+1] |= 1607 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1608 STATE_D_DL_R1|STATE_H_L_R2; 1609 state[j-1] |= 1610 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1611 STATE_D_DR_R1|STATE_H_R_R2; 1612 } 1613 } 1614 else { 1615 csj |= STATE_VISITED_R2; 1616 } 1617 } 1618 state[j] = csj; 1619 } 1620 } 1621 } 1622 1623 error = false; 1624 1625 // Check the byte padding if the pass is terminated 1626 if (isterm) { 1627 error = bin.checkBytePadding(); 1628 } 1629 1630 // Return error condition 1631 return error; 1632 } 1633 1634 /** 1635 * Performs the magnitude refinement pass on the specified data and 1636 * bit-plane. It decodes the samples which are significant and which do not 1637 * have the "visited" state bit turned on, using the MR primitive. The 1638 * "visited" state bit is not mofified for any samples. 1639 * 1640 * <P>This method also checks for segmentation markers if those are 1641 * present and returns true if an error is detected, or false 1642 * otherwise. If an error is detected it means that the bit stream contains 1643 * some erroneous bit that have led to the decoding of incorrect 1644 * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If 1645 * 'true' is returned the 'conceal' method should be called and no more 1646 * passes should be decoded for this code-block's bit stream. 1647 * 1648 * @param cblk The code-block data to decode 1649 * 1650 * @param mq The MQ-decoder to use 1651 * 1652 * @param bp The bit-plane to decode 1653 * 1654 * @param state The state information for the code-block 1655 * 1656 * @param isterm If this pass has been terminated. If the pass has been 1657 * terminated it can be used to check error resilience. 1658 * 1659 * @return True if an error was detected in the bit stream, false otherwise. 1660 * */ 1661 private boolean magRefPass(DataBlk cblk, MQDecoder mq, int bp, 1662 int state[], boolean isterm) { 1663 int j,sj; // The state index for line and stripe 1664 int k,sk; // The data index for line and stripe 1665 int dscanw; // The data scan-width 1666 int sscanw; // The state scan-width 1667 int jstep; // Stripe to stripe step for 'sj' 1668 int kstep; // Stripe to stripe step for 'sk' 1669 int stopsk; // The loop limit on the variable sk 1670 int csj; // Local copy (i.e. cached) of 'state[j]' 1671 int setmask; // The mask to set lower bit-planes to 1/2 approximation 1672 int resetmask; // The mask to reset approximation bit-planes 1673 int sym; // The symbol to decode 1674 int data[]; // The data buffer 1675 int s; // The stripe index 1676 int nstripes; // The number of stripes in the code-block 1677 int sheight; // Height of the current stripe 1678 boolean error; // The error condition 1679 1680 // Initialize local variables 1681 dscanw = cblk.scanw; 1682 sscanw = cblk.w+2; 1683 jstep = sscanw*STRIPE_HEIGHT/2-cblk.w; 1684 kstep = dscanw*STRIPE_HEIGHT-cblk.w; 1685 setmask = (1<<bp)>>1; 1686 resetmask = (-1)<<(bp+1); 1687 data = (int[]) cblk.getData(); 1688 nstripes = (cblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 1689 1690 // Decode stripe by stripe 1691 sk = cblk.offset; 1692 sj = sscanw+1; 1693 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 1694 sheight = (s != 0) ? STRIPE_HEIGHT : 1695 cblk.h-(nstripes-1)*STRIPE_HEIGHT; 1696 stopsk = sk+cblk.w; 1697 // Scan by set of 1 stripe column at a time 1698 for (; sk < stopsk; sk++, sj++) { 1699 // Do half top of column 1700 j = sj; 1701 csj = state[j]; 1702 // If any of the two samples is significant and not yet 1703 // visited in the current bit-plane we can not skip them 1704 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 1705 k = sk; 1706 // Scan first row 1707 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 1708 STATE_SIG_R1) { 1709 // Use MR primitive 1710 sym = mq.decodeSymbol(MR_LUT[csj&MR_MASK]); 1711 // Update the data 1712 data[k] &= resetmask; 1713 data[k] |= (sym<<bp)|setmask; 1714 // Update the STATE_PREV_MR bit 1715 csj |= STATE_PREV_MR_R1; 1716 } 1717 if (sheight < 2) { 1718 state[j] = csj; 1719 continue; 1720 } 1721 // Scan second row 1722 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 1723 STATE_SIG_R2) { 1724 k += dscanw; 1725 // Use MR primitive 1726 sym = mq.decodeSymbol(MR_LUT[(csj>>>STATE_SEP)& 1727 MR_MASK]); 1728 // Update the data 1729 data[k] &= resetmask; 1730 data[k] |= (sym<<bp)|setmask; 1731 // Update the STATE_PREV_MR bit 1732 csj |= STATE_PREV_MR_R2; 1733 } 1734 state[j] = csj; 1735 } 1736 // Do half bottom of column 1737 if (sheight < 3) continue; 1738 j += sscanw; 1739 csj = state[j]; 1740 // If any of the two samples is significant and not yet 1741 // visited in the current bit-plane we can not skip them 1742 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 1743 k = sk+(dscanw<<1); 1744 // Scan first row 1745 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 1746 STATE_SIG_R1) { 1747 // Use MR primitive 1748 sym = mq.decodeSymbol(MR_LUT[csj&MR_MASK]); 1749 // Update the data 1750 data[k] &= resetmask; 1751 data[k] |= (sym<<bp)|setmask; 1752 // Update the STATE_PREV_MR bit 1753 csj |= STATE_PREV_MR_R1; 1754 } 1755 if (sheight < 4) { 1756 state[j] = csj; 1757 continue; 1758 } 1759 // Scan second row 1760 if ((state[j] & (STATE_SIG_R2|STATE_VISITED_R2)) == 1761 STATE_SIG_R2) { 1762 k += dscanw; 1763 // Use MR primitive 1764 sym = mq.decodeSymbol(MR_LUT[(csj>>>STATE_SEP)& 1765 MR_MASK]); 1766 // Update the data 1767 data[k] &= resetmask; 1768 data[k] |= (sym<<bp)|setmask; 1769 // Update the STATE_PREV_MR bit 1770 csj |= STATE_PREV_MR_R2; 1771 } 1772 state[j] = csj; 1773 } 1774 } 1775 } 1776 1777 error = false; 1778 1779 // Check the error resilient termination 1780 if (isterm && (options & OPT_PRED_TERM) != 0) { 1781 error = mq.checkPredTerm(); 1782 } 1783 1784 // Reset the MQ context states if we need to 1785 if ((options & OPT_RESET_MQ) != 0) { 1786 mq.resetCtxts(); 1787 } 1788 1789 // Return error condition 1790 return error; 1791 } 1792 1793 /** 1794 * Performs the magnitude refinement pass on the specified data and 1795 * bit-plane. It decodes the samples which are significant and which do not 1796 * have the "visited" state bit turned on, using the MR primitive. The 1797 * "visited" state bit is not mofified for any samples. 1798 * 1799 * <P>This method bypasses the arithmetic coder and reads "raw" symbols 1800 * from the bit stream. 1801 * 1802 * <P>This method also checks for segmentation markers if those are 1803 * present and returns true if an error is detected, or false 1804 * otherwise. If an error is detected it measn that the bit stream contains 1805 * some erroneous bit that have led to the decoding of incorrect 1806 * data. This data affects the whole last decoded bit-plane (i.e. 'bp'). If 1807 * 'true' is returned the 'conceal' method should be called and no more 1808 * passes should be decoded for this code-block's bit stream. 1809 * 1810 * @param cblk The code-block data to decode 1811 * 1812 * @param bin The raw bit based input 1813 * 1814 * @param bp The bit-plane to decode 1815 * 1816 * @param state The state information for the code-block 1817 * 1818 * @param isterm If this pass has been terminated. If the pass has been 1819 * terminated it can be used to check error resilience. 1820 * 1821 * @return True if an error was detected in the bit stream, false otherwise. 1822 * */ 1823 private boolean rawMagRefPass(DataBlk cblk, ByteToBitInput bin, int bp, 1824 int state[], boolean isterm) { 1825 int j,sj; // The state index for line and stripe 1826 int k,sk; // The data index for line and stripe 1827 int dscanw; // The data scan-width 1828 int sscanw; // The state scan-width 1829 int jstep; // Stripe to stripe step for 'sj' 1830 int kstep; // Stripe to stripe step for 'sk' 1831 int stopsk; // The loop limit on the variable sk 1832 int csj; // Local copy (i.e. cached) of 'state[j]' 1833 int setmask; // The mask to set lower bit-planes to 1/2 approximation 1834 int resetmask; // The mask to reset approximation bit-planes 1835 int sym; // The symbol to decode 1836 int data[]; // The data buffer 1837 int s; // The stripe index 1838 int nstripes; // The number of stripes in the code-block 1839 int sheight; // Height of the current stripe 1840 boolean error; // The error condition 1841 1842 // Initialize local variables 1843 dscanw = cblk.scanw; 1844 sscanw = cblk.w+2; 1845 jstep = sscanw*STRIPE_HEIGHT/2-cblk.w; 1846 kstep = dscanw*STRIPE_HEIGHT-cblk.w; 1847 setmask = (1<<bp)>>1; 1848 resetmask = (-1)<<(bp+1); 1849 data = (int[]) cblk.getData(); 1850 nstripes = (cblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 1851 1852 // Decode stripe by stripe 1853 sk = cblk.offset; 1854 sj = sscanw+1; 1855 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 1856 sheight = (s != 0) ? STRIPE_HEIGHT : 1857 cblk.h-(nstripes-1)*STRIPE_HEIGHT; 1858 stopsk = sk+cblk.w; 1859 // Scan by set of 1 stripe column at a time 1860 for (; sk < stopsk; sk++, sj++) { 1861 // Do half top of column 1862 j = sj; 1863 csj = state[j]; 1864 // If any of the two samples is significant and not yet 1865 // visited in the current bit-plane we can not skip them 1866 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 1867 k = sk; 1868 // Scan first row 1869 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 1870 STATE_SIG_R1) { 1871 // Read raw bit (no MR primative) 1872 sym = bin.readBit(); 1873 // Update the data 1874 data[k] &= resetmask; 1875 data[k] |= (sym<<bp)|setmask; 1876 // No need to set STATE_PREV_MR_R1 since all magnitude 1877 // refinement passes to follow are "raw" 1878 } 1879 if (sheight < 2) continue; 1880 // Scan second row 1881 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 1882 STATE_SIG_R2) { 1883 k += dscanw; 1884 // Read raw bit (no MR primative) 1885 sym = bin.readBit(); 1886 // Update the data 1887 data[k] &= resetmask; 1888 data[k] |= (sym<<bp)|setmask; 1889 // No need to set STATE_PREV_MR_R1 since all magnitude 1890 // refinement passes to follow are "raw" 1891 } 1892 } 1893 // Do half bottom of column 1894 if (sheight < 3) continue; 1895 j += sscanw; 1896 csj = state[j]; 1897 // If any of the two samples is significant and not yet 1898 // visited in the current bit-plane we can not skip them 1899 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 1900 k = sk+(dscanw<<1); 1901 // Scan first row 1902 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 1903 STATE_SIG_R1) { 1904 // Read raw bit (no MR primative) 1905 sym = bin.readBit(); 1906 // Update the data 1907 data[k] &= resetmask; 1908 data[k] |= (sym<<bp)|setmask; 1909 // No need to set STATE_PREV_MR_R1 since all magnitude 1910 // refinement passes to follow are "raw" 1911 } 1912 if (sheight < 4) continue; 1913 // Scan second row 1914 if ((state[j] & (STATE_SIG_R2|STATE_VISITED_R2)) == 1915 STATE_SIG_R2) { 1916 k += dscanw; 1917 // Read raw bit (no MR primative) 1918 sym = bin.readBit(); 1919 // Update the data 1920 data[k] &= resetmask; 1921 data[k] |= (sym<<bp)|setmask; 1922 // No need to set STATE_PREV_MR_R1 since all magnitude 1923 // refinement passes to follow are "raw" 1924 } 1925 } 1926 } 1927 } 1928 1929 error = false; 1930 1931 // Check the byte padding if the pass is terminated 1932 if (isterm && (options & OPT_PRED_TERM)!=0 ) { 1933 error = bin.checkBytePadding(); 1934 } 1935 1936 // Return error condition 1937 return error; 1938 } 1939 1940 /** 1941 * Performs the cleanup pass on the specified data and bit-plane. It 1942 * decodes all insignificant samples which have its "visited" state bit 1943 * off, using the ZC, SC, and RLC primitives. It toggles the "visited" 1944 * state bit to 0 (off) for all samples in the code-block. 1945 * 1946 * <P>This method also checks for segmentation markers if those are 1947 * present and returns true if an error is detected, or false 1948 * otherwise. If an error is detected it measn that the bit stream 1949 * contains some erroneous bit that have led to the decoding of incorrect 1950 * data. This data affects the whole last decoded bit-plane 1951 * (i.e. 'bp'). If 'true' is returned the 'conceal' method should be 1952 * called and no more passes should be decoded for this code-block's bit 1953 * stream. 1954 * 1955 * @param cblk The code-block data to code 1956 * 1957 * @param mq The MQ-coder to use 1958 * 1959 * @param bp The bit-plane to decode 1960 * 1961 * @param state The state information for the code-block 1962 * 1963 * @param zc_lut The ZC lookup table to use in ZC. 1964 * 1965 * @param isterm If this pass has been terminated. If the pass has been 1966 * terminated it can be used to check error resilience. 1967 * 1968 * @return True if an error was detected in the bit stream, false 1969 * otherwise. 1970 * */ 1971 private boolean cleanuppass(DataBlk cblk, MQDecoder mq, int bp, 1972 int state[], int zc_lut[], boolean isterm) { 1973 int j,sj; // The state index for line and stripe 1974 int k,sk; // The data index for line and stripe 1975 int dscanw; // The data scan-width 1976 int sscanw; // The state scan-width 1977 int jstep; // Stripe to stripe step for 'sj' 1978 int kstep; // Stripe to stripe step for 'sk' 1979 int stopsk; // The loop limit on the variable sk 1980 int csj; // Local copy (i.e. cached) of 'state[j]' 1981 int setmask; // The mask to set current and lower bit-planes to 1/2 1982 // approximation 1983 int sym; // The decoded symbol 1984 int rlclen; // Length of RLC 1985 int ctxt; // The context to use 1986 int data[]; // The data buffer 1987 int s; // The stripe index 1988 boolean causal; // Flag to indicate if stripe-causal context 1989 // formation is to be used 1990 int nstripes; // The number of stripes in the code-block 1991 int sheight; // Height of the current stripe 1992 int off_ul,off_ur,off_dr,off_dl; // offsets 1993 boolean error; // The error condition 1994 1995 // Initialize local variables 1996 dscanw = cblk.scanw; 1997 sscanw = cblk.w+2; 1998 jstep = sscanw*STRIPE_HEIGHT/2-cblk.w; 1999 kstep = dscanw*STRIPE_HEIGHT-cblk.w; 2000 setmask = (3<<bp)>>1; 2001 data = (int[]) cblk.getData(); 2002 nstripes = (cblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 2003 causal = (options & OPT_VERT_STR_CAUSAL) != 0; 2004 2005 // Pre-calculate offsets in 'state' for diagonal neighbors 2006 off_ul = -sscanw-1; // up-left 2007 off_ur = -sscanw+1; // up-right 2008 off_dr = sscanw+1; // down-right 2009 off_dl = sscanw-1; // down-left 2010 2011 // Decode stripe by stripe 2012 sk = cblk.offset; 2013 sj = sscanw+1; 2014 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 2015 sheight = (s != 0) ? STRIPE_HEIGHT : 2016 cblk.h-(nstripes-1)*STRIPE_HEIGHT; 2017 stopsk = sk+cblk.w; 2018 // Scan by set of 1 stripe column at a time 2019 for (; sk < stopsk; sk++, sj++) { 2020 // Start column 2021 j = sj; 2022 csj = state[j]; 2023 top_half: 2024 { 2025 // Check for RLC: if all samples are not significant, not 2026 // visited and do not have a non-zero context, and column is 2027 // full height, we do RLC. 2028 if (csj == 0 && state[j+sscanw] == 0 && 2029 sheight == STRIPE_HEIGHT) { 2030 if (mq.decodeSymbol(RLC_CTXT) != 0) { 2031 // run-length is significant, decode length 2032 rlclen = mq.decodeSymbol(UNIF_CTXT)<<1; 2033 rlclen |= mq.decodeSymbol(UNIF_CTXT); 2034 // Set 'k' and 'j' accordingly 2035 k = sk+rlclen*dscanw; 2036 if (rlclen > 1) { 2037 j += sscanw; 2038 csj = state[j]; 2039 } 2040 } 2041 else { // RLC is insignificant 2042 // Goto next column 2043 continue; 2044 } 2045 // We just decoded the length of a significant RLC 2046 // and a sample became significant 2047 // Use sign coding 2048 if ((rlclen&0x01) == 0) { 2049 // Sample that became significant is first row of 2050 // its column half 2051 ctxt = SC_LUT[(csj>>SC_SHIFT_R1)&SC_MASK]; 2052 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 2053 (ctxt>>>SC_SPRED_SHIFT); 2054 // Update the data 2055 data[k] = (sym<<31) | setmask; 2056 // Update state information (significant bit, 2057 // visited bit, neighbor significant bit of 2058 // neighbors, non zero context of neighbors, sign 2059 // of neighbors) 2060 if (rlclen != 0 || !causal) { 2061 // If in causal mode do not change 2062 // contexts of previous stripe. 2063 state[j+off_ul] |= 2064 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 2065 state[j+off_ur] |= 2066 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 2067 } 2068 // Update sign state information of neighbors 2069 if (sym != 0) { 2070 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2071 STATE_NZ_CTXT_R2| 2072 STATE_V_U_R2|STATE_V_U_SIGN_R2; 2073 if (rlclen != 0 || !causal) { 2074 // If in causal mode do not change 2075 // contexts of previous stripe. 2076 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2077 STATE_V_D_R2|STATE_V_D_SIGN_R2; 2078 } 2079 state[j+1] |= 2080 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2081 STATE_H_L_R1|STATE_H_L_SIGN_R1| 2082 STATE_D_UL_R2; 2083 state[j-1] |= 2084 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2085 STATE_H_R_R1|STATE_H_R_SIGN_R1| 2086 STATE_D_UR_R2; 2087 } 2088 else { 2089 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2090 STATE_NZ_CTXT_R2|STATE_V_U_R2; 2091 if (rlclen != 0 || !causal) { 2092 // If in causal mode do not change 2093 // contexts of previous stripe. 2094 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2095 STATE_V_D_R2; 2096 } 2097 state[j+1] |= 2098 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2099 STATE_H_L_R1|STATE_D_UL_R2; 2100 state[j-1] |= 2101 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2102 STATE_H_R_R1|STATE_D_UR_R2; 2103 } 2104 // Changes to csj are saved later 2105 if ((rlclen>>1) != 0) { 2106 // Sample that became significant is in 2107 // bottom half of column => jump to bottom 2108 // half 2109 break top_half; 2110 } 2111 // Otherwise sample that became significant is in 2112 // top half of column => continue on top half 2113 } 2114 else { 2115 // Sample that became significant is second row of 2116 // its column half 2117 ctxt = SC_LUT[(csj>>SC_SHIFT_R2)&SC_MASK]; 2118 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 2119 (ctxt>>>SC_SPRED_SHIFT); 2120 // Update the data 2121 data[k] = (sym<<31) | setmask; 2122 // Update state information (significant bit, 2123 // neighbor significant bit of neighbors, 2124 // non zero context of neighbors, sign of neighbors) 2125 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 2126 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 2127 // Update sign state information of neighbors 2128 if (sym != 0) { 2129 csj |= STATE_SIG_R2|STATE_NZ_CTXT_R1| 2130 STATE_V_D_R1|STATE_V_D_SIGN_R1; 2131 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2132 STATE_V_U_R1|STATE_V_U_SIGN_R1; 2133 state[j+1] |= 2134 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2135 STATE_D_DL_R1| 2136 STATE_H_L_R2|STATE_H_L_SIGN_R2; 2137 state[j-1] |= 2138 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2139 STATE_D_DR_R1| 2140 STATE_H_R_R2|STATE_H_R_SIGN_R2; 2141 } 2142 else { 2143 csj |= STATE_SIG_R2|STATE_NZ_CTXT_R1| 2144 STATE_V_D_R1; 2145 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2146 STATE_V_U_R1; 2147 state[j+1] |= 2148 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2149 STATE_D_DL_R1|STATE_H_L_R2; 2150 state[j-1] |= 2151 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2152 STATE_D_DR_R1|STATE_H_R_R2; 2153 } 2154 // Save changes to csj 2155 state[j] = csj; 2156 if ((rlclen>>1) != 0) { 2157 // Sample that became significant is in bottom 2158 // half of column => we're done with this 2159 // column 2160 continue; 2161 } 2162 // Otherwise sample that became significant is in 2163 // top half of column => we're done with top 2164 // column 2165 j += sscanw; 2166 csj = state[j]; 2167 break top_half; 2168 } 2169 } 2170 // Do half top of column 2171 // If any of the two samples is not significant and has 2172 // not been visited in the current bit-plane we can not 2173 // skip them 2174 if ((((csj>>1)|csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) { 2175 k = sk; 2176 // Scan first row 2177 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 0) { 2178 // Use zero coding 2179 if (mq.decodeSymbol(zc_lut[csj&ZC_MASK]) != 0) { 2180 // Became significant 2181 // Use sign coding 2182 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 2183 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 2184 (ctxt>>>SC_SPRED_SHIFT); 2185 // Update the data 2186 data[k] = (sym<<31) | setmask; 2187 // Update state information (significant bit, 2188 // visited bit, neighbor significant bit of 2189 // neighbors, non zero context of neighbors, 2190 // sign of neighbors) 2191 if (!causal) { 2192 // If in causal mode do not change 2193 // contexts of previous stripe. 2194 state[j+off_ul] |= 2195 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 2196 state[j+off_ur] |= 2197 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 2198 } 2199 // Update sign state information of neighbors 2200 if (sym != 0) { 2201 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2202 STATE_NZ_CTXT_R2| 2203 STATE_V_U_R2|STATE_V_U_SIGN_R2; 2204 if (!causal) { 2205 // If in causal mode do not change 2206 // contexts of previous stripe. 2207 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2208 STATE_V_D_R2|STATE_V_D_SIGN_R2; 2209 } 2210 state[j+1] |= 2211 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2212 STATE_H_L_R1|STATE_H_L_SIGN_R1| 2213 STATE_D_UL_R2; 2214 state[j-1] |= 2215 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2216 STATE_H_R_R1|STATE_H_R_SIGN_R1| 2217 STATE_D_UR_R2; 2218 } 2219 else { 2220 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2221 STATE_NZ_CTXT_R2|STATE_V_U_R2; 2222 if (!causal) { 2223 // If in causal mode do not change 2224 // contexts of previous stripe. 2225 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2226 STATE_V_D_R2; 2227 } 2228 state[j+1] |= 2229 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2230 STATE_H_L_R1|STATE_D_UL_R2; 2231 state[j-1] |= 2232 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2233 STATE_H_R_R1|STATE_D_UR_R2; 2234 } 2235 } 2236 } 2237 if (sheight < 2) { 2238 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 2239 state[j] = csj; 2240 continue; 2241 } 2242 // Scan second row 2243 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 0) { 2244 k += dscanw; 2245 // Use zero coding 2246 if (mq.decodeSymbol(zc_lut[(csj>>>STATE_SEP)& 2247 ZC_MASK]) != 0) { 2248 // Became significant 2249 // Use sign coding 2250 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 2251 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 2252 (ctxt>>>SC_SPRED_SHIFT); 2253 // Update the data 2254 data[k] = (sym<<31) | setmask; 2255 // Update state information (significant bit, 2256 // visited bit, neighbor significant bit of 2257 // neighbors, non zero context of neighbors, 2258 // sign of neighbors) 2259 state[j+off_dl] |= 2260 STATE_NZ_CTXT_R1|STATE_D_UR_R1; 2261 state[j+off_dr] |= 2262 STATE_NZ_CTXT_R1|STATE_D_UL_R1; 2263 // Update sign state information of neighbors 2264 if (sym != 0) { 2265 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2266 STATE_NZ_CTXT_R1| 2267 STATE_V_D_R1|STATE_V_D_SIGN_R1; 2268 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2269 STATE_V_U_R1|STATE_V_U_SIGN_R1; 2270 state[j+1] |= 2271 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2272 STATE_D_DL_R1| 2273 STATE_H_L_R2|STATE_H_L_SIGN_R2; 2274 state[j-1] |= 2275 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2276 STATE_D_DR_R1| 2277 STATE_H_R_R2|STATE_H_R_SIGN_R2; 2278 } 2279 else { 2280 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2281 STATE_NZ_CTXT_R1|STATE_V_D_R1; 2282 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2283 STATE_V_U_R1; 2284 state[j+1] |= 2285 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2286 STATE_D_DL_R1|STATE_H_L_R2; 2287 state[j-1] |= 2288 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2289 STATE_D_DR_R1|STATE_H_R_R2; 2290 } 2291 } 2292 } 2293 } 2294 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 2295 state[j] = csj; 2296 // Do half bottom of column 2297 if (sheight < 3) continue; 2298 j += sscanw; 2299 csj = state[j]; 2300 } // end of 'top_half' block 2301 // If any of the two samples is not significant and has 2302 // not been visited in the current bit-plane we can not 2303 // skip them 2304 if ((((csj>>1)|csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) { 2305 k = sk+(dscanw<<1); 2306 // Scan first row 2307 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 0) { 2308 // Use zero coding 2309 if (mq.decodeSymbol(zc_lut[csj&ZC_MASK]) != 0) { 2310 // Became significant 2311 // Use sign coding 2312 ctxt = SC_LUT[(csj>>SC_SHIFT_R1)&SC_MASK]; 2313 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 2314 (ctxt>>>SC_SPRED_SHIFT); 2315 // Update the data 2316 data[k] = (sym<<31) | setmask; 2317 // Update state information (significant bit, 2318 // visited bit, neighbor significant bit of 2319 // neighbors, non zero context of neighbors, 2320 // sign of neighbors) 2321 state[j+off_ul] |= 2322 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 2323 state[j+off_ur] |= 2324 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 2325 // Update sign state information of neighbors 2326 if (sym != 0) { 2327 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2328 STATE_NZ_CTXT_R2| 2329 STATE_V_U_R2|STATE_V_U_SIGN_R2; 2330 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2331 STATE_V_D_R2|STATE_V_D_SIGN_R2; 2332 state[j+1] |= 2333 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2334 STATE_H_L_R1|STATE_H_L_SIGN_R1| 2335 STATE_D_UL_R2; 2336 state[j-1] |= 2337 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2338 STATE_H_R_R1|STATE_H_R_SIGN_R1| 2339 STATE_D_UR_R2; 2340 } 2341 else { 2342 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2343 STATE_NZ_CTXT_R2|STATE_V_U_R2; 2344 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2345 STATE_V_D_R2; 2346 state[j+1] |= 2347 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2348 STATE_H_L_R1|STATE_D_UL_R2; 2349 state[j-1] |= 2350 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2351 STATE_H_R_R1|STATE_D_UR_R2; 2352 } 2353 } 2354 } 2355 if (sheight < 4) { 2356 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 2357 state[j] = csj; 2358 continue; 2359 } 2360 // Scan second row 2361 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 0) { 2362 k += dscanw; 2363 // Use zero coding 2364 if (mq.decodeSymbol(zc_lut[(csj>>>STATE_SEP)& 2365 ZC_MASK]) != 0) { 2366 // Became significant 2367 // Use sign coding 2368 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 2369 sym = mq.decodeSymbol(ctxt & SC_LUT_MASK) ^ 2370 (ctxt>>>SC_SPRED_SHIFT); 2371 // Update the data 2372 data[k] = (sym<<31) | setmask; 2373 // Update state information (significant bit, 2374 // visited bit, neighbor significant bit of 2375 // neighbors, non zero context of neighbors, 2376 // sign of neighbors) 2377 state[j+off_dl] |= 2378 STATE_NZ_CTXT_R1|STATE_D_UR_R1; 2379 state[j+off_dr] |= 2380 STATE_NZ_CTXT_R1|STATE_D_UL_R1; 2381 // Update sign state information of neighbors 2382 if (sym != 0) { 2383 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2384 STATE_NZ_CTXT_R1| 2385 STATE_V_D_R1|STATE_V_D_SIGN_R1; 2386 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2387 STATE_V_U_R1|STATE_V_U_SIGN_R1; 2388 state[j+1] |= 2389 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2390 STATE_D_DL_R1| 2391 STATE_H_L_R2|STATE_H_L_SIGN_R2; 2392 state[j-1] |= 2393 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2394 STATE_D_DR_R1| 2395 STATE_H_R_R2|STATE_H_R_SIGN_R2; 2396 } 2397 else { 2398 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2399 STATE_NZ_CTXT_R1|STATE_V_D_R1; 2400 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2401 STATE_V_U_R1; 2402 state[j+1] |= 2403 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2404 STATE_D_DL_R1|STATE_H_L_R2; 2405 state[j-1] |= 2406 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2407 STATE_D_DR_R1|STATE_H_R_R2; 2408 } 2409 } 2410 } 2411 } 2412 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 2413 state[j] = csj; 2414 } 2415 } 2416 2417 // Decode segment marker if we need to 2418 if ((options & OPT_SEG_SYMBOLS) != 0) { 2419 sym = mq.decodeSymbol(UNIF_CTXT)<<3; 2420 sym |= mq.decodeSymbol(UNIF_CTXT)<<2; 2421 sym |= mq.decodeSymbol(UNIF_CTXT)<<1; 2422 sym |= mq.decodeSymbol(UNIF_CTXT); 2423 // Set error condition accordingly 2424 error = sym != SEG_SYMBOL; 2425 } 2426 else { // We can not detect any errors 2427 error = false; 2428 } 2429 2430 // Check the error resilient termination 2431 if (isterm && (options & OPT_PRED_TERM) != 0) { 2432 error = mq.checkPredTerm(); 2433 } 2434 2435 // Reset the MQ context states if we need to 2436 if ((options & OPT_RESET_MQ) != 0) { 2437 mq.resetCtxts(); 2438 } 2439 2440 // Return error condition 2441 return error; 2442 } 2443 2444 /** 2445 * Conceals decoding errors detected in the last bit-plane. The 2446 * concealement resets the state of the decoded data to what it was before 2447 * the decoding of bit-plane 'bp' started. No more data should be decoded 2448 * after this method is called for this code-block's data to which it is 2449 * applied. 2450 * 2451 * @param cblk The code-block's data 2452 * 2453 * @param bp The last decoded bit-plane (which contains errors). 2454 * */ 2455 private void conceal(DataBlk cblk, int bp) { 2456 int l; // line index 2457 int k; // array index 2458 int kmax; // 'k' limit 2459 int dk; // Value of data[k] 2460 int data[]; // the data array 2461 int setmask; // Bitmask to set approximation to 1/2 of 2462 // known interval on significant data 2463 int resetmask; // Bitmask to erase all the data from 2464 // bit-plane 'bp' 2465 2466 // Initialize masks 2467 setmask = 1<<bp; 2468 resetmask = (-1)<<(bp); 2469 2470 // Get the data array 2471 data = (int[]) cblk.getData(); 2472 2473 // Visit each sample, apply the reset mask to it and add an 2474 // approximation if significant. 2475 for (l=cblk.h-1, k=cblk.offset; l>=0; l--) { 2476 for (kmax = k+cblk.w; k<kmax; k++) { 2477 dk = data[k]; 2478 if ((dk & resetmask & 0x7FFFFFFF) != 0) { 2479 // Something was decoded in previous bit-planes => set the 2480 // approximation for previous bit-plane 2481 data[k] = (dk&resetmask)|setmask; 2482 } 2483 else { 2484 // Was insignificant in previous bit-planes = set to zero 2485 data[k] = 0; 2486 } 2487 } 2488 k += cblk.scanw-cblk.w; 2489 } 2490 2491 } 2492}