os/mm/devsound/devsoundrefplugin/src/codec/sbcencoder/SBCEncoder.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #ifndef __SBCENCODER_H__
    17 #define __SBCENCODER_H__
    18 
    19 #include <e32std.h>
    20 #include <mmf/server/mmfcodec.h>
    21 
    22 #include "SBCFrameParameters.h"
    23 
    24 const TUint8 KSbcMaxBlocks = 16;
    25 const TUint8 KSbcMaxChannels = 2;
    26 const TUint8 KSbcMaxSubbands = 8;
    27 
    28 /**
    29 This class is for manipulating (reading/writing) raw bit stream. 
    30 It is used in sbc encoder for generating sbc frame bit stream.
    31 @internalComponent
    32 */
    33 class CBitStreamParser : public CBase
    34 	{
    35 public:
    36 	static CBitStreamParser* NewLC(TDes8& aBitStream);
    37 	void 	Reset();
    38 	
    39 	/** relative position */
    40 	TUint8	ReadBits(TInt aBitsToRead);
    41 	void	WriteBits(TInt aBitsToWrite, TUint8 aBitsValue);
    42 	
    43 	inline TUint8	ReadByte();
    44 	inline void		WriteByte(TUint8 aByteValue);
    45 	
    46 	/** absolute position */
    47 	inline void		SetPosition(TUint aByteOffset, TUint8 aBitOffset);
    48 	inline void		Position(TUint& aByteOffset, TUint8& aBitOffset) const;
    49 
    50 private:
    51 	CBitStreamParser(TDes8& aBitStream);
    52 	~CBitStreamParser();
    53 	
    54 	void	ConstructL();
    55 
    56 private:
    57 	TDes8&  iBitStream;
    58 	TUint8*	iPtr;
    59 	TUint	iByteOffset;
    60 	TUint8	iBitOffset;
    61 	};
    62 	
    63 /**
    64 This class is for checking and generating SBC CRC code. 
    65 It is used in sbc encoder for generating SBC CRC check number.
    66 @internalComponent
    67 */
    68 class CSbcCRCCalculator : public CBase
    69 	{
    70 public:
    71 	CSbcCRCCalculator();
    72 	
    73 	void 	Reset();
    74 	void 	InputBit(TUint8 aBit);
    75 	void	InputBits(TUint8 aBits, TUint8 aValue);
    76 	void 	InputByte(TUint8 aByte);
    77 	
    78 	TUint8 	ShiftRegister();
    79 	
    80 private:
    81 	TUint8 	iShiftRegister;
    82 	};
    83 
    84 /**
    85 This class is for encoding one SBC frame
    86 @internalComponent
    87 */
    88 class CSBCFrameEncoder : public CBase
    89 	{
    90 public:
    91 	static CSBCFrameEncoder* NewL();
    92 	~CSBCFrameEncoder();
    93 	void EncodeFrameL(const TDesC8& aSrc, TDes8& aFrame);
    94 	void Configure(const TSBCFrameParameters& aParameters);
    95 	void Reset();
    96 	
    97 private:
    98 	CSBCFrameEncoder();
    99 	void 	ConstructL();
   100 	
   101 	/** encode frame */
   102 	void 	Analyse(const TDesC8& aSrc);
   103 	void 	CalcScaleFactors();
   104 	void	JoinSubbands();
   105 	void 	CalcBitAllocation();
   106 	void 	Quantize();
   107 	
   108 	void 	AnalyseMono(const TInt16 aInputSamples[]);
   109 	void 	AnalyseOneChannel(const TInt16 aInputSamples[], TUint8 aChannel);
   110 	void 	Analyse4Subbands(const TInt16 aInputSamples[], TUint8 aBlock, TUint8 aChannel);
   111 	void 	Analyse8Subbands(const TInt16 aInputSamples[], TUint8 aBlock, TUint8 aChannel);
   112 
   113 	void	DoJoinSubbands();
   114 	
   115 	void 	CalcBitAllocIndependent();
   116 	void 	CalcBitneedIndependent(TInt8 aBitneed[], const TUint8 aScaleFactors[]);
   117 	TInt8 	MaxBitneedIndependent(const TInt8 aBitneed[]);
   118 	TInt8 	CalcBitSlicesIndependent(const TInt8 aBitneed[], TInt& aBitCount);
   119 	void 	DistributeBitsIndependent(const TInt8 aBitneed[], TUint8 aBits[]);
   120 
   121 	void 	CalcBitAllocCombined();
   122 	void 	CalcBitneedCombined(TInt8 aBitneed[][KSbcMaxSubbands]);
   123 	TInt8 	MaxBitneedCombined(const TInt8 aBitneed[][KSbcMaxSubbands]);
   124 	TInt8 	CalcBitSlicesCombined(const TInt8 aBitneed[][KSbcMaxSubbands], TInt& aBitCount);
   125 	void 	DistributeBitsCombined(const TInt8 aBitneed[][KSbcMaxSubbands]);
   126 	
   127 	TUint8 	CalcCRC();
   128 	
   129 	/** writing result back */
   130 	void	WriteFrameL(TDes8& aFrame);
   131 	void 	WriteHeader(CBitStreamParser& aParser);
   132 	void 	WriteScaleFactors(CBitStreamParser& aParser);
   133 	void 	WriteData(CBitStreamParser& aParser);
   134 	void 	WritePaddingL(CBitStreamParser& aParser);
   135 	
   136 private:
   137 	/** input parameters, set before encode */
   138 	TSBCFrameParameters	iParameters;
   139 	
   140 	TInt16 iAnalysisSamples[2][KSbcMaxSubbands * 10];
   141 	TUint8 iScaleFactors[2][KSbcMaxSubbands];
   142 	TUint8 iBits[2][KSbcMaxSubbands];
   143 	TInt32 iOutputSamples[KSbcMaxBlocks][2][KSbcMaxSubbands];
   144 	TUint8 iJoin[KSbcMaxSubbands];
   145 	};
   146 	
   147 /**
   148 This class is for encoding one SBC frame
   149 @internalComponent
   150 */
   151 class CSBCEncoder : public CMMFCodec
   152 	{
   153 public:
   154 	static CMMFCodec* NewL(TAny* aInitParams);
   155 	virtual ~CSBCEncoder();
   156 	void ConfigureL(TUid aConfigType, const TDesC8& aConfigData);
   157 	TCodecProcessResult ProcessL(const CMMFBuffer& aSrc, CMMFBuffer& aDst);
   158 
   159 private:
   160 	CSBCEncoder();
   161 	void 	ConstructL(TAny* aInitParams);
   162 	void 	ResetL();
   163 	
   164 	TUint 	EncodeFrameL(const TDesC8& aSrc, TDes8& aDst);
   165 	TUint 	CachePcmSamplesL(const CMMFDataBuffer& aSrc, TUint aSrcPos);
   166 	TUint 	CachedSampleSize();
   167 	
   168 private:
   169 	/** input parameters, set before encode */
   170 	TSBCFrameParameters	iParameters;
   171 	TUint iPcmFrameSize;
   172 	
   173 	/** CSBCFrameEncoder encodes each frame */
   174 	CSBCFrameEncoder*	iSbcFrameEncoder;
   175 	TUint iSbcFrameLength;
   176 	
   177 	/** pcm audio sample cach */
   178 	HBufC8* iPcmSampleCach;
   179 	};
   180 	
   181 #endif // __SBCENCODER_H__
   182