os/kernelhwsrv/kernel/eka/include/e32huffman.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/include/e32huffman.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,131 @@
     1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32\include\e32huffman.h
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32std.h>
    1.22 +
    1.23 +/** @file
    1.24 +	@internalTechnology
    1.25 +*/
    1.26 +
    1.27 +/** Bit output stream.
    1.28 +	Good for writing bit streams for packed, compressed or huffman data algorithms.
    1.29 +
    1.30 +	This class must be derived from and OverflowL() reimplemented if the bitstream data
    1.31 +	cannot be generated into a single memory buffer.
    1.32 +*/
    1.33 +class TBitOutput
    1.34 +	{
    1.35 +public:
    1.36 +	IMPORT_C TBitOutput();
    1.37 +	IMPORT_C TBitOutput(TUint8* aBuf,TInt aSize);
    1.38 +	inline void Set(TUint8* aBuf,TInt aSize);
    1.39 +	inline const TUint8* Ptr() const;
    1.40 +	inline TInt BufferedBits() const;
    1.41 +//
    1.42 +	IMPORT_C void WriteL(TUint aValue, TInt aLength);
    1.43 +	IMPORT_C void HuffmanL(TUint aHuffCode);
    1.44 +	IMPORT_C void PadL(TUint aPadding);
    1.45 +private:
    1.46 +	void DoWriteL(TUint aBits, TInt aSize);
    1.47 +	virtual void OverflowL();
    1.48 +private:
    1.49 +	TUint iCode;		// code in production
    1.50 +	TInt iBits;
    1.51 +	TUint8* iPtr;
    1.52 +	TUint8* iEnd;
    1.53 +	};
    1.54 +
    1.55 +/** Set the memory buffer to use for output
    1.56 +
    1.57 +	Data will be written to this buffer until it is full, at which point OverflowL() will
    1.58 +	be called. This should handle the data and then can Set() again to reset the buffer
    1.59 +	for further output.
    1.60 +	
    1.61 +	@param aBuf The buffer for output
    1.62 +	@param aSize The size of the buffer in bytes
    1.63 +*/
    1.64 +inline void TBitOutput::Set(TUint8* aBuf,TInt aSize)
    1.65 +	{iPtr=aBuf;iEnd=aBuf+aSize;}
    1.66 +	
    1.67 +/** Get the current write position in the output buffer
    1.68 +
    1.69 +	In conjunction with the address of the buffer, which should be known to the
    1.70 +	caller, this describes the data in the bitstream.
    1.71 +*/
    1.72 +inline const TUint8* TBitOutput::Ptr() const
    1.73 +	{return iPtr;}
    1.74 +	
    1.75 +/** Get the number of bits that are buffered
    1.76 +
    1.77 +	This reports the number of bits that have not yet been written into the
    1.78 +	output buffer. It will always lie in the range 0..7. Use PadL() to
    1.79 +	pad the data out to the next byte and write it to the buffer.
    1.80 +*/
    1.81 +inline TInt TBitOutput::BufferedBits() const
    1.82 +	{return iBits+8;}
    1.83 +
    1.84 +
    1.85 +/** Bit input stream. Good for reading bit streams for packed, compressed or huffman
    1.86 +	data algorithms.
    1.87 +*/
    1.88 +class TBitInput
    1.89 +	{
    1.90 +public:
    1.91 +	IMPORT_C TBitInput();
    1.92 +	IMPORT_C TBitInput(const TUint8* aPtr, TInt aLength, TInt aOffset=0);
    1.93 +	IMPORT_C void Set(const TUint8* aPtr, TInt aLength, TInt aOffset=0);
    1.94 +//
    1.95 +	IMPORT_C TUint ReadL();
    1.96 +	IMPORT_C TUint ReadL(TInt aSize);
    1.97 +	IMPORT_C TUint HuffmanL(const TUint32* aTree);
    1.98 +private:
    1.99 +	virtual void UnderflowL();
   1.100 +private:
   1.101 +	TInt iCount;
   1.102 +	TUint iBits;
   1.103 +	TInt iRemain;
   1.104 +	const TUint32* iPtr;
   1.105 +	};
   1.106 +
   1.107 +/** Huffman code toolkit.
   1.108 +
   1.109 +	This class builds a huffman encoding from a frequency table and builds
   1.110 +	a decoding tree from a code-lengths table
   1.111 +
   1.112 +	The encoding generated is based on the rule that given two symbols s1 and s2, with 
   1.113 +	code length l1 and l2, and huffman codes h1 and h2:
   1.114 +
   1.115 +		if l1<l2 then h1<h2 when compared lexicographically
   1.116 +		if l1==l2 and s1<s2 then h1<h2 ditto
   1.117 +
   1.118 +	This allows the encoding to be stored compactly as a table of code lengths
   1.119 +*/
   1.120 +class Huffman
   1.121 +	{
   1.122 +public:
   1.123 +	enum {KMaxCodeLength=27};
   1.124 +	enum {KMetaCodes=KMaxCodeLength+1};
   1.125 +	enum {KMaxCodes=0x8000};
   1.126 +public:
   1.127 +	IMPORT_C static void HuffmanL(const TUint32 aFrequency[],TInt aNumCodes,TUint32 aHuffman[]);
   1.128 +	IMPORT_C static void Encoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aEncodeTable[]);
   1.129 +	IMPORT_C static void Decoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aDecodeTree[],TInt aSymbolBase=0);
   1.130 +	IMPORT_C static TBool IsValid(const TUint32 aHuffman[],TInt aNumCodes);
   1.131 +//
   1.132 +	IMPORT_C static void ExternalizeL(TBitOutput& aOutput,const TUint32 aHuffman[],TInt aNumCodes);
   1.133 +	IMPORT_C static void InternalizeL(TBitInput& aInput,TUint32 aHuffman[],TInt aNumCodes);
   1.134 +	};