Back to index

tetex-bin  3.0
JArithmeticDecoder.cc
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // JArithmeticDecoder.cc
00004 //
00005 // Copyright 2002-2004 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 
00011 #ifdef USE_GCC_PRAGMAS
00012 #pragma implementation
00013 #endif
00014 
00015 #include "Object.h"
00016 #include "Stream.h"
00017 #include "JArithmeticDecoder.h"
00018 
00019 //------------------------------------------------------------------------
00020 // JArithmeticDecoderStates
00021 //------------------------------------------------------------------------
00022 
00023 JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) {
00024   contextSize = contextSizeA;
00025   cxTab = (Guchar *)gmalloc(contextSize * sizeof(Guchar));
00026   reset();
00027 }
00028 
00029 JArithmeticDecoderStats::~JArithmeticDecoderStats() {
00030   gfree(cxTab);
00031 }
00032 
00033 JArithmeticDecoderStats *JArithmeticDecoderStats::copy() {
00034   JArithmeticDecoderStats *stats;
00035 
00036   stats = new JArithmeticDecoderStats(contextSize);
00037   memcpy(stats->cxTab, cxTab, contextSize);
00038   return stats;
00039 }
00040 
00041 void JArithmeticDecoderStats::reset() {
00042   memset(cxTab, 0, contextSize);
00043 }
00044 
00045 void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) {
00046   memcpy(cxTab, stats->cxTab, contextSize);
00047 }
00048 
00049 void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) {
00050   cxTab[cx] = (i << 1) + mps;
00051 }
00052 
00053 //------------------------------------------------------------------------
00054 // JArithmeticDecoder
00055 //------------------------------------------------------------------------
00056 
00057 Guint JArithmeticDecoder::qeTab[47] = {
00058   0x56010000, 0x34010000, 0x18010000, 0x0AC10000,
00059   0x05210000, 0x02210000, 0x56010000, 0x54010000,
00060   0x48010000, 0x38010000, 0x30010000, 0x24010000,
00061   0x1C010000, 0x16010000, 0x56010000, 0x54010000,
00062   0x51010000, 0x48010000, 0x38010000, 0x34010000,
00063   0x30010000, 0x28010000, 0x24010000, 0x22010000,
00064   0x1C010000, 0x18010000, 0x16010000, 0x14010000,
00065   0x12010000, 0x11010000, 0x0AC10000, 0x09C10000,
00066   0x08A10000, 0x05210000, 0x04410000, 0x02A10000,
00067   0x02210000, 0x01410000, 0x01110000, 0x00850000,
00068   0x00490000, 0x00250000, 0x00150000, 0x00090000,
00069   0x00050000, 0x00010000, 0x56010000
00070 };
00071 
00072 int JArithmeticDecoder::nmpsTab[47] = {
00073    1,  2,  3,  4,  5, 38,  7,  8,  9, 10, 11, 12, 13, 29, 15, 16,
00074   17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
00075   33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 45, 46
00076 };
00077 
00078 int JArithmeticDecoder::nlpsTab[47] = {
00079    1,  6,  9, 12, 29, 33,  6, 14, 14, 14, 17, 18, 20, 21, 14, 14,
00080   15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
00081   30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46
00082 };
00083 
00084 int JArithmeticDecoder::switchTab[47] = {
00085   1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
00086   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00087   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00088 };
00089 
00090 JArithmeticDecoder::JArithmeticDecoder() {
00091   str = NULL;
00092 }
00093 
00094 inline Guint JArithmeticDecoder::readByte() {
00095   if (dataLen == 0) {
00096     return 0xff;
00097   }
00098   if (dataLen > 0) {
00099     --dataLen;
00100   }
00101   return (Guint)str->getChar() & 0xff;
00102 }
00103 
00104 JArithmeticDecoder::~JArithmeticDecoder() {
00105   while (dataLen > 0) {
00106     readByte();
00107   }
00108 }
00109 
00110 void JArithmeticDecoder::start() {
00111   buf0 = readByte();
00112   buf1 = readByte();
00113 
00114   // INITDEC
00115   c = (buf0 ^ 0xff) << 16;
00116   byteIn();
00117   c <<= 7;
00118   ct -= 7;
00119   a = 0x80000000;
00120 }
00121 
00122 int JArithmeticDecoder::decodeBit(Guint context,
00123                               JArithmeticDecoderStats *stats) {
00124   int bit;
00125   Guint qe;
00126   int iCX, mpsCX;
00127 
00128   iCX = stats->cxTab[context] >> 1;
00129   mpsCX = stats->cxTab[context] & 1;
00130   qe = qeTab[iCX];
00131   a -= qe;
00132   if (c < a) {
00133     if (a & 0x80000000) {
00134       bit = mpsCX;
00135     } else {
00136       // MPS_EXCHANGE
00137       if (a < qe) {
00138        bit = 1 - mpsCX;
00139        if (switchTab[iCX]) {
00140          stats->cxTab[context] = (nlpsTab[iCX] << 1) | (1 - mpsCX);
00141        } else {
00142          stats->cxTab[context] = (nlpsTab[iCX] << 1) | mpsCX;
00143        }
00144       } else {
00145        bit = mpsCX;
00146        stats->cxTab[context] = (nmpsTab[iCX] << 1) | mpsCX;
00147       }
00148       // RENORMD
00149       do {
00150        if (ct == 0) {
00151          byteIn();
00152        }
00153        a <<= 1;
00154        c <<= 1;
00155        --ct;
00156       } while (!(a & 0x80000000));
00157     }
00158   } else {
00159     c -= a;
00160     // LPS_EXCHANGE
00161     if (a < qe) {
00162       bit = mpsCX;
00163       stats->cxTab[context] = (nmpsTab[iCX] << 1) | mpsCX;
00164     } else {
00165       bit = 1 - mpsCX;
00166       if (switchTab[iCX]) {
00167        stats->cxTab[context] = (nlpsTab[iCX] << 1) | (1 - mpsCX);
00168       } else {
00169        stats->cxTab[context] = (nlpsTab[iCX] << 1) | mpsCX;
00170       }
00171     }
00172     a = qe;
00173     // RENORMD
00174     do {
00175       if (ct == 0) {
00176        byteIn();
00177       }
00178       a <<= 1;
00179       c <<= 1;
00180       --ct;
00181     } while (!(a & 0x80000000));
00182   }
00183   return bit;
00184 }
00185 
00186 int JArithmeticDecoder::decodeByte(Guint context,
00187                                JArithmeticDecoderStats *stats) {
00188   int byte;
00189   int i;
00190 
00191   byte = 0;
00192   for (i = 0; i < 8; ++i) {
00193     byte = (byte << 1) | decodeBit(context, stats);
00194   }
00195   return byte;
00196 }
00197 
00198 GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) {
00199   int s;
00200   Guint v;
00201   int i;
00202 
00203   prev = 1;
00204   s = decodeIntBit(stats);
00205   if (decodeIntBit(stats)) {
00206     if (decodeIntBit(stats)) {
00207       if (decodeIntBit(stats)) {
00208        if (decodeIntBit(stats)) {
00209          if (decodeIntBit(stats)) {
00210            v = 0;
00211            for (i = 0; i < 32; ++i) {
00212              v = (v << 1) | decodeIntBit(stats);
00213            }
00214            v += 4436;
00215          } else {
00216            v = 0;
00217            for (i = 0; i < 12; ++i) {
00218              v = (v << 1) | decodeIntBit(stats);
00219            }
00220            v += 340;
00221          }
00222        } else {
00223          v = 0;
00224          for (i = 0; i < 8; ++i) {
00225            v = (v << 1) | decodeIntBit(stats);
00226          }
00227          v += 84;
00228        }
00229       } else {
00230        v = 0;
00231        for (i = 0; i < 6; ++i) {
00232          v = (v << 1) | decodeIntBit(stats);
00233        }
00234        v += 20;
00235       }
00236     } else {
00237       v = decodeIntBit(stats);
00238       v = (v << 1) | decodeIntBit(stats);
00239       v = (v << 1) | decodeIntBit(stats);
00240       v = (v << 1) | decodeIntBit(stats);
00241       v += 4;
00242     }
00243   } else {
00244     v = decodeIntBit(stats);
00245     v = (v << 1) | decodeIntBit(stats);
00246   }
00247 
00248   if (s) {
00249     if (v == 0) {
00250       return gFalse;
00251     }
00252     *x = -(int)v;
00253   } else {
00254     *x = (int)v;
00255   }
00256   return gTrue;
00257 }
00258 
00259 int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) {
00260   int bit;
00261 
00262   bit = decodeBit(prev, stats);
00263   if (prev < 0x100) {
00264     prev = (prev << 1) | bit;
00265   } else {
00266     prev = (((prev << 1) | bit) & 0x1ff) | 0x100;
00267   }
00268   return bit;
00269 }
00270 
00271 Guint JArithmeticDecoder::decodeIAID(Guint codeLen,
00272                                  JArithmeticDecoderStats *stats) {
00273   Guint i;
00274   int bit;
00275 
00276   prev = 1;
00277   for (i = 0; i < codeLen; ++i) {
00278     bit = decodeBit(prev, stats);
00279     prev = (prev << 1) | bit;
00280   }
00281   return prev - (1 << codeLen);
00282 }
00283 
00284 void JArithmeticDecoder::byteIn() {
00285   if (buf0 == 0xff) {
00286     if (buf1 > 0x8f) {
00287       ct = 8;
00288     } else {
00289       buf0 = buf1;
00290       buf1 = readByte();
00291       c = c + 0xfe00 - (buf0 << 9);
00292       ct = 7;
00293     }
00294   } else {
00295     buf0 = buf1;
00296     buf1 = readByte();
00297     c = c + 0xff00 - (buf0 << 8);
00298     ct = 8;
00299   }
00300 }