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