os/mm/devsound/sounddevbt/src/Plugin/Codec/SBCEncoder/BtSBCFrameParameters.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/devsound/sounddevbt/src/Plugin/Codec/SBCEncoder/BtSBCFrameParameters.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,588 @@
     1.4 +// Copyright (c) 2005-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 "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 +//
    1.18 +
    1.19 +#ifndef __BTSBCFRAMEPARAMETERS_H__
    1.20 +#define __BTSBCFRAMEPARAMETERS_H__
    1.21 +
    1.22 +/**
    1.23 +This class contains 6 SBC frame parameters: sampling frequency, block length, 
    1.24 +channel mode, allocation mode, subbands and bitpool. It can validate all the 
    1.25 +parameters, calculate frame length and bit rate.
    1.26 +@internalComponent
    1.27 +*/
    1.28 +class TSBCFrameParameters
    1.29 +	{
    1.30 +public:
    1.31 +	/**
    1.32 +	This enum list all the possible sampling frequency settings
    1.33 +	*/
    1.34 +	enum TSamplingFrequency
    1.35 +		{
    1.36 +		/**
    1.37 +		sampling frequency is 16000 Hz
    1.38 +		*/
    1.39 +		E16000Hz = 0,
    1.40 +		/**
    1.41 +		sampling frequency is 32000 Hz
    1.42 +		*/
    1.43 +		E32000Hz,
    1.44 +		/**
    1.45 +		sampling frequency is 44100 Hz
    1.46 +		*/
    1.47 +		E44100Hz,
    1.48 +		/**
    1.49 +		sampling frequency is 48000 Hz
    1.50 +		*/
    1.51 +		E48000Hz
    1.52 +		};
    1.53 +
    1.54 +	/**
    1.55 +	This enum list all the possible block length settings
    1.56 +	*/
    1.57 +	enum TBlockLength
    1.58 +		{
    1.59 +		/**
    1.60 +		block length is 4, one frame contains 4 blocks of audio samples
    1.61 +		*/
    1.62 +		E4Blocks = 0,
    1.63 +		/**
    1.64 +		block length is 8, one frame contains 8 blocks of audio samples
    1.65 +		*/
    1.66 +		E8Blocks,
    1.67 +		/**
    1.68 +		block length is 12, one frame contains 12 blocks of audio samples
    1.69 +		*/
    1.70 +		E12Blocks,
    1.71 +		/**
    1.72 +		block length is 16, one frame contains 16 blocks of audio samples
    1.73 +		*/
    1.74 +		E16Blocks
    1.75 +		};
    1.76 +
    1.77 +	/**
    1.78 +	This enum list all the possible channel mode settings
    1.79 +	*/
    1.80 +	enum TChannelMode
    1.81 +		{
    1.82 +		/**
    1.83 +		channel mode is Mono, in this mode,
    1.84 +		only one channel contains audio samples.
    1.85 +		*/
    1.86 +		EMono = 0,
    1.87 +		/**
    1.88 +		channel mode is Dual Channel, in this mode,
    1.89 +		it contains two seperate mono audio samples.
    1.90 +		*/
    1.91 +		EDualChannel,
    1.92 +		/**
    1.93 +		channel mode is Stereo, in this mode,
    1.94 +		it contains stereo audio samples.
    1.95 +		*/
    1.96 +		EStereo,
    1.97 +		/**
    1.98 +		channel mode is Joint Stereo, in this mode,
    1.99 +		the left channel stores half of sum of both channels, 
   1.100 +		the right channel stores half of difference of both channels. 
   1.101 +		*/
   1.102 +		EJointStereo
   1.103 +		};
   1.104 +
   1.105 +	/**
   1.106 +	This enum list all the possible allocation method settings
   1.107 +	*/
   1.108 +	enum TAllocationMethod
   1.109 +		{
   1.110 +		/**
   1.111 +		allocation method is Loudness, in this mode, 
   1.112 +		the bit allocation calculation uses Table offset4 or offset8 as well as scale factors
   1.113 +		*/
   1.114 +		ELoudness = 0,
   1.115 +		/**
   1.116 +		allocation method is SNR, in this mode,
   1.117 +		bit allocation only uses scale factors
   1.118 +		*/
   1.119 +		ESNR
   1.120 +		};
   1.121 +
   1.122 +	/**
   1.123 +	This enum list all the possible subbands settings
   1.124 +	*/
   1.125 +	enum TSubbands
   1.126 +		{
   1.127 +		/**
   1.128 +		subbands is 4, each channel contains 4 subbands in each block, 
   1.129 +		each subband contains one sample
   1.130 +		*/
   1.131 +		E4Subbands = 0,
   1.132 +		/**
   1.133 +		subbands is 8, each channel contains 8 subbands in each block, 
   1.134 +		each subband contains one sample
   1.135 +		*/
   1.136 +		E8Subbands
   1.137 +		};
   1.138 +		
   1.139 +public:
   1.140 +	inline TSBCFrameParameters();
   1.141 +	
   1.142 +	inline void Reset();
   1.143 +
   1.144 +	inline TUint8 	SamplingFrequencyEnum() const;
   1.145 +	inline TUint	SamplingFrequencyHz() const;
   1.146 +	inline void		SetSamplingFrequency(TSamplingFrequency aSamplingFrequency);
   1.147 +	
   1.148 +	inline TUint8	BlockLength() const;
   1.149 +	inline void		SetBlockLength(TBlockLength aBlockLength);
   1.150 +	
   1.151 +	inline TUint8 	ChannelMode() const;
   1.152 +	inline TUint8	Channels() const;
   1.153 +	inline void 	SetChannelMode(TChannelMode aChannelMode);
   1.154 +	
   1.155 +	inline TUint8 	AllocationMethod() const;
   1.156 +	inline void 	SetAllocationMethod(TAllocationMethod aAllocationMethod);
   1.157 +	
   1.158 +	inline TUint8	SubbandsEnum() const;
   1.159 +	inline TUint8	Subbands() const;
   1.160 +	inline void 	SetSubbands(TSubbands aSubbands);
   1.161 +	
   1.162 +	inline TUint8	Bitpool() const;
   1.163 +	inline void		SetBitpool(TUint8 aBitpool);
   1.164 +	
   1.165 +	inline TUint8	Parameters() const;
   1.166 +	inline TInt 	Validate() const;
   1.167 +
   1.168 +	inline TUint 	CalcFrameLength() const;
   1.169 +	inline TUint 	CalcBitRate(TUint aFrameLength) const;
   1.170 +
   1.171 +private:
   1.172 +	TUint8 	iParameters;
   1.173 +	TUint8 	iBitpool;
   1.174 +	};
   1.175 +
   1.176 +/**
   1.177 +The minimum SBC bitpool value is 2 
   1.178 +*/	
   1.179 +const TUint8 KSBCMinBitpoolValue = 2;
   1.180 +/**
   1.181 +The maximum SBC bitpool value is 250 
   1.182 +*/	
   1.183 +const TUint8 KSBCMaxBitpoolValue = 250;
   1.184 +
   1.185 +/**
   1.186 +The sampling frequency bits mask is 0b11, 2 bits
   1.187 +*/	
   1.188 +const TUint8 KSBCSampFreqBitsMask  = 0x3;
   1.189 +/**
   1.190 +The bit offset of sampling frequency field in TSBCFrameParameters::iParameters is 6
   1.191 +*/
   1.192 +const TUint8 KSBCSampFreqBitOffset = 6;
   1.193 +
   1.194 +/**
   1.195 +The block length bits mask is 0b11, 2 bits
   1.196 +*/	
   1.197 +const TUint8 KSBCBlckLengBitsMask  = 0x3;
   1.198 +/**
   1.199 +The bit offset of block length field in TSBCFrameParameters::iParameters is 4
   1.200 +*/
   1.201 +const TUint8 KSBCBlckLengBitOffset = 4;
   1.202 +
   1.203 +/**
   1.204 +The block length bits mask is 0b11, 2 bits
   1.205 +*/	
   1.206 +const TUint8 KSBCChnlModeBitsMask  = 0x3;
   1.207 +/**
   1.208 +The bit offset of block length field in TSBCFrameParameters::iParameters is 2
   1.209 +*/
   1.210 +const TUint8 KSBCChnlModeBitOffset = 2;
   1.211 +
   1.212 +/**
   1.213 +The block length bits mask is 0b01, 1 bit
   1.214 +*/	
   1.215 +const TUint8 KSBCAllcMthdBitsMask  = 0x1;
   1.216 +/**
   1.217 +The bit offset of block length field in TSBCFrameParameters::iParameters is 1
   1.218 +*/
   1.219 +const TUint8 KSBCAllcMthdBitOffset = 1;
   1.220 +
   1.221 +/**
   1.222 +The block length bits mask is 0b01, 1 bit
   1.223 +*/	
   1.224 +const TUint8 KSBCSubbandsBitsMask  = 0x1;
   1.225 +/**
   1.226 +The bit offset of block length field in TSBCFrameParameters::iParameters is 0
   1.227 +*/
   1.228 +const TUint8 KSBCSubbandsBitOffset = 0;
   1.229 +
   1.230 +/**
   1.231 +Constructor
   1.232 +@internalComponent
   1.233 +*/
   1.234 +inline TSBCFrameParameters::TSBCFrameParameters() : iParameters(0), iBitpool(0)
   1.235 +	{
   1.236 +	}
   1.237 +	
   1.238 +/**
   1.239 +This function reset all the parameters
   1.240 +@internalComponent
   1.241 +*/
   1.242 +inline void TSBCFrameParameters::Reset()
   1.243 +	{
   1.244 +	iParameters = 0;
   1.245 +	iBitpool = 0;
   1.246 +	}
   1.247 +	
   1.248 +/**
   1.249 +This function gets the sampling frequency enum value
   1.250 +@internalComponent
   1.251 +@return enum value of sampling frequency
   1.252 +*/
   1.253 +inline TUint8 TSBCFrameParameters::SamplingFrequencyEnum() const
   1.254 +	{
   1.255 +	return static_cast<TUint8>( (iParameters >> KSBCSampFreqBitOffset) & KSBCSampFreqBitsMask);
   1.256 +	}
   1.257 +	
   1.258 +/**
   1.259 +This function gets the sampling frequency value in Hz
   1.260 +@internalComponent
   1.261 +@return samplinng frequency value in Hz
   1.262 +*/
   1.263 +inline TUint TSBCFrameParameters::SamplingFrequencyHz() const
   1.264 +	{
   1.265 +	switch (SamplingFrequencyEnum() )
   1.266 +		{
   1.267 +		case E16000Hz:
   1.268 +			return 16000;
   1.269 +			
   1.270 +		case E32000Hz:
   1.271 +			return 32000;
   1.272 +			
   1.273 +		case E44100Hz:
   1.274 +			return 44100;
   1.275 +			
   1.276 +		case E48000Hz:
   1.277 +			return 48000;
   1.278 +		}
   1.279 +	return 0;
   1.280 +	}
   1.281 +
   1.282 +/**
   1.283 +This function sets the sampling frequency value
   1.284 +@internalComponent
   1.285 +@param aSampFreq
   1.286 +New sampling frequency enum value to set
   1.287 +*/
   1.288 +inline void TSBCFrameParameters::SetSamplingFrequency(TSamplingFrequency aSampFreq)
   1.289 +	{
   1.290 +	// clear sampling frequency bits
   1.291 +	iParameters &= ~(KSBCSampFreqBitsMask << KSBCSampFreqBitOffset);
   1.292 +	// set new sampling frequency bits
   1.293 +	iParameters |= ( (aSampFreq & KSBCSampFreqBitsMask) << KSBCSampFreqBitOffset);
   1.294 +	}
   1.295 +
   1.296 +/**
   1.297 +This function gets the block length value
   1.298 +@internalComponent
   1.299 +@return number of blocks in one frame
   1.300 +*/
   1.301 +inline TUint8 TSBCFrameParameters::BlockLength() const
   1.302 +	{
   1.303 +	switch ( (iParameters >> KSBCBlckLengBitOffset) & KSBCBlckLengBitsMask)
   1.304 +		{
   1.305 +		case E4Blocks:
   1.306 +			return 4;
   1.307 +			
   1.308 +		case E8Blocks:
   1.309 +			return 8;
   1.310 +			
   1.311 +		case E12Blocks:
   1.312 +			return 12;
   1.313 +			
   1.314 +		case E16Blocks:
   1.315 +			return 16;
   1.316 +		}
   1.317 +	return 0;
   1.318 +	}
   1.319 +
   1.320 +/**
   1.321 +This function sets the block length value
   1.322 +@internalComponent
   1.323 +@param aBlockLen
   1.324 +New block length value to set
   1.325 +*/
   1.326 +inline void TSBCFrameParameters::SetBlockLength(TBlockLength aBlockLen)
   1.327 +	{
   1.328 +	// clear block length bits
   1.329 +	iParameters &= ~(KSBCBlckLengBitsMask << KSBCBlckLengBitOffset);
   1.330 +	// set new block length bits
   1.331 +	iParameters |= ( (aBlockLen & KSBCBlckLengBitsMask) << KSBCBlckLengBitOffset);
   1.332 +	}
   1.333 +
   1.334 +/**
   1.335 +This function gets the channel mode enum value
   1.336 +@internalComponent
   1.337 +@return channel mode enum value
   1.338 +*/
   1.339 +inline TUint8 TSBCFrameParameters::ChannelMode() const
   1.340 +	{
   1.341 +	return static_cast<TUint8>( (iParameters >> KSBCChnlModeBitOffset) & KSBCChnlModeBitsMask);
   1.342 +	}
   1.343 +
   1.344 +/**
   1.345 +This function gets number of channels
   1.346 +@internalComponent
   1.347 +@return number of channels
   1.348 +*/
   1.349 +inline TUint8 TSBCFrameParameters::Channels() const
   1.350 +	{
   1.351 +	switch (ChannelMode() )
   1.352 +		{
   1.353 +		case EMono:
   1.354 +			return 1;
   1.355 +			
   1.356 +		case EDualChannel:
   1.357 +		case EStereo:
   1.358 +		case EJointStereo:
   1.359 +			return 2;
   1.360 +		}
   1.361 +	return 0;
   1.362 +	}
   1.363 +	
   1.364 +/**
   1.365 +This function sets the channel mode enum value
   1.366 +@internalComponent
   1.367 +@param aChnlMode
   1.368 +New channel mode enum value to set
   1.369 +*/
   1.370 +inline void TSBCFrameParameters::SetChannelMode(TChannelMode aChnlMode)
   1.371 +	{
   1.372 +	// clear channel mode bits
   1.373 +	iParameters &= ~(KSBCChnlModeBitsMask << KSBCChnlModeBitOffset);
   1.374 +	// set new channel mode bits
   1.375 +	iParameters |= ( (aChnlMode & KSBCChnlModeBitsMask) << KSBCChnlModeBitOffset);
   1.376 +	}
   1.377 +
   1.378 +/**
   1.379 +This function gets the allocation method enum value
   1.380 +@internalComponent
   1.381 +@return allocation method enum value
   1.382 +*/
   1.383 +inline TUint8 TSBCFrameParameters::AllocationMethod() const
   1.384 +	{
   1.385 +	return static_cast<TUint8>( (iParameters >> KSBCAllcMthdBitOffset) & KSBCAllcMthdBitsMask);
   1.386 +	}
   1.387 +
   1.388 +/**
   1.389 +This function sets the channel mode enum value
   1.390 +@internalComponent
   1.391 +@param aAllocMethod
   1.392 +New channel mode enum value to set
   1.393 +*/
   1.394 +inline void TSBCFrameParameters::SetAllocationMethod(TAllocationMethod aAllocMethod)
   1.395 +	{
   1.396 +	// clear allocation method bits
   1.397 +	iParameters &= ~(KSBCAllcMthdBitsMask << KSBCAllcMthdBitOffset);
   1.398 +	// set new allocation method bits
   1.399 +	iParameters |= ( (aAllocMethod & KSBCAllcMthdBitsMask) << KSBCAllcMthdBitOffset);
   1.400 +	}		
   1.401 +
   1.402 +/**
   1.403 +This function gets the subbands enum value
   1.404 +@internalComponent
   1.405 +@return subbands enum value
   1.406 +*/
   1.407 +inline TUint8 TSBCFrameParameters::SubbandsEnum() const
   1.408 +	{
   1.409 +	return static_cast<TUint8>( (iParameters >> KSBCSubbandsBitOffset) & KSBCSubbandsBitsMask);
   1.410 +	}
   1.411 +
   1.412 +/**
   1.413 +This function gets the subbands value
   1.414 +@internalComponent
   1.415 +@return subbands value, i.e 4, 8
   1.416 +*/
   1.417 +inline TUint8 TSBCFrameParameters::Subbands() const
   1.418 +	{
   1.419 +	switch (SubbandsEnum() )
   1.420 +		{
   1.421 +		case E4Subbands:
   1.422 +			return 4;
   1.423 +			
   1.424 +		case E8Subbands:
   1.425 +			return 8;
   1.426 +		}
   1.427 +	return 0;
   1.428 +	}
   1.429 +	
   1.430 +/**
   1.431 +This function sets the subbands enum value
   1.432 +@internalComponent
   1.433 +@param aSubbands
   1.434 +New subbands enum value to set
   1.435 +*/
   1.436 +inline void TSBCFrameParameters::SetSubbands(TSubbands aSubbands)
   1.437 +	{
   1.438 +	// clear subbands bits
   1.439 +	iParameters &= ~(KSBCSubbandsBitsMask << KSBCSubbandsBitOffset);
   1.440 +	// set new subbands bits
   1.441 +	iParameters |= ( (aSubbands & KSBCSubbandsBitsMask) << KSBCSubbandsBitOffset);
   1.442 +	}		
   1.443 +
   1.444 +/**
   1.445 +This function gets the bitpool value
   1.446 +@internalComponent
   1.447 +@return bitpool value
   1.448 +*/
   1.449 +inline TUint8 TSBCFrameParameters::Bitpool() const
   1.450 +	{
   1.451 +	return iBitpool;
   1.452 +	}
   1.453 +
   1.454 +/**
   1.455 +This function sets the bitpool enum value
   1.456 +@internalComponent
   1.457 +@param aSubbands
   1.458 +New bitpool enum value to set
   1.459 +*/
   1.460 +inline void TSBCFrameParameters::SetBitpool(TUint8 aBitpool)
   1.461 +	{
   1.462 +	iBitpool = aBitpool;
   1.463 +	}
   1.464 +
   1.465 +/**
   1.466 +This function gets the 5 parameters (except bitpool) byte value
   1.467 +@internalComponent
   1.468 +@return 5 parameters byte value
   1.469 +*/
   1.470 +inline TUint8 TSBCFrameParameters::Parameters() const
   1.471 +	{
   1.472 +	return iParameters;
   1.473 +	}
   1.474 +	
   1.475 +/**
   1.476 +This function checks the bitpool value according to:
   1.477 +1. bitpool >= 2 and bitpool <= 250
   1.478 +2. bitpool <= 16 * subbands for Mono and Dual Channel,
   1.479 +   bitpool <= 32 * subbands for Stereo and Joint Stereo 
   1.480 +3. results in bit_rate <= 320 kbps for Mono
   1.481 +   results in bit_rate <= 512 kpbs for two-channel modes
   1.482 +@internalComponent
   1.483 +@return -1 if invalid; 0 if valid
   1.484 +*/
   1.485 +inline TInt TSBCFrameParameters::Validate() const
   1.486 +	{
   1.487 +	if (iBitpool < KSBCMinBitpoolValue || iBitpool > KSBCMaxBitpoolValue)
   1.488 +		{
   1.489 +		return -1;
   1.490 +		}
   1.491 +		
   1.492 +	const TUint16 numSubbands = Subbands(); // use 16 bits to avoid overflow
   1.493 +	const TUint8 channelMode = ChannelMode();
   1.494 +	
   1.495 +	if (channelMode == EMono || channelMode == EDualChannel)
   1.496 +		{
   1.497 +		// bitpool <= 16 * subbands, for Mono and Dual_Channel modes
   1.498 +		if (iBitpool > (numSubbands << 4) )
   1.499 +			{
   1.500 +			return -1;
   1.501 +			}
   1.502 +		}
   1.503 +	else
   1.504 +		{
   1.505 +		// bitpool <= 32 * subbands, for Stereo and Joint_Stereo modes
   1.506 +		if (iBitpool > (numSubbands << 5) )
   1.507 +			{
   1.508 +			return -1;
   1.509 +			}
   1.510 +		}
   1.511 +	
   1.512 +	if (channelMode == EMono)
   1.513 +		{
   1.514 +		// bit rate <= 320kbps for Mono mode
   1.515 +		if (CalcBitRate(CalcFrameLength() ) > 320)
   1.516 +			{
   1.517 +			return -1;
   1.518 +			}
   1.519 +		}
   1.520 +	else
   1.521 +		{
   1.522 +		// bit rate <= 512kpbs for two-channels modes
   1.523 +		if (CalcBitRate(CalcFrameLength() ) > 512)
   1.524 +			{
   1.525 +			return -1;
   1.526 +			}
   1.527 +		}
   1.528 +	
   1.529 +	return 0;
   1.530 +	}
   1.531 +
   1.532 +/**
   1.533 +This function calculates the frame length value according to:
   1.534 +1. for MONO or DUAL_CHANNEL
   1.535 +   frame_len = 4 + (4 * subbands * channels) / 8 + ceil(blocks * channels * bitpool / 8)
   1.536 +2. for STEREO
   1.537 +   frame_len = 4 + (4 * subbands * channels) / 8 + ceil(blocks * bitpool / 8)
   1.538 +3. for JOINT_STEREO
   1.539 +   frame_len = 4 + (4 * subbands * channels) / 8 + ceil((subbands + blocks * bitpool) / 8)
   1.540 +ceil(), taking the upper integer value
   1.541 +@internalComponent
   1.542 +@return frame length value
   1.543 +*/
   1.544 +inline TUint TSBCFrameParameters::CalcFrameLength() const
   1.545 +	{
   1.546 +	TUint temp = 0;
   1.547 +	switch (ChannelMode() )
   1.548 +		{
   1.549 +		case EMono:
   1.550 +			temp = BlockLength() * Bitpool(); // blocks * bitpool
   1.551 +			break;
   1.552 +			
   1.553 +		case EDualChannel:
   1.554 +			temp = (BlockLength() * Bitpool() ) << 1; // blocks * bitpool * 2
   1.555 +			break;
   1.556 +			
   1.557 +		case EStereo:
   1.558 +			temp = BlockLength() * Bitpool(); // blocks * bitpool
   1.559 +			break;
   1.560 +			
   1.561 +		case EJointStereo:
   1.562 +			temp = Subbands() + BlockLength() * Bitpool(); // subbands + blocks * bitpool
   1.563 +			break;
   1.564 +			
   1.565 +		default:
   1.566 +			User::Panic(_L("Invalid channel mode"), KErrNotSupported);
   1.567 +			break;
   1.568 +		}
   1.569 +		
   1.570 +	TUint frameLen = 4 + ( (Subbands() * Channels() ) >> 1) + (temp >> 3);
   1.571 +	if (temp & 0x7)
   1.572 +		{
   1.573 +		frameLen++;
   1.574 +		}
   1.575 +		
   1.576 +	return frameLen;
   1.577 +	}
   1.578 +
   1.579 +/**
   1.580 +This function calculates the bit rate value according to:
   1.581 +	bit_rate = 8 * frame_len * sampling_freq / subbands / blocks
   1.582 +@internalComponent
   1.583 +@return bit rate value in kHz
   1.584 +*/
   1.585 +inline TUint TSBCFrameParameters::CalcBitRate(TUint aFrameLen) const
   1.586 +	{
   1.587 +	return (aFrameLen << 3) * SamplingFrequencyHz() / (Subbands() * BlockLength() * 1000);
   1.588 +	}
   1.589 +
   1.590 +#endif // __BTSBCFRAMEPARAMETERS_H__
   1.591 +