001/* 002 * $RCSfile: StdEntropyCoder.java,v $ 003 * $Revision: 1.3 $ 004 * $Date: 2005/09/26 22:08:13 $ 005 * $State: Exp $ 006 * 007 * Class: StdEntropyCoder 008 * 009 * Description: Entropy coding 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.encoder; 045import java.awt.Point; 046 047import jj2000.j2k.quantization.quantizer.*; 048import jj2000.j2k.wavelet.analysis.*; 049import jj2000.j2k.codestream.*; 050import jj2000.j2k.wavelet.*; 051import jj2000.j2k.entropy.*; 052import jj2000.j2k.image.*; 053import jj2000.j2k.util.*; 054import jj2000.j2k.io.*; 055import jj2000.j2k.*; 056 057import java.util.*; 058import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava; 059 060/** 061 * This class implements the JPEG 2000 entropy coder, which codes stripes in 062 * code-blocks. This entropy coding engine can function in a single-threaded 063 * mode where one code-block is encoded at a time, or in a multi-threaded mode 064 * where multiple code-blocks are entropy coded in parallel. The interface 065 * presented by this class is the same in both modes. 066 * 067 * <p>The number of threads used by this entropy coder is specified by the 068 * "jj2000.j2k.entropy.encoder.StdEntropyCoder.nthreads" Java system 069 * property. If set to "0" the single threaded implementation is used. If set 070 * to 'n' ('n' larger than 0) then 'n' extra threads are started by this class 071 * which are used to encode the code-blocks in parallel (i.e. ideally 'n' 072 * code-blocks will be encoded in parallel at a time). On multiprocessor 073 * machines under a "native threads" Java Virtual Machine implementation each 074 * one of these threads can run on a separate processor speeding up the 075 * encoding time. By default the single-threaded implementation is used. The 076 * multi-threaded implementation currently assumes that the vast majority of 077 * consecutive calls to 'getNextCodeBlock()' will be done on the same 078 * component. If this is not the case, the speed-up that can be expected on 079 * multiprocessor machines might be significantly decreased. 080 * 081 * <p>The code-blocks are rectangular, with dimensions which must be powers of 082 * 2. Each dimension has to be no smaller than 4 and no larger than 256. The 083 * product of the two dimensions (i.e. area of the code-block) may not exceed 084 * 4096. 085 * 086 * <p>Context 0 of the MQ-coder is used as the uniform one (uniform, 087 * non-adaptive probability distribution). Context 1 is used for RLC 088 * coding. Contexts 2-10 are used for zero-coding (ZC), contexts 11-15 are 089 * used for sign-coding (SC) and contexts 16-18 are used for 090 * magnitude-refinement (MR). 091 * 092 * <p>This implementation buffers the symbols and calls the MQ coder only once 093 * per stripe and per coding pass, to reduce the method call overhead. 094 * 095 * <p>This implementation also provides some timing features. They can be 096 * enabled by setting the 'DO_TIMING' constant of this class to true and 097 * recompiling. The timing uses the 'System.currentTimeMillis()' Java API 098 * call, which returns wall clock time, not the actual CPU time used. The 099 * timing results will be printed on the message output. Since the times 100 * reported are wall clock times and not CPU usage times they can not be added 101 * to find the total used time (i.e. some time might be counted in several 102 * places). When timing is disabled ('DO_TIMING' is false) there is no penalty 103 * if the compiler performs some basic optimizations. Even if not the penalty 104 * should be negligeable. 105 * 106 * <p>The source module must implement the CBlkQuantDataSrcEnc interface and 107 * code-block's data is received in a CBlkWTData instance. This modules sends 108 * code-block's information in a CBlkRateDistStats instance. 109 * 110 * @see CBlkQuantDataSrcEnc 111 * @see CBlkWTData 112 * @see CBlkRateDistStats 113 * */ 114public class StdEntropyCoder extends EntropyCoder 115 implements StdEntropyCoderOptions { 116 117 /** Whether to collect timing information or not: false. Used as a compile 118 * time directive. */ 119 private final static boolean DO_TIMING = false; 120 121 /** The cumulative wall time for the entropy coding engine, for each 122 * component. In the single-threaded implementation it is the total time, 123 * in the multi-threaded implementation it is the time spent managing the 124 * compressor threads only. */ 125 private long time[]; 126 127 /** The Java system property name for the number of threads to use: 128 jj2000.j2k.entropy.encoder.StdEntropyCoder.nthreads */ 129 public static final String THREADS_PROP_NAME = 130 "jj2000.j2k.entropy.encoder.StdEntropyCoder.nthreads"; 131 132 /** The default value for the property in THREADS_PROP_NAME: 0 */ 133 public static final String DEF_THREADS_NUM = "0"; 134 135 /** The increase in priority for the compressor threads, currently 3. The 136 * compressor threads will have a priority of THREADS_PRIORITY_INC more 137 * than the priority of the thread calling this class constructor. Used 138 * only in the multi-threaded implementation. */ 139 public static final int THREADS_PRIORITY_INC = 0; 140 141 /** The pool of threads, for the threaded implementation. It is null, if 142 * non threaded implementation is used */ 143 private ThreadPool tPool; 144 145 /** The queue of idle compressors. Used in multithreaded 146 implementation only */ 147 private Stack idleComps; 148 149 /** The queue of completed compressors, for each component. Used 150 in multithreaded implementation only. */ 151 private Stack completedComps[]; 152 153 /** The number of busy compressors, for each component. Used in 154 multithreaded implementation only. */ 155 private int nBusyComps[]; 156 157 /** A flag indicating for each component if all the code-blocks of the * 158 current tile have been returned. Used in multithreaded implementation 159 only. */ 160 private boolean finishedTileComponent[]; 161 162 /** The MQ coder used, for each thread */ 163 private MQCoder mqT[]; 164 165 /** The raw bit output used, for each thread */ 166 private BitToByteOutput boutT[]; 167 168 /** The output stream used, for each thread */ 169 private ByteOutputBuffer outT[]; 170 171 /** The code-block size specifications */ 172 private CBlkSizeSpec cblks; 173 174 /** The precinct partition specifications */ 175 private PrecinctSizeSpec pss; 176 177 /** By-pass mode specifications */ 178 public StringSpec bms; 179 180 /** MQ reset specifications */ 181 public StringSpec mqrs; 182 183 /** Regular termination specifications */ 184 public StringSpec rts; 185 186 /** Causal stripes specifications */ 187 public StringSpec css; 188 189 /** Error resilience segment symbol use specifications */ 190 public StringSpec sss; 191 192 /** The length calculation specifications */ 193 public StringSpec lcs; 194 195 /** The termination type specifications */ 196 public StringSpec tts; 197 198 /** The options that are turned on, as flag bits. One element for 199 * each tile-component. The options are 'OPT_TERM_PASS', 200 * 'OPT_RESET_MQ', 'OPT_VERT_STR_CAUSAL', 'OPT_BYPASS' and 201 * 'OPT_SEG_SYMBOLS' as defined in the StdEntropyCoderOptions 202 * interface 203 * 204 * @see StdEntropyCoderOptions 205 * */ 206 private int[][] opts = null; 207 208 /** The length calculation type for each tile-component */ 209 private int[][] lenCalc = null; 210 211 /** The termination type for each tile-component */ 212 private int[][] tType = null; 213 214 /** Number of bits used for the Zero Coding lookup table */ 215 private static final int ZC_LUT_BITS = 8; 216 217 /** Zero Coding context lookup tables for the LH global orientation */ 218 private static final int ZC_LUT_LH[] = new int[1<<ZC_LUT_BITS]; 219 220 /** Zero Coding context lookup tables for the HL global orientation */ 221 private static final int ZC_LUT_HL[] = new int[1<<ZC_LUT_BITS]; 222 223 /** Zero Coding context lookup tables for the HH global orientation */ 224 private static final int ZC_LUT_HH[] = new int[1<<ZC_LUT_BITS]; 225 226 /** Number of bits used for the Sign Coding lookup table */ 227 private static final int SC_LUT_BITS = 9; 228 229 /** Sign Coding context lookup table. The index into the table is a 9 bit 230 * index, which correspond the the value in the 'state' array shifted by 231 * 'SC_SHIFT'. Bits 8-5 are the signs of the horizontal-left, 232 * horizontal-right, vertical-up and vertical-down neighbors, 233 * respectively. Bit 4 is not used (0 or 1 makes no difference). Bits 3-0 234 * are the significance of the horizontal-left, horizontal-right, 235 * vertical-up and vertical-down neighbors, respectively. The least 4 bits 236 * of the value in the lookup table define the context number and the sign 237 * bit defines the "sign predictor". */ 238 private static final int SC_LUT[] = new int[1<<SC_LUT_BITS]; 239 240 /** The mask to obtain the context index from the 'SC_LUT' */ 241 private static final int SC_LUT_MASK = (1<<4)-1; 242 243 /** The shift to obtain the sign predictor from the 'SC_LUT'. It must be 244 * an unsigned shift. */ 245 private static final int SC_SPRED_SHIFT = 31; 246 247 /** The sign bit for int data */ 248 private static final int INT_SIGN_BIT = 1<<31; 249 250 /** The number of bits used for the Magnitude Refinement lookup table */ 251 private static final int MR_LUT_BITS = 9; 252 253 /** Magnitude Refinement context lookup table */ 254 private static final int MR_LUT[] = new int[1<<MR_LUT_BITS]; 255 256 /** The number of contexts used */ 257 private static final int NUM_CTXTS = 19; 258 259 /** The RLC context */ 260 private static final int RLC_CTXT = 1; 261 262 /** The UNIFORM context (with a uniform probability distribution which 263 * does not adapt) */ 264 private static final int UNIF_CTXT = 0; 265 266 /** The initial states for the MQ coder */ 267 private static final int MQ_INIT[] = {46, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 268 0, 0, 0, 0, 0, 0, 0, 0}; 269 270 /** The 4 symbol segmentation marker (1010) */ 271 private static final int SEG_SYMBOLS[] = {1,0,1,0}; 272 273 /** The 4 contexts for the segmentation marker (always the UNIFORM context, 274 * UNIF_CTXT) */ 275 private static final int SEG_SYMB_CTXTS[] = {UNIF_CTXT, UNIF_CTXT, 276 UNIF_CTXT, UNIF_CTXT}; 277 278 /** 279 * The state array for each thread. Each element of the state array stores 280 * the state of two coefficients. The lower 16 bits store the state of a 281 * coefficient in row 'i' and column 'j', while the upper 16 bits store 282 * the state of a coefficient in row 'i+1' and column 'j'. The 'i' row is 283 * either the first or the third row of a stripe. This packing of the 284 * states into 32 bit words allows a faster scan of all coefficients on 285 * each coding pass and diminished the amount of data transferred. The 286 * size of the state array is increased by 1 on each side (top, bottom, 287 * left, right) to handle boundary conditions without any special logic. 288 * 289 * <P>The state of a coefficient is stored in the following way in the 290 * lower 16 bits, where bit 0 is the least significant bit. Bit 15 is the 291 * significance of a coefficient (0 if non-significant, 1 otherwise). Bit 292 * 14 is the visited state (i.e. if a coefficient has been coded in the 293 * significance propagation pass of the current bit-plane). Bit 13 is the 294 * "non zero-context" state (i.e. if one of the eight immediate neighbors 295 * is significant it is 1, otherwise is 0). Bits 12 to 9 store the sign of 296 * the already significant left, right, up and down neighbors (1 for 297 * negative, 0 for positive or not yet significant). Bit 8 indicates if 298 * the magnitude refinement has already been applied to the 299 * coefficient. Bits 7 to 4 store the significance of the left, right, up 300 * and down neighbors (1 for significant, 0 for non significant). Bits 3 301 * to 0 store the significance of the diagonal coefficients (up-left, 302 * up-right, down-left and down-right; 1 for significant, 0 for non 303 * significant). 304 * 305 * <P>The upper 16 bits the state is stored as in the lower 16 bits, 306 * but with the bits shifted up by 16. 307 * 308 * <P>The lower 16 bits are referred to as "row 1" ("R1") while the upper 309 * 16 bits are referred to as "row 2" ("R2"). 310 * */ 311 private int stateT[][]; 312 313 /* The separation between the upper and lower bits in the state array: 16 314 * */ 315 private static final int STATE_SEP = 16; 316 317 /** The flag bit for the significance in the state array, for row 1. */ 318 private static final int STATE_SIG_R1 = 1<<15; 319 320 /** The flag bit for the "visited" bit in the state array, for row 1. */ 321 private static final int STATE_VISITED_R1 = 1<<14; 322 323 /** The flag bit for the "not zero context" bit in the state array, for 324 * row 1. This bit is always the OR of bits STATE_H_L_R1, STATE_H_R_R1, 325 * STATE_V_U_R1, STATE_V_D_R1, STATE_D_UL_R1, STATE_D_UR_R1, STATE_D_DL_R1 326 * and STATE_D_DR_R1. */ 327 private static final int STATE_NZ_CTXT_R1 = 1<<13; 328 329 /** The flag bit for the horizontal-left sign in the state array, for row 330 * 1. This bit can only be set if the STATE_H_L_R1 is also set. */ 331 private static final int STATE_H_L_SIGN_R1 = 1<<12; 332 333 /** The flag bit for the horizontal-right sign in the state array, for 334 * row 1. This bit can only be set if the STATE_H_R_R1 is also set. */ 335 private static final int STATE_H_R_SIGN_R1 = 1<<11; 336 337 /** The flag bit for the vertical-up sign in the state array, for row 338 * 1. This bit can only be set if the STATE_V_U_R1 is also set. */ 339 private static final int STATE_V_U_SIGN_R1 = 1<<10; 340 341 /** The flag bit for the vertical-down sign in the state array, for row 342 * 1. This bit can only be set if the STATE_V_D_R1 is also set. */ 343 private static final int STATE_V_D_SIGN_R1 = 1<<9; 344 345 /** The flag bit for the previous MR primitive applied in the state array, 346 for row 1. */ 347 private static final int STATE_PREV_MR_R1 = 1<<8; 348 349 /** The flag bit for the horizontal-left significance in the state array, 350 for row 1. */ 351 private static final int STATE_H_L_R1 = 1<<7; 352 353 /** The flag bit for the horizontal-right significance in the state array, 354 for row 1. */ 355 private static final int STATE_H_R_R1 = 1<<6; 356 357 /** The flag bit for the vertical-up significance in the state array, for 358 row 1. */ 359 private static final int STATE_V_U_R1 = 1<<5; 360 361 /** The flag bit for the vertical-down significance in the state array, 362 for row 1. */ 363 private static final int STATE_V_D_R1 = 1<<4; 364 365 /** The flag bit for the diagonal up-left significance in the state array, 366 for row 1. */ 367 private static final int STATE_D_UL_R1 = 1<<3; 368 369 /** The flag bit for the diagonal up-right significance in the state 370 array, for row 1.*/ 371 private static final int STATE_D_UR_R1 = 1<<2; 372 373 /** The flag bit for the diagonal down-left significance in the state 374 array, for row 1. */ 375 private static final int STATE_D_DL_R1 = 1<<1; 376 377 /** The flag bit for the diagonal down-right significance in the state 378 array , for row 1.*/ 379 private static final int STATE_D_DR_R1 = 1; 380 381 /** The flag bit for the significance in the state array, for row 2. */ 382 private static final int STATE_SIG_R2 = STATE_SIG_R1<<STATE_SEP; 383 384 /** The flag bit for the "visited" bit in the state array, for row 2. */ 385 private static final int STATE_VISITED_R2 = STATE_VISITED_R1<<STATE_SEP; 386 387 /** The flag bit for the "not zero context" bit in the state array, for 388 * row 2. This bit is always the OR of bits STATE_H_L_R2, STATE_H_R_R2, 389 * STATE_V_U_R2, STATE_V_D_R2, STATE_D_UL_R2, STATE_D_UR_R2, STATE_D_DL_R2 390 * and STATE_D_DR_R2. */ 391 private static final int STATE_NZ_CTXT_R2 = STATE_NZ_CTXT_R1<<STATE_SEP; 392 393 /** The flag bit for the horizontal-left sign in the state array, for row 394 * 2. This bit can only be set if the STATE_H_L_R2 is also set. */ 395 private static final int STATE_H_L_SIGN_R2 = STATE_H_L_SIGN_R1<<STATE_SEP; 396 397 /** The flag bit for the horizontal-right sign in the state array, for 398 * row 2. This bit can only be set if the STATE_H_R_R2 is also set. */ 399 private static final int STATE_H_R_SIGN_R2 = STATE_H_R_SIGN_R1<<STATE_SEP; 400 401 /** The flag bit for the vertical-up sign in the state array, for row 402 * 2. This bit can only be set if the STATE_V_U_R2 is also set. */ 403 private static final int STATE_V_U_SIGN_R2 = STATE_V_U_SIGN_R1<<STATE_SEP; 404 405 /** The flag bit for the vertical-down sign in the state array, for row 406 * 2. This bit can only be set if the STATE_V_D_R2 is also set. */ 407 private static final int STATE_V_D_SIGN_R2 = STATE_V_D_SIGN_R1<<STATE_SEP; 408 409 /** The flag bit for the previous MR primitive applied in the state array, 410 for row 2. */ 411 private static final int STATE_PREV_MR_R2 = STATE_PREV_MR_R1<<STATE_SEP; 412 413 /** The flag bit for the horizontal-left significance in the state array, 414 for row 2. */ 415 private static final int STATE_H_L_R2 = STATE_H_L_R1<<STATE_SEP; 416 417 /** The flag bit for the horizontal-right significance in the state array, 418 for row 2. */ 419 private static final int STATE_H_R_R2 = STATE_H_R_R1<<STATE_SEP; 420 421 /** The flag bit for the vertical-up significance in the state array, for 422 row 2. */ 423 private static final int STATE_V_U_R2 = STATE_V_U_R1<<STATE_SEP; 424 425 /** The flag bit for the vertical-down significance in the state array, 426 for row 2. */ 427 private static final int STATE_V_D_R2 = STATE_V_D_R1<<STATE_SEP; 428 429 /** The flag bit for the diagonal up-left significance in the state array, 430 for row 2. */ 431 private static final int STATE_D_UL_R2 = STATE_D_UL_R1<<STATE_SEP; 432 433 /** The flag bit for the diagonal up-right significance in the state 434 array, for row 2.*/ 435 private static final int STATE_D_UR_R2 = STATE_D_UR_R1<<STATE_SEP; 436 437 /** The flag bit for the diagonal down-left significance in the state 438 array, for row 2. */ 439 private static final int STATE_D_DL_R2 = STATE_D_DL_R1<<STATE_SEP; 440 441 /** The flag bit for the diagonal down-right significance in the state 442 array , for row 2.*/ 443 private static final int STATE_D_DR_R2 = STATE_D_DR_R1<<STATE_SEP; 444 445 /** The mask to isolate the significance bits for row 1 and 2 of the state 446 * array. */ 447 private static final int SIG_MASK_R1R2 = STATE_SIG_R1|STATE_SIG_R2; 448 449 /** The mask to isolate the visited bits for row 1 and 2 of the state 450 * array. */ 451 private static final int VSTD_MASK_R1R2 = 452 STATE_VISITED_R1|STATE_VISITED_R2; 453 454 /** The mask to isolate the bits necessary to identify RLC coding state 455 * (significant, visited and non-zero context, for row 1 and 2). */ 456 private static final int RLC_MASK_R1R2 = 457 STATE_SIG_R1|STATE_SIG_R2| 458 STATE_VISITED_R1|STATE_VISITED_R2| 459 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2; 460 461 /** The mask to obtain the ZC_LUT index from the state information */ 462 // This is needed because of the STATE_V_D_SIGN_R1, STATE_V_U_SIGN_R1, 463 // STATE_H_R_SIGN_R1, and STATE_H_L_SIGN_R1 bits. 464 private static final int ZC_MASK = (1<<8)-1; 465 466 /** The shift to obtain the SC index to 'SC_LUT' from the state 467 * information, for row 1. */ 468 private static final int SC_SHIFT_R1 = 4; 469 470 /** The shift to obtain the SC index to 'SC_LUT' from the state 471 * information, for row 2. */ 472 private static final int SC_SHIFT_R2 = SC_SHIFT_R1+STATE_SEP; 473 474 /** The bit mask to isolate the state bits relative to the sign coding 475 * lookup table ('SC_LUT'). */ 476 private static final int SC_MASK = (1<<SC_LUT_BITS)-1; 477 478 /** The mask to obtain the MR index to 'MR_LUT' from the 'state' 479 * information. It is to be applied after the 'MR_SHIFT'. */ 480 private static final int MR_MASK = (1<<MR_LUT_BITS)-1; 481 482 /** The number of bits used to index in the 'fm' lookup table, 7. The 'fs' 483 * table is indexed with one less bit. */ 484 private static final int MSE_LKP_BITS = 7; 485 486 /** The number of fractional bits used to store data in the 'fm' and 'fs' 487 * lookup tables. */ 488 private static final int MSE_LKP_FRAC_BITS = 13; 489 490 /** Distortion estimation lookup table for bits coded using the sign-code 491 * (SC) primative, for lossy coding (i.e. normal). */ 492 private static final int FS_LOSSY[] = new int[1<<(MSE_LKP_BITS-1)]; 493 494 /** Distortion estimation lookup table for bits coded using the 495 * magnitude-refinement (MR) primative, for lossy coding (i.e. normal) */ 496 private static final int FM_LOSSY[] = new int[1<<MSE_LKP_BITS]; 497 498 /** Distortion estimation lookup table for bits coded using the sign-code 499 * (SC) primative, for lossless coding and last bit-plane. This table is 500 * different from 'fs_lossy' since when doing lossless coding the residual 501 * distortion after the last bit-plane is coded is strictly 0. */ 502 private static final int FS_LOSSLESS[] = new int[1<<(MSE_LKP_BITS-1)]; 503 504 /** Distortion estimation lookup table for bits coded using the 505 * magnitude-refinement (MR) primative, for lossless coding and last 506 * bit-plane. This table is different from 'fs_lossless' since when doing 507 * lossless coding the residual distortion after the last bit-plane is 508 * coded is strictly 0.*/ 509 private static final int FM_LOSSLESS[] = new int[1<<MSE_LKP_BITS]; 510 511 /** The buffer for distortion values (avoids reallocation for each 512 code-block), for each thread. */ 513 private double distbufT[][]; 514 515 /** The buffer for rate values (avoids reallocation for each 516 code-block), for each thread. */ 517 private int ratebufT[][]; 518 519 /** The buffer for indicating terminated passes (avoids reallocation for 520 * each code-block), for each thread. */ 521 private boolean istermbufT[][]; 522 523 /** The source code-block to entropy code (avoids reallocation for each 524 code-block), for each thread. */ 525 private CBlkWTData srcblkT[]; 526 527 /** Buffer for symbols to send to the MQ-coder, for each thread. Used to 528 * reduce the number of calls to the MQ coder. */ 529 // NOTE: The symbol buffer has not prooved to be of any great improvement 530 // in encoding time, but it does not hurt. It's performance should be 531 // better studied under different JVMs. 532 private int symbufT[][]; 533 534 /** Buffer for the contexts to use when sending buffered symbols to the 535 * MQ-coder, for each thread. Used to reduce the number of calls to the MQ 536 * coder. */ 537 private int ctxtbufT[][]; 538 539 /** boolean used to signal if the precinct partition is used for 540 * each component and each tile. */ 541 private boolean precinctPartition[][]; 542 543 /** 544 * Class that takes care of running the 'compressCodeBlock()' method with 545 * thread local arguments. Used only in multithreaded implementation. 546 * */ 547 private class Compressor implements Runnable { 548 /** The index of this compressor. Used to access thread local 549 * variables */ 550 private final int idx; 551 552 /** The object where to store the compressed code-block */ 553 // Should be private, but some buggy JDK 1.1 compilers complain 554 CBlkRateDistStats ccb; 555 556 /** The component on which to compress */ 557 // Should be private, but some buggy JDK 1.1 compilers complain 558 int c; 559 560 /** The options bitmask to use in compression */ 561 // Should be private, but some buggy JDK 1.1 compilers complain 562 int options; 563 564 /** The reversible flag to use in compression */ 565 // Should be private, but some buggy JDK 1.1 compilers complain 566 boolean rev; 567 568 /** The length calculation type to use in compression */ 569 // Should be private, but some buggy JDK 1.1 compilers complain 570 int lcType; 571 572 /** The MQ termination type to use in compression */ 573 // Should be private, but some buggy JDK 1.1 compilers complain 574 int tType; 575 576 /** The cumulative wall time for this compressor, for each 577 * component. */ 578 private long time[]; 579 580 /** 581 * Creates a new compressor object with the given index. 582 * 583 * @param idx The index of this compressor. 584 * */ 585 Compressor(int idx) { 586 this.idx = idx; 587 if (DO_TIMING) time = new long[src.getNumComps()]; 588 } 589 590 /** 591 * Calls the 'compressCodeBlock()' method with thread local 592 * arguments. Once completed it adds itself to the 'completedComps[c]' 593 * stack, where 'c' is the component for which this compressor is 594 * running. This last step occurs even if exceptions are thrown by the 595 * 'compressCodeBlock()' method. 596 * */ 597 public void run() { 598 // Start the code-block compression 599 try { 600 long stime = 0L; 601 if (DO_TIMING) stime = System.currentTimeMillis(); 602 compressCodeBlock(c,ccb,srcblkT[idx],mqT[idx],boutT[idx], 603 outT[idx],stateT[idx],distbufT[idx], 604 ratebufT[idx],istermbufT[idx], 605 symbufT[idx],ctxtbufT[idx],options, 606 rev,lcType,tType); 607 if (DO_TIMING) time[c] += System.currentTimeMillis()-stime; 608 } 609 finally { 610 // Join the queue of completed compression, even if exceptions 611 // occurred. 612 completedComps[c].push(this); 613 } 614 } 615 616 /** 617 * Returns the wall time spent by this compressor for component 'c' 618 * since the last call to this method (or the creation of this 619 * compressor if not yet called). If DO_TIMING is false 0 is returned. 620 * 621 * @return The wall time in milliseconds spent by this compressor 622 * since the last call to this method. 623 * */ 624 synchronized long getTiming(int c) { 625 if (DO_TIMING) { 626 long t = time[c]; 627 time[c] = 0L; 628 return t; 629 } 630 else { 631 return 0L; 632 } 633 } 634 635 /** 636 * Returns the index of this compressor. 637 * 638 * @return The index of this compressor. 639 * */ 640 public int getIdx() { 641 return idx; 642 } 643 } 644 645 /** Static initializer: initializes all the lookup tables. */ 646 static { 647 int i,j; 648 double val, deltaMSE; 649 int inter_sc_lut[]; 650 int ds,us,rs,ls; 651 int dsgn,usgn,rsgn,lsgn; 652 int h,v; 653 654 // Initialize the zero coding lookup tables 655 656 // LH 657 658 // - No neighbors significant 659 ZC_LUT_LH[0] = 2; 660 661 // - No horizontal or vertical neighbors significant 662 for (i=1; i<16; i++) { // Two or more diagonal coeffs significant 663 ZC_LUT_LH[i] = 4; 664 } 665 for (i=0; i<4; i++) { // Only one diagonal coeff significant 666 ZC_LUT_LH[1<<i] = 3; 667 } 668 // - No horizontal neighbors significant, diagonal irrelevant 669 for (i=0; i<16; i++) { 670 // Only one vertical coeff significant 671 ZC_LUT_LH[STATE_V_U_R1 | i] = 5; 672 ZC_LUT_LH[STATE_V_D_R1 | i] = 5; 673 // The two vertical coeffs significant 674 ZC_LUT_LH[STATE_V_U_R1 | STATE_V_D_R1 | i] = 6; 675 } 676 // - One horiz. neighbor significant, diagonal/vertical non-significant 677 ZC_LUT_LH[STATE_H_L_R1] = 7; 678 ZC_LUT_LH[STATE_H_R_R1] = 7; 679 // - One horiz. significant, no vertical significant, one or more 680 // diagonal significant 681 for (i=1; i<16; i++) { 682 ZC_LUT_LH[STATE_H_L_R1 | i] = 8; 683 ZC_LUT_LH[STATE_H_R_R1 | i] = 8; 684 } 685 // - One horiz. significant, one or more vertical significant, 686 // diagonal irrelevant 687 for (i=1; i<4; i++) { 688 for (j=0; j<16; j++) { 689 ZC_LUT_LH[STATE_H_L_R1 | (i<<4) | j] = 9; 690 ZC_LUT_LH[STATE_H_R_R1 | (i<<4) | j] = 9; 691 } 692 } 693 // - Two horiz. significant, others irrelevant 694 for (i=0; i<64; i++) { 695 ZC_LUT_LH[STATE_H_L_R1 | STATE_H_R_R1 | i] = 10; 696 } 697 698 // HL 699 700 // - No neighbors significant 701 ZC_LUT_HL[0] = 2; 702 // - No horizontal or vertical neighbors significant 703 for (i=1; i<16; i++) { // Two or more diagonal coeffs significant 704 ZC_LUT_HL[i] = 4; 705 } 706 for (i=0; i<4; i++) { // Only one diagonal coeff significant 707 ZC_LUT_HL[1<<i] = 3; 708 } 709 // - No vertical significant, diagonal irrelevant 710 for (i=0; i<16; i++) { 711 // One horiz. significant 712 ZC_LUT_HL[STATE_H_L_R1 | i] = 5; 713 ZC_LUT_HL[STATE_H_R_R1 | i] = 5; 714 // Two horiz. significant 715 ZC_LUT_HL[STATE_H_L_R1 | STATE_H_R_R1 | i] = 6; 716 } 717 // - One vert. significant, diagonal/horizontal non-significant 718 ZC_LUT_HL[STATE_V_U_R1] = 7; 719 ZC_LUT_HL[STATE_V_D_R1] = 7; 720 // - One vert. significant, horizontal non-significant, one or more 721 // diag. significant 722 for (i=1; i<16; i++) { 723 ZC_LUT_HL[STATE_V_U_R1 | i] = 8; 724 ZC_LUT_HL[STATE_V_D_R1 | i] = 8; 725 } 726 // - One vertical significant, one or more horizontal significant, 727 // diagonal irrelevant 728 for (i=1; i<4; i++) { 729 for (j=0; j<16; j++) { 730 ZC_LUT_HL[(i<<6) | STATE_V_U_R1 | j] = 9; 731 ZC_LUT_HL[(i<<6) | STATE_V_D_R1 | j] = 9; 732 } 733 } 734 // - Two vertical significant, others irrelevant 735 for (i=0; i<4; i++) { 736 for (j=0; j<16; j++) { 737 ZC_LUT_HL[(i<<6) | STATE_V_U_R1 | STATE_V_D_R1 | j] = 10; 738 } 739 } 740 741 // HH 742 int[] twoBits = {3,5,6,9,10,12}; // Figures (between 0 and 15) 743 // countaning 2 and only 2 bits on in its binary representation. 744 745 int[] oneBit = {1,2,4,8}; // Figures (between 0 and 15) 746 // countaning 1 and only 1 bit on in its binary representation. 747 748 int[] twoLeast = {3,5,6,7,9,10,11,12,13,14,15}; // Figures 749 // (between 0 and 15) countaining, at least, 2 bits on in its 750 // binary representation. 751 752 int[] threeLeast = {7,11,13,14,15}; // Figures 753 // (between 0 and 15) countaining, at least, 3 bits on in its 754 // binary representation. 755 756 // - None significant 757 ZC_LUT_HH[0] = 2; 758 759 // - One horizontal+vertical significant, none diagonal 760 for(i=0; i<oneBit.length; i++) 761 ZC_LUT_HH[ oneBit[i]<<4 ] = 3; 762 763 // - Two or more horizontal+vertical significant, diagonal non-signif 764 for(i=0; i<twoLeast.length; i++) 765 ZC_LUT_HH[ twoLeast[i]<<4 ] = 4; 766 767 // - One diagonal significant, horiz./vert. non-significant 768 for(i=0; i<oneBit.length; i++) 769 ZC_LUT_HH[ oneBit[i] ] = 5; 770 771 // - One diagonal significant, one horiz.+vert. significant 772 for(i=0; i<oneBit.length; i++) 773 for(j=0; j<oneBit.length; j++) 774 ZC_LUT_HH[ (oneBit[i]<<4) | oneBit[j] ] = 6; 775 776 // - One diag signif, two or more horiz+vert signif 777 for(i=0; i<twoLeast.length; i++) 778 for(j=0; j<oneBit.length; j++) 779 ZC_LUT_HH[ (twoLeast[i]<<4) | oneBit[j] ] = 7; 780 781 // - Two diagonal significant, none horiz+vert significant 782 for(i=0; i<twoBits.length; i++) 783 ZC_LUT_HH[ twoBits[i] ] = 8; 784 785 // - Two diagonal significant, one or more horiz+vert significant 786 for(j=0; j<twoBits.length; j++) 787 for(i=1; i<16; i++) 788 ZC_LUT_HH[ (i<<4) | twoBits[j] ] = 9; 789 790 // - Three or more diagonal significant, horiz+vert irrelevant 791 for(i=0; i<16; i++) 792 for(j=0; j<threeLeast.length; j++) 793 ZC_LUT_HH[ (i<<4) | threeLeast[j] ] = 10; 794 795 // Initialize the SC lookup tables 796 797 // Use an intermediate sign code lookup table that is similar to the 798 // one in the VM text, in that it depends on the 'h' and 'v' 799 // quantities. The index into this table is a 6 bit index, the top 3 800 // bits are (h+1) and the low 3 bits (v+1). 801 inter_sc_lut = new int[36]; 802 inter_sc_lut[(2<<3)|2] = 15; 803 inter_sc_lut[(2<<3)|1] = 14; 804 inter_sc_lut[(2<<3)|0] = 13; 805 inter_sc_lut[(1<<3)|2] = 12; 806 inter_sc_lut[(1<<3)|1] = 11; 807 inter_sc_lut[(1<<3)|0] = 12 | INT_SIGN_BIT; 808 inter_sc_lut[(0<<3)|2] = 13 | INT_SIGN_BIT; 809 inter_sc_lut[(0<<3)|1] = 14 | INT_SIGN_BIT; 810 inter_sc_lut[(0<<3)|0] = 15 | INT_SIGN_BIT; 811 812 // Using the intermediate sign code lookup table create the final 813 // one. The index into this table is a 9 bit index, the low 4 bits are 814 // the significance of the 4 horizontal/vertical neighbors, while the 815 // top 4 bits are the signs of those neighbors. The bit in the middle 816 // is ignored. This index arrangement matches the state bits in the 817 // 'state' array, thus direct addressing of the table can be done from 818 // the sate information. 819 for (i=0; i<(1<<SC_LUT_BITS)-1; i++) { 820 ds = i & 0x01; // significance of down neighbor 821 us = (i >> 1) & 0x01; // significance of up neighbor 822 rs = (i >> 2) & 0x01; // significance of right neighbor 823 ls = (i >> 3) & 0x01; // significance of left neighbor 824 dsgn = (i >> 5) & 0x01; // sign of down neighbor 825 usgn = (i >> 6) & 0x01; // sign of up neighbor 826 rsgn = (i >> 7) & 0x01; // sign of right neighbor 827 lsgn = (i >> 8) & 0x01; // sign of left neighbor 828 // Calculate 'h' and 'v' as in VM text 829 h = ls*(1-2*lsgn)+rs*(1-2*rsgn); 830 h = (h >= -1) ? h : -1; 831 h = (h <= 1) ? h : 1; 832 v = us*(1-2*usgn)+ds*(1-2*dsgn); 833 v = (v >= -1) ? v : -1; 834 v = (v <= 1) ? v : 1; 835 // Get context and sign predictor from 'inter_sc_lut' 836 SC_LUT[i] = inter_sc_lut[(h+1)<<3|(v+1)]; 837 } 838 inter_sc_lut = null; 839 840 // Initialize the MR lookup tables 841 842 // None significant, prev MR off 843 MR_LUT[0] = 16; 844 // One or more significant, prev MR off 845 for (i=1; i<(1<<(MR_LUT_BITS-1)); i++) { 846 MR_LUT[i] = 17; 847 } 848 // Previous MR on, significance irrelevant 849 for (; i<(1<<MR_LUT_BITS); i++) { 850 MR_LUT[i] = 18; 851 } 852 853 // Initialize the distortion estimation lookup tables 854 855 // fs tables 856 for (i=0; i<(1<<(MSE_LKP_BITS-1)); i++) { 857 // In fs we index by val-1, since val is really: 1 <= val < 2 858 val = (double)i / (1<<(MSE_LKP_BITS-1)) + 1.0; 859 deltaMSE = val*val; 860 FS_LOSSLESS[i] = 861 (int) Math.floor(deltaMSE * 862 ((double)(1<<MSE_LKP_FRAC_BITS)) + 0.5); 863 val -= 1.5; 864 deltaMSE -= val*val; 865 FS_LOSSY[i] = 866 (int) Math.floor(deltaMSE * 867 ((double)(1<<MSE_LKP_FRAC_BITS)) + 0.5); 868 } 869 870 // fm tables 871 for (i=0; i<(1<<MSE_LKP_BITS); i++) { 872 val = (double)i / (1<<(MSE_LKP_BITS-1)); 873 deltaMSE = (val-1.0)*(val-1.0); 874 FM_LOSSLESS[i] = 875 (int) Math.floor(deltaMSE * 876 ((double)(1<<MSE_LKP_FRAC_BITS)) + 0.5); 877 val -= (i<(1<<(MSE_LKP_BITS-1))) ? 0.5 : 1.5; 878 deltaMSE -= val*val; 879 FM_LOSSY[i] = 880 (int) Math.floor(deltaMSE * 881 ((double)(1<<MSE_LKP_FRAC_BITS)) + 0.5); 882 } 883 } 884 885 886 /** 887 * Instantiates a new entropy coder engine, with the specified source of 888 * data, nominal block width and height. 889 * 890 * <p>If the 'OPT_PRED_TERM' option is given then the MQ termination must 891 * be 'TERM_PRED_ER' or an exception is thrown.</p> 892 * 893 * @param src The source of data 894 * 895 * @param cbks Code-block size specifications 896 * 897 * @param pss Precinct partition specifications 898 * 899 * @param bms By-pass mode specifications 900 * 901 * @param mqrs MQ-reset specifications 902 * 903 * @param rts Regular termination specifications 904 * 905 * @param css Causal stripes specifications 906 * 907 * @param sss Error resolution segment symbol use specifications 908 * 909 * @param lcs Length computation specifications 910 * 911 * @param tts Termination type specifications 912 * 913 * @see MQCoder 914 * */ 915 public StdEntropyCoder(CBlkQuantDataSrcEnc src,CBlkSizeSpec cblks, 916 PrecinctSizeSpec pss,StringSpec bms,StringSpec mqrs, 917 StringSpec rts,StringSpec css,StringSpec sss, 918 StringSpec lcs,StringSpec tts) { 919 super(src); 920 this.cblks = cblks; 921 this.pss = pss; 922 this.bms = bms; 923 this.mqrs = mqrs; 924 this.rts = rts; 925 this.css = css; 926 this.sss = sss; 927 this.lcs = lcs; 928 this.tts = tts; 929 int maxCBlkWidth, maxCBlkHeight; 930 int i; // Counter 931 int nt; // The number of threads 932 int tsl; // Size for thread structures 933 934 // Get the biggest width/height for the code-blocks 935 maxCBlkWidth = cblks.getMaxCBlkWidth(); 936 maxCBlkHeight = cblks.getMaxCBlkHeight(); 937 938 // Get the number of threads to use, or default to one 939 try { 940 try { 941 nt = Integer.parseInt(System.getProperty(THREADS_PROP_NAME, 942 DEF_THREADS_NUM)); 943 } catch(SecurityException se) { 944 // Use the default value. 945 nt = Integer.parseInt(DEF_THREADS_NUM); 946 } 947 if (nt < 0) throw new NumberFormatException(); 948 } catch (NumberFormatException e) { 949 throw new IllegalArgumentException("Invalid number of threads "+ 950 "for "+ 951 "entropy coding in property "+ 952 THREADS_PROP_NAME); 953 } 954 955 // If we do timing create necessary structures 956 if (DO_TIMING) { 957 time = new long[src.getNumComps()]; 958 // If we are timing make sure that 'finalize' gets called. 959 System.runFinalizersOnExit(true); 960 } 961 962 // If using multithreaded implementation get necessasry objects 963 if (nt > 0) { 964 FacilityManager.getMsgLogger(). 965 printmsg(MsgLogger.INFO, 966 "Using multithreaded entropy coder "+ 967 "with "+nt+" compressor threads."); 968 tsl = nt; 969 tPool = new ThreadPool(nt,Thread.currentThread().getPriority()+ 970 THREADS_PRIORITY_INC,"StdEntropyCoder"); 971 idleComps = new Stack(); 972 completedComps = new Stack[src.getNumComps()]; 973 nBusyComps = new int[src.getNumComps()]; 974 finishedTileComponent = new boolean[src.getNumComps()]; 975 for (i=src.getNumComps()-1; i>=0; i--) { 976 completedComps[i] = new Stack(); 977 } 978 for (i=0; i<nt; i++) { 979 idleComps.push(new StdEntropyCoder.Compressor(i)); 980 } 981 } 982 else { 983 tsl = 1; 984 tPool = null; 985 idleComps = null; 986 completedComps = null; 987 nBusyComps = null; 988 finishedTileComponent = null; 989 } 990 991 // Allocate data structures 992 outT = new ByteOutputBuffer[tsl]; 993 mqT = new MQCoder[tsl]; 994 boutT = new BitToByteOutput[tsl]; 995 stateT = new int[tsl][(maxCBlkWidth+2)*((maxCBlkHeight+1)/2+2)]; 996 symbufT = new int[tsl][maxCBlkWidth*(STRIPE_HEIGHT*2+2)]; 997 ctxtbufT = new int[tsl][maxCBlkWidth*(STRIPE_HEIGHT*2+2)]; 998 distbufT = new double[tsl][32*NUM_PASSES]; 999 ratebufT = new int[tsl][32*NUM_PASSES]; 1000 istermbufT = new boolean[tsl][32*NUM_PASSES]; 1001 srcblkT = new CBlkWTData[tsl]; 1002 for (i=0; i<tsl; i++) { 1003 outT[i] = new ByteOutputBuffer(); 1004 mqT[i] = new MQCoder(outT[i],NUM_CTXTS,MQ_INIT); 1005 } 1006 precinctPartition = new boolean [src.getNumComps()][src.getNumTiles()]; 1007 1008 // Create the subband description for each component and each tile 1009 Point numTiles = src.getNumTiles(null); 1010 //Subband sb = null; 1011 int nc = getNumComps(); 1012 initTileComp(getNumTiles(),nc); 1013 1014 for (int c=0; c<nc; c++) { 1015 for (int tY=0; tY<numTiles.y; tY++) { 1016 for (int tX=0; tX<numTiles.x; tX++) { 1017 precinctPartition[c][tIdx] = false; 1018 } 1019 } 1020 } 1021 } 1022 1023 /** 1024 * Prints the timing information, if collected, and calls 'finalize' on 1025 * the super class. 1026 * */ 1027 public void finalize() throws Throwable { 1028 if (DO_TIMING) { 1029 int c; 1030 StringBuffer sb; 1031 1032 if (tPool == null) { // Single threaded implementation 1033 sb = new StringBuffer("StdEntropyCoder compression wall "+ 1034 "clock time:"); 1035 for (c=0; c<time.length; c++) { 1036 sb.append("\n component "); 1037 sb.append(c); 1038 sb.append(": "); 1039 sb.append(time[c]); 1040 sb.append(" ms"); 1041 } 1042 FacilityManager.getMsgLogger(). 1043 printmsg(MsgLogger.INFO,sb.toString()); 1044 } 1045 else { // Multithreaded implementation 1046 Compressor compr; 1047 MsgLogger msglog = FacilityManager.getMsgLogger(); 1048 1049 sb = new StringBuffer("StdEntropyCoder manager thread "+ 1050 "wall clock time:"); 1051 for (c=0; c<time.length; c++) { 1052 sb.append("\n component "); 1053 sb.append(c); 1054 sb.append(": "); 1055 sb.append(time[c]); 1056 sb.append(" ms"); 1057 } 1058 Enumeration enumVar = idleComps.elements(); 1059 sb.append("\nStdEntropyCoder compressor threads wall clock "+ 1060 "time:"); 1061 while (enumVar.hasMoreElements()) { 1062 compr = (Compressor)(enumVar.nextElement()); 1063 for (c=0; c<time.length; c++) { 1064 sb.append("\n compressor "); 1065 sb.append(compr.getIdx()); 1066 sb.append(", component "); 1067 sb.append(c); 1068 sb.append(": "); 1069 sb.append(compr.getTiming(c)); 1070 sb.append(" ms"); 1071 } 1072 } 1073 FacilityManager.getMsgLogger(). 1074 printmsg(MsgLogger.INFO,sb.toString()); 1075 } 1076 } 1077 super.finalize(); 1078 } 1079 1080 /** 1081 * Returns the code-block width for the specified tile and component. 1082 * 1083 * @param t The tile index 1084 * 1085 * @param c the component index 1086 * 1087 * @return The code-block width for the specified tile and component 1088 * */ 1089 public int getCBlkWidth(int t, int c) { 1090 return cblks.getCBlkWidth(ModuleSpec.SPEC_TILE_COMP,t,c); 1091 } 1092 1093 /** 1094 * Returns the code-block height for the specified tile and component. 1095 * 1096 * @param t The tile index 1097 * 1098 * @param c The component index 1099 * 1100 * @return The code-block height for the specified tile and component. 1101 * */ 1102 public int getCBlkHeight(int t,int c) { 1103 return cblks.getCBlkHeight(ModuleSpec.SPEC_TILE_COMP,t,c); 1104 } 1105 1106 /** 1107 * Returns the next coded code-block in the current tile for the specified 1108 * component, as a copy (see below). The order in which code-blocks are 1109 * returned is not specified. However each code-block is returned only 1110 * once and all code-blocks will be returned if the method is called 'N' 1111 * times, where 'N' is the number of code-blocks in the tile. After all 1112 * the code-blocks have been returned for the current tile calls to this 1113 * method will return 'null'. 1114 * 1115 * <P>When changing the current tile (through 'setTile()' or 'nextTile()') 1116 * this method will always return the first code-block, as if this method 1117 * was never called before for the new current tile. 1118 * 1119 * <P>The data returned by this method is always a copy of the internal 1120 * data of this object, if any, and it can be modified "in place" without 1121 * any problems after being returned. 1122 * 1123 * @param c The component for which to return the next code-block. 1124 * 1125 * @param ccb If non-null this object might be used in returning the coded 1126 * code-block in this or any subsequent call to this method. If null a new 1127 * one is created and returned. If the 'data' array of 'cbb' is not null 1128 * it may be reused to return the compressed data. 1129 * 1130 * @return The next coded code-block in the current tile for component 1131 * 'n', or null if all code-blocks for the current tile have been 1132 * returned. 1133 * 1134 * @see CBlkRateDistStats 1135 * */ 1136 public CBlkRateDistStats getNextCodeBlock(int c, CBlkRateDistStats ccb) { 1137 long stime = 0L; // Start time for timed sections 1138 if (tPool == null) { // Use single threaded implementation 1139 // Get code-block data from source 1140 srcblkT[0] = src.getNextInternCodeBlock(c,srcblkT[0]); 1141 1142 if (DO_TIMING) stime = System.currentTimeMillis(); 1143 if (srcblkT[0] == null) { // We got all code-blocks 1144 return null; 1145 } 1146 // Initialize thread local variables 1147 if((opts[tIdx][c]&OPT_BYPASS) != 0 && boutT[0] == null) { 1148 boutT[0] = new BitToByteOutput(outT[0]); 1149 } 1150 // Initialize output code-block 1151 if (ccb == null) { 1152 ccb = new CBlkRateDistStats(); 1153 } 1154 // Compress code-block 1155 compressCodeBlock(c,ccb,srcblkT[0],mqT[0],boutT[0],outT[0], 1156 stateT[0],distbufT[0],ratebufT[0], 1157 istermbufT[0],symbufT[0],ctxtbufT[0], 1158 opts[tIdx][c],isReversible(tIdx,c), 1159 lenCalc[tIdx][c],tType[tIdx][c]); 1160 if (DO_TIMING) time[c] += System.currentTimeMillis()-stime; 1161 // Return result 1162 return ccb; 1163 } 1164 else { // Use multiple threaded implementation 1165 int cIdx; // Compressor idx 1166 Compressor compr; // Compressor 1167 1168 if (DO_TIMING) stime = System.currentTimeMillis(); 1169 // Give data to all free compressors, using the current component 1170 while (!finishedTileComponent[c] && !idleComps.empty()) { 1171 // Get an idle compressor 1172 compr = (Compressor) idleComps.pop(); 1173 cIdx = compr.getIdx(); 1174 // Get data for the compressor and wake it up 1175 if (DO_TIMING) time[c] += System.currentTimeMillis()-stime; 1176 srcblkT[cIdx] = src.getNextInternCodeBlock(c,srcblkT[cIdx]); 1177 if (DO_TIMING) stime = System.currentTimeMillis(); 1178 if (srcblkT[cIdx] != null) { 1179 // Initialize thread local variables 1180 if((opts[tIdx][c]&OPT_BYPASS) != 0 && boutT[cIdx] == null){ 1181 boutT[cIdx] = new BitToByteOutput(outT[cIdx]); 1182 } 1183 // Initialize output code-block and compressor thread 1184 if (ccb == null) ccb = new CBlkRateDistStats(); 1185 compr.ccb = ccb; 1186 compr.c = c; 1187 compr.options = opts[tIdx][c]; 1188 compr.rev = isReversible(tIdx,c); 1189 compr.lcType = lenCalc[tIdx][c]; 1190 compr.tType = tType[tIdx][c]; 1191 nBusyComps[c]++; 1192 ccb = null; 1193 // Send compressor to execution in thread pool 1194 tPool.runTarget(compr,completedComps[c]); 1195 } 1196 else { 1197 // We finished with all the code-blocks in the current 1198 // tile component 1199 idleComps.push(compr); 1200 finishedTileComponent[c] = true; 1201 } 1202 } 1203 // If there are threads for this component which result has not 1204 // been returned yet, get it 1205 if (nBusyComps[c] > 0) { 1206 synchronized (completedComps[c]) { 1207 // If no compressor is done, wait until one is 1208 if (completedComps[c].empty()) { 1209 try { 1210 if (DO_TIMING) { 1211 time[c] += System.currentTimeMillis()-stime; 1212 } 1213 completedComps[c].wait(); 1214 if (DO_TIMING) { 1215 stime = System.currentTimeMillis(); 1216 } 1217 } catch (InterruptedException e) { 1218 } 1219 } 1220 // Remove the thread from the completed queue and put it 1221 // on the idle queue 1222 compr = (Compressor) completedComps[c].pop(); 1223 cIdx = compr.getIdx(); 1224 nBusyComps[c]--; 1225 idleComps.push(compr); 1226 // Check targets error condition 1227 tPool.checkTargetErrors(); 1228 // Get the result of compression and return that. 1229 if (DO_TIMING) time[c] += System.currentTimeMillis()-stime; 1230 return compr.ccb; 1231 } 1232 } 1233 else { 1234 // Check targets error condition 1235 tPool.checkTargetErrors(); 1236 // Printing timing info if necessary 1237 if (DO_TIMING) time[c] += System.currentTimeMillis()-stime; 1238 // Nothing is running => no more code-blocks 1239 return null; 1240 } 1241 } 1242 } 1243 1244 /** 1245 * Changes the current tile, given the new indexes. An 1246 * IllegalArgumentException is thrown if the indexes do not 1247 * correspond to a valid tile. 1248 * 1249 * <P>This default implementation just changes the tile in the 1250 * source. 1251 * 1252 * @param x The horizontal index of the tile. 1253 * 1254 * @param y The vertical index of the new tile. 1255 * */ 1256 public void setTile(int x, int y) { 1257 super.setTile(x,y); 1258 // Reset the tilespecific variables 1259 if (finishedTileComponent != null) { 1260 for (int c=src.getNumComps()-1; c>=0; c--) { 1261 finishedTileComponent[c] = false; 1262 } 1263 } 1264 } 1265 1266 /** 1267 * Advances to the next tile, in standard scan-line order (by rows 1268 * then columns). An NoNextElementException is thrown if the 1269 * current tile is the last one (i.e. there is no next tile). 1270 * 1271 * <P>This default implementation just advances to the next tile 1272 * in the source. 1273 * */ 1274 public void nextTile() { 1275 // Reset the tilespecific variables 1276 if (finishedTileComponent != null) { 1277 for (int c=src.getNumComps()-1; c>=0; c--) { 1278 finishedTileComponent[c] = false; 1279 } 1280 } 1281 super.nextTile(); 1282 } 1283 1284 1285 /** 1286 * Compresses the code-block in 'srcblk' and puts the results in 'ccb', 1287 * using the specified options and temporary storage. 1288 * 1289 * @param c The component for which to return the next code-block. 1290 * 1291 * @param ccb The object where the compressed data will be stored. If the 1292 * 'data' array of 'cbb' is not null it may be reused to return the 1293 * compressed data. 1294 * 1295 * @param srcblk The code-block data to code 1296 * 1297 * @param mq The MQ-coder to use 1298 * 1299 * @param bout The bit level output to use. Used only if 'OPT_BYPASS' is 1300 * turned on in the 'options' argument. 1301 * 1302 * @param out The byte buffer trough which the compressed data is stored. 1303 * 1304 * @param state The state information for the code-block 1305 * 1306 * @param distbuf The buffer where to store the distortion at 1307 * the end of each coding pass. 1308 * 1309 * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at 1310 * the end of each coding pass. 1311 * 1312 * @param istermbuf The buffer where to store the terminated flag for each 1313 * coding pass. 1314 * 1315 * @param symbuf The buffer to hold symbols to send to the MQ coder 1316 * 1317 * @param ctxtbuf A buffer to hold the contexts to use in sending the 1318 * buffered symbols to the MQ coder. 1319 * 1320 * @param options The options to use when coding this code-block 1321 * 1322 * @param rev The reversible flag. Should be true if the source of this 1323 * code-block's data is reversible. 1324 * 1325 * @param lcType The type of length calculation to use with the MQ coder. 1326 * 1327 * @param tType The type of termination to use with the MQ coder. 1328 * 1329 * @see #getNextCodeBlock 1330 * */ 1331 static private void compressCodeBlock(int c, CBlkRateDistStats ccb, 1332 CBlkWTData srcblk, MQCoder mq, 1333 BitToByteOutput bout, 1334 ByteOutputBuffer out, 1335 int state[], 1336 double distbuf[], int ratebuf[], 1337 boolean istermbuf[], int symbuf[], 1338 int ctxtbuf[], int options, 1339 boolean rev, 1340 int lcType, int tType) { 1341 // NOTE: This method should not access any non-final instance or 1342 // static variables, either directly or indirectly through other 1343 // methods in order to be sure that the method is thread safe. 1344 1345 int zc_lut[]; // The ZC lookup table to use 1346 int skipbp; // The number of non-significant bit-planes to skip 1347 int curbp; // The current magnitude bit-plane (starts at 30) 1348 int fm[]; // The distortion estimation lookup table for MR 1349 int fs[]; // The distortion estimation lookup table for SC 1350 int lmb; // The least significant magnitude bit 1351 int npass; // The number of coding passes, for R-D statistics 1352 double msew; // The distortion (MSE weight) for the current bit-plane 1353 double totdist;// The total cumulative distortion decrease 1354 int ltpidx; // The index of the last pass which is terminated 1355 1356 1357 // Check error-resilient termination 1358 if ((options & OPT_PRED_TERM) != 0 && tType != MQCoder.TERM_PRED_ER) { 1359 throw 1360 new IllegalArgumentException("Embedded error-resilient info "+ 1361 "in MQ termination option "+ 1362 "specified but incorrect MQ "+ 1363 "termination "+ 1364 "policy specified"); 1365 } 1366 // Set MQ flags 1367 mq.setLenCalcType(lcType); 1368 mq.setTermType(tType); 1369 1370 lmb = 30-srcblk.magbits+1; 1371 // If there are are more bit-planes to code than the implementation 1372 // bitdepth set lmb to 0 1373 lmb = (lmb < 0) ? 0:lmb; 1374 1375 // Reset state 1376 ArrayUtil.intArraySet(state,0); 1377 1378 // Find the most significant bit-plane 1379 skipbp = calcSkipMSBP(srcblk,lmb); 1380 1381 // Initialize output code-block 1382 ccb.m = srcblk.m; 1383 ccb.n = srcblk.n; 1384 ccb.sb = srcblk.sb; 1385 ccb.nROIcoeff = srcblk.nROIcoeff; 1386 ccb.skipMSBP = skipbp; 1387 if(ccb.nROIcoeff!=0) { 1388 ccb.nROIcp = 3*(srcblk.nROIbp-skipbp-1)+1; 1389 } else { 1390 ccb.nROIcp = 0; 1391 } 1392 1393 // Choose correct ZC lookup table for global orientation 1394 switch (srcblk.sb.orientation) { 1395 case Subband.WT_ORIENT_HL: 1396 zc_lut = ZC_LUT_HL; 1397 break; 1398 case Subband.WT_ORIENT_LL: 1399 case Subband.WT_ORIENT_LH: 1400 zc_lut = ZC_LUT_LH; 1401 break; 1402 case Subband.WT_ORIENT_HH: 1403 zc_lut = ZC_LUT_HH; 1404 break; 1405 default: 1406 throw new Error("JJ2000 internal error"); 1407 } 1408 1409 // Loop on significant magnitude bit-planes doing the 3 passes 1410 curbp = 30-skipbp; 1411 fs = FS_LOSSY; 1412 fm = FM_LOSSY; 1413 msew = Math.pow(2,((curbp-lmb)<<1)-MSE_LKP_FRAC_BITS)* 1414 srcblk.sb.stepWMSE*srcblk.wmseScaling; 1415 totdist = 0f; 1416 npass = 0; 1417 ltpidx = -1; 1418 // First significant bit-plane has only the pass pass 1419 if (curbp >= lmb) { 1420 // Do we need the "lossless" 'fs' table ? 1421 if (rev && curbp == lmb) { 1422 fs = FM_LOSSLESS; 1423 } 1424 // We terminate if regular termination, last bit-plane, or next 1425 // bit-plane is "raw". 1426 istermbuf[npass] = (options & OPT_TERM_PASS) != 0 || curbp == lmb || 1427 ((options & OPT_BYPASS) != 0 && 1428 (31-NUM_NON_BYPASS_MS_BP-skipbp)>=curbp); 1429 totdist += cleanuppass(srcblk,mq,istermbuf[npass],curbp,state, 1430 fs,zc_lut,symbuf,ctxtbuf,ratebuf, 1431 npass,ltpidx,options)*msew; 1432 distbuf[npass] = totdist; 1433 if (istermbuf[npass]) ltpidx = npass; 1434 npass++; 1435 msew *= 0.25; 1436 curbp--; 1437 } 1438 // Other bit-planes have all passes 1439 while (curbp >= lmb) { 1440 // Do we need the "lossless" 'fs' and 'fm' tables ? 1441 if (rev && curbp == lmb) { 1442 fs = FS_LOSSLESS; 1443 fm = FM_LOSSLESS; 1444 } 1445 1446 // Do the significance propagation pass 1447 // We terminate if regular termination only 1448 istermbuf[npass] = (options & OPT_TERM_PASS) != 0; 1449 if ((options & OPT_BYPASS) == 0 || 1450 (31-NUM_NON_BYPASS_MS_BP-skipbp<=curbp)) { // No bypass coding 1451 totdist += sigProgPass(srcblk,mq,istermbuf[npass],curbp, 1452 state,fs,zc_lut, 1453 symbuf,ctxtbuf,ratebuf, 1454 npass,ltpidx,options)*msew; 1455 } 1456 else { // Bypass ("raw") coding 1457 bout.setPredTerm((options & OPT_PRED_TERM)!=0); 1458 totdist += rawSigProgPass(srcblk,bout,istermbuf[npass],curbp, 1459 state,fs,ratebuf,npass,ltpidx, 1460 options)*msew; 1461 } 1462 distbuf[npass] = totdist; 1463 if (istermbuf[npass]) ltpidx = npass; 1464 npass++; 1465 1466 // Do the magnitude refinement pass 1467 // We terminate if regular termination or bypass ("raw") coding 1468 istermbuf[npass] = (options & OPT_TERM_PASS) != 0 || 1469 ((options & OPT_BYPASS) != 0 && 1470 (31-NUM_NON_BYPASS_MS_BP-skipbp>curbp)); 1471 if ((options & OPT_BYPASS) == 0 || 1472 (31-NUM_NON_BYPASS_MS_BP-skipbp<=curbp)) { // No bypass coding 1473 totdist += magRefPass(srcblk,mq,istermbuf[npass],curbp,state, 1474 fm,symbuf,ctxtbuf,ratebuf, 1475 npass,ltpidx,options)*msew; 1476 } 1477 else { // Bypass ("raw") coding 1478 bout.setPredTerm((options & OPT_PRED_TERM)!=0); 1479 totdist += rawMagRefPass(srcblk,bout,istermbuf[npass],curbp, 1480 state,fm,ratebuf, 1481 npass,ltpidx,options)*msew; 1482 } 1483 distbuf[npass] = totdist; 1484 if (istermbuf[npass]) ltpidx = npass; 1485 npass++; 1486 1487 // Do the clenup pass 1488 // We terminate if regular termination, last bit-plane, or next 1489 // bit-plane is "raw". 1490 istermbuf[npass] = (options & OPT_TERM_PASS) != 0 || curbp == lmb || 1491 ((options & OPT_BYPASS) != 0 && 1492 (31-NUM_NON_BYPASS_MS_BP-skipbp)>=curbp); 1493 totdist += cleanuppass(srcblk,mq,istermbuf[npass],curbp,state, 1494 fs,zc_lut,symbuf,ctxtbuf,ratebuf, 1495 npass,ltpidx,options)*msew; 1496 distbuf[npass] = totdist; 1497 if (istermbuf[npass]) ltpidx = npass; 1498 npass++; 1499 1500 // Goto next bit-plane 1501 msew *= 0.25; 1502 curbp--; 1503 } 1504 1505 // Copy compressed data and rate-distortion statistics to output 1506 ccb.data = new byte[out.size()]; 1507 out.toByteArray(0,out.size(),ccb.data,0); 1508 checkEndOfPassFF(ccb.data,ratebuf,istermbuf,npass); 1509 ccb.selectConvexHull(ratebuf,distbuf, 1510 (options&(OPT_BYPASS|OPT_TERM_PASS))!=0?istermbuf: 1511 null,npass,rev); 1512 1513 // Reset MQ coder and bit output for next code-block 1514 mq.reset(); 1515 if (bout != null) bout.reset(); 1516 1517 // Done 1518 } 1519 1520 /** 1521 * Calculates the number of magnitude bit-planes that are to be skipped, 1522 * because they are non-significant. The algorithm looks for the largest 1523 * magnitude and calculates the most significant bit-plane of it. 1524 * 1525 * @param cblk The code-block of data to scan 1526 * 1527 * @param lmb The least significant magnitude bit in the data 1528 * 1529 * @return The number of magnitude bit-planes to skip (i.e. all zero most 1530 * significant bit-planes). 1531 **/ 1532 static private int calcSkipMSBP(CBlkWTData cblk, int lmb) { 1533 int k,kmax,mask; 1534 int data[]; 1535 int maxmag; 1536 int mag; 1537 int w,h; 1538 int msbp; 1539 int l; 1540 1541 data = (int[]) cblk.getData(); 1542 w = cblk.w; 1543 h = cblk.h; 1544 1545 // First look for the maximum magnitude in the code-block 1546 maxmag = 0; 1547 // Consider only magnitude bits that are in non-fractional bit-planes. 1548 mask = 0x7FFFFFFF&(~((1<<lmb)-1)); 1549 for (l=h-1, k=cblk.offset; l>=0; l--) { 1550 for (kmax = k+w; k<kmax; k++) { 1551 mag = data[k]&mask; 1552 if (mag > maxmag) maxmag = mag; 1553 } 1554 k += cblk.scanw-w; 1555 } 1556 // Now calculate the number of all zero most significant bit-planes for 1557 // the maximum magnitude. 1558 msbp = 30; 1559 do { 1560 if (((1<<msbp)&maxmag)!=0) break; 1561 msbp--; 1562 } while (msbp>=lmb); 1563 1564 // Return the number of non-significant bit-planes to skip 1565 return 30-msbp; 1566 } 1567 1568 /** 1569 * Performs the significance propagation pass on the specified data and 1570 * bit-plane. It codes all insignificant samples which have, at least, one 1571 * of its immediate eight neighbors already significant, using the ZC and 1572 * SC primitives as needed. It toggles the "visited" state bit to 1 for 1573 * all those samples. 1574 * 1575 * @param srcblk The code-block data to code 1576 * 1577 * @param mq The MQ-coder to use 1578 * 1579 * @param doterm If true it performs an MQ-coder termination after the end 1580 * of the pass 1581 * 1582 * @param bp The bit-plane to code 1583 * 1584 * @param state The state information for the code-block 1585 * 1586 * @param fs The distortion estimation lookup table for SC 1587 * 1588 * @param zc_lut The ZC lookup table to use in ZC. 1589 * 1590 * @param symbuf The buffer to hold symbols to send to the MQ coder 1591 * 1592 * @param ctxtbuf A buffer to hold the contexts to use in sending the 1593 * buffered symbols to the MQ coder. 1594 * 1595 * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at 1596 * the end of this coding pass. 1597 * 1598 * @param pidx The coding pass index. Is the index in the 'ratebuf' array 1599 * where to store the coded length after this coding pass. 1600 * 1601 * @param ltpidx The index of the last pass that was terminated, or 1602 * negative if none. 1603 * 1604 * @param options The bitmask of entropy coding options to apply to the 1605 * code-block 1606 * 1607 * @return The decrease in distortion for this pass, in the fixed-point 1608 * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables. 1609 * */ 1610 static private int sigProgPass(CBlkWTData srcblk, MQCoder mq, 1611 boolean doterm, int bp, int state[], 1612 int fs[], int zc_lut[], 1613 int symbuf[], int ctxtbuf[], 1614 int ratebuf[], int pidx, int ltpidx, 1615 int options) { 1616 int j,sj; // The state index for line and stripe 1617 int k,sk; // The data index for line and stripe 1618 int nsym; // Symbol counter for symbol and context buffers 1619 int dscanw; // The data scan-width 1620 int sscanw; // The state and packed state scan-width 1621 int jstep; // Stripe to stripe step for 'sj' 1622 int kstep; // Stripe to stripe step for 'sk' 1623 int stopsk; // The loop limit on the variable sk 1624 int csj; // Local copy (i.e. cached) of 'state[j]' 1625 int mask; // The mask for the current bit-plane 1626 int sym; // The symbol to code 1627 int ctxt; // The context to use 1628 int data[]; // The data buffer 1629 int dist; // The distortion reduction for this pass 1630 int shift; // Shift amount for distortion 1631 int upshift; // Shift left amount for distortion 1632 int downshift; // Shift right amount for distortion 1633 int normval; // The normalized sample magnitude value 1634 int s; // The stripe index 1635 boolean causal; // Flag to indicate if stripe-causal context 1636 // formation is to be used 1637 int nstripes; // The number of stripes in the code-block 1638 int sheight; // Height of the current stripe 1639 int off_ul,off_ur,off_dr,off_dl; // offsets 1640 1641 // Initialize local variables 1642 dscanw = srcblk.scanw; 1643 sscanw = srcblk.w+2; 1644 jstep = sscanw*STRIPE_HEIGHT/2-srcblk.w; 1645 kstep = dscanw*STRIPE_HEIGHT-srcblk.w; 1646 mask = 1<<bp; 1647 data = (int[]) srcblk.getData(); 1648 nstripes = (srcblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 1649 dist = 0; 1650 // We use the MSE_LKP_BITS-1 bits below the bit just coded for 1651 // distortion estimation. 1652 shift = bp-(MSE_LKP_BITS-1); 1653 upshift = (shift>=0) ? 0 : -shift; 1654 downshift = (shift<=0) ? 0 : shift; 1655 causal = (options & OPT_VERT_STR_CAUSAL) != 0; 1656 1657 // Pre-calculate offsets in 'state' for diagonal neighbors 1658 off_ul = -sscanw-1; // up-left 1659 off_ur = -sscanw+1; // up-right 1660 off_dr = sscanw+1; // down-right 1661 off_dl = sscanw-1; // down-left 1662 1663 // Code stripe by stripe 1664 sk = srcblk.offset; 1665 sj = sscanw+1; 1666 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 1667 sheight = (s != 0) ? STRIPE_HEIGHT : 1668 srcblk.h-(nstripes-1)*STRIPE_HEIGHT; 1669 stopsk = sk+srcblk.w; 1670 // Scan by set of 1 stripe column at a time 1671 for (nsym = 0; sk < stopsk; sk++, sj++) { 1672 // Do half top of column 1673 j = sj; 1674 csj = state[j]; 1675 // If any of the two samples is not significant and has a 1676 // non-zero context (i.e. some neighbor is significant) we can 1677 // not skip them 1678 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 1679 k = sk; 1680 // Scan first row 1681 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 1682 STATE_NZ_CTXT_R1) { 1683 // Apply zero coding 1684 ctxtbuf[nsym] = zc_lut[csj&ZC_MASK]; 1685 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 1686 // Became significant 1687 // Apply sign coding 1688 sym = data[k]>>>31; 1689 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 1690 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 1691 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 1692 // Update state information (significant bit, 1693 // visited bit, neighbor significant bit of 1694 // neighbors, non zero context of neighbors, sign 1695 // of neighbors) 1696 if (!causal) { 1697 // If in causal mode do not change contexts of 1698 // previous stripe. 1699 state[j+off_ul] |= 1700 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 1701 state[j+off_ur] |= 1702 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 1703 } 1704 // Update sign state information of neighbors 1705 if (sym != 0) { 1706 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1707 STATE_NZ_CTXT_R2| 1708 STATE_V_U_R2|STATE_V_U_SIGN_R2; 1709 if (!causal) { 1710 // If in causal mode do not change 1711 // contexts of previous stripe. 1712 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1713 STATE_V_D_R2|STATE_V_D_SIGN_R2; 1714 } 1715 state[j+1] |= 1716 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1717 STATE_H_L_R1|STATE_H_L_SIGN_R1| 1718 STATE_D_UL_R2; 1719 state[j-1] |= 1720 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1721 STATE_H_R_R1|STATE_H_R_SIGN_R1| 1722 STATE_D_UR_R2; 1723 } 1724 else { 1725 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1726 STATE_NZ_CTXT_R2|STATE_V_U_R2; 1727 if (!causal) { 1728 // If in causal mode do not change 1729 // contexts of previous stripe. 1730 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1731 STATE_V_D_R2; 1732 } 1733 state[j+1] |= 1734 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1735 STATE_H_L_R1|STATE_D_UL_R2; 1736 state[j-1] |= 1737 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1738 STATE_H_R_R1|STATE_D_UR_R2; 1739 } 1740 // Update distortion 1741 normval = (data[k] >> downshift) << upshift; 1742 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 1743 } 1744 else { 1745 csj |= STATE_VISITED_R1; 1746 } 1747 } 1748 if (sheight < 2) { 1749 state[j] = csj; 1750 continue; 1751 } 1752 // Scan second row 1753 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 1754 STATE_NZ_CTXT_R2) { 1755 k += dscanw; 1756 // Apply zero coding 1757 ctxtbuf[nsym] = zc_lut[(csj>>>STATE_SEP)&ZC_MASK]; 1758 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 1759 // Became significant 1760 // Apply sign coding 1761 sym = data[k]>>>31; 1762 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 1763 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 1764 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 1765 // Update state information (significant bit, 1766 // visited bit, neighbor significant bit of 1767 // neighbors, non zero context of neighbors, sign 1768 // of neighbors) 1769 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 1770 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 1771 // Update sign state information of neighbors 1772 if (sym != 0) { 1773 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1774 STATE_NZ_CTXT_R1| 1775 STATE_V_D_R1|STATE_V_D_SIGN_R1; 1776 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1777 STATE_V_U_R1|STATE_V_U_SIGN_R1; 1778 state[j+1] |= 1779 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1780 STATE_D_DL_R1| 1781 STATE_H_L_R2|STATE_H_L_SIGN_R2; 1782 state[j-1] |= 1783 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1784 STATE_D_DR_R1| 1785 STATE_H_R_R2|STATE_H_R_SIGN_R2; 1786 } 1787 else { 1788 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1789 STATE_NZ_CTXT_R1|STATE_V_D_R1; 1790 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1791 STATE_V_U_R1; 1792 state[j+1] |= 1793 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1794 STATE_D_DL_R1|STATE_H_L_R2; 1795 state[j-1] |= 1796 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1797 STATE_D_DR_R1|STATE_H_R_R2; 1798 } 1799 // Update distortion 1800 normval = (data[k] >> downshift) << upshift; 1801 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 1802 } 1803 else { 1804 csj |= STATE_VISITED_R2; 1805 } 1806 } 1807 state[j] = csj; 1808 } 1809 // Do half bottom of column 1810 if (sheight < 3) continue; 1811 j += sscanw; 1812 csj = state[j]; 1813 // If any of the two samples is not significant and has a 1814 // non-zero context (i.e. some neighbor is significant) we can 1815 // not skip them 1816 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 1817 k = sk+(dscanw<<1); 1818 // Scan first row 1819 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 1820 STATE_NZ_CTXT_R1) { 1821 // Apply zero coding 1822 ctxtbuf[nsym] = zc_lut[csj&ZC_MASK]; 1823 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 1824 // Became significant 1825 // Apply sign coding 1826 sym = data[k]>>>31; 1827 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 1828 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 1829 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 1830 // Update state information (significant bit, 1831 // visited bit, neighbor significant bit of 1832 // neighbors, non zero context of neighbors, sign 1833 // of neighbors) 1834 state[j+off_ul] |= STATE_NZ_CTXT_R2|STATE_D_DR_R2; 1835 state[j+off_ur] |= STATE_NZ_CTXT_R2|STATE_D_DL_R2; 1836 // Update sign state information of neighbors 1837 if (sym != 0) { 1838 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1839 STATE_NZ_CTXT_R2| 1840 STATE_V_U_R2|STATE_V_U_SIGN_R2; 1841 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1842 STATE_V_D_R2|STATE_V_D_SIGN_R2; 1843 state[j+1] |= 1844 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1845 STATE_H_L_R1|STATE_H_L_SIGN_R1| 1846 STATE_D_UL_R2; 1847 state[j-1] |= 1848 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1849 STATE_H_R_R1|STATE_H_R_SIGN_R1| 1850 STATE_D_UR_R2; 1851 } 1852 else { 1853 csj |= STATE_SIG_R1|STATE_VISITED_R1| 1854 STATE_NZ_CTXT_R2|STATE_V_U_R2; 1855 state[j-sscanw] |= STATE_NZ_CTXT_R2| 1856 STATE_V_D_R2; 1857 state[j+1] |= 1858 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1859 STATE_H_L_R1|STATE_D_UL_R2; 1860 state[j-1] |= 1861 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1862 STATE_H_R_R1|STATE_D_UR_R2; 1863 } 1864 // Update distortion 1865 normval = (data[k] >> downshift) << upshift; 1866 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 1867 } 1868 else { 1869 csj |= STATE_VISITED_R1; 1870 } 1871 } 1872 if (sheight < 4) { 1873 state[j] = csj; 1874 continue; 1875 } 1876 // Scan second row 1877 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 1878 STATE_NZ_CTXT_R2) { 1879 k += dscanw; 1880 // Apply zero coding 1881 ctxtbuf[nsym] = zc_lut[(csj>>>STATE_SEP)&ZC_MASK]; 1882 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 1883 // Became significant 1884 // Apply sign coding 1885 sym = data[k]>>>31; 1886 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 1887 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 1888 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 1889 // Update state information (significant bit, 1890 // visited bit, neighbor significant bit of 1891 // neighbors, non zero context of neighbors, sign 1892 // of neighbors) 1893 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 1894 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 1895 // Update sign state information of neighbors 1896 if (sym != 0) { 1897 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1898 STATE_NZ_CTXT_R1| 1899 STATE_V_D_R1|STATE_V_D_SIGN_R1; 1900 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1901 STATE_V_U_R1|STATE_V_U_SIGN_R1; 1902 state[j+1] |= 1903 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1904 STATE_D_DL_R1| 1905 STATE_H_L_R2|STATE_H_L_SIGN_R2; 1906 state[j-1] |= 1907 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1908 STATE_D_DR_R1| 1909 STATE_H_R_R2|STATE_H_R_SIGN_R2; 1910 } 1911 else { 1912 csj |= STATE_SIG_R2|STATE_VISITED_R2| 1913 STATE_NZ_CTXT_R1|STATE_V_D_R1; 1914 state[j+sscanw] |= STATE_NZ_CTXT_R1| 1915 STATE_V_U_R1; 1916 state[j+1] |= 1917 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1918 STATE_D_DL_R1|STATE_H_L_R2; 1919 state[j-1] |= 1920 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 1921 STATE_D_DR_R1|STATE_H_R_R2; 1922 } 1923 // Update distortion 1924 normval = (data[k] >> downshift) << upshift; 1925 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 1926 } 1927 else { 1928 csj |= STATE_VISITED_R2; 1929 } 1930 } 1931 state[j] = csj; 1932 } 1933 } 1934 // Code all buffered symbols 1935 mq.codeSymbols(symbuf,ctxtbuf,nsym); 1936 } 1937 1938 // Reset the MQ context states if we need to 1939 if ((options & OPT_RESET_MQ) != 0) { 1940 mq.resetCtxts(); 1941 } 1942 1943 // Terminate the MQ bit stream if we need to 1944 if (doterm) { 1945 ratebuf[pidx] = mq.terminate(); // Termination has special length 1946 } 1947 else { // Use normal length calculation 1948 ratebuf[pidx] = mq.getNumCodedBytes(); 1949 } 1950 // Add length of previous segments, if any 1951 if (ltpidx >=0) { 1952 ratebuf[pidx] += ratebuf[ltpidx]; 1953 } 1954 // Finish length calculation if needed 1955 if (doterm) { 1956 mq.finishLengthCalculation(ratebuf,pidx); 1957 } 1958 1959 // Return the reduction in distortion 1960 return dist; 1961 } 1962 1963 /** 1964 * Performs the significance propagation pass on the specified data and 1965 * bit-plane, without using the arithmetic coder. It codes all 1966 * insignificant samples which have, at least, one of its immediate eight 1967 * neighbors already significant, using the ZC and SC primitives as 1968 * needed. It toggles the "visited" state bit to 1 for all those samples. 1969 * 1970 * <P>In this method, the arithmetic coder is bypassed, and raw bits are 1971 * directly written in the bit stream (useful when distribution are close 1972 * to uniform, for intance, at high bit-rates and at lossless 1973 * compression). 1974 * 1975 * @param srcblk The code-block data to code 1976 * 1977 * @param bout The bit based output 1978 * 1979 * @param doterm If true the bit based output is byte aligned after the 1980 * end of the pass. 1981 * 1982 * @param bp The bit-plane to code 1983 * 1984 * @param state The state information for the code-block 1985 * 1986 * @param fs The distortion estimation lookup table for SC 1987 * 1988 * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at 1989 * the end of this coding pass. 1990 * 1991 * @param pidx The coding pass index. Is the index in the 'ratebuf' array 1992 * where to store the coded length after this coding pass. 1993 * 1994 * @param ltpidx The index of the last pass that was terminated, or 1995 * negative if none. 1996 * 1997 * @param options The bitmask of entropy coding options to apply to the 1998 * code-block 1999 * 2000 * @return The decrease in distortion for this pass, in the fixed-point 2001 * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables. 2002 * */ 2003 static private int rawSigProgPass(CBlkWTData srcblk, BitToByteOutput bout, 2004 boolean doterm, int bp, int state[], 2005 int fs[], int ratebuf[], int pidx, 2006 int ltpidx, int options) { 2007 int j,sj; // The state index for line and stripe 2008 int k,sk; // The data index for line and stripe 2009 int dscanw; // The data scan-width 2010 int sscanw; // The state scan-width 2011 int jstep; // Stripe to stripe step for 'sj' 2012 int kstep; // Stripe to stripe step for 'sk' 2013 int stopsk; // The loop limit on the variable sk 2014 int csj; // Local copy (i.e. cached) of 'state[j]' 2015 int mask; // The mask for the current bit-plane 2016 int nsym = 0; // Number of symbol 2017 int sym; // The symbol to code 2018 int data[]; // The data buffer 2019 int dist; // The distortion reduction for this pass 2020 int shift; // Shift amount for distortion 2021 int upshift; // Shift left amount for distortion 2022 int downshift; // Shift right amount for distortion 2023 int normval; // The normalized sample magnitude value 2024 int s; // The stripe index 2025 boolean causal; // Flag to indicate if stripe-causal context 2026 // formation is to be used 2027 int nstripes; // The number of stripes in the code-block 2028 int sheight; // Height of the current stripe 2029 int off_ul,off_ur,off_dr,off_dl; // offsets 2030 2031 // Initialize local variables 2032 dscanw = srcblk.scanw; 2033 sscanw = srcblk.w+2; 2034 jstep = sscanw*STRIPE_HEIGHT/2-srcblk.w; 2035 kstep = dscanw*STRIPE_HEIGHT-srcblk.w; 2036 mask = 1<<bp; 2037 data = (int[]) srcblk.getData(); 2038 nstripes = (srcblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 2039 dist = 0; 2040 // We use the MSE_LKP_BITS-1 bits below the bit just coded for 2041 // distortion estimation. 2042 shift = bp-(MSE_LKP_BITS-1); 2043 upshift = (shift>=0) ? 0 : -shift; 2044 downshift = (shift<=0) ? 0 : shift; 2045 causal = (options & OPT_VERT_STR_CAUSAL) != 0; 2046 2047 // Pre-calculate offsets in 'state' for neighbors 2048 off_ul = -sscanw-1; // up-left 2049 off_ur = -sscanw+1; // up-right 2050 off_dr = sscanw+1; // down-right 2051 off_dl = sscanw-1; // down-left 2052 2053 // Code stripe by stripe 2054 sk = srcblk.offset; 2055 sj = sscanw+1; 2056 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 2057 sheight = (s != 0) ? STRIPE_HEIGHT : 2058 srcblk.h-(nstripes-1)*STRIPE_HEIGHT; 2059 stopsk = sk+srcblk.w; 2060 // Scan by set of 1 stripe column at a time 2061 for (; sk < stopsk; sk++, sj++) { 2062 // Do half top of column 2063 j = sj; 2064 csj = state[j]; 2065 // If any of the two samples is not significant and has a 2066 // non-zero context (i.e. some neighbor is significant) we can 2067 // not skip them 2068 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 2069 k = sk; 2070 // Scan first row 2071 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 2072 STATE_NZ_CTXT_R1) { 2073 // Apply zero coding 2074 sym = (data[k]&mask)>>>bp; 2075 bout.writeBit(sym); 2076 nsym++; 2077 2078 if (sym != 0) { 2079 // Became significant 2080 // Apply sign coding 2081 sym = data[k]>>>31; 2082 bout.writeBit(sym); 2083 nsym++; 2084 2085 // Update state information (significant bit, 2086 // visited bit, neighbor significant bit of 2087 // neighbors, non zero context of neighbors, sign 2088 // of neighbors) 2089 if (!causal) { 2090 // If in causal mode do not change contexts of 2091 // previous stripe. 2092 state[j+off_ul] |= 2093 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 2094 state[j+off_ur] |= 2095 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 2096 } 2097 // Update sign state information of neighbors 2098 if (sym != 0) { 2099 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2100 STATE_NZ_CTXT_R2| 2101 STATE_V_U_R2|STATE_V_U_SIGN_R2; 2102 if (!causal) { 2103 // If in causal mode do not change 2104 // contexts of previous stripe. 2105 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2106 STATE_V_D_R2|STATE_V_D_SIGN_R2; 2107 } 2108 state[j+1] |= 2109 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2110 STATE_H_L_R1|STATE_H_L_SIGN_R1| 2111 STATE_D_UL_R2; 2112 state[j-1] |= 2113 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2114 STATE_H_R_R1|STATE_H_R_SIGN_R1| 2115 STATE_D_UR_R2; 2116 } 2117 else { 2118 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2119 STATE_NZ_CTXT_R2|STATE_V_U_R2; 2120 if (!causal) { 2121 // If in causal mode do not change 2122 // contexts of previous stripe. 2123 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2124 STATE_V_D_R2; 2125 } 2126 state[j+1] |= 2127 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2128 STATE_H_L_R1|STATE_D_UL_R2; 2129 state[j-1] |= 2130 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2131 STATE_H_R_R1|STATE_D_UR_R2; 2132 } 2133 // Update distortion 2134 normval = (data[k] >> downshift) << upshift; 2135 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 2136 } 2137 else { 2138 csj |= STATE_VISITED_R1; 2139 } 2140 } 2141 if (sheight < 2) { 2142 state[j] = csj; 2143 continue; 2144 } 2145 // Scan second row 2146 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 2147 STATE_NZ_CTXT_R2) { 2148 k += dscanw; 2149 // Apply zero coding 2150 sym = (data[k]&mask)>>>bp; 2151 bout.writeBit(sym); 2152 nsym++; 2153 if (sym != 0) { 2154 // Became significant 2155 // Apply sign coding 2156 sym = data[k]>>>31; 2157 bout.writeBit(sym); 2158 nsym++; 2159 // Update state information (significant bit, 2160 // visited bit, neighbor significant bit of 2161 // neighbors, non zero context of neighbors, sign 2162 // of neighbors) 2163 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 2164 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 2165 // Update sign state information of neighbors 2166 if (sym != 0) { 2167 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2168 STATE_NZ_CTXT_R1| 2169 STATE_V_D_R1|STATE_V_D_SIGN_R1; 2170 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2171 STATE_V_U_R1|STATE_V_U_SIGN_R1; 2172 state[j+1] |= 2173 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2174 STATE_D_DL_R1| 2175 STATE_H_L_R2|STATE_H_L_SIGN_R2; 2176 state[j-1] |= 2177 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2178 STATE_D_DR_R1| 2179 STATE_H_R_R2|STATE_H_R_SIGN_R2; 2180 } 2181 else { 2182 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2183 STATE_NZ_CTXT_R1|STATE_V_D_R1; 2184 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2185 STATE_V_U_R1; 2186 state[j+1] |= 2187 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2188 STATE_D_DL_R1|STATE_H_L_R2; 2189 state[j-1] |= 2190 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2191 STATE_D_DR_R1|STATE_H_R_R2; 2192 } 2193 // Update distortion 2194 normval = (data[k] >> downshift) << upshift; 2195 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 2196 } 2197 else { 2198 csj |= STATE_VISITED_R2; 2199 } 2200 } 2201 state[j] = csj; 2202 } 2203 // Do half bottom of column 2204 if (sheight < 3) continue; 2205 j += sscanw; 2206 csj = state[j]; 2207 // If any of the two samples is not significant and has a 2208 // non-zero context (i.e. some neighbor is significant) we can 2209 // not skip them 2210 if ((((~csj) & (csj<<2)) & SIG_MASK_R1R2) != 0) { 2211 k = sk+(dscanw<<1); 2212 // Scan first row 2213 if ((csj & (STATE_SIG_R1|STATE_NZ_CTXT_R1)) == 2214 STATE_NZ_CTXT_R1) { 2215 sym = (data[k]&mask)>>>bp; 2216 bout.writeBit(sym); 2217 nsym++; 2218 if (sym != 0) { 2219 // Became significant 2220 // Apply sign coding 2221 sym = data[k]>>>31; 2222 bout.writeBit(sym); 2223 nsym++; 2224 // Update state information (significant bit, 2225 // visited bit, neighbor significant bit of 2226 // neighbors, non zero context of neighbors, sign 2227 // of neighbors) 2228 state[j+off_ul] |= STATE_NZ_CTXT_R2|STATE_D_DR_R2; 2229 state[j+off_ur] |= STATE_NZ_CTXT_R2|STATE_D_DL_R2; 2230 // Update sign state information of neighbors 2231 if (sym != 0) { 2232 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2233 STATE_NZ_CTXT_R2| 2234 STATE_V_U_R2|STATE_V_U_SIGN_R2; 2235 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2236 STATE_V_D_R2|STATE_V_D_SIGN_R2; 2237 state[j+1] |= 2238 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2239 STATE_H_L_R1|STATE_H_L_SIGN_R1| 2240 STATE_D_UL_R2; 2241 state[j-1] |= 2242 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2243 STATE_H_R_R1|STATE_H_R_SIGN_R1| 2244 STATE_D_UR_R2; 2245 } 2246 else { 2247 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2248 STATE_NZ_CTXT_R2|STATE_V_U_R2; 2249 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2250 STATE_V_D_R2; 2251 state[j+1] |= 2252 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2253 STATE_H_L_R1|STATE_D_UL_R2; 2254 state[j-1] |= 2255 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2256 STATE_H_R_R1|STATE_D_UR_R2; 2257 } 2258 // Update distortion 2259 normval = (data[k] >> downshift) << upshift; 2260 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 2261 } 2262 else { 2263 csj |= STATE_VISITED_R1; 2264 } 2265 } 2266 if (sheight < 4) { 2267 state[j] = csj; 2268 continue; 2269 } 2270 if ((csj & (STATE_SIG_R2|STATE_NZ_CTXT_R2)) == 2271 STATE_NZ_CTXT_R2) { 2272 k += dscanw; 2273 // Apply zero coding 2274 sym = (data[k]&mask)>>>bp; 2275 bout.writeBit(sym); 2276 nsym++; 2277 if (sym != 0) { 2278 // Became significant 2279 // Apply sign coding 2280 sym = data[k]>>>31; 2281 bout.writeBit(sym); 2282 nsym++; 2283 // Update state information (significant bit, 2284 // visited bit, neighbor significant bit of 2285 // neighbors, non zero context of neighbors, sign 2286 // of neighbors) 2287 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 2288 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 2289 // Update sign state information of neighbors 2290 if (sym != 0) { 2291 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2292 STATE_NZ_CTXT_R1| 2293 STATE_V_D_R1|STATE_V_D_SIGN_R1; 2294 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2295 STATE_V_U_R1|STATE_V_U_SIGN_R1; 2296 state[j+1] |= 2297 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2298 STATE_D_DL_R1| 2299 STATE_H_L_R2|STATE_H_L_SIGN_R2; 2300 state[j-1] |= 2301 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2302 STATE_D_DR_R1| 2303 STATE_H_R_R2|STATE_H_R_SIGN_R2; 2304 } 2305 else { 2306 csj |= STATE_SIG_R2|STATE_VISITED_R2| 2307 STATE_NZ_CTXT_R1|STATE_V_D_R1; 2308 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2309 STATE_V_U_R1; 2310 state[j+1] |= 2311 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2312 STATE_D_DL_R1|STATE_H_L_R2; 2313 state[j-1] |= 2314 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2315 STATE_D_DR_R1|STATE_H_R_R2; 2316 } 2317 // Update distortion 2318 normval = (data[k] >> downshift) << upshift; 2319 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 2320 } 2321 else { 2322 csj |= STATE_VISITED_R2; 2323 } 2324 } 2325 state[j] = csj; 2326 } 2327 } 2328 } 2329 2330 // Get length and terminate if needed 2331 if (doterm) { 2332 ratebuf[pidx] = bout.terminate(); 2333 } else { 2334 ratebuf[pidx] = bout.length(); 2335 } 2336 // Add length of previous segments, if any 2337 if (ltpidx >=0) { 2338 ratebuf[pidx] += ratebuf[ltpidx]; 2339 } 2340 2341 // Return the reduction in distortion 2342 return dist; 2343 } 2344 2345 /** 2346 * Performs the magnitude refinement pass on the specified data and 2347 * bit-plane. It codes the samples which are significant and which do not 2348 * have the "visited" state bit turned on, using the MR primitive. The 2349 * "visited" state bit is not mofified for any samples. 2350 * 2351 * @param srcblk The code-block data to code 2352 * 2353 * @param mq The MQ-coder to use 2354 * 2355 * @param doterm If true it performs an MQ-coder termination after the end 2356 * of the pass 2357 * 2358 * @param bp The bit-plane to code 2359 * 2360 * @param state The state information for the code-block 2361 * 2362 * @param fm The distortion estimation lookup table for MR 2363 * 2364 * @param symbuf The buffer to hold symbols to send to the MQ coder 2365 * 2366 * @param ctxtbuf A buffer to hold the contexts to use in sending the 2367 * buffered symbols to the MQ coder. 2368 * 2369 * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at 2370 * the end of this coding pass. 2371 * 2372 * @param pidx The coding pass index. Is the index in the 'ratebuf' array 2373 * where to store the coded length after this coding pass. 2374 * 2375 * @param ltpidx The index of the last pass that was terminated, or 2376 * negative if none. 2377 * 2378 * @param options The bitmask of entropy coding options to apply to the 2379 * code-block 2380 * 2381 * @return The decrease in distortion for this pass, in the fixed-point 2382 * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables. 2383 * */ 2384 static private int magRefPass(CBlkWTData srcblk, MQCoder mq, boolean doterm, 2385 int bp, int state[], int fm[], int symbuf[], 2386 int ctxtbuf[], int ratebuf[], int pidx, 2387 int ltpidx, int options) { 2388 int j,sj; // The state index for line and stripe 2389 int k,sk; // The data index for line and stripe 2390 int nsym=0; // Symbol counter for symbol and context buffers 2391 int dscanw; // The data scan-width 2392 int sscanw; // The state scan-width 2393 int jstep; // Stripe to stripe step for 'sj' 2394 int kstep; // Stripe to stripe step for 'sk' 2395 int stopsk; // The loop limit on the variable sk 2396 int csj; // Local copy (i.e. cached) of 'state[j]' 2397 int mask; // The mask for the current bit-plane 2398 int data[]; // The data buffer 2399 int dist; // The distortion reduction for this pass 2400 int shift; // Shift amount for distortion 2401 int upshift; // Shift left amount for distortion 2402 int downshift; // Shift right amount for distortion 2403 int normval; // The normalized sample magnitude value 2404 int s; // The stripe index 2405 int nstripes; // The number of stripes in the code-block 2406 int sheight; // Height of the current stripe 2407 2408 // Initialize local variables 2409 dscanw = srcblk.scanw; 2410 sscanw = srcblk.w+2; 2411 jstep = sscanw*STRIPE_HEIGHT/2-srcblk.w; 2412 kstep = dscanw*STRIPE_HEIGHT-srcblk.w; 2413 mask = 1<<bp; 2414 data = (int[]) srcblk.getData(); 2415 nstripes = (srcblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 2416 dist = 0; 2417 // We use the bit just coded plus MSE_LKP_BITS-1 bits below the bit 2418 // just coded for distortion estimation. 2419 shift = bp-(MSE_LKP_BITS-1); 2420 upshift = (shift>=0) ? 0 : -shift; 2421 downshift = (shift<=0) ? 0 : shift; 2422 2423 // Code stripe by stripe 2424 sk = srcblk.offset; 2425 sj = sscanw+1; 2426 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 2427 sheight = (s != 0) ? STRIPE_HEIGHT : 2428 srcblk.h-(nstripes-1)*STRIPE_HEIGHT; 2429 stopsk = sk+srcblk.w; 2430 // Scan by set of 1 stripe column at a time 2431 for (nsym = 0; sk < stopsk; sk++, sj++) { 2432 // Do half top of column 2433 j = sj; 2434 csj = state[j]; 2435 // If any of the two samples is significant and not yet 2436 // visited in the current bit-plane we can not skip them 2437 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 2438 k = sk; 2439 // Scan first row 2440 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 2441 STATE_SIG_R1) { 2442 // Apply MR primitive 2443 symbuf[nsym] = (data[k]&mask)>>>bp; 2444 ctxtbuf[nsym++] = MR_LUT[csj&MR_MASK]; 2445 // Update the STATE_PREV_MR bit 2446 csj |= STATE_PREV_MR_R1; 2447 // Update distortion 2448 normval = (data[k] >> downshift) << upshift; 2449 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2450 } 2451 if (sheight < 2) { 2452 state[j] = csj; 2453 continue; 2454 } 2455 // Scan second row 2456 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 2457 STATE_SIG_R2) { 2458 k += dscanw; 2459 // Apply MR primitive 2460 symbuf[nsym] = (data[k]&mask)>>>bp; 2461 ctxtbuf[nsym++] = MR_LUT[(csj>>>STATE_SEP)&MR_MASK]; 2462 // Update the STATE_PREV_MR bit 2463 csj |= STATE_PREV_MR_R2; 2464 // Update distortion 2465 normval = (data[k] >> downshift) << upshift; 2466 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2467 } 2468 state[j] = csj; 2469 } 2470 // Do half bottom of column 2471 if (sheight < 3) continue; 2472 j += sscanw; 2473 csj = state[j]; 2474 // If any of the two samples is significant and not yet 2475 // visited in the current bit-plane we can not skip them 2476 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 2477 k = sk+(dscanw<<1); 2478 // Scan first row 2479 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 2480 STATE_SIG_R1) { 2481 // Apply MR primitive 2482 symbuf[nsym] = (data[k]&mask)>>>bp; 2483 ctxtbuf[nsym++] = MR_LUT[csj&MR_MASK]; 2484 // Update the STATE_PREV_MR bit 2485 csj |= STATE_PREV_MR_R1; 2486 // Update distortion 2487 normval = (data[k] >> downshift) << upshift; 2488 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2489 } 2490 if (sheight < 4) { 2491 state[j] = csj; 2492 continue; 2493 } 2494 // Scan second row 2495 if ((state[j] & (STATE_SIG_R2|STATE_VISITED_R2)) == 2496 STATE_SIG_R2) { 2497 k += dscanw; 2498 // Apply MR primitive 2499 symbuf[nsym] = (data[k]&mask)>>>bp; 2500 ctxtbuf[nsym++] = MR_LUT[(csj>>>STATE_SEP)&MR_MASK]; 2501 // Update the STATE_PREV_MR bit 2502 csj |= STATE_PREV_MR_R2; 2503 // Update distortion 2504 normval = (data[k] >> downshift) << upshift; 2505 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2506 } 2507 state[j] = csj; 2508 } 2509 } 2510 // Code all buffered symbols, if any 2511 if (nsym > 0) mq.codeSymbols(symbuf,ctxtbuf,nsym); 2512 } 2513 2514 // Reset the MQ context states if we need to 2515 if ((options & OPT_RESET_MQ) != 0) { 2516 mq.resetCtxts(); 2517 } 2518 2519 // Terminate the MQ bit stream if we need to 2520 if (doterm) { 2521 ratebuf[pidx] = mq.terminate(); // Termination has special length 2522 } 2523 else { // Use normal length calculation 2524 ratebuf[pidx] = mq.getNumCodedBytes(); 2525 } 2526 // Add length of previous segments, if any 2527 if (ltpidx >=0) { 2528 ratebuf[pidx] += ratebuf[ltpidx]; 2529 } 2530 // Finish length calculation if needed 2531 if (doterm) { 2532 mq.finishLengthCalculation(ratebuf,pidx); 2533 } 2534 2535 // Return the reduction in distortion 2536 return dist; 2537 } 2538 2539 /** 2540 * Performs the magnitude refinement pass on the specified data and 2541 * bit-plane, without using the arithmetic coder. It codes the samples 2542 * which are significant and which do not have the "visited" state bit 2543 * turned on, using the MR primitive. The "visited" state bit is not 2544 * mofified for any samples. 2545 * 2546 * <P>In this method, the arithmetic coder is bypassed, and raw bits are 2547 * directly written in the bit stream (useful when distribution are close 2548 * to uniform, for intance, at high bit-rates and at lossless 2549 * compression). The 'STATE_PREV_MR_R1' and 'STATE_PREV_MR_R2' bits are 2550 * not set because they are used only when the arithmetic coder is not 2551 * bypassed. 2552 * 2553 * @param srcblk The code-block data to code 2554 * 2555 * @param bout The bit based output 2556 * 2557 * @param doterm If true the bit based output is byte aligned after the 2558 * end of the pass. 2559 * 2560 * @param bp The bit-plane to code 2561 * 2562 * @param state The state information for the code-block 2563 * 2564 * @param fm The distortion estimation lookup table for MR 2565 * 2566 * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at 2567 * the end of this coding pass. 2568 * 2569 * @param pidx The coding pass index. Is the index in the 'ratebuf' array 2570 * where to store the coded length after this coding pass. 2571 * 2572 * @param ltpidx The index of the last pass that was terminated, or 2573 * negative if none. 2574 * 2575 * @param options The bitmask of entropy coding options to apply to the 2576 * code-block 2577 * 2578 * @return The decrease in distortion for this pass, in the fixed-point 2579 * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables. 2580 * */ 2581 static private int rawMagRefPass(CBlkWTData srcblk, BitToByteOutput bout, 2582 boolean doterm, int bp, int state[], 2583 int fm[], int ratebuf[], int pidx, 2584 int ltpidx, int options) { 2585 int j,sj; // The state index for line and stripe 2586 int k,sk; // The data index for line and stripe 2587 int dscanw; // The data scan-width 2588 int sscanw; // The state scan-width 2589 int jstep; // Stripe to stripe step for 'sj' 2590 int kstep; // Stripe to stripe step for 'sk' 2591 int stopsk; // The loop limit on the variable sk 2592 int csj; // Local copy (i.e. cached) of 'state[j]' 2593 int mask; // The mask for the current bit-plane 2594 int data[]; // The data buffer 2595 int dist; // The distortion reduction for this pass 2596 int shift; // Shift amount for distortion 2597 int upshift; // Shift left amount for distortion 2598 int downshift; // Shift right amount for distortion 2599 int normval; // The normalized sample magnitude value 2600 int s; // The stripe index 2601 int nstripes; // The number of stripes in the code-block 2602 int sheight; // Height of the current stripe 2603 int nsym = 0; 2604 2605 // Initialize local variables 2606 dscanw = srcblk.scanw; 2607 sscanw = srcblk.w+2; 2608 jstep = sscanw*STRIPE_HEIGHT/2-srcblk.w; 2609 kstep = dscanw*STRIPE_HEIGHT-srcblk.w; 2610 mask = 1<<bp; 2611 data = (int[]) srcblk.getData(); 2612 nstripes = (srcblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 2613 dist = 0; 2614 // We use the bit just coded plus MSE_LKP_BITS-1 bits below the bit 2615 // just coded for distortion estimation. 2616 shift = bp-(MSE_LKP_BITS-1); 2617 upshift = (shift>=0) ? 0 : -shift; 2618 downshift = (shift<=0) ? 0 : shift; 2619 2620 // Code stripe by stripe 2621 sk = srcblk.offset; 2622 sj = sscanw+1; 2623 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 2624 sheight = (s != 0) ? STRIPE_HEIGHT : 2625 srcblk.h-(nstripes-1)*STRIPE_HEIGHT; 2626 stopsk = sk+srcblk.w; 2627 // Scan by set of 1 stripe column at a time 2628 for (; sk < stopsk; sk++, sj++) { 2629 // Do half top of column 2630 j = sj; 2631 csj = state[j]; 2632 // If any of the two samples is significant and not yet 2633 // visited in the current bit-plane we can not skip them 2634 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 2635 k = sk; 2636 // Scan first row 2637 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 2638 STATE_SIG_R1) { 2639 // Code bit "raw" 2640 bout.writeBit((data[k]&mask)>>>bp); 2641 nsym++; 2642 // No need to set STATE_PREV_MR_R1 since all magnitude 2643 // refinement passes to follow are "raw" 2644 // Update distortion 2645 normval = (data[k] >> downshift) << upshift; 2646 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2647 } 2648 if (sheight < 2) continue; 2649 // Scan second row 2650 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 2651 STATE_SIG_R2) { 2652 k += dscanw; 2653 // Code bit "raw" 2654 bout.writeBit((data[k]&mask)>>>bp); 2655 nsym++; 2656 // No need to set STATE_PREV_MR_R2 since all magnitude 2657 // refinement passes to follow are "raw" 2658 // Update distortion 2659 normval = (data[k] >> downshift) << upshift; 2660 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2661 } 2662 } 2663 // Do half bottom of column 2664 if (sheight < 3) continue; 2665 j += sscanw; 2666 csj = state[j]; 2667 // If any of the two samples is significant and not yet 2668 // visited in the current bit-plane we can not skip them 2669 if ((((csj >>> 1) & (~csj)) & VSTD_MASK_R1R2) != 0) { 2670 k = sk+(dscanw<<1); 2671 // Scan first row 2672 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 2673 STATE_SIG_R1) { 2674 // Code bit "raw" 2675 bout.writeBit((data[k]&mask)>>>bp); 2676 nsym++; 2677 // No need to set STATE_PREV_MR_R1 since all magnitude 2678 // refinement passes to follow are "raw" 2679 // Update distortion 2680 normval = (data[k] >> downshift) << upshift; 2681 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2682 } 2683 if (sheight < 4) continue; 2684 // Scan second row 2685 if ((state[j] & (STATE_SIG_R2|STATE_VISITED_R2)) == 2686 STATE_SIG_R2) { 2687 k += dscanw; 2688 // Code bit "raw" 2689 bout.writeBit((data[k]&mask)>>>bp); 2690 nsym++; 2691 // No need to set STATE_PREV_MR_R2 since all magnitude 2692 // refinement passes to follow are "raw" 2693 // Update distortion 2694 normval = (data[k] >> downshift) << upshift; 2695 dist += fm[normval & ((1<<MSE_LKP_BITS)-1)]; 2696 } 2697 } 2698 } 2699 } 2700 2701 // Get length and terminate if needed 2702 if (doterm) { 2703 ratebuf[pidx] = bout.terminate(); 2704 } else { 2705 ratebuf[pidx] = bout.length(); 2706 } 2707 2708 // Add length of previous segments, if any 2709 if (ltpidx >=0) { 2710 ratebuf[pidx] += ratebuf[ltpidx]; 2711 } 2712 2713 // Return the reduction in distortion 2714 return dist; 2715 } 2716 2717 /** 2718 * Performs the cleanup pass on the specified data and bit-plane. It codes 2719 * all insignificant samples which have its "visited" state bit off, using 2720 * the ZC, SC, and RLC primitives. It toggles the "visited" state bit to 0 2721 * (off) for all samples in the code-block. 2722 * 2723 * @param srcblk The code-block data to code 2724 * 2725 * @param mq The MQ-coder to use 2726 * 2727 * @param doterm If true it performs an MQ-coder termination after the end 2728 * of the pass 2729 * 2730 * @param bp The bit-plane to code 2731 * 2732 * @param state The state information for the code-block 2733 * 2734 * @param fs The distortion estimation lookup table for SC 2735 * 2736 * @param zc_lut The ZC lookup table to use in ZC. 2737 * 2738 * @param symbuf The buffer to hold symbols to send to the MQ coder 2739 * 2740 * @param ctxtbuf A buffer to hold the contexts to use in sending the 2741 * buffered symbols to the MQ coder. 2742 * 2743 * @param ratebuf The buffer where to store the rate (i.e. coded lenth) at 2744 * the end of this coding pass. 2745 * 2746 * @param pidx The coding pass index. Is the index in the 'ratebuf' array 2747 * where to store the coded length after this coding pass. 2748 * 2749 * @param ltpidx The index of the last pass that was terminated, or 2750 * negative if none. 2751 * 2752 * @param options The bitmask of entropy coding options to apply to the 2753 * code-block 2754 * 2755 * @return The decrease in distortion for this pass, in the fixed-point 2756 * normalized representation of the 'FS_LOSSY' and 'FS_LOSSLESS' tables. 2757 * */ 2758 static private int cleanuppass(CBlkWTData srcblk, MQCoder mq, 2759 boolean doterm, int bp, int state[], 2760 int fs[], int zc_lut[], int symbuf[], 2761 int ctxtbuf[], int ratebuf[], int pidx, 2762 int ltpidx, int options) { 2763 // NOTE: The speedup mode of the MQ coder has been briefly tried to 2764 // speed up the coding of insignificants RLCs, without any success 2765 // (i.e. no speedup whatsoever). The use of the speedup mode should be 2766 // revisisted more in depth and the implementationn of it in MQCoder 2767 // should be reviewed for optimization opportunities. 2768 int j,sj; // The state index for line and stripe 2769 int k,sk; // The data index for line and stripe 2770 int nsym=0; // Symbol counter for symbol and context buffers 2771 int dscanw; // The data scan-width 2772 int sscanw; // The state scan-width 2773 int jstep; // Stripe to stripe step for 'sj' 2774 int kstep; // Stripe to stripe step for 'sk' 2775 int stopsk; // The loop limit on the variable sk 2776 int csj; // Local copy (i.e. cached) of 'state[j]' 2777 int mask; // The mask for the current bit-plane 2778 int sym; // The symbol to code 2779 int rlclen; // Length of RLC 2780 int ctxt; // The context to use 2781 int data[]; // The data buffer 2782 int dist; // The distortion reduction for this pass 2783 int shift; // Shift amount for distortion 2784 int upshift; // Shift left amount for distortion 2785 int downshift; // Shift right amount for distortion 2786 int normval; // The normalized sample magnitude value 2787 int s; // The stripe index 2788 boolean causal; // Flag to indicate if stripe-causal context 2789 // formation is to be used 2790 int nstripes; // The number of stripes in the code-block 2791 int sheight; // Height of the current stripe 2792 int off_ul,off_ur,off_dr,off_dl; // offsets 2793 2794 // Initialize local variables 2795 dscanw = srcblk.scanw; 2796 sscanw = srcblk.w+2; 2797 jstep = sscanw*STRIPE_HEIGHT/2-srcblk.w; 2798 kstep = dscanw*STRIPE_HEIGHT-srcblk.w; 2799 mask = 1<<bp; 2800 data = (int[]) srcblk.getData(); 2801 nstripes = (srcblk.h+STRIPE_HEIGHT-1)/STRIPE_HEIGHT; 2802 dist = 0; 2803 // We use the MSE_LKP_BITS-1 bits below the bit just coded for 2804 // distortion estimation. 2805 shift = bp-(MSE_LKP_BITS-1); 2806 upshift = (shift>=0) ? 0 : -shift; 2807 downshift = (shift<=0) ? 0 : shift; 2808 causal = (options & OPT_VERT_STR_CAUSAL) != 0; 2809 2810 // Pre-calculate offsets in 'state' for diagonal neighbors 2811 off_ul = -sscanw-1; // up-left 2812 off_ur = -sscanw+1; // up-right 2813 off_dr = sscanw+1; // down-right 2814 off_dl = sscanw-1; // down-left 2815 2816 // Code stripe by stripe 2817 sk = srcblk.offset; 2818 sj = sscanw+1; 2819 for (s = nstripes-1; s >= 0; s--, sk+=kstep, sj+=jstep) { 2820 sheight = (s != 0) ? STRIPE_HEIGHT : 2821 srcblk.h-(nstripes-1)*STRIPE_HEIGHT; 2822 stopsk = sk+srcblk.w; 2823 // Scan by set of 1 stripe column at a time 2824 for (nsym = 0; sk < stopsk; sk++, sj++) { 2825 // Start column 2826 j = sj; 2827 csj = state[j]; 2828 top_half: 2829 { 2830 // Check for RLC: if all samples are not significant, not 2831 // visited and do not have a non-zero context, and column is 2832 // full height, we do RLC. 2833 if (csj == 0 && state[j+sscanw] == 0 && 2834 sheight == STRIPE_HEIGHT) { 2835 k = sk; 2836 if ((data[k]&mask) != 0) { 2837 rlclen = 0; 2838 } 2839 else if ((data[k+=dscanw]&mask) != 0) { 2840 rlclen = 1; 2841 } 2842 else if ((data[k+=dscanw]&mask) != 0) { 2843 rlclen = 2; 2844 j += sscanw; 2845 csj = state[j]; 2846 } 2847 else if ((data[k+=dscanw]&mask) != 0) { 2848 rlclen = 3; 2849 j += sscanw; 2850 csj = state[j]; 2851 } 2852 else { 2853 // Code insignificant RLC 2854 symbuf[nsym] = 0; 2855 ctxtbuf[nsym++] = RLC_CTXT; 2856 // Goto next column 2857 continue; 2858 } 2859 // Code significant RLC 2860 symbuf[nsym] = 1; 2861 ctxtbuf[nsym++] = RLC_CTXT; 2862 // Send MSB bit index 2863 symbuf[nsym] = rlclen>>1; 2864 ctxtbuf[nsym++] = UNIF_CTXT; 2865 // Send LSB bit index 2866 symbuf[nsym] = rlclen&0x01; 2867 ctxtbuf[nsym++] = UNIF_CTXT; 2868 // Code sign of sample that became significant 2869 // Update distortion 2870 normval = (data[k] >> downshift) << upshift; 2871 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 2872 // Apply sign coding 2873 sym = data[k]>>>31; 2874 if ((rlclen&0x01) == 0) { 2875 // Sample that became significant is first row of 2876 // its column half 2877 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 2878 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 2879 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 2880 // Update state information (significant bit, 2881 // visited bit, neighbor significant bit of 2882 // neighbors, non zero context of neighbors, sign 2883 // of neighbors) 2884 if (rlclen != 0 || !causal) { 2885 // If in causal mode do not change contexts of 2886 // previous stripe. 2887 state[j+off_ul] |= 2888 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 2889 state[j+off_ur] |= 2890 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 2891 } 2892 // Update sign state information of neighbors 2893 if (sym != 0) { 2894 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2895 STATE_NZ_CTXT_R2| 2896 STATE_V_U_R2|STATE_V_U_SIGN_R2; 2897 if (rlclen != 0 || !causal) { 2898 // If in causal mode do not change 2899 // contexts of previous stripe. 2900 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2901 STATE_V_D_R2|STATE_V_D_SIGN_R2; 2902 } 2903 state[j+1] |= 2904 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2905 STATE_H_L_R1|STATE_H_L_SIGN_R1| 2906 STATE_D_UL_R2; 2907 state[j-1] |= 2908 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2909 STATE_H_R_R1|STATE_H_R_SIGN_R1| 2910 STATE_D_UR_R2; 2911 } 2912 else { 2913 csj |= STATE_SIG_R1|STATE_VISITED_R1| 2914 STATE_NZ_CTXT_R2|STATE_V_U_R2; 2915 if (rlclen != 0 || !causal) { 2916 // If in causal mode do not change 2917 // contexts of previous stripe. 2918 state[j-sscanw] |= STATE_NZ_CTXT_R2| 2919 STATE_V_D_R2; 2920 } 2921 state[j+1] |= 2922 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2923 STATE_H_L_R1|STATE_D_UL_R2; 2924 state[j-1] |= 2925 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2926 STATE_H_R_R1|STATE_D_UR_R2; 2927 } 2928 // Changes to csj are saved later 2929 if ((rlclen>>1) != 0) { 2930 // Sample that became significant is in bottom 2931 // half of column => jump to bottom half 2932 break top_half; 2933 } 2934 // Otherwise sample that became significant is in 2935 // top half of column => continue on top half 2936 } 2937 else { 2938 // Sample that became significant is second row of 2939 // its column half 2940 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 2941 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 2942 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 2943 // Update state information (significant bit, 2944 // neighbor significant bit of neighbors, 2945 // non zero context of neighbors, sign of neighbors) 2946 state[j+off_dl] |= STATE_NZ_CTXT_R1|STATE_D_UR_R1; 2947 state[j+off_dr] |= STATE_NZ_CTXT_R1|STATE_D_UL_R1; 2948 // Update sign state information of neighbors 2949 if (sym != 0) { 2950 csj |= STATE_SIG_R2|STATE_NZ_CTXT_R1| 2951 STATE_V_D_R1|STATE_V_D_SIGN_R1; 2952 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2953 STATE_V_U_R1|STATE_V_U_SIGN_R1; 2954 state[j+1] |= 2955 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2956 STATE_D_DL_R1| 2957 STATE_H_L_R2|STATE_H_L_SIGN_R2; 2958 state[j-1] |= 2959 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2960 STATE_D_DR_R1| 2961 STATE_H_R_R2|STATE_H_R_SIGN_R2; 2962 } 2963 else { 2964 csj |= STATE_SIG_R2|STATE_NZ_CTXT_R1| 2965 STATE_V_D_R1; 2966 state[j+sscanw] |= STATE_NZ_CTXT_R1| 2967 STATE_V_U_R1; 2968 state[j+1] |= 2969 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2970 STATE_D_DL_R1|STATE_H_L_R2; 2971 state[j-1] |= 2972 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 2973 STATE_D_DR_R1|STATE_H_R_R2; 2974 } 2975 // Save changes to csj 2976 state[j] = csj; 2977 if ((rlclen>>1) != 0) { 2978 // Sample that became significant is in bottom 2979 // half of column => we're done with this 2980 // column 2981 continue; 2982 } 2983 // Otherwise sample that became significant is in 2984 // top half of column => we're done with top 2985 // column 2986 j += sscanw; 2987 csj = state[j]; 2988 break top_half; 2989 } 2990 } 2991 // Do half top of column 2992 // If any of the two samples is not significant and has 2993 // not been visited in the current bit-plane we can not 2994 // skip them 2995 if ((((csj>>1)|csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) { 2996 k = sk; 2997 // Scan first row 2998 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 0) { 2999 // Apply zero coding 3000 ctxtbuf[nsym] = zc_lut[csj&ZC_MASK]; 3001 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 3002 // Became significant 3003 // Apply sign coding 3004 sym = data[k]>>>31; 3005 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 3006 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 3007 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 3008 // Update state information (significant bit, 3009 // visited bit, neighbor significant bit of 3010 // neighbors, non zero context of neighbors, 3011 // sign of neighbors) 3012 if (!causal) { 3013 // If in causal mode do not change 3014 // contexts of previous stripe. 3015 state[j+off_ul] |= 3016 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 3017 state[j+off_ur] |= 3018 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 3019 } 3020 // Update sign state information of neighbors 3021 if (sym != 0) { 3022 csj |= STATE_SIG_R1|STATE_VISITED_R1| 3023 STATE_NZ_CTXT_R2| 3024 STATE_V_U_R2|STATE_V_U_SIGN_R2; 3025 if (!causal) { 3026 // If in causal mode do not change 3027 // contexts of previous stripe. 3028 state[j-sscanw] |= STATE_NZ_CTXT_R2| 3029 STATE_V_D_R2|STATE_V_D_SIGN_R2; 3030 } 3031 state[j+1] |= 3032 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3033 STATE_H_L_R1|STATE_H_L_SIGN_R1| 3034 STATE_D_UL_R2; 3035 state[j-1] |= 3036 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3037 STATE_H_R_R1|STATE_H_R_SIGN_R1| 3038 STATE_D_UR_R2; 3039 } 3040 else { 3041 csj |= STATE_SIG_R1|STATE_VISITED_R1| 3042 STATE_NZ_CTXT_R2|STATE_V_U_R2; 3043 if (!causal) { 3044 // If in causal mode do not change 3045 // contexts of previous stripe. 3046 state[j-sscanw] |= STATE_NZ_CTXT_R2| 3047 STATE_V_D_R2; 3048 } 3049 state[j+1] |= 3050 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3051 STATE_H_L_R1|STATE_D_UL_R2; 3052 state[j-1] |= 3053 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3054 STATE_H_R_R1|STATE_D_UR_R2; 3055 } 3056 // Update distortion 3057 normval = (data[k] >> downshift) << upshift; 3058 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 3059 } 3060 } 3061 if (sheight < 2) { 3062 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 3063 state[j] = csj; 3064 continue; 3065 } 3066 // Scan second row 3067 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 0) { 3068 k += dscanw; 3069 // Apply zero coding 3070 ctxtbuf[nsym] = zc_lut[(csj>>>STATE_SEP)&ZC_MASK]; 3071 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 3072 // Became significant 3073 // Apply sign coding 3074 sym = data[k]>>>31; 3075 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 3076 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 3077 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 3078 // Update state information (significant bit, 3079 // visited bit, neighbor significant bit of 3080 // neighbors, non zero context of neighbors, 3081 // sign of neighbors) 3082 state[j+off_dl] |= 3083 STATE_NZ_CTXT_R1|STATE_D_UR_R1; 3084 state[j+off_dr] |= 3085 STATE_NZ_CTXT_R1|STATE_D_UL_R1; 3086 // Update sign state information of neighbors 3087 if (sym != 0) { 3088 csj |= STATE_SIG_R2|STATE_VISITED_R2| 3089 STATE_NZ_CTXT_R1| 3090 STATE_V_D_R1|STATE_V_D_SIGN_R1; 3091 state[j+sscanw] |= STATE_NZ_CTXT_R1| 3092 STATE_V_U_R1|STATE_V_U_SIGN_R1; 3093 state[j+1] |= 3094 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3095 STATE_D_DL_R1| 3096 STATE_H_L_R2|STATE_H_L_SIGN_R2; 3097 state[j-1] |= 3098 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3099 STATE_D_DR_R1| 3100 STATE_H_R_R2|STATE_H_R_SIGN_R2; 3101 } 3102 else { 3103 csj |= STATE_SIG_R2|STATE_VISITED_R2| 3104 STATE_NZ_CTXT_R1|STATE_V_D_R1; 3105 state[j+sscanw] |= STATE_NZ_CTXT_R1| 3106 STATE_V_U_R1; 3107 state[j+1] |= 3108 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3109 STATE_D_DL_R1|STATE_H_L_R2; 3110 state[j-1] |= 3111 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3112 STATE_D_DR_R1|STATE_H_R_R2; 3113 } 3114 // Update distortion 3115 normval = (data[k] >> downshift) << upshift; 3116 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 3117 } 3118 } 3119 } 3120 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 3121 state[j] = csj; 3122 // Do half bottom of column 3123 if (sheight < 3) continue; 3124 j += sscanw; 3125 csj = state[j]; 3126 } // end of 'top_half' block 3127 // If any of the two samples is not significant and has 3128 // not been visited in the current bit-plane we can not 3129 // skip them 3130 if ((((csj>>1)|csj) & VSTD_MASK_R1R2) != VSTD_MASK_R1R2) { 3131 k = sk+(dscanw<<1); 3132 // Scan first row 3133 if ((csj & (STATE_SIG_R1|STATE_VISITED_R1)) == 0) { 3134 // Apply zero coding 3135 ctxtbuf[nsym] = zc_lut[csj&ZC_MASK]; 3136 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 3137 // Became significant 3138 // Apply sign coding 3139 sym = data[k]>>>31; 3140 ctxt = SC_LUT[(csj>>>SC_SHIFT_R1)&SC_MASK]; 3141 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 3142 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 3143 // Update state information (significant bit, 3144 // visited bit, neighbor significant bit of 3145 // neighbors, non zero context of neighbors, 3146 // sign of neighbors) 3147 state[j+off_ul] |= 3148 STATE_NZ_CTXT_R2|STATE_D_DR_R2; 3149 state[j+off_ur] |= 3150 STATE_NZ_CTXT_R2|STATE_D_DL_R2; 3151 // Update sign state information of neighbors 3152 if (sym != 0) { 3153 csj |= STATE_SIG_R1|STATE_VISITED_R1| 3154 STATE_NZ_CTXT_R2| 3155 STATE_V_U_R2|STATE_V_U_SIGN_R2; 3156 state[j-sscanw] |= STATE_NZ_CTXT_R2| 3157 STATE_V_D_R2|STATE_V_D_SIGN_R2; 3158 state[j+1] |= 3159 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3160 STATE_H_L_R1|STATE_H_L_SIGN_R1| 3161 STATE_D_UL_R2; 3162 state[j-1] |= 3163 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3164 STATE_H_R_R1|STATE_H_R_SIGN_R1| 3165 STATE_D_UR_R2; 3166 } 3167 else { 3168 csj |= STATE_SIG_R1|STATE_VISITED_R1| 3169 STATE_NZ_CTXT_R2|STATE_V_U_R2; 3170 state[j-sscanw] |= STATE_NZ_CTXT_R2| 3171 STATE_V_D_R2; 3172 state[j+1] |= 3173 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3174 STATE_H_L_R1|STATE_D_UL_R2; 3175 state[j-1] |= 3176 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3177 STATE_H_R_R1|STATE_D_UR_R2; 3178 } 3179 // Update distortion 3180 normval = (data[k] >> downshift) << upshift; 3181 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 3182 } 3183 } 3184 if (sheight < 4) { 3185 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 3186 state[j] = csj; 3187 continue; 3188 } 3189 // Scan second row 3190 if ((csj & (STATE_SIG_R2|STATE_VISITED_R2)) == 0) { 3191 k += dscanw; 3192 // Apply zero coding 3193 ctxtbuf[nsym] = zc_lut[(csj>>>STATE_SEP)&ZC_MASK]; 3194 if ((symbuf[nsym++] = (data[k]&mask)>>>bp) != 0) { 3195 // Became significant 3196 // Apply sign coding 3197 sym = data[k]>>>31; 3198 ctxt = SC_LUT[(csj>>>SC_SHIFT_R2)&SC_MASK]; 3199 symbuf[nsym] = sym ^ (ctxt>>>SC_SPRED_SHIFT); 3200 ctxtbuf[nsym++] = ctxt & SC_LUT_MASK; 3201 // Update state information (significant bit, 3202 // visited bit, neighbor significant bit of 3203 // neighbors, non zero context of neighbors, 3204 // sign of neighbors) 3205 state[j+off_dl] |= 3206 STATE_NZ_CTXT_R1|STATE_D_UR_R1; 3207 state[j+off_dr] |= 3208 STATE_NZ_CTXT_R1|STATE_D_UL_R1; 3209 // Update sign state information of neighbors 3210 if (sym != 0) { 3211 csj |= STATE_SIG_R2|STATE_VISITED_R2| 3212 STATE_NZ_CTXT_R1| 3213 STATE_V_D_R1|STATE_V_D_SIGN_R1; 3214 state[j+sscanw] |= STATE_NZ_CTXT_R1| 3215 STATE_V_U_R1|STATE_V_U_SIGN_R1; 3216 state[j+1] |= 3217 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3218 STATE_D_DL_R1| 3219 STATE_H_L_R2|STATE_H_L_SIGN_R2; 3220 state[j-1] |= 3221 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3222 STATE_D_DR_R1| 3223 STATE_H_R_R2|STATE_H_R_SIGN_R2; 3224 } 3225 else { 3226 csj |= STATE_SIG_R2|STATE_VISITED_R2| 3227 STATE_NZ_CTXT_R1|STATE_V_D_R1; 3228 state[j+sscanw] |= STATE_NZ_CTXT_R1| 3229 STATE_V_U_R1; 3230 state[j+1] |= 3231 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3232 STATE_D_DL_R1|STATE_H_L_R2; 3233 state[j-1] |= 3234 STATE_NZ_CTXT_R1|STATE_NZ_CTXT_R2| 3235 STATE_D_DR_R1|STATE_H_R_R2; 3236 } 3237 // Update distortion 3238 normval = (data[k] >> downshift) << upshift; 3239 dist += fs[normval & ((1<<(MSE_LKP_BITS-1))-1)]; 3240 } 3241 } 3242 } 3243 csj &= ~(STATE_VISITED_R1|STATE_VISITED_R2); 3244 state[j] = csj; 3245 } 3246 // Code all buffered symbols, if any 3247 if (nsym > 0) mq.codeSymbols(symbuf,ctxtbuf,nsym); 3248 } 3249 3250 // Insert a segment marker if we need to 3251 if ((options & OPT_SEG_SYMBOLS) != 0) { 3252 mq.codeSymbols(SEG_SYMBOLS,SEG_SYMB_CTXTS,SEG_SYMBOLS.length); 3253 } 3254 3255 // Reset the MQ context states if we need to 3256 if ((options & OPT_RESET_MQ) != 0) { 3257 mq.resetCtxts(); 3258 } 3259 3260 // Terminate the MQ bit stream if we need to 3261 if (doterm) { 3262 ratebuf[pidx] = mq.terminate(); // Termination has special length 3263 } 3264 else { // Use normal length calculation 3265 ratebuf[pidx] = mq.getNumCodedBytes(); 3266 } 3267 // Add length of previous segments, if any 3268 if (ltpidx >=0) { 3269 ratebuf[pidx] += ratebuf[ltpidx]; 3270 } 3271 // Finish length calculation if needed 3272 if (doterm) { 3273 mq.finishLengthCalculation(ratebuf,pidx); 3274 } 3275 3276 // Return the reduction in distortion 3277 return dist; 3278 } 3279 3280 /** 3281 * Ensures that at the end of a non-terminated coding pass there is not a 3282 * 0xFF byte, modifying the stored rates if necessary. 3283 * 3284 * <P>Due to error resiliance reasons, a coding pass should never have its 3285 * last byte be a 0xFF, since that can lead to the emulation of a resync 3286 * marker. This method checks if that is the case, and reduces the rate 3287 * for a given pass if necessary. The ommitted 0xFF will be synthetized by 3288 * the decoder if necessary, as required by JPEG 2000. This method should 3289 * only be called once that the entire code-block is coded. 3290 * 3291 * <P>Passes that are terminated are not checked for the 0xFF byte, since 3292 * it is assumed that the termination procedure does not output any 3293 * trailing 0xFF. Checking the terminated segments would involve much more 3294 * than just modifying the stored rates. 3295 * 3296 * <P>NOTE: It is assumed by this method that the coded data does not 3297 * contain consecutive 0xFF bytes, as is the case with the MQ and 3298 * 'arithemetic coding bypass' bit stuffing policy. However, the 3299 * termination policies used should also respect this requirement. 3300 * 3301 * @param data The coded data for the code-block 3302 * 3303 * @param rates The rate (i.e. accumulated number of bytes) for each 3304 * coding pass 3305 * 3306 * @param isterm An array of flags indicating, for each pass, if it is 3307 * terminated or not. If null it is assumed that no pass is terminated, 3308 * except the last one. 3309 * 3310 * @param n The number of coding passes 3311 * */ 3312 static private void checkEndOfPassFF(byte data[], int rates[], 3313 boolean isterm[], int n) { 3314 int dp; // the position to test in 'data' 3315 3316 // If a pass ends in 0xFF we need to reduce the number of bytes in it, 3317 // so that it does not end in 0xFF. We only need to go back one byte 3318 // since there can be no consecutive 0xFF bytes. 3319 3320 // If there are no terminated passes avoid the test on 'isterm' 3321 if (isterm == null) { 3322 for (n--; n>=0; n--) { 3323 dp = rates[n]-1; 3324 if (dp >= 0 && (data[dp] == (byte)0xFF)) { 3325 rates[n]--; 3326 } 3327 } 3328 } 3329 else { 3330 for (n--; n>=0; n--) { 3331 if (!isterm[n]) { 3332 dp = rates[n]-1; 3333 if (dp >= 0 && (data[dp] == (byte)0xFF)) { 3334 rates[n]--; 3335 } 3336 } 3337 } 3338 } 3339 } 3340 3341 /** 3342 * Load options, length calculation type and termination type for 3343 * each tile-component. 3344 * 3345 * @param nt The number of tiles 3346 * 3347 * @param nc The number of components 3348 * */ 3349 public void initTileComp(int nt, int nc) { 3350 3351 opts = new int[nt][nc]; 3352 lenCalc = new int[nt][nc]; 3353 tType = new int[nt][nc]; 3354 3355 for(int t=0; t<nt; t++){ 3356 for(int c=0; c<nc; c++){ 3357 opts[t][c] = 0; 3358 3359 // Bypass coding mode ? 3360 if( ((String)bms.getTileCompVal(t,c)). 3361 equalsIgnoreCase("true")) { 3362 opts[t][c] |= OPT_BYPASS; 3363 } 3364 // MQ reset after each coding pass ? 3365 if( ((String)mqrs.getTileCompVal(t,c)). 3366 equalsIgnoreCase("true")) { 3367 opts[t][c] |= OPT_RESET_MQ; 3368 } 3369 // MQ termination after each arithmetically coded coding pass ? 3370 if( ((String)rts.getTileCompVal(t,c)). 3371 equalsIgnoreCase("true") ) { 3372 opts[t][c] |= OPT_TERM_PASS; 3373 } 3374 // Vertically stripe-causal context mode ? 3375 if( ((String)css.getTileCompVal(t,c)). 3376 equalsIgnoreCase("true") ) { 3377 opts[t][c] |= OPT_VERT_STR_CAUSAL; 3378 } 3379 // Error resilience segmentation symbol insertion ? 3380 if( ((String)sss.getTileCompVal(t,c)). 3381 equalsIgnoreCase("true")) { 3382 opts[t][c] |= OPT_SEG_SYMBOLS; 3383 } 3384 3385 // Set length calculation type of the MQ coder 3386 String lCalcType = (String)lcs.getTileCompVal(t,c); 3387 if(lCalcType.equals("near_opt")){ 3388 lenCalc[t][c] = MQCoder.LENGTH_NEAR_OPT; 3389 } 3390 else if(lCalcType.equals("lazy_good")){ 3391 lenCalc[t][c] = MQCoder.LENGTH_LAZY_GOOD; 3392 } 3393 else if(lCalcType.equals("lazy")){ 3394 lenCalc[t][c] = MQCoder.LENGTH_LAZY; 3395 } 3396 else { 3397 throw new IllegalArgumentException("Unrecognized or "+ 3398 "unsupported MQ "+ 3399 "length calculation."); 3400 } 3401 3402 // Set termination type of MQ coder 3403 String termType = (String)tts.getTileCompVal(t,c); 3404 if(termType.equalsIgnoreCase("easy")){ 3405 tType[t][c] = MQCoder.TERM_EASY; 3406 } 3407 else if(termType.equalsIgnoreCase("full")){ 3408 tType[t][c] = MQCoder.TERM_FULL; 3409 } 3410 else if(termType.equalsIgnoreCase("near_opt")){ 3411 tType[t][c] = MQCoder.TERM_NEAR_OPT; 3412 } 3413 else if (termType.equalsIgnoreCase("predict")) { 3414 tType[t][c] = MQCoder.TERM_PRED_ER; 3415 opts[t][c] |= OPT_PRED_TERM; 3416 if ((opts[t][c] & (OPT_TERM_PASS|OPT_BYPASS)) == 0) { 3417 FacilityManager.getMsgLogger(). 3418 printmsg(MsgLogger.INFO,"Using error resilient MQ"+ 3419 " termination, but terminating only at "+ 3420 "the end of code-blocks. The error "+ 3421 "protection offered by this option will"+ 3422 " be very weak. Specify the 'Creg_term' "+ 3423 "and/or 'Cbypass' option for "+ 3424 "increased error resilience."); 3425 } 3426 } 3427 else{ 3428 throw new IllegalArgumentException("Unrecognized or "+ 3429 "unsupported "+ 3430 "MQ coder termination."); 3431 } 3432 3433 } // End loop on components 3434 } // End loop on tiles 3435 } 3436 3437 /** 3438 * Returns the precinct partition width for the specified 3439 * component, tile and resolution level. 3440 * 3441 * @param t the tile index 3442 * 3443 * @param c the component 3444 * 3445 * @param rl the resolution level 3446 * 3447 * @return The precinct partition width for the specified 3448 * component, tile and resolution level 3449 * */ 3450 public int getPPX(int t, int c, int rl) { 3451 return pss.getPPX(t, c, rl); 3452 } 3453 3454 /** 3455 * Returns the precinct partition height for the specified 3456 * component, tile and resolution level. 3457 * 3458 * @param t the tile index 3459 * 3460 * @param c the component 3461 * 3462 * @param rl the resolution level 3463 * 3464 * @return The precinct partition height for the specified 3465 * component, tile and resolution level 3466 * */ 3467 public int getPPY(int t, int c, int rl) { 3468 return pss.getPPY(t, c, rl); 3469 } 3470 3471 /** 3472 * Returns true if precinct partition is used for the specified 3473 * component and tile, returns false otherwise. 3474 * 3475 * @param c The component 3476 * 3477 * @param t The tile 3478 * 3479 * @return True if precinct partition is used for the specified 3480 * component and tile, returns false otherwise. 3481 * */ 3482 public boolean precinctPartitionUsed(int c, int t) { 3483 return precinctPartition[c][t]; 3484 } 3485}