Path: blob/main/misc/emulator/xnes/snes9x/jma/btreecd.h
28798 views
/*1Copyright (C) 2002 Andrea Mazzoleni ( http://advancemame.sf.net )2Copyright (C) 2001-4 Igor Pavlov ( http://www.7-zip.org )34This library is free software; you can redistribute it and/or5modify it under the terms of the GNU Lesser General Public6License version 2.1 as published by the Free Software Foundation.78This library is distributed in the hope that it will be useful,9but WITHOUT ANY WARRANTY; without even the implied warranty of10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11Lesser General Public License for more details.1213You should have received a copy of the GNU Lesser General Public14License along with this library; if not, write to the Free Software15Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA16*/1718#ifndef __BITTREECODER_H19#define __BITTREECODER_H2021#include "aribitcd.h"22#include "rcdefs.h"232425//////////////////////////26// CBitTreeDecoder2728template <int aNumMoveBits, UINT32 m_NumBitLevels>29class CBitTreeDecoder30{31CMyBitDecoder<aNumMoveBits> m_Models[1 << m_NumBitLevels];32public:33void Init()34{35for(UINT32 i = 1; i < (1 << m_NumBitLevels); i++)36m_Models[i].Init();37}38UINT32 Decode(CMyRangeDecoder *aRangeDecoder)39{40UINT32 aModelIndex = 1;41RC_INIT_VAR42for(UINT32 aBitIndex = m_NumBitLevels; aBitIndex > 0; aBitIndex--)43{44// aModelIndex = (aModelIndex << 1) + m_Models[aModelIndex].Decode(aRangeDecoder);45RC_GETBIT(aNumMoveBits, m_Models[aModelIndex].m_Probability, aModelIndex)46}47RC_FLUSH_VAR48return aModelIndex - (1 << m_NumBitLevels);49};50};5152////////////////////////////////53// CReverseBitTreeDecoder5455template <int aNumMoveBits>56class CReverseBitTreeDecoder257{58CMyBitDecoder<aNumMoveBits> *m_Models;59UINT32 m_NumBitLevels;60public:61CReverseBitTreeDecoder2(): m_Models(0) { }62~CReverseBitTreeDecoder2() { delete []m_Models; }63bool Create(UINT32 aNumBitLevels)64{65m_NumBitLevels = aNumBitLevels;66m_Models = new CMyBitDecoder<aNumMoveBits>[1 << aNumBitLevels];67return (m_Models != 0);68}69void Init()70{71UINT32 aNumModels = 1 << m_NumBitLevels;72for(UINT32 i = 1; i < aNumModels; i++)73m_Models[i].Init();74}75UINT32 Decode(CMyRangeDecoder *aRangeDecoder)76{77UINT32 aModelIndex = 1;78UINT32 aSymbol = 0;79RC_INIT_VAR80for(UINT32 aBitIndex = 0; aBitIndex < m_NumBitLevels; aBitIndex++)81{82// UINT32 aBit = m_Models[aModelIndex].Decode(aRangeDecoder);83// aModelIndex <<= 1;84// aModelIndex += aBit;85// aSymbol |= (aBit << aBitIndex);86RC_GETBIT2(aNumMoveBits, m_Models[aModelIndex].m_Probability, aModelIndex, ; , aSymbol |= (1 << aBitIndex))87}88RC_FLUSH_VAR89return aSymbol;90};91};92////////////////////////////93// CReverseBitTreeDecoder29495template <int aNumMoveBits, UINT32 m_NumBitLevels>96class CReverseBitTreeDecoder97{98CMyBitDecoder<aNumMoveBits> m_Models[1 << m_NumBitLevels];99public:100void Init()101{102for(UINT32 i = 1; i < (1 << m_NumBitLevels); i++)103m_Models[i].Init();104}105UINT32 Decode(CMyRangeDecoder *aRangeDecoder)106{107UINT32 aModelIndex = 1;108UINT32 aSymbol = 0;109RC_INIT_VAR110for(UINT32 aBitIndex = 0; aBitIndex < m_NumBitLevels; aBitIndex++)111{112// UINT32 aBit = m_Models[aModelIndex].Decode(aRangeDecoder);113// aModelIndex <<= 1;114// aModelIndex += aBit;115// aSymbol |= (aBit << aBitIndex);116RC_GETBIT2(aNumMoveBits, m_Models[aModelIndex].m_Probability, aModelIndex, ; , aSymbol |= (1 << aBitIndex))117}118RC_FLUSH_VAR119return aSymbol;120}121};122123124125#endif126127128