This commit is contained in:
Igor Pavlov
2005-05-30 00:00:00 +00:00
committed by Kornel Lesiński
parent 8c1b5c7b7e
commit 3c510ba80b
926 changed files with 40559 additions and 23519 deletions

View File

@@ -1,9 +1,7 @@
// Compress/RangeCoder/RangeCoderBitTree.h
// #pragma once
#ifndef __COMPRESS_RANGECODER_BIT_H
#define __COMPRESS_RANGECODER_BIT_H
#ifndef __COMPRESS_RANGECODER_BIT_TREE_H
#define __COMPRESS_RANGECODER_BIT_TREE_H
#include "RangeCoderBit.h"
#include "RangeCoderOpt.h"
@@ -11,292 +9,152 @@
namespace NCompress {
namespace NRangeCoder {
/*
template <int numMoveBits> class CMyBitEncoder:
public NCompression::NArithmetic::CBitEncoder<numMoveBits> {};
template <int numMoveBits> class CMyBitDecoder:
public NCompression::NArithmetic::CBitDecoder<numMoveBits> {};
*/
//////////////////////////
// CBitTreeEncoder
template <int numMoveBits, UINT32 NumBitLevels>
template <int numMoveBits, int NumBitLevels>
class CBitTreeEncoder
{
CBitEncoder<numMoveBits> Models[1 << NumBitLevels];
public:
void Init()
{
for(UINT32 i = 1; i < (1 << NumBitLevels); i++)
for(UInt32 i = 1; i < (1 << NumBitLevels); i++)
Models[i].Init();
}
void Encode(CEncoder *rangeEncoder, UINT32 symbol)
void Encode(CEncoder *rangeEncoder, UInt32 symbol)
{
UINT32 modelIndex = 1;
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
UInt32 modelIndex = 1;
for (int bitIndex = NumBitLevels; bitIndex != 0 ;)
{
bitIndex--;
UINT32 bit = (symbol >> bitIndex ) & 1;
UInt32 bit = (symbol >> bitIndex) & 1;
Models[modelIndex].Encode(rangeEncoder, bit);
modelIndex = (modelIndex << 1) | bit;
}
};
UINT32 GetPrice(UINT32 symbol) const
void ReverseEncode(CEncoder *rangeEncoder, UInt32 symbol)
{
UINT32 price = 0;
UINT32 modelIndex = 1;
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
UInt32 modelIndex = 1;
for (int i = 0; i < NumBitLevels; i++)
{
bitIndex--;
UINT32 bit = (symbol >> bitIndex ) & 1;
UInt32 bit = symbol & 1;
Models[modelIndex].Encode(rangeEncoder, bit);
modelIndex = (modelIndex << 1) | bit;
symbol >>= 1;
}
}
UInt32 GetPrice(UInt32 symbol) const
{
symbol |= (1 << NumBitLevels);
UInt32 price = 0;
while (symbol != 1)
{
price += Models[symbol >> 1].GetPrice(symbol & 1);
symbol >>= 1;
}
return price;
}
UInt32 ReverseGetPrice(UInt32 symbol) const
{
UInt32 price = 0;
UInt32 modelIndex = 1;
for (int i = NumBitLevels; i != 0; i--)
{
UInt32 bit = symbol & 1;
symbol >>= 1;
price += Models[modelIndex].GetPrice(bit);
modelIndex = (modelIndex << 1) + bit;
modelIndex = (modelIndex << 1) | bit;
}
return price;
}
};
//////////////////////////
// CBitTreeDecoder
template <int numMoveBits, UINT32 NumBitLevels>
template <int numMoveBits, int NumBitLevels>
class CBitTreeDecoder
{
CBitDecoder<numMoveBits> Models[1 << NumBitLevels];
public:
void Init()
{
for(UINT32 i = 1; i < (1 << NumBitLevels); i++)
for(UInt32 i = 1; i < (1 << NumBitLevels); i++)
Models[i].Init();
}
UINT32 Decode(CDecoder *rangeDecoder)
UInt32 Decode(CDecoder *rangeDecoder)
{
UINT32 modelIndex = 1;
UInt32 modelIndex = 1;
RC_INIT_VAR
for(UINT32 bitIndex = NumBitLevels; bitIndex > 0; bitIndex--)
for(int bitIndex = NumBitLevels; bitIndex != 0; bitIndex--)
{
// modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder);
RC_GETBIT(numMoveBits, Models[modelIndex].Probability, modelIndex)
RC_GETBIT(numMoveBits, Models[modelIndex].Prob, modelIndex)
}
RC_FLUSH_VAR
return modelIndex - (1 << NumBitLevels);
};
};
////////////////////////////////
// CReverseBitTreeEncoder
template <int numMoveBits>
class CReverseBitTreeEncoder2
{
CBitEncoder<numMoveBits> *Models;
UINT32 NumBitLevels;
public:
CReverseBitTreeEncoder2(): Models(0) { }
~CReverseBitTreeEncoder2() { delete []Models; }
void Create(UINT32 numBitLevels)
UInt32 ReverseDecode(CDecoder *rangeDecoder)
{
NumBitLevels = numBitLevels;
Models = new CBitEncoder<numMoveBits>[1 << numBitLevels];
// return (Models != 0);
}
void Init()
{
UINT32 numModels = 1 << NumBitLevels;
for(UINT32 i = 1; i < numModels; i++)
Models[i].Init();
}
void Encode(CEncoder *rangeEncoder, UINT32 symbol)
{
UINT32 modelIndex = 1;
for (UINT32 i = 0; i < NumBitLevels; i++)
{
UINT32 bit = symbol & 1;
Models[modelIndex].Encode(rangeEncoder, bit);
modelIndex = (modelIndex << 1) | bit;
symbol >>= 1;
}
}
UINT32 GetPrice(UINT32 symbol) const
{
UINT32 price = 0;
UINT32 modelIndex = 1;
for (UINT32 i = NumBitLevels; i > 0; i--)
{
UINT32 bit = symbol & 1;
symbol >>= 1;
price += Models[modelIndex].GetPrice(bit);
modelIndex = (modelIndex << 1) | bit;
}
return price;
}
};
/*
template <int numMoveBits, int numBitLevels>
class CReverseBitTreeEncoder: public CReverseBitTreeEncoder2<numMoveBits>
{
public:
CReverseBitTreeEncoder()
{ Create(numBitLevels); }
};
*/
////////////////////////////////
// CReverseBitTreeDecoder
template <int numMoveBits>
class CReverseBitTreeDecoder2
{
CBitDecoder<numMoveBits> *Models;
UINT32 NumBitLevels;
public:
CReverseBitTreeDecoder2(): Models(0) { }
~CReverseBitTreeDecoder2() { delete []Models; }
void Create(UINT32 numBitLevels)
{
NumBitLevels = numBitLevels;
Models = new CBitDecoder<numMoveBits>[1 << numBitLevels];
// return (Models != 0);
}
void Init()
{
UINT32 numModels = 1 << NumBitLevels;
for(UINT32 i = 1; i < numModels; i++)
Models[i].Init();
}
UINT32 Decode(CDecoder *rangeDecoder)
{
UINT32 modelIndex = 1;
UINT32 symbol = 0;
UInt32 modelIndex = 1;
UInt32 symbol = 0;
RC_INIT_VAR
for(UINT32 bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
{
// UINT32 bit = Models[modelIndex].Decode(rangeDecoder);
// UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
// modelIndex <<= 1;
// modelIndex += bit;
// symbol |= (bit << bitIndex);
RC_GETBIT2(numMoveBits, Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << bitIndex))
}
RC_FLUSH_VAR
return symbol;
};
};
////////////////////////////
// CReverseBitTreeDecoder2
template <int numMoveBits, UINT32 NumBitLevels>
class CReverseBitTreeDecoder
{
CBitDecoder<numMoveBits> Models[1 << NumBitLevels];
public:
void Init()
{
for(UINT32 i = 1; i < (1 << NumBitLevels); i++)
Models[i].Init();
}
UINT32 Decode(CDecoder *rangeDecoder)
{
UINT32 modelIndex = 1;
UINT32 symbol = 0;
RC_INIT_VAR
for(UINT32 bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
{
// UINT32 bit = Models[modelIndex].Decode(rangeDecoder);
// modelIndex <<= 1;
// modelIndex += bit;
// symbol |= (bit << bitIndex);
RC_GETBIT2(numMoveBits, Models[modelIndex].Probability, modelIndex, ; , symbol |= (1 << bitIndex))
RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex))
}
RC_FLUSH_VAR
return symbol;
}
};
/*
//////////////////////////
// CBitTreeEncoder2
template <int numMoveBits>
void ReverseBitTreeEncode(CBitEncoder<numMoveBits> *Models,
CEncoder *rangeEncoder, int NumBitLevels, UInt32 symbol)
{
UInt32 modelIndex = 1;
for (int i = 0; i < NumBitLevels; i++)
{
UInt32 bit = symbol & 1;
Models[modelIndex].Encode(rangeEncoder, bit);
modelIndex = (modelIndex << 1) | bit;
symbol >>= 1;
}
}
template <int numMoveBits>
class CBitTreeEncoder2
UInt32 ReverseBitTreeGetPrice(CBitEncoder<numMoveBits> *Models,
UInt32 NumBitLevels, UInt32 symbol)
{
NCompression::NArithmetic::CBitEncoder<numMoveBits> *Models;
UINT32 NumBitLevels;
public:
bool Create(UINT32 numBitLevels)
{
NumBitLevels = numBitLevels;
Models = new NCompression::NArithmetic::CBitEncoder<numMoveBits>[1 << numBitLevels];
return (Models != 0);
}
void Init()
UInt32 price = 0;
UInt32 modelIndex = 1;
for (int i = NumBitLevels; i != 0; i--)
{
UINT32 numModels = 1 << NumBitLevels;
for(UINT32 i = 1; i < numModels; i++)
Models[i].Init();
UInt32 bit = symbol & 1;
symbol >>= 1;
price += Models[modelIndex].GetPrice(bit);
modelIndex = (modelIndex << 1) | bit;
}
void Encode(CMyRangeEncoder *rangeEncoder, UINT32 symbol)
{
UINT32 modelIndex = 1;
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
{
bitIndex--;
UINT32 bit = (symbol >> bitIndex ) & 1;
Models[modelIndex].Encode(rangeEncoder, bit);
modelIndex = (modelIndex << 1) | bit;
}
}
UINT32 GetPrice(UINT32 symbol) const
{
UINT32 price = 0;
UINT32 modelIndex = 1;
for (UINT32 bitIndex = NumBitLevels; bitIndex > 0 ;)
{
bitIndex--;
UINT32 bit = (symbol >> bitIndex ) & 1;
price += Models[modelIndex].GetPrice(bit);
modelIndex = (modelIndex << 1) + bit;
}
return price;
}
};
//////////////////////////
// CBitTreeDecoder2
return price;
}
template <int numMoveBits>
class CBitTreeDecoder2
UInt32 ReverseBitTreeDecode(CBitDecoder<numMoveBits> *Models,
CDecoder *rangeDecoder, int NumBitLevels)
{
NCompression::NArithmetic::CBitDecoder<numMoveBits> *Models;
UINT32 NumBitLevels;
public:
bool Create(UINT32 numBitLevels)
{
NumBitLevels = numBitLevels;
Models = new NCompression::NArithmetic::CBitDecoder<numMoveBits>[1 << numBitLevels];
return (Models != 0);
}
void Init()
UInt32 modelIndex = 1;
UInt32 symbol = 0;
RC_INIT_VAR
for(int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
{
UINT32 numModels = 1 << NumBitLevels;
for(UINT32 i = 1; i < numModels; i++)
Models[i].Init();
// UInt32 bit = Models[modelIndex].Decode(rangeDecoder);
// modelIndex <<= 1;
// modelIndex += bit;
// symbol |= (bit << bitIndex);
RC_GETBIT2(numMoveBits, Models[modelIndex].Prob, modelIndex, ; , symbol |= (1 << bitIndex))
}
UINT32 Decode(CMyRangeDecoder *rangeDecoder)
{
UINT32 modelIndex = 1;
RC_INIT_VAR
for(UINT32 bitIndex = NumBitLevels; bitIndex > 0; bitIndex--)
{
// modelIndex = (modelIndex << 1) + Models[modelIndex].Decode(rangeDecoder);
RC_GETBIT(numMoveBits, Models[modelIndex].Probability, modelIndex)
}
RC_FLUSH_VAR
return modelIndex - (1 << NumBitLevels);
}
};
*/
RC_FLUSH_VAR
return symbol;
}
}}