Path: blob/main/misc/emulator/xnes/snes9x/jma/litcoder.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 __LITERALCODER_H19#define __LITERALCODER_H2021#include "aribitcd.h"22#include "rcdefs.h"2324namespace NLiteral {2526const int kNumMoveBits = 5;2728class CDecoder229{30CMyBitDecoder<kNumMoveBits> m_Decoders[3][1 << 8];31public:32void Init()33{34for (int i = 0; i < 3; i++)35for (int j = 1; j < (1 << 8); j++)36m_Decoders[i][j].Init();37}3839BYTE DecodeNormal(CMyRangeDecoder *aRangeDecoder)40{41UINT32 aSymbol = 1;42RC_INIT_VAR43do44{45// aSymbol = (aSymbol << 1) | m_Decoders[0][aSymbol].Decode(aRangeDecoder);46RC_GETBIT(kNumMoveBits, m_Decoders[0][aSymbol].m_Probability, aSymbol)47}48while (aSymbol < 0x100);49RC_FLUSH_VAR50return aSymbol;51}5253BYTE DecodeWithMatchByte(CMyRangeDecoder *aRangeDecoder, BYTE aMatchByte)54{55UINT32 aSymbol = 1;56RC_INIT_VAR57do58{59UINT32 aMatchBit = (aMatchByte >> 7) & 1;60aMatchByte <<= 1;61// UINT32 aBit = m_Decoders[1 + aMatchBit][aSymbol].Decode(aRangeDecoder);62// aSymbol = (aSymbol << 1) | aBit;63UINT32 aBit;64RC_GETBIT2(kNumMoveBits, m_Decoders[1 + aMatchBit][aSymbol].m_Probability, aSymbol,65aBit = 0, aBit = 1)66if (aMatchBit != aBit)67{68while (aSymbol < 0x100)69{70// aSymbol = (aSymbol << 1) | m_Decoders[0][aSymbol].Decode(aRangeDecoder);71RC_GETBIT(kNumMoveBits, m_Decoders[0][aSymbol].m_Probability, aSymbol)72}73break;74}75}76while (aSymbol < 0x100);77RC_FLUSH_VAR78return aSymbol;79}80};8182class CDecoder83{84CDecoder2 *m_Coders;85UINT32 m_NumPrevBits;86UINT32 m_NumPosBits;87UINT32 m_PosMask;88public:89CDecoder(): m_Coders(0) {}90~CDecoder() { Free(); }91void Free()92{93delete []m_Coders;94m_Coders = 0;95}96void Create(UINT32 aNumPosBits, UINT32 aNumPrevBits)97{98Free();99m_NumPosBits = aNumPosBits;100m_PosMask = (1 << aNumPosBits) - 1;101m_NumPrevBits = aNumPrevBits;102UINT32 aNumStates = 1 << (m_NumPrevBits + m_NumPosBits);103m_Coders = new CDecoder2[aNumStates];104}105void Init()106{107UINT32 aNumStates = 1 << (m_NumPrevBits + m_NumPosBits);108for (UINT32 i = 0; i < aNumStates; i++)109m_Coders[i].Init();110}111UINT32 GetState(UINT32 aPos, BYTE aPrevByte) const112{ return ((aPos & m_PosMask) << m_NumPrevBits) + (aPrevByte >> (8 - m_NumPrevBits)); }113BYTE DecodeNormal(CMyRangeDecoder *aRangeDecoder, UINT32 aPos, BYTE aPrevByte)114{ return m_Coders[GetState(aPos, aPrevByte)].DecodeNormal(aRangeDecoder); }115BYTE DecodeWithMatchByte(CMyRangeDecoder *aRangeDecoder, UINT32 aPos, BYTE aPrevByte, BYTE aMatchByte)116{ return m_Coders[GetState(aPos, aPrevByte)].DecodeWithMatchByte(aRangeDecoder, aMatchByte); }117};118119}120121#endif122123124