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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #ifndef __SBCFRAMEPARAMETERS_H__
17 #define __SBCFRAMEPARAMETERS_H__
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.
25 class TSBCFrameParameters
29 This enum list all the possible sampling frequency settings
31 enum TSamplingFrequency
34 sampling frequency is 16000 Hz
38 sampling frequency is 32000 Hz
42 sampling frequency is 44100 Hz
46 sampling frequency is 48000 Hz
52 This enum list all the possible block length settings
57 block length is 4, one frame contains 4 blocks of audio samples
61 block length is 8, one frame contains 8 blocks of audio samples
65 block length is 12, one frame contains 12 blocks of audio samples
69 block length is 16, one frame contains 16 blocks of audio samples
75 This enum list all the possible channel mode settings
80 channel mode is Mono, in this mode,
81 only one channel contains audio samples.
85 channel mode is Dual Channel, in this mode,
86 it contains two seperate mono audio samples.
90 channel mode is Stereo, in this mode,
91 it contains stereo audio samples.
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.
103 This enum list all the possible allocation method settings
105 enum TAllocationMethod
108 allocation method is Loudness, in this mode,
109 the bit allocation calculation uses Table offset4 or offset8 as well as scale factors
113 allocation method is SNR, in this mode,
114 bit allocation only uses scale factors
120 This enum list all the possible subbands settings
125 subbands is 4, each channel contains 4 subbands in each block,
126 each subband contains one sample
130 subbands is 8, each channel contains 8 subbands in each block,
131 each subband contains one sample
137 inline TSBCFrameParameters();
141 inline TUint8 SamplingFrequencyEnum() const;
142 inline TUint SamplingFrequencyHz() const;
143 inline void SetSamplingFrequency(TSamplingFrequency aSamplingFrequency);
145 inline TUint8 BlockLength() const;
146 inline void SetBlockLength(TBlockLength aBlockLength);
148 inline TUint8 ChannelMode() const;
149 inline TUint8 Channels() const;
150 inline void SetChannelMode(TChannelMode aChannelMode);
152 inline TUint8 AllocationMethod() const;
153 inline void SetAllocationMethod(TAllocationMethod aAllocationMethod);
155 inline TUint8 SubbandsEnum() const;
156 inline TUint8 Subbands() const;
157 inline void SetSubbands(TSubbands aSubbands);
159 inline TUint8 Bitpool() const;
160 inline void SetBitpool(TUint8 aBitpool);
162 inline TUint8 Parameters() const;
163 inline TInt Validate() const;
165 inline TUint CalcFrameLength() const;
166 inline TUint CalcBitRate(TUint aFrameLength) const;
174 The minimum SBC bitpool value is 2
176 const TUint8 KSBCMinBitpoolValue = 2;
178 The maximum SBC bitpool value is 250
180 const TUint8 KSBCMaxBitpoolValue = 250;
183 The sampling frequency bits mask is 0b11, 2 bits
185 const TUint8 KSBCSampFreqBitsMask = 0x3;
187 The bit offset of sampling frequency field in TSBCFrameParameters::iParameters is 6
189 const TUint8 KSBCSampFreqBitOffset = 6;
192 The block length bits mask is 0b11, 2 bits
194 const TUint8 KSBCBlckLengBitsMask = 0x3;
196 The bit offset of block length field in TSBCFrameParameters::iParameters is 4
198 const TUint8 KSBCBlckLengBitOffset = 4;
201 The block length bits mask is 0b11, 2 bits
203 const TUint8 KSBCChnlModeBitsMask = 0x3;
205 The bit offset of block length field in TSBCFrameParameters::iParameters is 2
207 const TUint8 KSBCChnlModeBitOffset = 2;
210 The block length bits mask is 0b01, 1 bit
212 const TUint8 KSBCAllcMthdBitsMask = 0x1;
214 The bit offset of block length field in TSBCFrameParameters::iParameters is 1
216 const TUint8 KSBCAllcMthdBitOffset = 1;
219 The block length bits mask is 0b01, 1 bit
221 const TUint8 KSBCSubbandsBitsMask = 0x1;
223 The bit offset of block length field in TSBCFrameParameters::iParameters is 0
225 const TUint8 KSBCSubbandsBitOffset = 0;
231 inline TSBCFrameParameters::TSBCFrameParameters() : iParameters(0), iBitpool(0)
236 This function reset all the parameters
239 inline void TSBCFrameParameters::Reset()
246 This function gets the sampling frequency enum value
248 @return enum value of sampling frequency
250 inline TUint8 TSBCFrameParameters::SamplingFrequencyEnum() const
252 return static_cast<TUint8>( (iParameters >> KSBCSampFreqBitOffset) & KSBCSampFreqBitsMask);
256 This function gets the sampling frequency value in Hz
258 @return samplinng frequency value in Hz
260 inline TUint TSBCFrameParameters::SamplingFrequencyHz() const
262 switch (SamplingFrequencyEnum() )
280 This function sets the sampling frequency value
283 New sampling frequency enum value to set
285 inline void TSBCFrameParameters::SetSamplingFrequency(TSamplingFrequency aSampFreq)
287 // clear sampling frequency bits
288 iParameters &= ~(KSBCSampFreqBitsMask << KSBCSampFreqBitOffset);
289 // set new sampling frequency bits
290 iParameters |= ( (aSampFreq & KSBCSampFreqBitsMask) << KSBCSampFreqBitOffset);
294 This function gets the block length value
296 @return number of blocks in one frame
298 inline TUint8 TSBCFrameParameters::BlockLength() const
300 switch ( (iParameters >> KSBCBlckLengBitOffset) & KSBCBlckLengBitsMask)
318 This function sets the block length value
321 New block length value to set
323 inline void TSBCFrameParameters::SetBlockLength(TBlockLength aBlockLen)
325 // clear block length bits
326 iParameters &= ~(KSBCBlckLengBitsMask << KSBCBlckLengBitOffset);
327 // set new block length bits
328 iParameters |= ( (aBlockLen & KSBCBlckLengBitsMask) << KSBCBlckLengBitOffset);
332 This function gets the channel mode enum value
334 @return channel mode enum value
336 inline TUint8 TSBCFrameParameters::ChannelMode() const
338 return static_cast<TUint8>( (iParameters >> KSBCChnlModeBitOffset) & KSBCChnlModeBitsMask);
342 This function gets number of channels
344 @return number of channels
346 inline TUint8 TSBCFrameParameters::Channels() const
348 switch (ChannelMode() )
362 This function sets the channel mode enum value
365 New channel mode enum value to set
367 inline void TSBCFrameParameters::SetChannelMode(TChannelMode aChnlMode)
369 // clear channel mode bits
370 iParameters &= ~(KSBCChnlModeBitsMask << KSBCChnlModeBitOffset);
371 // set new channel mode bits
372 iParameters |= ( (aChnlMode & KSBCChnlModeBitsMask) << KSBCChnlModeBitOffset);
376 This function gets the allocation method enum value
378 @return allocation method enum value
380 inline TUint8 TSBCFrameParameters::AllocationMethod() const
382 return static_cast<TUint8>( (iParameters >> KSBCAllcMthdBitOffset) & KSBCAllcMthdBitsMask);
386 This function sets the channel mode enum value
389 New channel mode enum value to set
391 inline void TSBCFrameParameters::SetAllocationMethod(TAllocationMethod aAllocMethod)
393 // clear allocation method bits
394 iParameters &= ~(KSBCAllcMthdBitsMask << KSBCAllcMthdBitOffset);
395 // set new allocation method bits
396 iParameters |= ( (aAllocMethod & KSBCAllcMthdBitsMask) << KSBCAllcMthdBitOffset);
400 This function gets the subbands enum value
402 @return subbands enum value
404 inline TUint8 TSBCFrameParameters::SubbandsEnum() const
406 return static_cast<TUint8>( (iParameters >> KSBCSubbandsBitOffset) & KSBCSubbandsBitsMask);
410 This function gets the subbands value
412 @return subbands value, i.e 4, 8
414 inline TUint8 TSBCFrameParameters::Subbands() const
416 switch (SubbandsEnum() )
428 This function sets the subbands enum value
431 New subbands enum value to set
433 inline void TSBCFrameParameters::SetSubbands(TSubbands aSubbands)
435 // clear subbands bits
436 iParameters &= ~(KSBCSubbandsBitsMask << KSBCSubbandsBitOffset);
437 // set new subbands bits
438 iParameters |= ( (aSubbands & KSBCSubbandsBitsMask) << KSBCSubbandsBitOffset);
442 This function gets the bitpool value
444 @return bitpool value
446 inline TUint8 TSBCFrameParameters::Bitpool() const
452 This function sets the bitpool enum value
455 New bitpool enum value to set
457 inline void TSBCFrameParameters::SetBitpool(TUint8 aBitpool)
463 This function gets the 5 parameters (except bitpool) byte value
465 @return 5 parameters byte value
467 inline TUint8 TSBCFrameParameters::Parameters() const
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
480 @return -1 if invalid; 0 if valid
482 inline TInt TSBCFrameParameters::Validate() const
484 if (iBitpool < KSBCMinBitpoolValue || iBitpool > KSBCMaxBitpoolValue)
489 const TUint16 numSubbands = Subbands(); // use 16 bits to avoid overflow
490 const TUint8 channelMode = ChannelMode();
492 if (channelMode == EMono || channelMode == EDualChannel)
494 // bitpool <= 16 * subbands, for Mono and Dual_Channel modes
495 if (iBitpool > (numSubbands << 4) )
502 // bitpool <= 32 * subbands, for Stereo and Joint_Stereo modes
503 if (iBitpool > (numSubbands << 5) )
509 if (channelMode == EMono)
511 // bit rate <= 320kbps for Mono mode
512 if (CalcBitRate(CalcFrameLength() ) > 320)
519 // bit rate <= 512kpbs for two-channels modes
520 if (CalcBitRate(CalcFrameLength() ) > 512)
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)
534 frame_len = 4 + (4 * subbands * channels) / 8 + ceil(blocks * bitpool / 8)
536 frame_len = 4 + (4 * subbands * channels) / 8 + ceil((subbands + blocks * bitpool) / 8)
537 ceil(), taking the upper integer value
539 @return frame length value
541 inline TUint TSBCFrameParameters::CalcFrameLength() const
544 switch (ChannelMode() )
547 temp = BlockLength() * Bitpool(); // blocks * bitpool
551 temp = (BlockLength() * Bitpool() ) << 1; // blocks * bitpool * 2
555 temp = BlockLength() * Bitpool(); // blocks * bitpool
559 temp = Subbands() + BlockLength() * Bitpool(); // subbands + blocks * bitpool
563 User::Panic(_L("Invalid channel mode"), KErrNotSupported);
567 TUint frameLen = 4 + ( (Subbands() * Channels() ) >> 1) + (temp >> 3);
577 This function calculates the bit rate value according to:
578 bit_rate = 8 * frame_len * sampling_freq / subbands / blocks
580 @return bit rate value in kHz
582 inline TUint TSBCFrameParameters::CalcBitRate(TUint aFrameLen) const
584 return (aFrameLen << 3) * SamplingFrequencyHz() / (Subbands() * BlockLength() * 1000);
587 #endif // __SBCFRAMEPARAMETERS_H__