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