symport/e32/include/e32huffman.h
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\include\e32huffman.h
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 /** @file
       
    21 	@internalTechnology
       
    22 */
       
    23 
       
    24 /** Bit output stream.
       
    25 	Good for writing bit streams for packed, compressed or huffman data algorithms.
       
    26 
       
    27 	This class must be derived from and OverflowL() reimplemented if the bitstream data
       
    28 	cannot be generated into a single memory buffer.
       
    29 */
       
    30 class TBitOutput
       
    31 	{
       
    32 public:
       
    33 	IMPORT_C TBitOutput();
       
    34 	IMPORT_C TBitOutput(TUint8* aBuf,TInt aSize);
       
    35 	inline void Set(TUint8* aBuf,TInt aSize);
       
    36 	inline const TUint8* Ptr() const;
       
    37 	inline TInt BufferedBits() const;
       
    38 //
       
    39 	IMPORT_C void WriteL(TUint aValue, TInt aLength);
       
    40 	IMPORT_C void HuffmanL(TUint aHuffCode);
       
    41 	IMPORT_C void PadL(TUint aPadding);
       
    42 private:
       
    43 	void DoWriteL(TUint aBits, TInt aSize);
       
    44 	virtual void OverflowL();
       
    45 private:
       
    46 	TUint iCode;		// code in production
       
    47 	TInt iBits;
       
    48 	TUint8* iPtr;
       
    49 	TUint8* iEnd;
       
    50 	};
       
    51 
       
    52 /** Set the memory buffer to use for output
       
    53 
       
    54 	Data will be written to this buffer until it is full, at which point OverflowL() will
       
    55 	be called. This should handle the data and then can Set() again to reset the buffer
       
    56 	for further output.
       
    57 	
       
    58 	@param aBuf The buffer for output
       
    59 	@param aSize The size of the buffer in bytes
       
    60 */
       
    61 inline void TBitOutput::Set(TUint8* aBuf,TInt aSize)
       
    62 	{iPtr=aBuf;iEnd=aBuf+aSize;}
       
    63 	
       
    64 /** Get the current write position in the output buffer
       
    65 
       
    66 	In conjunction with the address of the buffer, which should be known to the
       
    67 	caller, this describes the data in the bitstream.
       
    68 */
       
    69 inline const TUint8* TBitOutput::Ptr() const
       
    70 	{return iPtr;}
       
    71 	
       
    72 /** Get the number of bits that are buffered
       
    73 
       
    74 	This reports the number of bits that have not yet been written into the
       
    75 	output buffer. It will always lie in the range 0..7. Use PadL() to
       
    76 	pad the data out to the next byte and write it to the buffer.
       
    77 */
       
    78 inline TInt TBitOutput::BufferedBits() const
       
    79 	{return iBits+8;}
       
    80 
       
    81 
       
    82 /** Bit input stream. Good for reading bit streams for packed, compressed or huffman
       
    83 	data algorithms.
       
    84 */
       
    85 class TBitInput
       
    86 	{
       
    87 public:
       
    88 	IMPORT_C TBitInput();
       
    89 	IMPORT_C TBitInput(const TUint8* aPtr, TInt aLength, TInt aOffset=0);
       
    90 	IMPORT_C void Set(const TUint8* aPtr, TInt aLength, TInt aOffset=0);
       
    91 //
       
    92 	IMPORT_C TUint ReadL();
       
    93 	IMPORT_C TUint ReadL(TInt aSize);
       
    94 	IMPORT_C TUint HuffmanL(const TUint32* aTree);
       
    95 private:
       
    96 	virtual void UnderflowL();
       
    97 private:
       
    98 	TInt iCount;
       
    99 	TUint iBits;
       
   100 	TInt iRemain;
       
   101 	const TUint32* iPtr;
       
   102 	};
       
   103 
       
   104 /** Huffman code toolkit.
       
   105 
       
   106 	This class builds a huffman encoding from a frequency table and builds
       
   107 	a decoding tree from a code-lengths table
       
   108 
       
   109 	The encoding generated is based on the rule that given two symbols s1 and s2, with 
       
   110 	code length l1 and l2, and huffman codes h1 and h2:
       
   111 
       
   112 		if l1<l2 then h1<h2 when compared lexicographically
       
   113 		if l1==l2 and s1<s2 then h1<h2 ditto
       
   114 
       
   115 	This allows the encoding to be stored compactly as a table of code lengths
       
   116 */
       
   117 class Huffman
       
   118 	{
       
   119 public:
       
   120 	enum {KMaxCodeLength=27};
       
   121 	enum {KMetaCodes=KMaxCodeLength+1};
       
   122 	enum {KMaxCodes=0x8000};
       
   123 public:
       
   124 	IMPORT_C static void HuffmanL(const TUint32 aFrequency[],TInt aNumCodes,TUint32 aHuffman[]);
       
   125 	IMPORT_C static void Encoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aEncodeTable[]);
       
   126 	IMPORT_C static void Decoding(const TUint32 aHuffman[],TInt aNumCodes,TUint32 aDecodeTree[],TInt aSymbolBase=0);
       
   127 	IMPORT_C static TBool IsValid(const TUint32 aHuffman[],TInt aNumCodes);
       
   128 //
       
   129 	IMPORT_C static void ExternalizeL(TBitOutput& aOutput,const TUint32 aHuffman[],TInt aNumCodes);
       
   130 	IMPORT_C static void InternalizeL(TBitInput& aInput,TUint32 aHuffman[],TInt aNumCodes);
       
   131 	};