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