Esempio n. 1
0
        public int DecodeSymbol(ArithmeticModel model)
        {
            int range;
            int cum;
            int symbol;

            range = (_highBound - _lowBound) + 1;
            cum   = (((_value - _lowBound) + 1) * model.CumulativeFrequency[0] - 1) / range;

            for (symbol = 0; model.CumulativeFrequency[symbol + 1] > cum; symbol++)
            {
                /* do nothing */
                ;
            }

            _highBound = _lowBound + (range * model.CumulativeFrequency[symbol]) / model.CumulativeFrequency[0] - 1;
            _lowBound  = _lowBound + (range * model.CumulativeFrequency[symbol + 1]) / model.CumulativeFrequency[0];

            for (; ;)
            {
                if (_highBound < ArithmeticCodecConstants.HalfValue)
                {
                    /* do nothing */
                }
                else if (_lowBound >= ArithmeticCodecConstants.HalfValue)
                {
                    _value     -= ArithmeticCodecConstants.HalfValue;
                    _lowBound  -= ArithmeticCodecConstants.HalfValue;
                    _highBound -= ArithmeticCodecConstants.HalfValue;
                }
                else if (_lowBound >= ArithmeticCodecConstants.FirstQuarterValue &&
                         _highBound < ArithmeticCodecConstants.ThirdQuarterValue)
                {
                    _value     -= ArithmeticCodecConstants.FirstQuarterValue;
                    _lowBound  -= ArithmeticCodecConstants.FirstQuarterValue;
                    _highBound -= ArithmeticCodecConstants.FirstQuarterValue;
                }
                else
                {
                    break;
                }

                _lowBound  = 2 * _lowBound;
                _highBound = 2 * _highBound + 1;
                _value     = 2 * _value + (InputBit() ? 1 : 0);
            }

            if (model.Adaptive)
            {
                model.UpdateModel(symbol);
            }

            return(symbol);
        }
        public void EncodeSymbol(ArithmeticModel model, int symbol)
        {
            int range = (_highBound - _lowBound) + 1;

            _highBound = _lowBound + (range * model.CumulativeFrequency[symbol]) / model.CumulativeFrequency[0] - 1;
            _lowBound  = _lowBound + (range * model.CumulativeFrequency[symbol + 1]) / model.CumulativeFrequency[0];

            for (; ;)
            {
                if (_highBound < ArithmeticCodecConstants.HalfValue)
                {
                    BitPlusFollow(false);
                }
                else if (_lowBound >= ArithmeticCodecConstants.HalfValue)
                {
                    BitPlusFollow(true);
                    _lowBound  -= ArithmeticCodecConstants.HalfValue;
                    _highBound -= ArithmeticCodecConstants.HalfValue;
                }
                else if (_lowBound >= ArithmeticCodecConstants.FirstQuarterValue && _highBound < ArithmeticCodecConstants.ThirdQuarterValue)
                {
                    _followBits += 1;
                    _lowBound   -= ArithmeticCodecConstants.FirstQuarterValue;
                    _highBound  -= ArithmeticCodecConstants.FirstQuarterValue;
                }
                else
                {
                    break;
                }

                _lowBound  = 2 * _lowBound;
                _highBound = 2 * _highBound + 1;
            }

            if (model.Adaptive)
            {
                model.UpdateModel(symbol);
            }
        }