devsound/devsoundrefplugin/src/codec/sbcencoder/SBCFrameParameters.h
changeset 0 79dd3e2336a0
equal deleted inserted replaced
-1:000000000000 0:79dd3e2336a0
       
     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 __SBCFRAMEPARAMETERS_H__
       
    17 #define __SBCFRAMEPARAMETERS_H__
       
    18 
       
    19 /**
       
    20 This class contains 6 SBC frame parameters: sampling frequency, block length, 
       
    21 channel mode, allocation mode, subbands and bitpool. It can validate all the 
       
    22 parameters, calculate frame length and bit rate.
       
    23 @internalComponent
       
    24 */
       
    25 class TSBCFrameParameters
       
    26 	{
       
    27 public:
       
    28 	/**
       
    29 	This enum list all the possible sampling frequency settings
       
    30 	*/
       
    31 	enum TSamplingFrequency
       
    32 		{
       
    33 		/**
       
    34 		sampling frequency is 16000 Hz
       
    35 		*/
       
    36 		E16000Hz = 0,
       
    37 		/**
       
    38 		sampling frequency is 32000 Hz
       
    39 		*/
       
    40 		E32000Hz,
       
    41 		/**
       
    42 		sampling frequency is 44100 Hz
       
    43 		*/
       
    44 		E44100Hz,
       
    45 		/**
       
    46 		sampling frequency is 48000 Hz
       
    47 		*/
       
    48 		E48000Hz
       
    49 		};
       
    50 
       
    51 	/**
       
    52 	This enum list all the possible block length settings
       
    53 	*/
       
    54 	enum TBlockLength
       
    55 		{
       
    56 		/**
       
    57 		block length is 4, one frame contains 4 blocks of audio samples
       
    58 		*/
       
    59 		E4Blocks = 0,
       
    60 		/**
       
    61 		block length is 8, one frame contains 8 blocks of audio samples
       
    62 		*/
       
    63 		E8Blocks,
       
    64 		/**
       
    65 		block length is 12, one frame contains 12 blocks of audio samples
       
    66 		*/
       
    67 		E12Blocks,
       
    68 		/**
       
    69 		block length is 16, one frame contains 16 blocks of audio samples
       
    70 		*/
       
    71 		E16Blocks
       
    72 		};
       
    73 
       
    74 	/**
       
    75 	This enum list all the possible channel mode settings
       
    76 	*/
       
    77 	enum TChannelMode
       
    78 		{
       
    79 		/**
       
    80 		channel mode is Mono, in this mode,
       
    81 		only one channel contains audio samples.
       
    82 		*/
       
    83 		EMono = 0,
       
    84 		/**
       
    85 		channel mode is Dual Channel, in this mode,
       
    86 		it contains two seperate mono audio samples.
       
    87 		*/
       
    88 		EDualChannel,
       
    89 		/**
       
    90 		channel mode is Stereo, in this mode,
       
    91 		it contains stereo audio samples.
       
    92 		*/
       
    93 		EStereo,
       
    94 		/**
       
    95 		channel mode is Joint Stereo, in this mode,
       
    96 		the left channel stores half of sum of both channels, 
       
    97 		the right channel stores half of difference of both channels. 
       
    98 		*/
       
    99 		EJointStereo
       
   100 		};
       
   101 
       
   102 	/**
       
   103 	This enum list all the possible allocation method settings
       
   104 	*/
       
   105 	enum TAllocationMethod
       
   106 		{
       
   107 		/**
       
   108 		allocation method is Loudness, in this mode, 
       
   109 		the bit allocation calculation uses Table offset4 or offset8 as well as scale factors
       
   110 		*/
       
   111 		ELoudness = 0,
       
   112 		/**
       
   113 		allocation method is SNR, in this mode,
       
   114 		bit allocation only uses scale factors
       
   115 		*/
       
   116 		ESNR
       
   117 		};
       
   118 
       
   119 	/**
       
   120 	This enum list all the possible subbands settings
       
   121 	*/
       
   122 	enum TSubbands
       
   123 		{
       
   124 		/**
       
   125 		subbands is 4, each channel contains 4 subbands in each block, 
       
   126 		each subband contains one sample
       
   127 		*/
       
   128 		E4Subbands = 0,
       
   129 		/**
       
   130 		subbands is 8, each channel contains 8 subbands in each block, 
       
   131 		each subband contains one sample
       
   132 		*/
       
   133 		E8Subbands
       
   134 		};
       
   135 		
       
   136 public:
       
   137 	inline TSBCFrameParameters();
       
   138 	
       
   139 	inline void Reset();
       
   140 
       
   141 	inline TUint8 	SamplingFrequencyEnum() const;
       
   142 	inline TUint	SamplingFrequencyHz() const;
       
   143 	inline void		SetSamplingFrequency(TSamplingFrequency aSamplingFrequency);
       
   144 	
       
   145 	inline TUint8	BlockLength() const;
       
   146 	inline void		SetBlockLength(TBlockLength aBlockLength);
       
   147 	
       
   148 	inline TUint8 	ChannelMode() const;
       
   149 	inline TUint8	Channels() const;
       
   150 	inline void 	SetChannelMode(TChannelMode aChannelMode);
       
   151 	
       
   152 	inline TUint8 	AllocationMethod() const;
       
   153 	inline void 	SetAllocationMethod(TAllocationMethod aAllocationMethod);
       
   154 	
       
   155 	inline TUint8	SubbandsEnum() const;
       
   156 	inline TUint8	Subbands() const;
       
   157 	inline void 	SetSubbands(TSubbands aSubbands);
       
   158 	
       
   159 	inline TUint8	Bitpool() const;
       
   160 	inline void		SetBitpool(TUint8 aBitpool);
       
   161 	
       
   162 	inline TUint8	Parameters() const;
       
   163 	inline TInt 	Validate() const;
       
   164 
       
   165 	inline TUint 	CalcFrameLength() const;
       
   166 	inline TUint 	CalcBitRate(TUint aFrameLength) const;
       
   167 
       
   168 private:
       
   169 	TUint8 	iParameters;
       
   170 	TUint8 	iBitpool;
       
   171 	};
       
   172 
       
   173 /**
       
   174 The minimum SBC bitpool value is 2 
       
   175 */	
       
   176 const TUint8 KSBCMinBitpoolValue = 2;
       
   177 /**
       
   178 The maximum SBC bitpool value is 250 
       
   179 */	
       
   180 const TUint8 KSBCMaxBitpoolValue = 250;
       
   181 
       
   182 /**
       
   183 The sampling frequency bits mask is 0b11, 2 bits
       
   184 */	
       
   185 const TUint8 KSBCSampFreqBitsMask  = 0x3;
       
   186 /**
       
   187 The bit offset of sampling frequency field in TSBCFrameParameters::iParameters is 6
       
   188 */
       
   189 const TUint8 KSBCSampFreqBitOffset = 6;
       
   190 
       
   191 /**
       
   192 The block length bits mask is 0b11, 2 bits
       
   193 */	
       
   194 const TUint8 KSBCBlckLengBitsMask  = 0x3;
       
   195 /**
       
   196 The bit offset of block length field in TSBCFrameParameters::iParameters is 4
       
   197 */
       
   198 const TUint8 KSBCBlckLengBitOffset = 4;
       
   199 
       
   200 /**
       
   201 The block length bits mask is 0b11, 2 bits
       
   202 */	
       
   203 const TUint8 KSBCChnlModeBitsMask  = 0x3;
       
   204 /**
       
   205 The bit offset of block length field in TSBCFrameParameters::iParameters is 2
       
   206 */
       
   207 const TUint8 KSBCChnlModeBitOffset = 2;
       
   208 
       
   209 /**
       
   210 The block length bits mask is 0b01, 1 bit
       
   211 */	
       
   212 const TUint8 KSBCAllcMthdBitsMask  = 0x1;
       
   213 /**
       
   214 The bit offset of block length field in TSBCFrameParameters::iParameters is 1
       
   215 */
       
   216 const TUint8 KSBCAllcMthdBitOffset = 1;
       
   217 
       
   218 /**
       
   219 The block length bits mask is 0b01, 1 bit
       
   220 */	
       
   221 const TUint8 KSBCSubbandsBitsMask  = 0x1;
       
   222 /**
       
   223 The bit offset of block length field in TSBCFrameParameters::iParameters is 0
       
   224 */
       
   225 const TUint8 KSBCSubbandsBitOffset = 0;
       
   226 
       
   227 /**
       
   228 Constructor
       
   229 @internalComponent
       
   230 */
       
   231 inline TSBCFrameParameters::TSBCFrameParameters() : iParameters(0), iBitpool(0)
       
   232 	{
       
   233 	}
       
   234 	
       
   235 /**
       
   236 This function reset all the parameters
       
   237 @internalComponent
       
   238 */
       
   239 inline void TSBCFrameParameters::Reset()
       
   240 	{
       
   241 	iParameters = 0;
       
   242 	iBitpool = 0;
       
   243 	}
       
   244 	
       
   245 /**
       
   246 This function gets the sampling frequency enum value
       
   247 @internalComponent
       
   248 @return enum value of sampling frequency
       
   249 */
       
   250 inline TUint8 TSBCFrameParameters::SamplingFrequencyEnum() const
       
   251 	{
       
   252 	return static_cast<TUint8>( (iParameters >> KSBCSampFreqBitOffset) & KSBCSampFreqBitsMask);
       
   253 	}
       
   254 	
       
   255 /**
       
   256 This function gets the sampling frequency value in Hz
       
   257 @internalComponent
       
   258 @return samplinng frequency value in Hz
       
   259 */
       
   260 inline TUint TSBCFrameParameters::SamplingFrequencyHz() const
       
   261 	{
       
   262 	switch (SamplingFrequencyEnum() )
       
   263 		{
       
   264 		case E16000Hz:
       
   265 			return 16000;
       
   266 			
       
   267 		case E32000Hz:
       
   268 			return 32000;
       
   269 			
       
   270 		case E44100Hz:
       
   271 			return 44100;
       
   272 			
       
   273 		case E48000Hz:
       
   274 			return 48000;
       
   275 		}
       
   276 	return 0;
       
   277 	}
       
   278 
       
   279 /**
       
   280 This function sets the sampling frequency value
       
   281 @internalComponent
       
   282 @param aSampFreq
       
   283 New sampling frequency enum value to set
       
   284 */
       
   285 inline void TSBCFrameParameters::SetSamplingFrequency(TSamplingFrequency aSampFreq)
       
   286 	{
       
   287 	// clear sampling frequency bits
       
   288 	iParameters &= ~(KSBCSampFreqBitsMask << KSBCSampFreqBitOffset);
       
   289 	// set new sampling frequency bits
       
   290 	iParameters |= ( (aSampFreq & KSBCSampFreqBitsMask) << KSBCSampFreqBitOffset);
       
   291 	}
       
   292 
       
   293 /**
       
   294 This function gets the block length value
       
   295 @internalComponent
       
   296 @return number of blocks in one frame
       
   297 */
       
   298 inline TUint8 TSBCFrameParameters::BlockLength() const
       
   299 	{
       
   300 	switch ( (iParameters >> KSBCBlckLengBitOffset) & KSBCBlckLengBitsMask)
       
   301 		{
       
   302 		case E4Blocks:
       
   303 			return 4;
       
   304 			
       
   305 		case E8Blocks:
       
   306 			return 8;
       
   307 			
       
   308 		case E12Blocks:
       
   309 			return 12;
       
   310 			
       
   311 		case E16Blocks:
       
   312 			return 16;
       
   313 		}
       
   314 	return 0;
       
   315 	}
       
   316 
       
   317 /**
       
   318 This function sets the block length value
       
   319 @internalComponent
       
   320 @param aBlockLen
       
   321 New block length value to set
       
   322 */
       
   323 inline void TSBCFrameParameters::SetBlockLength(TBlockLength aBlockLen)
       
   324 	{
       
   325 	// clear block length bits
       
   326 	iParameters &= ~(KSBCBlckLengBitsMask << KSBCBlckLengBitOffset);
       
   327 	// set new block length bits
       
   328 	iParameters |= ( (aBlockLen & KSBCBlckLengBitsMask) << KSBCBlckLengBitOffset);
       
   329 	}
       
   330 
       
   331 /**
       
   332 This function gets the channel mode enum value
       
   333 @internalComponent
       
   334 @return channel mode enum value
       
   335 */
       
   336 inline TUint8 TSBCFrameParameters::ChannelMode() const
       
   337 	{
       
   338 	return static_cast<TUint8>( (iParameters >> KSBCChnlModeBitOffset) & KSBCChnlModeBitsMask);
       
   339 	}
       
   340 
       
   341 /**
       
   342 This function gets number of channels
       
   343 @internalComponent
       
   344 @return number of channels
       
   345 */
       
   346 inline TUint8 TSBCFrameParameters::Channels() const
       
   347 	{
       
   348 	switch (ChannelMode() )
       
   349 		{
       
   350 		case EMono:
       
   351 			return 1;
       
   352 			
       
   353 		case EDualChannel:
       
   354 		case EStereo:
       
   355 		case EJointStereo:
       
   356 			return 2;
       
   357 		}
       
   358 	return 0;
       
   359 	}
       
   360 	
       
   361 /**
       
   362 This function sets the channel mode enum value
       
   363 @internalComponent
       
   364 @param aChnlMode
       
   365 New channel mode enum value to set
       
   366 */
       
   367 inline void TSBCFrameParameters::SetChannelMode(TChannelMode aChnlMode)
       
   368 	{
       
   369 	// clear channel mode bits
       
   370 	iParameters &= ~(KSBCChnlModeBitsMask << KSBCChnlModeBitOffset);
       
   371 	// set new channel mode bits
       
   372 	iParameters |= ( (aChnlMode & KSBCChnlModeBitsMask) << KSBCChnlModeBitOffset);
       
   373 	}
       
   374 
       
   375 /**
       
   376 This function gets the allocation method enum value
       
   377 @internalComponent
       
   378 @return allocation method enum value
       
   379 */
       
   380 inline TUint8 TSBCFrameParameters::AllocationMethod() const
       
   381 	{
       
   382 	return static_cast<TUint8>( (iParameters >> KSBCAllcMthdBitOffset) & KSBCAllcMthdBitsMask);
       
   383 	}
       
   384 
       
   385 /**
       
   386 This function sets the channel mode enum value
       
   387 @internalComponent
       
   388 @param aAllocMethod
       
   389 New channel mode enum value to set
       
   390 */
       
   391 inline void TSBCFrameParameters::SetAllocationMethod(TAllocationMethod aAllocMethod)
       
   392 	{
       
   393 	// clear allocation method bits
       
   394 	iParameters &= ~(KSBCAllcMthdBitsMask << KSBCAllcMthdBitOffset);
       
   395 	// set new allocation method bits
       
   396 	iParameters |= ( (aAllocMethod & KSBCAllcMthdBitsMask) << KSBCAllcMthdBitOffset);
       
   397 	}		
       
   398 
       
   399 /**
       
   400 This function gets the subbands enum value
       
   401 @internalComponent
       
   402 @return subbands enum value
       
   403 */
       
   404 inline TUint8 TSBCFrameParameters::SubbandsEnum() const
       
   405 	{
       
   406 	return static_cast<TUint8>( (iParameters >> KSBCSubbandsBitOffset) & KSBCSubbandsBitsMask);
       
   407 	}
       
   408 
       
   409 /**
       
   410 This function gets the subbands value
       
   411 @internalComponent
       
   412 @return subbands value, i.e 4, 8
       
   413 */
       
   414 inline TUint8 TSBCFrameParameters::Subbands() const
       
   415 	{
       
   416 	switch (SubbandsEnum() )
       
   417 		{
       
   418 		case E4Subbands:
       
   419 			return 4;
       
   420 			
       
   421 		case E8Subbands:
       
   422 			return 8;
       
   423 		}
       
   424 	return 0;
       
   425 	}
       
   426 	
       
   427 /**
       
   428 This function sets the subbands enum value
       
   429 @internalComponent
       
   430 @param aSubbands
       
   431 New subbands enum value to set
       
   432 */
       
   433 inline void TSBCFrameParameters::SetSubbands(TSubbands aSubbands)
       
   434 	{
       
   435 	// clear subbands bits
       
   436 	iParameters &= ~(KSBCSubbandsBitsMask << KSBCSubbandsBitOffset);
       
   437 	// set new subbands bits
       
   438 	iParameters |= ( (aSubbands & KSBCSubbandsBitsMask) << KSBCSubbandsBitOffset);
       
   439 	}		
       
   440 
       
   441 /**
       
   442 This function gets the bitpool value
       
   443 @internalComponent
       
   444 @return bitpool value
       
   445 */
       
   446 inline TUint8 TSBCFrameParameters::Bitpool() const
       
   447 	{
       
   448 	return iBitpool;
       
   449 	}
       
   450 
       
   451 /**
       
   452 This function sets the bitpool enum value
       
   453 @internalComponent
       
   454 @param aSubbands
       
   455 New bitpool enum value to set
       
   456 */
       
   457 inline void TSBCFrameParameters::SetBitpool(TUint8 aBitpool)
       
   458 	{
       
   459 	iBitpool = aBitpool;
       
   460 	}
       
   461 
       
   462 /**
       
   463 This function gets the 5 parameters (except bitpool) byte value
       
   464 @internalComponent
       
   465 @return 5 parameters byte value
       
   466 */
       
   467 inline TUint8 TSBCFrameParameters::Parameters() const
       
   468 	{
       
   469 	return iParameters;
       
   470 	}
       
   471 	
       
   472 /**
       
   473 This function checks the bitpool value according to:
       
   474 1. bitpool >= 2 and bitpool <= 250
       
   475 2. bitpool <= 16 * subbands for Mono and Dual Channel,
       
   476    bitpool <= 32 * subbands for Stereo and Joint Stereo 
       
   477 3. results in bit_rate <= 320 kbps for Mono
       
   478    results in bit_rate <= 512 kpbs for two-channel modes
       
   479 @internalComponent
       
   480 @return -1 if invalid; 0 if valid
       
   481 */
       
   482 inline TInt TSBCFrameParameters::Validate() const
       
   483 	{
       
   484 	if (iBitpool < KSBCMinBitpoolValue || iBitpool > KSBCMaxBitpoolValue)
       
   485 		{
       
   486 		return -1;
       
   487 		}
       
   488 		
       
   489 	const TUint16 numSubbands = Subbands(); // use 16 bits to avoid overflow
       
   490 	const TUint8 channelMode = ChannelMode();
       
   491 	
       
   492 	if (channelMode == EMono || channelMode == EDualChannel)
       
   493 		{
       
   494 		// bitpool <= 16 * subbands, for Mono and Dual_Channel modes
       
   495 		if (iBitpool > (numSubbands << 4) )
       
   496 			{
       
   497 			return -1;
       
   498 			}
       
   499 		}
       
   500 	else
       
   501 		{
       
   502 		// bitpool <= 32 * subbands, for Stereo and Joint_Stereo modes
       
   503 		if (iBitpool > (numSubbands << 5) )
       
   504 			{
       
   505 			return -1;
       
   506 			}
       
   507 		}
       
   508 	
       
   509 	if (channelMode == EMono)
       
   510 		{
       
   511 		// bit rate <= 320kbps for Mono mode
       
   512 		if (CalcBitRate(CalcFrameLength() ) > 320)
       
   513 			{
       
   514 			return -1;
       
   515 			}
       
   516 		}
       
   517 	else
       
   518 		{
       
   519 		// bit rate <= 512kpbs for two-channels modes
       
   520 		if (CalcBitRate(CalcFrameLength() ) > 512)
       
   521 			{
       
   522 			return -1;
       
   523 			}
       
   524 		}
       
   525 	
       
   526 	return 0;
       
   527 	}
       
   528 
       
   529 /**
       
   530 This function calculates the frame length value according to:
       
   531 1. for MONO or DUAL_CHANNEL
       
   532    frame_len = 4 + (4 * subbands * channels) / 8 + ceil(blocks * channels * bitpool / 8)
       
   533 2. for STEREO
       
   534    frame_len = 4 + (4 * subbands * channels) / 8 + ceil(blocks * bitpool / 8)
       
   535 3. for JOINT_STEREO
       
   536    frame_len = 4 + (4 * subbands * channels) / 8 + ceil((subbands + blocks * bitpool) / 8)
       
   537 ceil(), taking the upper integer value
       
   538 @internalComponent
       
   539 @return frame length value
       
   540 */
       
   541 inline TUint TSBCFrameParameters::CalcFrameLength() const
       
   542 	{
       
   543 	TUint temp = 0;
       
   544 	switch (ChannelMode() )
       
   545 		{
       
   546 		case EMono:
       
   547 			temp = BlockLength() * Bitpool(); // blocks * bitpool
       
   548 			break;
       
   549 			
       
   550 		case EDualChannel:
       
   551 			temp = (BlockLength() * Bitpool() ) << 1; // blocks * bitpool * 2
       
   552 			break;
       
   553 			
       
   554 		case EStereo:
       
   555 			temp = BlockLength() * Bitpool(); // blocks * bitpool
       
   556 			break;
       
   557 			
       
   558 		case EJointStereo:
       
   559 			temp = Subbands() + BlockLength() * Bitpool(); // subbands + blocks * bitpool
       
   560 			break;
       
   561 			
       
   562 		default:
       
   563 			User::Panic(_L("Invalid channel mode"), KErrNotSupported);
       
   564 			break;
       
   565 		}
       
   566 		
       
   567 	TUint frameLen = 4 + ( (Subbands() * Channels() ) >> 1) + (temp >> 3);
       
   568 	if (temp & 0x7)
       
   569 		{
       
   570 		frameLen++;
       
   571 		}
       
   572 		
       
   573 	return frameLen;
       
   574 	}
       
   575 
       
   576 /**
       
   577 This function calculates the bit rate value according to:
       
   578 	bit_rate = 8 * frame_len * sampling_freq / subbands / blocks
       
   579 @internalComponent
       
   580 @return bit rate value in kHz
       
   581 */
       
   582 inline TUint TSBCFrameParameters::CalcBitRate(TUint aFrameLen) const
       
   583 	{
       
   584 	return (aFrameLen << 3) * SamplingFrequencyHz() / (Subbands() * BlockLength() * 1000);
       
   585 	}
       
   586 
       
   587 #endif // __SBCFRAMEPARAMETERS_H__
       
   588