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