001/*
002 * $RCSfile: EntropyCoder.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:08 $
005 * $State: Exp $
006 *
007 * Class:                   EntropyCoder
008 *
009 * Description:             The abstract class for entropy encoders
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;
045
046import jj2000.j2k.quantization.quantizer.*;
047import jj2000.j2k.codestream.writer.*;
048import jj2000.j2k.wavelet.analysis.*;
049import jj2000.j2k.wavelet.*;
050import jj2000.j2k.entropy.*;
051import jj2000.j2k.image.*;
052import jj2000.j2k.util.*;
053import jj2000.j2k.roi.*;
054import jj2000.j2k.*;
055
056import java.util.*;
057import java.io.*;
058
059import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriteParamJava;
060/**
061 * This abstract class provides the general interface for block-based entropy
062 * encoders. The input to the entropy coder is the quantized wavelet
063 * coefficients, or codewords, represented in sign magnitude. The output is a
064 * compressed code-block with rate-distortion information.
065 *
066 * <P>The source of data for objects of this class are 'CBlkQuantDataSrcEnc'
067 * objects.
068 *
069 * <P>For more details on the sign magnitude representation used see the
070 * Quantizer class.
071 *
072 * <P>This class provides default implemenations for most of the methods
073 * (wherever it makes sense), under the assumption that the image and
074 * component dimensions, and the tiles, are not modifed by the entropy
075 * coder. If that is not the case for a particular implementation then the
076 * methods should be overriden.
077 *
078 * @see Quantizer
079 * @see CBlkQuantDataSrcEnc
080 * */
081public abstract class EntropyCoder extends ImgDataAdapter
082    implements CodedCBlkDataSrcEnc, StdEntropyCoderOptions {
083
084    /** The prefix for entropy coder options: 'C' */
085    public final static char OPT_PREFIX = 'C';
086
087    /** The list of parameters that is accepted for entropy coding. Options
088     * for entropy coding start with 'C'. */
089    private final static String [][] pinfo = {
090        {"Cblksiz", "[<tile-component idx>] <width> <height> "+
091         "[[<tile-component idx>] <width> <height>]",
092         "Specifies the maximum code-block size to use for tile-component. "+
093         "The maximum width and height is 1024, however the surface area "+
094         "(i.e. width x height) must not exceed 4096. The minimum width and "+
095         "height is 4.","64 64"},
096        {"Cbypass", "[<tile-component idx>] true|false"+
097         "[ [<tile-component idx>] true|false ...]",
098         "Uses the lazy coding mode with the entropy coder. This will bypass "+
099         "the MQ coder for some of the coding passes, where the distribution "+
100         "is often close to uniform. Since the MQ codeword will be "+
101         "terminated "+
102         "at least once per lazy pass, it is important to use an efficient "+
103         "termination algorithm, see the 'Cterm' option."+
104         "'true' enables, 'false' disables it.","false"},
105        {"CresetMQ", "[<tile-component idx>] true|false"+
106         "[ [<tile-component idx>] true|false ...]",
107         "If this is enabled the probability estimates of the MQ coder are "+
108         "reset after each arithmetically coded (i.e. non-lazy) coding pass. "+
109         "'true' enables, 'false' disables it.","false"},
110        {"Creg_term", "[<tile-component idx>] true|false"+
111         "[ [<tile-component idx>] true|false ...]",
112         "If this is enabled the codeword (raw or MQ) is terminated on a "+
113         "byte boundary after each coding pass. In this case it is important "+
114         "to use an efficient termination algorithm, see the 'Cterm' option. "+
115         "'true' enables, 'false' disables it.","false"},
116        {"Ccausal","[<tile-component idx>] true|false"+
117         "[ [<tile-component idx>] true|false ...]",
118         "Uses vertically stripe causal context formation. If this is "+
119         "enabled "+
120         "the context formation process in one stripe is independant of the "+
121         "next stripe (i.e. the one below it). 'true' "+
122         "enables, 'false' disables it.","false"},
123        {"Cseg_symbol","[<tile-component idx>] true|false"+
124         "[ [<tile-component idx>] true|false ...]",
125         "Inserts an error resilience segmentation symbol in the MQ "+
126         "codeword at the end of "+
127         "each bit-plane (cleanup pass). Decoders can use this "+
128         "information to detect and "+
129         "conceal errors.'true' enables, 'false' disables "+
130         "it.","false"},
131        {"Cterm", "[<tile-component idx>] near_opt|easy|predict|full"+
132         "[ [<tile-component idx>] near_opt|easy|predict|full ...]",
133         "Specifies the algorithm used to terminate the MQ codeword. "+
134         "The most efficient one is 'near_opt', which delivers a codeword "+
135         "which in almost all cases is the shortest possible. The 'easy' is "+
136         "a simpler algorithm that delivers a codeword length that is close "+
137         "to the previous one (in average 1 bit longer). The 'predict' is"+
138         " almost "+
139         "the same as the 'easy' but it leaves error resilient information "+
140         "on "+
141         "the spare least significant bits (in average 3.5 bits), which can "+
142         "be used by a decoder to detect errors. The 'full' algorithm "+
143         "performs a full flush of the MQ coder and is highly inefficient.\n"+
144         "It is important to use a good termination policy since the MQ "+
145         "codeword can be terminated quite often, specially if the 'Cbypass'"+
146         " or "+
147         "'Creg_term' options are enabled (in the normal case it would be "+
148         "terminated once per code-block, while if 'Creg_term' is specified "+
149         "it will be done almost 3 times per bit-plane in each code-block).",
150         "near_opt"},
151        {"Clen_calc","[<tile-component idx>] near_opt|lazy_good|lazy"+
152         "[ [<tile-component idx>] ...]",
153         "Specifies the algorithm to use in calculating the necessary MQ "+
154         "length for each decoding pass. The best one is 'near_opt', which "+
155         "performs a rather sophisticated calculation and provides the best "+
156         "results. The 'lazy_good' and 'lazy' are very simple algorithms "+
157         "that "+
158         "provide rather conservative results, 'lazy_good' one being "+
159         "slightly "+
160         "better. Do not change this option unless you want to experiment "+
161         "the effect of different length calculation algorithms.","near_opt"},
162        {"Cpp","[<tile-component idx>] <dim> <dim> [<dim> <dim>] " +
163         "[ [<tile-component idx>] ...]",
164         "Specifies precinct partition dimensions for tile-component. The "+
165         "first "+
166         "two values apply to the highest resolution and the following ones "+
167         "(if "+
168         "any) apply to the remaining resolutions in decreasing order. If "+
169         "less "+
170         "values than the number of decomposition levels are specified, "+
171         "then the "+
172         "last two values are used for the remaining resolutions.", null},
173    };
174
175    /** The source of quantized wavelet coefficients */
176    protected CBlkQuantDataSrcEnc src;
177
178    /**
179     * Initializes the source of quantized wavelet coefficients.
180     *
181     * @param src The source of quantized wavelet coefficients.
182     * */
183    public EntropyCoder(CBlkQuantDataSrcEnc src) {
184        super(src);
185        this.src = src;
186    }
187
188    /**
189     * Returns the code-block width for the specified tile and component.
190     *
191     * @param t The tile index
192     *
193     * @param c the component index
194     *
195     * @return The code-block width for the specified tile and component
196     * */
197    public abstract int getCBlkWidth(int t, int c);
198
199    /**
200     * Returns the code-block height for the specified tile and component.
201     *
202     * @param t The tile index
203     *
204     * @param c the component index
205     *
206     * @return The code-block height for the specified tile and component
207     * */
208    public abstract int getCBlkHeight(int t, int c);
209
210    /**
211     * Returns the reversibility of the tile-component data that is provided
212     * by the object.  Data is reversible when it is suitable for lossless and
213     * lossy-to-lossless compression.
214     *
215     * <P>Since entropy coders themselves are always reversible, it returns
216     * the reversibility of the data that comes from the 'CBlkQuantDataSrcEnc'
217     * source object (i.e. ROIScaler).
218     *
219     * @param t Tile index
220     *
221     * @param c Component index
222     *
223     * @return true is the data is reversible, false if not.
224     *
225     * @see jj2000.j2k.roi.encoder.ROIScaler
226     * */
227    public boolean isReversible(int t,int c) {
228        return src.isReversible(t,c);
229    }
230
231    /**
232     * Returns a reference to the root of subband tree structure representing
233     * the subband decomposition for the specified tile-component.
234     *
235     * @param t The index of the tile.
236     *
237     * @param c The index of the component.
238     *
239     * @return The root of the subband tree structure, see Subband.
240     *
241     * @see SubbandAn
242     *
243     * @see Subband
244     * */
245    public SubbandAn getAnSubbandTree(int t,int c) {
246        return src.getAnSubbandTree(t,c);
247    }
248
249    /**
250     * Returns the horizontal offset of the code-block partition. Allowable
251     * values are 0 and 1, nothing else.
252     * */
253    public int getCbULX() {
254        return src.getCbULX();
255    }
256
257    /**
258     * Returns the vertical offset of the code-block partition. Allowable
259     * values are 0 and 1, nothing else.
260     * */
261    public int getCbULY() {
262        return src.getCbULY();
263    }
264
265    /**
266     * Returns the parameters that are used in this class and
267     * implementing classes. It returns a 2D String array. Each of the
268     * 1D arrays is for a different option, and they have 3
269     * elements. The first element is the option name, the second one
270     * is the synopsis, the third one is a long description of what
271     * the parameter is and the fourth is its default value. The
272     * synopsis or description may be 'null', in which case it is
273     * assumed that there is no synopsis or description of the option,
274     * respectively. Null may be returned if no options are supported.
275     *
276     * @return the options name, their synopsis and their explanation,
277     * or null if no options are supported.
278     * */
279    public static String[][] getParameterInfo() {
280        return pinfo;
281    }
282
283    /**
284     * Creates a EntropyCoder object for the appropriate entropy coding
285     * parameters in the parameter list 'pl', and having 'src' as the source
286     * of quantized data.
287     *
288     * @param src The source of data to be entropy coded
289     *
290     * @param wp The parameter list (or options).
291     *
292     * @param cbks Code-block size specifications
293     *
294     * @param pss Precinct partition specifications
295     *
296     * @param bms By-pass mode specifications
297     *
298     * @param mqrs MQ-reset specifications
299     *
300     * @param rts Regular termination specifications
301     *
302     * @param css Causal stripes specifications
303     *
304     * @param sss Error resolution segment symbol use specifications
305     *
306     * @param lcs Length computation specifications
307     *
308     * @param tts Termination type specifications
309     *
310     * @exception IllegalArgumentException If an error occurs while parsing
311     * the options in 'pl'
312     * */
313    public static EntropyCoder createInstance(CBlkQuantDataSrcEnc src,
314                                              J2KImageWriteParamJava wp,
315                                              CBlkSizeSpec cblks,
316                                              PrecinctSizeSpec pss,
317                                              StringSpec bms,StringSpec mqrs,
318                                              StringSpec rts,StringSpec css,
319                                              StringSpec sss,StringSpec lcs,
320                                              StringSpec tts) {
321        // Check parameters
322        //pl.checkList(OPT_PREFIX,pl.toNameArray(pinfo));
323        return new StdEntropyCoder(src,cblks,pss,bms,mqrs,rts,css,sss,lcs,tts);
324    }
325}