コード例 #1
0
        public int decodeChar(LZMA.RangeCoder.Decoder decoder)
        {
            if (minContext.NumStats != 1)
            {
                State s = tempState1.Initialize(Heap);
                s.Address = minContext.FreqData.GetStats();
                int i;
                int count, hiCnt;
                if ((count = (int)decoder.GetThreshold((uint)minContext.FreqData.SummFreq)) < (hiCnt = s.Freq))
                {
                    byte symbol;
                    decoder.Decode(0, (uint)s.Freq);
                    symbol = (byte)s.Symbol;
                    minContext.update1_0(this, s.Address);
                    nextContext();
                    return(symbol);
                }
                prevSuccess = 0;
                i           = minContext.NumStats - 1;
                do
                {
                    s.IncrementAddress();
                    if ((hiCnt += s.Freq) > count)
                    {
                        byte symbol;
                        decoder.Decode((uint)(hiCnt - s.Freq), (uint)s.Freq);
                        symbol = (byte)s.Symbol;
                        minContext.update1(this, s.Address);
                        nextContext();
                        return(symbol);
                    }
                } while (--i > 0);
                if (count >= minContext.FreqData.SummFreq)
                {
                    return(-2);
                }
                hiBitsFlag = HB2Flag[foundState.Symbol];
                decoder.Decode((uint)hiCnt, (uint)(minContext.FreqData.SummFreq - hiCnt));
                for (i = 0; i < 256; i++)
                {
                    charMask[i] = -1;
                }
                charMask[s.Symbol] = 0;
                i = minContext.NumStats - 1;
                do
                {
                    s.DecrementAddress();
                    charMask[s.Symbol] = 0;
                } while (--i > 0);
            }
            else
            {
                State rs = tempState1.Initialize(Heap);
                rs.Address = minContext.getOneState().Address;
                hiBitsFlag = getHB2Flag()[foundState.Symbol];
                int off1 = rs.Freq - 1;
                int off2 = minContext.getArrayIndex(this, rs);
                int bs   = binSumm[off1][off2];
                if (decoder.DecodeBit((uint)bs, 14) == 0)
                {
                    byte symbol;
                    binSumm[off1][off2] = (bs + INTERVAL - minContext.getMean(bs, PERIOD_BITS, 2)) & 0xFFFF;
                    foundState.Address  = rs.Address;
                    symbol = (byte)rs.Symbol;
                    rs.IncrementFreq((rs.Freq < 128) ? 1 : 0);
                    prevSuccess = 1;
                    incRunLength(1);
                    nextContext();
                    return(symbol);
                }
                bs = (bs - minContext.getMean(bs, PERIOD_BITS, 2)) & 0xFFFF;
                binSumm[off1][off2] = bs;
                initEsc             = PPMContext.ExpEscape[Utility.URShift(bs, 10)];
                int i;
                for (i = 0; i < 256; i++)
                {
                    charMask[i] = -1;
                }
                charMask[rs.Symbol] = 0;
                prevSuccess         = 0;
            }
            for (;;)
            {
                State       s = tempState1.Initialize(Heap);
                int         i;
                int         freqSum, count, hiCnt;
                SEE2Context see;
                int         num, numMasked = minContext.NumStats;
                do
                {
                    orderFall++;
                    minContext.Address = minContext.getSuffix();
                    if (minContext.Address <= subAlloc.PText || minContext.Address > subAlloc.HeapEnd)
                    {
                        return(-1);
                    }
                } while (minContext.NumStats == numMasked);
                hiCnt     = 0;
                s.Address = minContext.FreqData.GetStats();
                i         = 0;
                num       = minContext.NumStats - numMasked;
                do
                {
                    int k = charMask[s.Symbol];
                    hiCnt           += s.Freq & k;
                    minContext.ps[i] = s.Address;
                    s.IncrementAddress();
                    i -= k;
                } while (i != num);

                see      = minContext.makeEscFreq(this, numMasked, out freqSum);
                freqSum += hiCnt;
                count    = (int)decoder.GetThreshold((uint)freqSum);

                if (count < hiCnt)
                {
                    byte  symbol;
                    State ps = tempState2.Initialize(Heap);
                    for (hiCnt = 0, i = 0, ps.Address = minContext.ps[i];
                         (hiCnt += ps.Freq) <= count;
                         i++, ps.Address = minContext.ps[i])
                    {
                        ;
                    }
                    s.Address = ps.Address;
                    decoder.Decode((uint)(hiCnt - s.Freq), (uint)s.Freq);
                    see.update();
                    symbol = (byte)s.Symbol;
                    minContext.update2(this, s.Address);
                    updateModel();
                    return(symbol);
                }
                if (count >= freqSum)
                {
                    return(-2);
                }
                decoder.Decode((uint)hiCnt, (uint)(freqSum - hiCnt));
                see.Summ = see.Summ + freqSum;
                do
                {
                    s.Address          = minContext.ps[--i];
                    charMask[s.Symbol] = 0;
                } while (i != 0);
            }
        }
コード例 #2
0
        private void updateModel()
        {
            //System.out.println("ModelPPM.updateModel()");
            // STATE fs = *FoundState, *p = NULL;
            StateRef fs = tempStateRef1;

            fs.Values = foundState;
            State p         = tempState3.Initialize(Heap);
            State tempState = tempState4.Initialize(Heap);

            PPMContext pc        = tempPPMContext3.Initialize(Heap);
            PPMContext successor = tempPPMContext4.Initialize(Heap);

            int ns1, ns, cf, sf, s0;

            pc.Address = minContext.getSuffix();
            if (fs.Freq < MAX_FREQ / 4 && pc.Address != 0)
            {
                if (pc.NumStats != 1)
                {
                    p.Address = pc.FreqData.GetStats();
                    if (p.Symbol != fs.Symbol)
                    {
                        do
                        {
                            p.IncrementAddress();
                        } while (p.Symbol != fs.Symbol);
                        tempState.Address = p.Address - State.Size;
                        if (p.Freq >= tempState.Freq)
                        {
                            State.PPMDSwap(p, tempState);
                            p.DecrementAddress();
                        }
                    }
                    if (p.Freq < MAX_FREQ - 9)
                    {
                        p.IncrementFreq(2);
                        pc.FreqData.IncrementSummFreq(2);
                    }
                }
                else
                {
                    p.Address = pc.getOneState().Address;
                    if (p.Freq < 32)
                    {
                        p.IncrementFreq(1);
                    }
                }
            }
            if (orderFall == 0)
            {
                foundState.SetSuccessor(createSuccessors(true, p));
                minContext.Address = foundState.GetSuccessor();
                maxContext.Address = foundState.GetSuccessor();
                if (minContext.Address == 0)
                {
                    updateModelRestart();
                    return;
                }
                return;
            }
            subAlloc.Heap[subAlloc.PText] = (byte)fs.Symbol;
            subAlloc.incPText();
            successor.Address = subAlloc.PText;
            if (subAlloc.PText >= subAlloc.FakeUnitsStart)
            {
                updateModelRestart();
                return;
            }
            //        // Debug
            //        subAlloc.dumpHeap();
            if (fs.GetSuccessor() != 0)
            {
                if (fs.GetSuccessor() <= subAlloc.PText)
                {
                    fs.SetSuccessor(createSuccessors(false, p));
                    if (fs.GetSuccessor() == 0)
                    {
                        updateModelRestart();
                        return;
                    }
                }
                if (--orderFall == 0)
                {
                    successor.Address = fs.GetSuccessor();
                    if (maxContext.Address != minContext.Address)
                    {
                        subAlloc.decPText(1);
                    }
                }
            }
            else
            {
                foundState.SetSuccessor(successor.Address);
                fs.SetSuccessor(minContext);
            }
            //        // Debug
            //        subAlloc.dumpHeap();
            ns = minContext.NumStats;
            s0 = minContext.FreqData.SummFreq - (ns) - (fs.Freq - 1);
            for (pc.Address = maxContext.Address; pc.Address != minContext.Address; pc.Address = pc.getSuffix())
            {
                if ((ns1 = pc.NumStats) != 1)
                {
                    if ((ns1 & 1) == 0)
                    {
                        //System.out.println(ns1);
                        pc.FreqData.SetStats(subAlloc.expandUnits(pc.FreqData.GetStats(), Utility.URShift(ns1, 1)));
                        if (pc.FreqData.GetStats() == 0)
                        {
                            updateModelRestart();
                            return;
                        }
                    }
                    // bug fixed
                    //				int sum = ((2 * ns1 < ns) ? 1 : 0) +
                    //                        2 * ((4 * ((ns1 <= ns) ? 1 : 0)) & ((pc.getFreqData()
                    //								.getSummFreq() <= 8 * ns1) ? 1 : 0));
                    int sum = ((2 * ns1 < ns) ? 1 : 0) +
                              2 * (((4 * ns1 <= ns) ? 1 : 0) & ((pc.FreqData.SummFreq <= 8 * ns1) ? 1 : 0));
                    pc.FreqData.IncrementSummFreq(sum);
                }
                else
                {
                    p.Address = subAlloc.allocUnits(1);
                    if (p.Address == 0)
                    {
                        updateModelRestart();
                        return;
                    }
                    p.SetValues(pc.getOneState());
                    pc.FreqData.SetStats(p);
                    if (p.Freq < MAX_FREQ / 4 - 1)
                    {
                        p.IncrementFreq(p.Freq);
                    }
                    else
                    {
                        p.Freq = MAX_FREQ - 4;
                    }
                    pc.FreqData.SummFreq = (p.Freq + initEsc + (ns > 3 ? 1 : 0));
                }
                cf = 2 * fs.Freq * (pc.FreqData.SummFreq + 6);
                sf = s0 + pc.FreqData.SummFreq;
                if (cf < 6 * sf)
                {
                    cf = 1 + (cf > sf ? 1 : 0) + (cf >= 4 * sf ? 1 : 0);
                    pc.FreqData.IncrementSummFreq(3);
                }
                else
                {
                    cf = 4 + (cf >= 9 * sf ? 1 : 0) + (cf >= 12 * sf ? 1 : 0) + (cf >= 15 * sf ? 1 : 0);
                    pc.FreqData.IncrementSummFreq(cf);
                }
                p.Address = pc.FreqData.GetStats() + ns1 * State.Size;
                p.SetSuccessor(successor);
                p.Symbol    = fs.Symbol;
                p.Freq      = cf;
                pc.NumStats = ++ns1;
            }

            int address = fs.GetSuccessor();

            maxContext.Address = address;
            minContext.Address = address;
            //TODO-----debug
            //		int pos = minContext.getFreqData().getStats();
            //		State a = new State(getHeap());
            //		a.Address=pos);
            //		pos+=State.size;
            //		a.Address=pos);
            //--dbg end
            return;
        }
コード例 #3
0
ファイル: PPMContext.cs プロジェクト: zipgenius/sharpcompress
        internal bool decodeSymbol1(ModelPPM model)
        {
            RangeCoder coder = model.Coder;

            coder.SubRange.Scale = freqData.SummFreq;
            State p = new State(model.Heap);

            p.Address = freqData.GetStats();
            int  i, HiCnt;
            long count = coder.CurrentCount;

            if (count >= coder.SubRange.Scale)
            {
                return(false);
            }
            if (count < (HiCnt = p.Freq))
            {
                coder.SubRange.HighCount = HiCnt;
                model.PrevSuccess        = (2 * HiCnt > coder.SubRange.Scale) ? 1 : 0;
                model.incRunLength(model.PrevSuccess);
                HiCnt += 4;
                model.FoundState.Address = p.Address;
                model.FoundState.Freq    = HiCnt;
                freqData.IncrementSummFreq(4);
                if (HiCnt > ModelPPM.MAX_FREQ)
                {
                    rescale(model);
                }
                coder.SubRange.LowCount = 0;
                return(true);
            }
            else
            {
                if (model.FoundState.Address == 0)
                {
                    return(false);
                }
            }
            model.PrevSuccess = 0;
            int numStats = NumStats;

            i = numStats - 1;
            while ((HiCnt += p.IncrementAddress().Freq) <= count)
            {
                if (--i == 0)
                {
                    model.HiBitsFlag         = model.getHB2Flag()[model.FoundState.Symbol];
                    coder.SubRange.LowCount  = HiCnt;
                    model.CharMask[p.Symbol] = model.EscCount;
                    model.NumMasked          = numStats;
                    i = numStats - 1;
                    model.FoundState.Address = 0;
                    do
                    {
                        model.CharMask[p.DecrementAddress().Symbol] = model.EscCount;
                    } while (--i != 0);
                    coder.SubRange.HighCount = coder.SubRange.Scale;
                    return(true);
                }
            }
            coder.SubRange.LowCount  = HiCnt - p.Freq;
            coder.SubRange.HighCount = HiCnt;
            update1(model, p.Address);
            return(true);
        }
コード例 #4
0
ファイル: PPMContext.cs プロジェクト: zipgenius/sharpcompress
        internal void rescale(ModelPPM model)
        {
            int OldNS = NumStats, i = NumStats - 1, Adder, EscFreq;
            // STATE* p1, * p;
            State p1   = new State(model.Heap);
            State p    = new State(model.Heap);
            State temp = new State(model.Heap);

            for (p.Address = model.FoundState.Address; p.Address != freqData.GetStats(); p.DecrementAddress())
            {
                temp.Address = p.Address - State.Size;
                State.PPMDSwap(p, temp);
            }
            temp.Address = freqData.GetStats();
            temp.IncrementFreq(4);
            freqData.IncrementSummFreq(4);
            EscFreq           = freqData.SummFreq - p.Freq;
            Adder             = (model.OrderFall != 0) ? 1 : 0;
            p.Freq            = Utility.URShift((p.Freq + Adder), 1);
            freqData.SummFreq = p.Freq;
            do
            {
                p.IncrementAddress();
                EscFreq -= p.Freq;
                p.Freq   = Utility.URShift((p.Freq + Adder), 1);
                freqData.IncrementSummFreq(p.Freq);
                temp.Address = p.Address - State.Size;
                if (p.Freq > temp.Freq)
                {
                    p1.Address = p.Address;
                    StateRef tmp = new StateRef();
                    tmp.Values = p1;
                    State temp2 = new State(model.Heap);
                    State temp3 = new State(model.Heap);
                    do
                    {
                        // p1[0]=p1[-1];
                        temp2.Address = p1.Address - State.Size;
                        p1.SetValues(temp2);
                        p1.DecrementAddress();
                        temp3.Address = p1.Address - State.Size;
                    } while (p1.Address != freqData.GetStats() && tmp.Freq > temp3.Freq);
                    p1.SetValues(tmp);
                }
            } while (--i != 0);
            if (p.Freq == 0)
            {
                do
                {
                    i++;
                    p.DecrementAddress();
                } while (p.Freq == 0);
                EscFreq += i;
                NumStats = NumStats - i;
                if (NumStats == 1)
                {
                    StateRef tmp = new StateRef();
                    temp.Address = freqData.GetStats();
                    tmp.Values   = temp;
                    // STATE tmp=*U.Stats;
                    do
                    {
                        // tmp.Freq-=(tmp.Freq >> 1)
                        tmp.DecrementFreq(Utility.URShift(tmp.Freq, 1));
                        EscFreq = Utility.URShift(EscFreq, 1);
                    } while (EscFreq > 1);
                    model.SubAlloc.freeUnits(freqData.GetStats(), Utility.URShift((OldNS + 1), 1));
                    oneState.SetValues(tmp);
                    model.FoundState.Address = oneState.Address;
                    return;
                }
            }
            EscFreq -= Utility.URShift(EscFreq, 1);
            freqData.IncrementSummFreq(EscFreq);
            int n0 = Utility.URShift((OldNS + 1), 1), n1 = Utility.URShift((NumStats + 1), 1);

            if (n0 != n1)
            {
                freqData.SetStats(model.SubAlloc.shrinkUnits(freqData.GetStats(), n0, n1));
            }
            model.FoundState.Address = freqData.GetStats();
        }
コード例 #5
0
ファイル: PPMContext.cs プロジェクト: nickchal/pash
        internal bool decodeSymbol1(ModelPPM model)
        {

            RangeCoder coder = model.Coder;
            coder.SubRange.Scale = freqData.SummFreq;
            State p = new State(model.Heap);
            p.Address = freqData.GetStats();
            int i, HiCnt;
            long count = coder.CurrentCount;
            if (count >= coder.SubRange.Scale)
            {
                return false;
            }
            if (count < (HiCnt = p.Freq))
            {
                coder.SubRange.HighCount = HiCnt;
                model.PrevSuccess = (2 * HiCnt > coder.SubRange.Scale) ? 1 : 0;
                model.incRunLength(model.PrevSuccess);
                HiCnt += 4;
                model.FoundState.Address = p.Address;
                model.FoundState.Freq = HiCnt;
                freqData.IncrementSummFreq(4);
                if (HiCnt > ModelPPM.MAX_FREQ)
                {
                    rescale(model);
                }
                coder.SubRange.LowCount = 0;
                return true;
            }
            else
            {
                if (model.FoundState.Address == 0)
                {
                    return (false);
                }
            }
            model.PrevSuccess = 0;
            int numStats = NumStats;
            i = numStats - 1;
            while ((HiCnt += p.IncrementAddress().Freq) <= count)
            {
                if (--i == 0)
                {
                    model.HiBitsFlag = model.getHB2Flag()[model.FoundState.Symbol];
                    coder.SubRange.LowCount = HiCnt;
                    model.CharMask[p.Symbol] = model.EscCount;
                    model.NumMasked = numStats;
                    i = numStats - 1;
                    model.FoundState.Address = 0;
                    do
                    {
                        model.CharMask[p.DecrementAddress().Symbol] = model.EscCount;
                    }
                    while (--i != 0);
                    coder.SubRange.HighCount = coder.SubRange.Scale;
                    return (true);
                }
            }
            coder.SubRange.LowCount = HiCnt - p.Freq;
            coder.SubRange.HighCount = HiCnt;
            update1(model, p.Address);
            return (true);
        }
コード例 #6
0
ファイル: PPMContext.cs プロジェクト: nickchal/pash
        internal void rescale(ModelPPM model)
        {
            int OldNS = NumStats, i = NumStats - 1, Adder, EscFreq;
            // STATE* p1, * p;
            State p1 = new State(model.Heap);
            State p = new State(model.Heap);
            State temp = new State(model.Heap);

            for (p.Address = model.FoundState.Address; p.Address != freqData.GetStats(); p.DecrementAddress())
            {
                temp.Address = p.Address - State.Size;
                State.PPMDSwap(p, temp);
            }
            temp.Address = freqData.GetStats();
            temp.IncrementFreq(4);
            freqData.IncrementSummFreq(4);
            EscFreq = freqData.SummFreq - p.Freq;
            Adder = (model.OrderFall != 0) ? 1 : 0;
            p.Freq = Utility.URShift((p.Freq + Adder), 1);
            freqData.SummFreq = p.Freq;
            do
            {
                p.IncrementAddress();
                EscFreq -= p.Freq;
                p.Freq = Utility.URShift((p.Freq + Adder), 1);
                freqData.IncrementSummFreq(p.Freq);
                temp.Address = p.Address - State.Size;
                if (p.Freq > temp.Freq)
                {
                    p1.Address = p.Address;
                    StateRef tmp = new StateRef();
                    tmp.Values = p1;
                    State temp2 = new State(model.Heap);
                    State temp3 = new State(model.Heap);
                    do
                    {
                        // p1[0]=p1[-1];
                        temp2.Address = p1.Address - State.Size;
                        p1.SetValues(temp2);
                        p1.DecrementAddress();
                        temp3.Address = p1.Address - State.Size;
                    }
                    while (p1.Address != freqData.GetStats() && tmp.Freq > temp3.Freq);
                    p1.SetValues(tmp);
                }
            }
            while (--i != 0);
            if (p.Freq == 0)
            {
                do
                {
                    i++;
                    p.DecrementAddress();
                }
                while (p.Freq == 0);
                EscFreq += i;
                NumStats = NumStats - i;
                if (NumStats == 1)
                {
                    StateRef tmp = new StateRef();
                    temp.Address = freqData.GetStats();
                    tmp.Values = temp;
                    // STATE tmp=*U.Stats;
                    do
                    {
                        // tmp.Freq-=(tmp.Freq >> 1)
                        tmp.DecrementFreq(Utility.URShift(tmp.Freq, 1));
                        EscFreq = Utility.URShift(EscFreq, 1);
                    }
                    while (EscFreq > 1);
                    model.SubAlloc.freeUnits(freqData.GetStats(), Utility.URShift((OldNS + 1), 1));
                    oneState.SetValues(tmp);
                    model.FoundState.Address = oneState.Address;
                    return;
                }
            }
            EscFreq -= Utility.URShift(EscFreq, 1);
            freqData.IncrementSummFreq(EscFreq);
            int n0 = Utility.URShift((OldNS + 1), 1), n1 = Utility.URShift((NumStats + 1), 1);
            if (n0 != n1)
            {
                freqData.SetStats(model.SubAlloc.shrinkUnits(freqData.GetStats(), n0, n1));
            }
            model.FoundState.Address = freqData.GetStats();
        }