001/*
002 * $RCSfile: MQDecoder.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:06 $
005 * $State: Exp $
006 *
007 * Class:                   MQDecoder
008 *
009 * Description:             Class that encodes a number of bits using the
010 *                          MQ arithmetic decoder
011 *
012 *
013 *
014 * COPYRIGHT:
015 *
016 * This software module was originally developed by Raphaël Grosbois and
017 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
018 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
019 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
020 * Centre France S.A) in the course of development of the JPEG2000
021 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
022 * software module is an implementation of a part of the JPEG 2000
023 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
024 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
025 * Partners) agree not to assert against ISO/IEC and users of the JPEG
026 * 2000 Standard (Users) any of their rights under the copyright, not
027 * including other intellectual property rights, for this software module
028 * with respect to the usage by ISO/IEC and Users of this software module
029 * or modifications thereof for use in hardware or software products
030 * claiming conformance to the JPEG 2000 Standard. Those intending to use
031 * this software module in hardware or software products are advised that
032 * their use may infringe existing patents. The original developers of
033 * this software module, JJ2000 Partners and ISO/IEC assume no liability
034 * for use of this software module or modifications thereof. No license
035 * or right to this software module is granted for non JPEG 2000 Standard
036 * conforming products. JJ2000 Partners have full right to use this
037 * software module for his/her own purpose, assign or donate this
038 * software module to any third party and to inhibit third parties from
039 * using this software module for non JPEG 2000 Standard conforming
040 * products. This copyright notice must be included in all copies or
041 * derivative works of this software module.
042 *
043 * Copyright (c) 1999/2000 JJ2000 Partners.
044 *
045 *
046 *
047 */
048package jj2000.j2k.entropy.decoder;
049
050import jj2000.j2k.entropy.decoder.*;
051import jj2000.j2k.entropy.*;
052import jj2000.j2k.io.*;
053import jj2000.j2k.util.*;
054import java.io.*;
055
056/**
057 * This class implements the MQ arithmetic decoder. It is implemented using
058 * the software conventions decoder for better performance (i.e. execution
059 * time performance). The initial states for each context of the MQ-coder are
060 * specified in the constructor.
061 *
062 */
063
064// A trick to test for increased speed: merge the Qe and mPS into 1 thing by
065// using the sign bit of Qe to signal mPS (positive-or-0 is 0, negative is 1),
066// and doubling the Qe, nMPS and nLPS tables. This gets rid of the swicthLM
067// table since it can be integrated as special cases in the doubled nMPS and
068// nLPS tables. See the JPEG book, chapter 13. The decoded decision can be
069// calculated as (q>>>31).
070
071public class MQDecoder {
072
073    /** The data structures containing the probabilities for the LPS */
074    final static
075        int qe[]={0x5601, 0x3401, 0x1801, 0x0ac1, 0x0521, 0x0221, 0x5601,
076                  0x5401, 0x4801, 0x3801, 0x3001, 0x2401, 0x1c01, 0x1601,
077                  0x5601, 0x5401, 0x5101, 0x4801, 0x3801, 0x3401, 0x3001,
078                  0x2801, 0x2401, 0x2201, 0x1c01, 0x1801, 0x1601, 0x1401,
079                  0x1201, 0x1101, 0x0ac1, 0x09c1, 0x08a1, 0x0521, 0x0441,
080                  0x02a1, 0x0221, 0x0141, 0x0111, 0x0085, 0x0049, 0x0025,
081                  0x0015, 0x0009, 0x0005, 0x0001, 0x5601 };
082
083    /** The indexes of the next MPS */
084    final static
085        int nMPS[]={ 1 , 2, 3, 4, 5,38, 7, 8, 9,10,11,12,13,29,15,16,17,
086                     18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
087                     35,36,37,38,39,40,41,42,43,44,45,45,46 };
088
089    /** The indexes of the next LPS */
090    final static
091        int nLPS[]={ 1 , 6, 9,12,29,33, 6,14,14,14,17,18,20,21,14,14,15,
092                     16,17,18,19,19,20,21,22,23,24,25,26,27,28,29,30,31,
093                     32,33,34,35,36,37,38,39,40,41,42,43,46 };
094
095    /** Whether LPS and MPS should be switched */
096    final static
097        int switchLM[]={ 1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
098                         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
099
100    /** The ByteInputBuffer used to read the compressed bit stream. */
101    ByteInputBuffer in;
102
103    /** The current most probable signal for each context */
104    int[] mPS;
105
106    /** The current index of each context */
107    int[] I;
108
109    /** The current bit code */
110    int c;
111
112    /** The bit code counter */
113    int cT;
114
115    /** The current interval */
116    int a;
117
118    /** The last byte read */
119    int b;
120
121    /** Flag indicating if a marker has been found */
122    boolean markerFound;
123
124    /** The initial state of each context */
125    final int initStates[];
126
127    /**
128     * Instantiates a new MQ-decoder, with the specified number of contexts and
129     * initial states. The compressed bytestream is read from the 'iStream'
130     * object.
131     *
132     * @param iStream the stream that contains the coded bits
133     *
134     * @param nrOfContexts The number of contexts used
135     *
136     * @param initStates The initial state for each context. A reference is
137     * kept to this array to reinitialize the contexts whenever 'reset()' or
138     * 'resetCtxts()' is called.
139     *
140     *
141     *
142     */
143    public MQDecoder(ByteInputBuffer iStream, int nrOfContexts,
144                     int initStates[]){
145        in = iStream;
146
147        // Default initialization of the statistics bins is MPS=0 and
148        // I=0
149        I=new int[nrOfContexts];
150        mPS=new int[nrOfContexts];
151        // Save the initial states
152        this.initStates = initStates;
153
154        // Initialize
155        init();
156
157        // Set the contexts
158        resetCtxts();
159    }
160
161    /**
162     * Decodes 'n' symbols from the bit stream using the same context
163     * 'ctxt'. If possible the MQ-coder speedup mode will be used to speed up
164     * decoding. The speedup mode is used if Q (the LPS probability for 'ctxt'
165     * is low enough) and the A and C registers permit decoding several MPS
166     * symbols without renormalization.
167     *
168     * <P>Speedup mode should be used when decoding long runs of MPS with high
169     * probability with the same context.
170     *
171     * <P>This methiod will return the decoded symbols differently if speedup
172     * mode was used or not. If true is returned, then speedup mode was used
173     * and the 'n' decoded symbols are all the same and it is returned ain
174     * bits[0] only. If false is returned then speedup mode was not used, the
175     * decoded symbols are probably not all the same and they are returned in
176     * bits[0], bits[1], ... bits[n-1].
177     *
178     * @param bits The array where to put the decoded symbols. Must be of
179     * length 'n' or more.
180     *
181     * @param ctxt The context to use in decoding the symbols.
182     *
183     * @param n The number of symbols to decode.
184     *
185     * @return True if speedup mode was used, false if not. If speedup mode
186     * was used then all the decoded symbols are the same and its value is
187     * returned in 'bits[0]' only (not in bits[1], bits[2], etc.).
188     *
189     *
190     * */
191    public final boolean fastDecodeSymbols(int[] bits, int ctxt, int n) {
192        int q;   // LPS probability for context
193        int idx; // Index of current state
194        int la;  // cache for A register
195        int i;   // counter
196
197        idx = I[ctxt];
198        q = qe[idx];
199
200        // This is a first attempt to implement speedup mode, it is probably
201        // not the most efficient way of doing it.
202
203        if ((q<0x4000) && (n <= (a-(c>>>16)-1)/q) &&
204            (n <= (a-0x8000)/q+1)) {
205            // Q is small enough. There will be no modification of C that
206            // affects decoding, and Q can be substracted from A several
207            // times. We will decode all MPS.
208            a -= n*q;
209            if (a >= 0x8000) { // No renormalization needed
210                bits[0] = mPS[ctxt];
211                return true; // Done, used speedup mode
212            }
213            else { // renormalization needed
214                I[ctxt] = nMPS[idx];
215                // Renormalize (MPS: no need for while loop)
216                if (cT == 0)
217                    byteIn();
218                a <<= 1;
219                c <<= 1;
220                cT--;
221                // End renormalization
222                bits[0] = mPS[ctxt];
223                return true; // Done, used speedup mode
224            }
225        }
226        else { // Normal mode
227            la = a; // cache A register
228            for (i=0; i<n; i++) {
229                la -= q;
230                if ((c>>>16) < la) {
231                    if(la >= 0x8000){
232                        bits[i] = mPS[ctxt];
233                    }
234                    else {
235                        // -- MPS Exchange
236                        if(la >= q){
237                            bits[i] = mPS[ctxt];
238                            idx = nMPS[idx];
239                            q = qe[idx];
240                            // I[ctxt] set at end of loop
241                            // -- Renormalize (MPS: no need for while loop)
242                            if(cT==0)
243                                byteIn();
244                            la<<=1;
245                            c<<=1;
246                            cT--;
247                            // -- End renormalization
248                        }
249                        else{
250                            bits[i] = 1-mPS[ctxt];
251                            if(switchLM[idx]==1)
252                                mPS[ctxt] = 1-mPS[ctxt];
253                            idx =  nLPS[idx];
254                            q = qe[idx];
255                            // I[ctxt] set at end of loop
256                            // -- Renormalize
257                            do{
258                                if(cT==0)
259                                    byteIn();
260                                la<<=1;
261                                c<<=1;
262                                cT--;
263                            }while(la < 0x8000);
264                            // -- End renormalization
265                        }
266                        // -- End MPS Exchange
267                    }
268                }
269                else {
270                    c -= (la<<16);
271                    // -- LPS Exchange
272                    if(la < q){
273                        la = q;
274                        bits[i] = mPS[ctxt];
275                        idx = nMPS[idx];
276                        q = qe[idx];
277                        // I[ctxt] set at end of loop
278                        // -- Renormalize (MPS: no need for while loop)
279                        if(cT==0)
280                            byteIn();
281                        la<<=1;
282                        c<<=1;
283                        cT--;
284                        // -- End renormalization
285                    }
286                    else {
287                        la = q;
288                        bits[i] = 1-mPS[ctxt];
289                        if(switchLM[idx] == 1)
290                            mPS[ctxt] = 1-mPS[ctxt];
291                        idx =  nLPS[idx];
292                        q = qe[idx];
293                        // I[ctxt] set at end of loop
294                        // -- Renormalize
295                        do{
296                            if(cT==0)
297                                byteIn();
298                            la<<=1;
299                            c<<=1;
300                            cT--;
301                        } while (la < 0x8000);
302                        // -- End renormalization
303                    }
304                    // -- End LPS Exchange
305                }
306            }
307            a = la;           // save cached A register
308            I[ctxt] = idx;    // save current index for context
309            return false;     // done, did not use speedup mode
310        } // End normal mode
311    }
312
313    /**
314     * This function performs the arithmetic decoding. The function receives
315     * an array in which to put the decoded symbols and an array of contexts
316     * with which to decode them.
317     *
318     * <P>Each context has a current MPS and an index describing what the
319     * current probability is for the LPS. Each bit is decoded and if the
320     * probability of the LPS exceeds .5, the MPS and LPS are switched.
321     *
322     * @param bits The array where to place the decoded symbols. It should be
323     * long enough to contain 'n' elements.
324     *
325     * @param cX The context to use in decoding each symbol.
326     *
327     * @param n The number of symbols to decode
328     *
329     *
330     */
331    public final void decodeSymbols(int[] bits, int[] cX, int n){
332        int q;
333        int ctxt;
334        int la; // cache for A register value
335        int index;
336        int i;
337
338        // NOTE: (a < 0x8000) is equivalent to ((a & 0x8000)==0)
339        // since 'a' is always less than or equal to 0xFFFF
340
341        // NOTE: conditional exchange guarantees that A for MPS is
342        // always greater than 0x4000 (i.e. 0.375)
343        // => one renormalization shift is enough for MPS
344        // => no need to do a renormalization while loop for MPS
345
346        for (i=0; i<n; i++) {
347            ctxt = cX[i];
348
349            index = I[ctxt];
350            q = qe[index];
351
352            a -= q;
353            if ((c>>>16) < a) {
354                if(a >= 0x8000){
355                    bits[i] = mPS[ctxt];
356                }
357                else {
358                    la = a;
359                    // -- MPS Exchange
360                    if(la >= q){
361                        bits[i] = mPS[ctxt];
362                        I[ctxt] = nMPS[index];
363                        // -- Renormalize (MPS: no need for while loop)
364                        if(cT==0)
365                            byteIn();
366                        la<<=1;
367                        c<<=1;
368                        cT--;
369                        // -- End renormalization
370                    }
371                    else{
372                        bits[i] = 1-mPS[ctxt];
373                        if(switchLM[index]==1)
374                            mPS[ctxt] = 1-mPS[ctxt];
375                        I[ctxt] = nLPS[index];
376                        // -- Renormalize
377                        do{
378                            if(cT==0)
379                                byteIn();
380                            la<<=1;
381                            c<<=1;
382                            cT--;
383                        }while(la < 0x8000);
384                        // -- End renormalization
385                    }
386                    // -- End MPS Exchange
387                    a = la;
388                }
389            }
390            else {
391                la = a;
392                c -= (la<<16);
393                // -- LPS Exchange
394                if(la < q){
395                    la = q;
396                    bits[i] = mPS[ctxt];
397                    I[ctxt] = nMPS[index];
398                    // -- Renormalize (MPS: no need for while loop)
399                    if(cT==0)
400                        byteIn();
401                    la<<=1;
402                    c<<=1;
403                    cT--;
404                    // -- End renormalization
405                }
406                else {
407                    la = q;
408                    bits[i] = 1-mPS[ctxt];
409                    if(switchLM[index] == 1)
410                        mPS[ctxt] = 1-mPS[ctxt];
411                    I[ctxt] = nLPS[index];
412                    // -- Renormalize
413                    do{
414                        if(cT==0)
415                            byteIn();
416                        la<<=1;
417                        c<<=1;
418                        cT--;
419                    } while (la < 0x8000);
420                    // -- End renormalization
421                }
422                // -- End LPS Exchange
423
424                a = la;
425            }
426        }
427    }
428
429
430    /**
431     * Arithmetically decodes one symbol from the bit stream with the given
432     * context and returns its decoded value.
433     *
434     * <P>Each context has a current MPS and an index describing what the
435     * current probability is for the LPS. Each bit is encoded and if the
436     * probability of the LPS exceeds .5, the MPS and LPS are switched.
437     *
438     * @param context The context to use in decoding the symbol
439     *
440     * @return The decoded symbol, 0 or 1.
441     *
442     *
443     */
444    public final int decodeSymbol(int context){
445        int q;
446        int la;
447        int index;
448        int decision;
449
450        index = I[context];
451        q = qe[index];
452
453        // NOTE: (a < 0x8000) is equivalent to ((a & 0x8000)==0)
454        // since 'a' is always less than or equal to 0xFFFF
455
456        // NOTE: conditional exchange guarantees that A for MPS is
457        // always greater than 0x4000 (i.e. 0.375)
458        // => one renormalization shift is enough for MPS
459        // => no need to do a renormalization while loop for MPS
460
461        a -= q;
462        if ((c>>>16) < a) {
463            if(a >= 0x8000){
464                decision = mPS[context];
465            }
466            else {
467                la = a;
468                // -- MPS Exchange
469                if(la >= q){
470                    decision = mPS[context];
471                    I[context] = nMPS[index];
472                    // -- Renormalize (MPS: no need for while loop)
473                    if(cT==0)
474                        byteIn();
475                    la<<=1;
476                    c<<=1;
477                    cT--;
478                    // -- End renormalization
479                }
480                else{
481                    decision = 1-mPS[context];
482                    if(switchLM[index]==1)
483                        mPS[context] = 1-mPS[context];
484                    I[context] = nLPS[index];
485                    // -- Renormalize
486                    do{
487                        if(cT==0)
488                            byteIn();
489                        la<<=1;
490                        c<<=1;
491                        cT--;
492                    }while(la < 0x8000);
493                    // -- End renormalization
494                }
495                // -- End MPS Exchange
496                a = la;
497            }
498        }
499        else {
500            la = a;
501            c -= (la<<16);
502            // -- LPS Exchange
503            if(la < q){
504                la = q;
505                decision = mPS[context];
506                I[context] = nMPS[index];
507                // -- Renormalize (MPS: no need for while loop)
508                if(cT==0)
509                    byteIn();
510                la<<=1;
511                c<<=1;
512                cT--;
513                // -- End renormalization
514            }
515            else {
516                la = q;
517                decision = 1-mPS[context];
518                if(switchLM[index] == 1)
519                    mPS[context] = 1-mPS[context];
520                I[context] = nLPS[index];
521                // -- Renormalize
522                do{
523                    if(cT==0)
524                        byteIn();
525                    la<<=1;
526                    c<<=1;
527                    cT--;
528                } while (la < 0x8000);
529                // -- End renormalization
530            }
531            // -- End LPS Exchange
532
533            a = la;
534        }
535        return decision;
536    }
537
538    /**
539     * Checks for past errors in the decoding process using the predictable
540     * error resilient termination. This works only if the encoder used the
541     * predictable error resilient MQ termination, otherwise it reports wrong
542     * results. If an error is detected it means that the MQ bit stream has
543     * been wrongly decoded or that the MQ terminated segment length is too
544     * long. If no errors are detected it does not necessarily mean that the
545     * MQ bit stream has been correctly decoded.
546     *
547     * @return True if errors are found, false otherwise.
548     * */
549    public boolean checkPredTerm() {
550        int k;  // Number of bits that where added in the termination process
551        int q;
552
553        // 1) if everything has been OK, 'b' must be 0xFF if a terminating
554        // marker has not yet been found
555        if (b != 0xFF && !markerFound) return true;
556
557        // 2) if cT is not 0, we must have already reached the terminating
558        // marker
559        if (cT != 0 && !markerFound) return true;
560
561        // 3) If cT is 1 there where no spare bits at the encoder, this is all
562        // that we can check
563        if (cT == 1) return false;
564
565        // 4) if cT is 0, then next byte must be the second byte of a
566        // terminating marker (i.e. larger than 0x8F) if the terminating
567        // marker has not been reached yet
568        if (cT == 0) {
569            if (!markerFound) {
570                // Get next byte and check
571                b=in.read()&0xFF;
572                if (b <= 0x8F) return true;
573            }
574            // Adjust cT for last byte
575            cT = 8;
576        }
577
578        // 5) Now we can calculate the number 'k' of bits having error
579        // resilience information, which is the number of bits left to
580        // normalization in the C register, minus 1.
581        k = cT-1;
582
583        // 6) The predictable termination policy is as if an LPS interval was
584        // coded that caused a renormalization of 'k' bits, before the
585        // termination marker started
586
587        // We first check if an LPS is decoded, that causes a renormalization
588        // of 'k' bits. Worst case is smallest LPS probability 'q' that causes
589        // a renormalization of 'k' bits.
590        q = 0x8000>>k;
591
592        // Check that we can decode an LPS interval of probability 'q'
593        a -= q;
594        if ((c>>>16) < a) {
595            // Error: MPS interval decoded
596            return true;
597        }
598        // OK: LPS interval decoded
599        c -= (a<<16);
600        // -- LPS Exchange
601        // Here 'a' can not be smaller than 'q' because the minimum value
602        // for 'a' is 0x8000-0x4000=0x4000 and 'q' is set to a value equal
603        // to or smaller than that.
604        a = q;
605        // -- Renormalize
606        do{
607            if(cT==0) byteIn();
608            a<<=1;
609            c<<=1;
610            cT--;
611        } while (a < 0x8000);
612        // -- End renormalization
613        // -- End LPS Exchange
614
615        // 7) Everything seems OK, we have checked the C register for the LPS
616        // symbols and ensured that it is followed by bits synthetized by the
617        // termination marker.
618        return false;
619    }
620
621    /**
622     * This function gets one byte of compressed bits from the in-stream.
623     * the byte is added to c. If the byte is 0xFF and the next byte is greater
624     * than 0x8F, the byte after 0xFF is a marker.
625     *
626     *
627     *
628     */
629    private void byteIn(){
630        if(!markerFound){
631            if(b==0xFF){
632                b=in.read()&0xFF; // Convert EOFs (-1) to 0xFF
633
634                if(b>0x8F){
635                    markerFound=true;
636                    // software-convention decoder: c unchanged
637                    cT=8;
638                }else{
639                    c += 0xFE00 - (b<<9);
640                    cT=7;
641                }
642            }else{
643                b=in.read()&0xFF; // Convert EOFs (-1) to 0xFF
644                c += 0xFF00 - (b<<8);
645                cT=8;
646            }
647        }
648        else{
649            // software-convention decoder: c unchanged
650            cT=8;
651        }
652    }
653
654    /**
655      * Returns the number of contexts in the arithmetic coder.
656      *
657      * @return The number of contexts
658      *
659      *
660      **/
661    public final int getNumCtxts(){
662        return I.length;
663    }
664
665    /**
666     * Resets a context to the original probability distribution.
667     *
668     * @param c The number of the context (it starts at 0).
669     *
670     *
671     *
672     */
673    public final void resetCtxt(int c){
674        I[c] = initStates[c];
675        mPS[c] = 0;
676    }
677
678    /**
679     * Resets a context to the original probability distribution. The
680     * original probability distribution depends on the actual
681     * implementation of the arithmetic coder or decoder.
682     *
683     * @param c The index of the context (it starts at 0).
684     *
685     *
686     *
687     */
688    public final void resetCtxts(){
689        System.arraycopy(initStates,0,I,0,I.length);
690        ArrayUtil.intArraySet(mPS,0);
691    }
692
693    /**
694     * Resets the MQ decoder to start a new segment. This is like recreating a
695     * new MQDecoder object with new input data.
696     *
697     * @param buf The byte array containing the MQ encoded data. If null the
698     * current byte array is assumed.
699     *
700     * @param off The index of the first element in 'buf' to be decoded. If
701     * negative the byte just after the previous segment is assumed, only
702     * valid if 'buf' is null.
703     *
704     * @param len The number of bytes in 'buf' to be decoded. Any subsequent
705     * bytes are taken to be 0xFF.
706     *
707     *
708     * */
709    public final void nextSegment(byte buf[], int off, int len) {
710        // Set the new input
711        in.setByteArray(buf,off,len);
712        // Reinitialize MQ
713        init();
714    }
715
716    /**
717     * Returns the underlying 'ByteInputBuffer' from where the MQ
718     * coded input bytes are read.
719     *
720     * @return The underlying ByteInputBuffer.
721     *
722     * */
723    public ByteInputBuffer getByteInputBuffer() {
724        return in;
725    }
726
727    /**
728     * Initializes the state of the MQ coder, without modifying the current
729     * context states. It sets the registers (A,C,B) and the "marker found"
730     * state to the initial state, to start the decoding of a new segment.
731     *
732     * <P>To have a complete reset of the MQ (as if a new MQDecoder object was
733     * created) 'resetCtxts()' should be called after this method.
734     *
735     *
736     * */
737    private void init() {
738        // --- INITDEC
739        markerFound = false;
740
741        // Read first byte
742        b=in.read()&0xFF;
743
744        // Software conventions decoder
745        c=(b^0xFF)<<16;
746        byteIn();
747        c=c<<7;
748        cT=cT-7;
749        a=0x8000;
750
751        // End of INITDEC ---
752    }
753}