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