示例#1
0
 internal void SetValues(State ptr)
 {
     Array.Copy(ptr.Memory, ptr.Address, Memory, Address, Size);
 }
示例#2
0
 internal static void PPMDSwap(State ptr1, State ptr2)
 {
     byte[] mem1 = ptr1.Memory, mem2 = ptr2.Memory;
     for (int i = 0, pos1 = ptr1.Address, pos2 = ptr2.Address; i < Size; i++, pos1++, pos2++)
     {
         byte temp = mem1[pos1];
         mem1[pos1] = mem2[pos2];
         mem2[pos2] = temp;
     }
 }
示例#3
0
        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
 public PPMContext(byte[] Memory)
     : base(Memory)
 {
     oneState = new State(Memory);
     freqData = new FreqData(Memory);
 }
示例#5
0
        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 = Utilities.URShift((p.Freq + Adder), 1);
            freqData.SummFreq = p.Freq;
            do
            {
                p.IncrementAddress();
                EscFreq -= p.Freq;
                p.Freq = Utilities.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(Utilities.URShift(tmp.Freq, 1));
                        EscFreq = Utilities.URShift(EscFreq, 1);
                    }
                    while (EscFreq > 1);
                    model.SubAlloc.freeUnits(freqData.GetStats(), Utilities.URShift((OldNS + 1), 1));
                    oneState.SetValues(tmp);
                    model.FoundState.Address = oneState.Address;
                    return;
                }
            }
            EscFreq -= Utilities.URShift(EscFreq, 1);
            freqData.IncrementSummFreq(EscFreq);
            int n0 = Utilities.URShift((OldNS + 1), 1), n1 = Utilities.URShift((NumStats + 1), 1);
            if (n0 != n1)
            {
                freqData.SetStats(model.SubAlloc.shrinkUnits(freqData.GetStats(), n0, n1));
            }
            model.FoundState.Address = freqData.GetStats();
        }
示例#6
0
 private int getArrayIndex(ModelPPM Model, State rs)
 {
     PPMContext tempSuffix = getTempPPMContext(Model.SubAlloc.Heap);
     tempSuffix.Address = getSuffix();
     int ret = 0;
     ret += Model.PrevSuccess;
     ret += Model.getNS2BSIndx()[tempSuffix.NumStats - 1];
     ret += Model.HiBitsFlag + 2 * Model.getHB2Flag()[rs.Symbol];
     ret += ((Utilities.URShift(Model.RunLength, 26)) & 0x20);
     return ret;
 }
示例#7
0
 internal int createChild(ModelPPM model, State pStats, StateRef firstState)
 {
     PPMContext pc = getTempPPMContext(model.SubAlloc.Heap);
     pc.Address = model.SubAlloc.allocContext();
     if (pc != null)
     {
         pc.NumStats = 1;
         pc.setOneState(firstState);
         pc.setSuffix(this);
         pStats.SetSuccessor(pc);
     }
     return pc.Address;
 }
示例#8
0
 internal virtual void SetStats(State state)
 {
     SetStats(state.Address);
 }
示例#9
0
        private int createSuccessors(bool Skip, State p1)
        {
            //State upState = tempState1.Initialize(null);
            StateRef upState = tempStateRef2;
            State tempState = tempState1.Initialize(Heap);

            // PPM_CONTEXT* pc=MinContext, * UpBranch=FoundState->Successor;
            PPMContext pc = tempPPMContext1.Initialize(Heap);
            pc.Address = minContext.Address;
            PPMContext upBranch = tempPPMContext2.Initialize(Heap);
            upBranch.Address = foundState.GetSuccessor();

            // STATE * p, * ps[MAX_O], ** pps=ps;
            State p = tempState2.Initialize(Heap);
            int pps = 0;

            bool noLoop = false;

            if (!Skip)
            {
                ps[pps++] = foundState.Address; // *pps++ = FoundState;
                if (pc.getSuffix() == 0)
                {
                    noLoop = true;
                }
            }
            if (!noLoop)
            {
                bool loopEntry = false;
                if (p1.Address != 0)
                {
                    p.Address = p1.Address;
                    pc.Address = pc.getSuffix(); // =pc->Suffix;
                    loopEntry = true;
                }
                do
                {
                    if (!loopEntry)
                    {
                        pc.Address = pc.getSuffix(); // pc=pc->Suffix;
                        if (pc.NumStats != 1)
                        {
                            p.Address = pc.FreqData.GetStats(); // p=pc->U.Stats
                            if (p.Symbol != foundState.Symbol)
                            {
                                do
                                {
                                    p.IncrementAddress();
                                }
                                while (p.Symbol != foundState.Symbol);
                            }
                        }
                        else
                        {
                            p.Address = pc.getOneState().Address; // p=&(pc->OneState);
                        }
                    } // LOOP_ENTRY:
                    loopEntry = false;
                    if (p.GetSuccessor() != upBranch.Address)
                    {
                        pc.Address = p.GetSuccessor(); // =p->Successor;
                        break;
                    }
                    ps[pps++] = p.Address;
                }
                while (pc.getSuffix() != 0);
            } // NO_LOOP:
            if (pps == 0)
            {
                return pc.Address;
            }
            upState.Symbol = Heap[upBranch.Address]; // UpState.Symbol=*(byte*)
            // UpBranch;
            // UpState.Successor=(PPM_CONTEXT*) (((byte*) UpBranch)+1);
            upState.SetSuccessor(upBranch.Address + 1); //TODO check if +1 necessary
            if (pc.NumStats != 1)
            {
                if (pc.Address <= subAlloc.PText)
                {
                    return (0);
                }
                p.Address = pc.FreqData.GetStats();
                if (p.Symbol != upState.Symbol)
                {
                    do
                    {
                        p.IncrementAddress();
                    }
                    while (p.Symbol != upState.Symbol);
                }
                int cf = p.Freq - 1;
                int s0 = pc.FreqData.SummFreq - pc.NumStats - cf;
                // UpState.Freq=1+((2*cf <= s0)?(5*cf > s0):((2*cf+3*s0-1)/(2*s0)));
                upState.Freq = 1 + ((2 * cf <= s0) ? (5 * cf > s0 ? 1 : 0) : ((2 * cf + 3 * s0 - 1) / (2 * s0)));
            }
            else
            {
                upState.Freq = pc.getOneState().Freq; // UpState.Freq=pc->OneState.Freq;
            }
            do
            {
                // pc = pc->createChild(this,*--pps,UpState);
                tempState.Address = ps[--pps];
                pc.Address = pc.createChild(this, tempState, upState);
                if (pc.Address == 0)
                {
                    return 0;
                }
            }
            while (pps != 0);
            return pc.Address;
        }
示例#10
0
        internal bool decodeInit(Unpack unpackRead, int escChar)
        {

            int MaxOrder = unpackRead.Char & 0xff;
            bool reset = ((MaxOrder & 0x20) != 0);

            int MaxMB = 0;
            if (reset)
            {
                MaxMB = unpackRead.Char;
            }
            else
            {
                if (subAlloc.GetAllocatedMemory() == 0)
                {
                    return (false);
                }
            }
            if ((MaxOrder & 0x40) != 0)
            {
                escChar = unpackRead.Char;
                unpackRead.PpmEscChar = escChar;
            }
            coder = new RangeCoder(unpackRead);
            if (reset)
            {
                MaxOrder = (MaxOrder & 0x1f) + 1;
                if (MaxOrder > 16)
                {
                    MaxOrder = 16 + (MaxOrder - 16) * 3;
                }
                if (MaxOrder == 1)
                {
                    subAlloc.stopSubAllocator();
                    return (false);
                }
                subAlloc.startSubAllocator(MaxMB + 1);
                minContext = new PPMContext(Heap);
                //medContext = new PPMContext(Heap);
                maxContext = new PPMContext(Heap);
                foundState = new State(Heap);
                dummySEE2Cont = new SEE2Context();
                for (int i = 0; i < 25; i++)
                {
                    for (int j = 0; j < 16; j++)
                    {
                        SEE2Cont[i][j] = new SEE2Context();
                    }
                }
                startModelRare(MaxOrder);
            }
            return (minContext.Address != 0);
        }
示例#11
0
        private void restartModelRare()
        {
            Utilities.Fill(charMask, 0);
            subAlloc.initSubAllocator();
            initRL = -(maxOrder < 12 ? maxOrder : 12) - 1;
            int addr = subAlloc.allocContext();
            minContext.Address = addr;
            maxContext.Address = addr;
            minContext.setSuffix(0);
            orderFall = maxOrder;
            minContext.NumStats = 256;
            minContext.FreqData.SummFreq = minContext.NumStats + 1;

            addr = subAlloc.allocUnits(256 / 2);
            foundState.Address = addr;
            minContext.FreqData.SetStats(addr);

            State state = new State(subAlloc.Heap);
            addr = minContext.FreqData.GetStats();
            runLength = initRL;
            prevSuccess = 0;
            for (int i = 0; i < 256; i++)
            {
                state.Address = addr + i * State.Size;
                state.Symbol = i;
                state.Freq = 1;
                state.SetSuccessor(0);
            }

            for (int i = 0; i < 128; i++)
            {
                for (int k = 0; k < 8; k++)
                {
                    for (int m = 0; m < 64; m += 8)
                    {
                        binSumm[i][k + m] = BIN_SCALE - InitBinEsc[k] / (i + 2);
                    }
                }
            }
            for (int i = 0; i < 25; i++)
            {
                for (int k = 0; k < 16; k++)
                {
                    SEE2Cont[i][k].Initialize(5 * i + 10);
                }
            }
        }