Example #1
0
        private void DecodeBinarySymbol(PpmContext context)
        {
            PpmState state  = context.FirstState;
            int      index1 = probabilities[state.Frequency - 1];
            int      index2 = numberStatisticsToBinarySummaryIndex[context.Suffix.NumberStatistics] + previousSuccess +
                              context.Flags + ((runLength >> 26) & 0x20);

            if (Coder.RangeGetCurrentShiftCount(TotalBitCount) < binarySummary[index1, index2])
            {
                foundState       = state;
                state.Frequency += (byte)((state.Frequency < 196) ? 1 : 0);
                Coder.LowCount   = 0;
                Coder.HighCount  = binarySummary[index1, index2];
                binarySummary[index1, index2] +=
                    (ushort)(Interval - Mean(binarySummary[index1, index2], PeriodBitCount, 2));
                previousSuccess = 1;
                runLength++;
            }
            else
            {
                Coder.LowCount = binarySummary[index1, index2];
                binarySummary[index1, index2] -= (ushort)Mean(binarySummary[index1, index2], PeriodBitCount, 2);
                Coder.HighCount             = BinaryScale;
                initialEscape               = ExponentialEscapes[binarySummary[index1, index2] >> 10];
                characterMask[state.Symbol] = escapeCount;
                previousSuccess             = 0;
                numberMasked = 0;
                foundState   = PpmState.Zero;
            }
        }
Example #2
0
        private void DecodeBinarySymbol(PpmContext context)
        {
            PpmState state  = context.FirstState;
            int      index1 = _probabilities[state.Frequency - 1];
            int      index2 = _numberStatisticsToBinarySummaryIndex[context.Suffix.NumberStatistics] + _previousSuccess +
                              context.Flags + ((_runLength >> 26) & 0x20);

            if (_coder.RangeGetCurrentShiftCount(TOTAL_BIT_COUNT) < _binarySummary[index1, index2])
            {
                _foundState       = state;
                state.Frequency  += (byte)((state.Frequency < 196) ? 1 : 0);
                _coder._lowCount  = 0;
                _coder._highCount = _binarySummary[index1, index2];
                _binarySummary[index1, index2] +=
                    (ushort)(INTERVAL - Mean(_binarySummary[index1, index2], PERIOD_BIT_COUNT, 2));
                _previousSuccess = 1;
                _runLength++;
            }
            else
            {
                _coder._lowCount = _binarySummary[index1, index2];
                _binarySummary[index1, index2] -= (ushort)Mean(_binarySummary[index1, index2], PERIOD_BIT_COUNT, 2);
                _coder._highCount            = BINARY_SCALE;
                _initialEscape               = EXPONENTIAL_ESCAPES[_binarySummary[index1, index2] >> 10];
                _characterMask[state.Symbol] = _escapeCount;
                _previousSuccess             = 0;
                _numberMasked = 0;
                _foundState   = PpmState.ZERO;
            }
        }
Example #3
0
        private void Refresh(uint oldUnitCount, bool scale, PpmContext context)
        {
            int index = context.NumberStatistics;
            int escapeFrequency;
            int scaleValue = (scale ? 1 : 0);

            context.Statistics = _allocator.ShrinkUnits(context.Statistics, oldUnitCount, (uint)((index + 2) >> 1));
            PpmState statistics = context.Statistics;

            context.Flags =
                (byte)((context.Flags & (0x10 + (scale ? 0x04 : 0x00))) + ((statistics.Symbol >= 0x40) ? 0x08 : 0x00));
            escapeFrequency          = context.SummaryFrequency - statistics.Frequency;
            statistics.Frequency     = (byte)((statistics.Frequency + scaleValue) >> scaleValue);
            context.SummaryFrequency = statistics.Frequency;

            do
            {
                escapeFrequency          -= (++statistics).Frequency;
                statistics.Frequency      = (byte)((statistics.Frequency + scaleValue) >> scaleValue);
                context.SummaryFrequency += statistics.Frequency;
                context.Flags            |= (byte)((statistics.Symbol >= 0x40) ? 0x08 : 0x00);
            }while (--index != 0);

            escapeFrequency           = (escapeFrequency + scaleValue) >> scaleValue;
            context.SummaryFrequency += (ushort)escapeFrequency;
        }
Example #4
0
 /// <summary>
 /// Indicates whether this instance and a specified object are equal.
 /// </summary>
 /// <returns>true if obj and this instance are the same type and represent the same value; otherwise, false.</returns>
 /// <param name="obj">Another object to compare to.</param>
 public override bool Equals(object obj)
 {
     if (obj is PpmState)
     {
         PpmState state = (PpmState)obj;
         return(state.Address == Address);
     }
     return(base.Equals(obj));
 }
Example #5
0
 private void Update2(PpmState state, PpmContext context)
 {
     _foundState               = state;
     _foundState.Frequency    += 4;
     context.SummaryFrequency += 4;
     if (state.Frequency > MAXIMUM_FREQUENCY)
     {
         Rescale(context);
     }
     _escapeCount++;
     _runLength = _initialRunLength;
 }
Example #6
0
        private void DecodeSymbol2(PpmContext context)
        {
            See2Context see2Context = MakeEscapeFrequency(context);
            uint        currentSymbol;
            uint        count;
            uint        highCount  = 0;
            uint        index      = (uint)(context.NumberStatistics - _numberMasked);
            uint        stateIndex = 0;
            PpmState    state      = context.Statistics - 1;

            do
            {
                do
                {
                    currentSymbol = state[1].Symbol;
                    state++;
                }while (_characterMask[currentSymbol] == _escapeCount);
                highCount += state.Frequency;
                _decodeStates[stateIndex++] = state;

                // note that decodeStates is a static array that is re-used on each call to this method (for performance reasons)
            }while (--index != 0);

            _coder._scale += highCount;
            count          = _coder.RangeGetCurrentCount();
            stateIndex     = 0;
            state          = _decodeStates[stateIndex];
            if (count < highCount)
            {
                highCount = 0;
                while ((highCount += state.Frequency) <= count)
                {
                    state = _decodeStates[++stateIndex];
                }
                _coder._highCount = highCount;
                _coder._lowCount  = _coder._highCount - state.Frequency;
                see2Context.Update();
                Update2(state, context);
            }
            else
            {
                _coder._lowCount  = highCount;
                _coder._highCount = _coder._scale;
                index             = (uint)(context.NumberStatistics - _numberMasked);
                _numberMasked     = context.NumberStatistics;
                do
                {
                    _characterMask[_decodeStates[stateIndex].Symbol] = _escapeCount;
                    stateIndex++;
                }while (--index != 0);
                see2Context._summary += (ushort)_coder._scale;
            }
        }
Example #7
0
 private void Update2(PpmState state, PpmContext context)
 {
     foundState                = state;
     foundState.Frequency     += 4;
     context.SummaryFrequency += 4;
     if (state.Frequency > MaximumFrequency)
     {
         Rescale(context);
     }
     escapeCount++;
     runLength = initialRunLength;
 }
Example #8
0
        private void DecodeSymbol1(PpmContext context)
        {
            uint     index;
            uint     count;
            uint     highCount = context.Statistics.Frequency;
            PpmState state     = context.Statistics;

            _coder._scale = context.SummaryFrequency;

            count = _coder.RangeGetCurrentCount();
            if (count < highCount)
            {
                _coder._highCount         = highCount;
                _previousSuccess          = (byte)((2 * _coder._highCount >= _coder._scale) ? 1 : 0);
                _foundState               = state;
                highCount                += 4;
                _foundState.Frequency     = (byte)highCount;
                context.SummaryFrequency += 4;
                _runLength               += _previousSuccess;
                if (highCount > MAXIMUM_FREQUENCY)
                {
                    Rescale(context);
                }
                _coder._lowCount = 0;
                return;
            }

            index            = context.NumberStatistics;
            _previousSuccess = 0;
            while ((highCount += (++state).Frequency) <= count)
            {
                if (--index == 0)
                {
                    _coder._lowCount             = highCount;
                    _characterMask[state.Symbol] = _escapeCount;
                    _numberMasked = context.NumberStatistics;
                    index         = context.NumberStatistics;
                    _foundState   = PpmState.ZERO;
                    do
                    {
                        _characterMask[(--state).Symbol] = _escapeCount;
                    }while (--index != 0);
                    _coder._highCount = _coder._scale;
                    return;
                }
            }
            _coder._highCount = highCount;
            _coder._lowCount  = _coder._highCount - state.Frequency;
            Update1(state, context);
        }
Example #9
0
        private void DecodeSymbol1(PpmContext context)
        {
            uint     index;
            uint     count;
            uint     highCount = context.Statistics.Frequency;
            PpmState state     = context.Statistics;

            Coder.Scale = context.SummaryFrequency;

            count = Coder.RangeGetCurrentCount();
            if (count < highCount)
            {
                Coder.HighCount           = highCount;
                previousSuccess           = (byte)((2 * Coder.HighCount >= Coder.Scale) ? 1 : 0);
                foundState                = state;
                highCount                += 4;
                foundState.Frequency      = (byte)highCount;
                context.SummaryFrequency += 4;
                runLength                += previousSuccess;
                if (highCount > MaximumFrequency)
                {
                    Rescale(context);
                }
                Coder.LowCount = 0;
                return;
            }

            index           = context.NumberStatistics;
            previousSuccess = 0;
            while ((highCount += (++state).Frequency) <= count)
            {
                if (--index == 0)
                {
                    Coder.LowCount = highCount;
                    characterMask[state.Symbol] = escapeCount;
                    numberMasked = context.NumberStatistics;
                    index        = context.NumberStatistics;
                    foundState   = PpmState.Zero;
                    do
                    {
                        characterMask[(--state).Symbol] = escapeCount;
                    }while (--index != 0);
                    Coder.HighCount = Coder.Scale;
                    return;
                }
            }
            Coder.HighCount = highCount;
            Coder.LowCount  = Coder.HighCount - state.Frequency;
            Update1(state, context);
        }
Example #10
0
 private void Update1(PpmState state, PpmContext context)
 {
     _foundState               = state;
     _foundState.Frequency    += 4;
     context.SummaryFrequency += 4;
     if (state[0].Frequency > state[-1].Frequency)
     {
         Swap(state[0], state[-1]);
         _foundState = --state;
         if (state.Frequency > MAXIMUM_FREQUENCY)
         {
             Rescale(context);
         }
     }
 }
Example #11
0
 private void Update1(PpmState state, PpmContext context)
 {
     foundState                = state;
     foundState.Frequency     += 4;
     context.SummaryFrequency += 4;
     if (state[0].Frequency > state[-1].Frequency)
     {
         Swap(state[0], state[-1]);
         foundState = --state;
         if (state.Frequency > MaximumFrequency)
         {
             Rescale(context);
         }
     }
 }
Example #12
0
        private void EncodeSymbol2(int symbol, PpmContext context)
        {
            See2Context see2Context = MakeEscapeFrequency(context);
            uint        currentSymbol;
            uint        lowCount = 0;
            uint        index    = (uint)(context.NumberStatistics - _numberMasked);
            PpmState    state    = context.Statistics - 1;

            do
            {
                do
                {
                    currentSymbol = state[1].Symbol;
                    state++;
                }while (_characterMask[currentSymbol] == _escapeCount);
                _characterMask[currentSymbol] = _escapeCount;
                if (currentSymbol == symbol)
                {
                    goto SymbolFound;
                }
                lowCount += state.Frequency;
            }while (--index != 0);

            _coder._lowCount      = lowCount;
            _coder._scale        += _coder._lowCount;
            _coder._highCount     = _coder._scale;
            see2Context._summary += (ushort)_coder._scale;
            _numberMasked         = context.NumberStatistics;
            return;

SymbolFound:
            _coder._lowCount  = lowCount;
            lowCount         += state.Frequency;
            _coder._highCount = lowCount;
            for (PpmState p1 = state; --index != 0;)
            {
                do
                {
                    currentSymbol = p1[1].Symbol;
                    p1++;
                }while (_characterMask[currentSymbol] == _escapeCount);
                lowCount += p1.Frequency;
            }
            _coder._scale += lowCount;
            see2Context.Update();
            Update2(state, context);
        }
Example #13
0
        private void EncodeSymbol1(int symbol, PpmContext context)
        {
            uint     lowCount;
            uint     index = context.Statistics.Symbol;
            PpmState state = context.Statistics;

            _coder._scale = context.SummaryFrequency;
            if (index == symbol)
            {
                _coder._highCount         = state.Frequency;
                _previousSuccess          = (byte)((2 * _coder._highCount >= _coder._scale) ? 1 : 0);
                _foundState               = state;
                _foundState.Frequency    += 4;
                context.SummaryFrequency += 4;
                _runLength += _previousSuccess;
                if (state.Frequency > MAXIMUM_FREQUENCY)
                {
                    Rescale(context);
                }
                _coder._lowCount = 0;
                return;
            }

            lowCount         = state.Frequency;
            index            = context.NumberStatistics;
            _previousSuccess = 0;
            while ((++state).Symbol != symbol)
            {
                lowCount += state.Frequency;
                if (--index == 0)
                {
                    _coder._lowCount             = lowCount;
                    _characterMask[state.Symbol] = _escapeCount;
                    _numberMasked = context.NumberStatistics;
                    index         = context.NumberStatistics;
                    _foundState   = PpmState.ZERO;
                    do
                    {
                        _characterMask[(--state).Symbol] = _escapeCount;
                    }while (--index != 0);
                    _coder._highCount = _coder._scale;
                    return;
                }
            }
            _coder._highCount = (_coder._lowCount = lowCount) + state.Frequency;
            Update1(state, context);
        }
Example #14
0
        private void EncodeSymbol1(int symbol, PpmContext context)
        {
            uint     lowCount;
            uint     index = context.Statistics.Symbol;
            PpmState state = context.Statistics;

            Coder.Scale = context.SummaryFrequency;
            if (index == symbol)
            {
                Coder.HighCount           = state.Frequency;
                previousSuccess           = (byte)((2 * Coder.HighCount >= Coder.Scale) ? 1 : 0);
                foundState                = state;
                foundState.Frequency     += 4;
                context.SummaryFrequency += 4;
                runLength += previousSuccess;
                if (state.Frequency > MaximumFrequency)
                {
                    Rescale(context);
                }
                Coder.LowCount = 0;
                return;
            }

            lowCount        = state.Frequency;
            index           = context.NumberStatistics;
            previousSuccess = 0;
            while ((++state).Symbol != symbol)
            {
                lowCount += state.Frequency;
                if (--index == 0)
                {
                    Coder.LowCount = lowCount;
                    characterMask[state.Symbol] = escapeCount;
                    numberMasked = context.NumberStatistics;
                    index        = context.NumberStatistics;
                    foundState   = PpmState.Zero;
                    do
                    {
                        characterMask[(--state).Symbol] = escapeCount;
                    }while (--index != 0);
                    Coder.HighCount = Coder.Scale;
                    return;
                }
            }
            Coder.HighCount = (Coder.LowCount = lowCount) + state.Frequency;
            Update1(state, context);
        }
Example #15
0
        private PpmContext RemoveBinaryContexts(int order, PpmContext context)
        {
            if (context.NumberStatistics == 0)
            {
                PpmState state = context.FirstState;
                if ((Pointer)state.Successor >= Allocator.BaseUnit && order < modelOrder)
                {
                    state.Successor = RemoveBinaryContexts(order + 1, state.Successor);
                }
                else
                {
                    state.Successor = PpmContext.Zero;
                }
                if ((state.Successor == PpmContext.Zero) &&
                    (context.Suffix.NumberStatistics == 0 || context.Suffix.Flags == 0xff))
                {
                    Allocator.FreeUnits(context, 1);
                    return(PpmContext.Zero);
                }
                else
                {
                    return(context);
                }
            }

            for (PpmState state = context.Statistics + context.NumberStatistics; state >= context.Statistics; state--)
            {
                if ((Pointer)state.Successor >= Allocator.BaseUnit && order < modelOrder)
                {
                    state.Successor = RemoveBinaryContexts(order + 1, state.Successor);
                }
                else
                {
                    state.Successor = PpmContext.Zero;
                }
            }

            return(context);
        }
Example #16
0
 private void Update2(PpmState state, PpmContext context)
 {
     foundState = state;
     foundState.Frequency += 4;
     context.SummaryFrequency += 4;
     if (state.Frequency > MaximumFrequency)
         Rescale(context);
     escapeCount++;
     runLength = initialRunLength;
 }
Example #17
0
 private void Update1(PpmState state, PpmContext context)
 {
     foundState = state;
     foundState.Frequency += 4;
     context.SummaryFrequency += 4;
     if (state[0].Frequency > state[-1].Frequency)
     {
         Swap(state[0], state[-1]);
         foundState = --state;
         if (state.Frequency > MaximumFrequency)
             Rescale(context);
     }
 }