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