multimediacommscontroller/mmccamrpayloadformat/inc/amrpayloadformatter.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Formatter classes for encoding/decoding of RTP payload format
       
    15 *                for AMR ( narrow band ) and AMR-WB ( wide band ) audio codecs.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #ifndef AMRPAYLOADFORMATTER_H
       
    23 #define AMRPAYLOADFORMATTER_H
       
    24 
       
    25 // INCLUDES
       
    26 #include <e32base.h>
       
    27 #include "amrpayloadformatutil.h"
       
    28 #include "mccinternalevents.h"
       
    29 #include "amrframe.h"
       
    30 
       
    31 #ifdef FTD_ENABLED
       
    32 #include <e32msgqueue.h>
       
    33 #include "MccCodecStats.h"
       
    34 #include "MccJBufferStats.h"
       
    35 #endif
       
    36 
       
    37 #ifdef _DEBUG 
       
    38     #include <e32svr.h>
       
    39     #define _DEBUG_AMR_DECODE
       
    40     #define _DEBUG_AMR_ENCODE 
       
    41     #define TRACE_AMR_PAYLOAD_FORMAT_READ
       
    42     #define TRACE_AMR_PAYLOAD_FORMAT_WRITE
       
    43     #define TRACE_PAYLOAD_FORMAT
       
    44 #endif
       
    45 
       
    46   
       
    47 
       
    48 // CONSTANTS
       
    49 const TUint KAmrNbSampleRate = 8000;
       
    50 const TUint KAmrWbSampleRate = 16000;
       
    51 
       
    52 const TUint KCMRFieldBits = 4;
       
    53 // Max two frames is used with AMR FEC redundancy
       
    54 const TInt KMaxAmrRedCount      =   5;
       
    55 
       
    56 // Max number of frames to save for redundancy detection
       
    57 const TInt KMaxAmrFramesSaved   =   5;
       
    58 
       
    59 // How many octets are compared while determining is a frame already received
       
    60 const TInt KMaxCompareOctets    =   6;
       
    61 
       
    62 const TInt KMaxChannelCount = 2;
       
    63 const TInt KMaxFrameBlocksPerPacket = 20;
       
    64 const TInt KMaxFrameCountPerPacket
       
    65     = KMaxChannelCount * ( KMaxFrameBlocksPerPacket + KMaxAmrRedCount );
       
    66 
       
    67 const TInt KHeaderBitsBE = 4;
       
    68 const TInt KHeaderBitsOA = 8;
       
    69 const TInt KTOCFieldBitsBE = 6;
       
    70 const TInt KTOCFieldBitsOA = 8;
       
    71 
       
    72 const TInt KNumberOfNbModes = 8;
       
    73 // In bytes; equals to 
       
    74 // [( KMaxFrameCountPerPacket * maximum-frame-size ) + header + TOC + CRC]
       
    75 // in octet-aligned mode maximum-frame-size = 31 ( AMR-NB ); header = 1;
       
    76 // TOC = CRC = KMaxFrameCountPerPacket
       
    77 
       
    78 // Refer to 3GPP TS 26.101 V5.0.0 and RFC 3267
       
    79 // the total number of bits in speech data for different AMR frame types
       
    80 //                                    total speech
       
    81 //                Index   Mode           bits
       
    82 //                ----------------------------
       
    83 //                  0     AMR 4.75        95
       
    84 //                  1     AMR 5.15       103
       
    85 //                  2     AMR 5.9        118
       
    86 //                  3     AMR 6.7        134
       
    87 //                  4     AMR 7.4        148
       
    88 //                  5     AMR 7.95       159
       
    89 //                  6     AMR 10.2       204
       
    90 //                  7     AMR 12.2       244
       
    91 //                  8     AMR SID         39
       
    92 
       
    93 const TInt KNumberOfNbFrameTypes = 9;
       
    94 const TInt KAmrNbTotalSpeechBits[KNumberOfNbFrameTypes] =
       
    95     {95, 103, 118, 134, 148, 159, 204, 244, 39};
       
    96 const TInt KMaxBitsLength = 32; // max length in bits that TStreamFormatter 
       
    97                                 // can handle once when encoding and decoding
       
    98 const TInt KAmrNbModes[KNumberOfNbModes] =
       
    99     {4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200};
       
   100 
       
   101 const TInt KNumberOfWbModes = 9;
       
   102 // Refer to 3GPP TS 26.201 V7.0.0 and RFC 3267
       
   103 // the total number of bits in speech data for different AMR frame types
       
   104 //                                    total speech
       
   105 //                Index   Mode           bits
       
   106 //                ----------------------------
       
   107 //                  0     AMR-WB 6.60    132
       
   108 //                  1     AMR-WB 8.85    177
       
   109 //                  2     AMR-WB 12.65   253
       
   110 //                  3     AMR-WB 14.25   285
       
   111 //                  4     AMR-WB 15.85   317
       
   112 //                  5     AMR-WB 18.25   365
       
   113 //                  6     AMR-WB 19.85   397
       
   114 //                  7     AMR-WB 23.05   461
       
   115 //                  8     AMR-WB 23.85   477
       
   116 //                  9     AMR-WB SID      40
       
   117 
       
   118 const TInt KNumberOfWbFrameTypes = 10;
       
   119 const TInt KAmrWbTotalSpeechBits[KNumberOfWbFrameTypes] =
       
   120     {132, 177,253, 285, 317, 365, 397, 461, 477, 40};
       
   121 const TInt KAmrWbModes[KNumberOfWbModes] =
       
   122     {6600, 8850, 12650, 14250, 15850, 18250, 19850, 23050, 23850};
       
   123 
       
   124 // MACROS
       
   125 #ifdef _DEBUG_AMR_DECODE
       
   126     #define DP_AMR_DECODE(a) { _LIT( KName, a); RDebug::Print(KName); }
       
   127     #define DP_AMR_DECODE2(a, b) \
       
   128     	{ _LIT(KName, a); RDebug::Print(KName, b); }
       
   129  	#define DP_AMR_DECODE3(a, b, c) \
       
   130  		{ _LIT(KName, a);  RDebug::Print(KName, b, c); }
       
   131  	#define DP_AMR_DECODE4(a, b, c, d) \
       
   132  		{ _LIT(KName, a);  RDebug::Print(KName, b, c, d); }
       
   133  	#define DP_AMR_DECODE5(a, b, c, d, e) \
       
   134  		{ _LIT(KName, a);  RDebug::Print(KName, b, c, d, e); }
       
   135  	#define DP_AMR_DECODE6(a, b, c, d, e, f) \
       
   136  		{ _LIT(KName, a);  RDebug::Print(KName, b, c, d, e, f); }
       
   137 #else
       
   138     #define DP_AMR_DECODE(a);
       
   139     #define DP_AMR_DECODE2(a, b);
       
   140     #define DP_AMR_DECODE3(a, b, c);
       
   141     #define DP_AMR_DECODE4(a, b, c, d);
       
   142     #define DP_AMR_DECODE5(a, b, c, d, e);
       
   143     #define DP_AMR_DECODE6(a, b, c, d, e, f);
       
   144 #endif
       
   145 
       
   146 #ifdef _DEBUG_AMR_ENCODE
       
   147     #define DP_AMR_ENCODE(a) { _LIT( KName, a); RDebug::Print(KName); }
       
   148   	#define DP_AMR_ENCODE2(a, b) \
       
   149   		{ _LIT(KName, a); RDebug::Print(KName, b); }
       
   150     #define DP_AMR_ENCODE3(a, b, c) \
       
   151     	{ _LIT(KName, a);  RDebug::Print(KName, b, c); }
       
   152     #define DP_AMR_ENCODE4(a, b, c, d) \
       
   153     	{ _LIT(KName, a); RDebug::Print(KName, b, c, d); }
       
   154   	#define DP_AMR_ENCODE5(a, b, c, d, e) \
       
   155   		{ _LIT(KName, a); RDebug::Print(KName, b, c, d, e); }
       
   156   	#define DP_AMR_ENCODE6(a, b, c, d, e, f) \
       
   157   		{ _LIT(KName, a);  RDebug::Print(KName, b, c, d, e, f); }  
       
   158   	#define DP_AMR_ENCODE7(a, b, c, d, e, f, g) \
       
   159   		{ _LIT(KName, a);  RDebug::Print(KName, b, c, d, e, f, g); }  
       
   160 #else
       
   161     #define DP_AMR_ENCODE(a);
       
   162     #define DP_AMR_ENCODE2(a, b);
       
   163     #define DP_AMR_ENCODE3(a, b, c);
       
   164     #define DP_AMR_ENCODE4(a, b, c, d);
       
   165     #define DP_AMR_ENCODE5(a, b, c, d, e);
       
   166     #define DP_AMR_ENCODE6(a, b, c, d, e, f);
       
   167     #define DP_AMR_ENCODE7(a, b, c, d, e, f, g);
       
   168 #endif
       
   169 
       
   170 #ifdef TRACE_AMR_PAYLOAD_FORMAT_READ
       
   171     #define AMR_PAYLOAD_FORMAT_READ(a) \
       
   172     	{ _LIT( KName, a); RDebug::Print(KName); }
       
   173   	#define AMR_PAYLOAD_FORMAT_READ2(a, b) \
       
   174   		{ _LIT(KName, a);  RDebug::Print(KName, b); }
       
   175     #define AMR_PAYLOAD_FORMAT_READ3(a, b, c) \
       
   176     	{ _LIT(KName, a);  RDebug::Print(KName, b, c); }
       
   177     #define AMR_PAYLOAD_FORMAT_READ4(a, b, c, d) \
       
   178     	{ _LIT(KName, a); RDebug::Print(KName, b, c, d); }
       
   179     #define AMR_PAYLOAD_FORMAT_READ5(a, b, c, d, e) \
       
   180     	{ _LIT(KName, a); RDebug::Print(KName, b, c, d, e); }
       
   181 #else
       
   182 	#define AMR_PAYLOAD_FORMAT_READ(a);
       
   183 	#define AMR_PAYLOAD_FORMAT_READ2(a, b);
       
   184 	#define AMR_PAYLOAD_FORMAT_READ3(a, b, c);
       
   185 	#define AMR_PAYLOAD_FORMAT_READ4(a, b, c, d);
       
   186 	#define AMR_PAYLOAD_FORMAT_READ5(a, b, c, d, e)
       
   187 #endif
       
   188 
       
   189 #ifdef TRACE_AMR_PAYLOAD_FORMAT_WRITE
       
   190     #define AMR_PAYLOAD_FORMAT_WRITE(a) \
       
   191     	{ _LIT( KName, a); RDebug::Print(KName); }
       
   192   	#define AMR_PAYLOAD_FORMAT_WRITE2(a, b) \
       
   193   		{ _LIT(KName, a);RDebug::Print(KName, b); }
       
   194     #define AMR_PAYLOAD_FORMAT_WRITE3(a, b, c) \
       
   195     	{ _LIT(KName, a);  RDebug::Print(KName, b, c); }
       
   196     #define AMR_PAYLOAD_FORMAT_WRITE4(a, b, c, d) \
       
   197     	{ _LIT(KName, a); RDebug::Print(KName, b, c, d); }
       
   198 #else
       
   199 	#define AMR_PAYLOAD_FORMAT_WRITE(a)
       
   200 	#define AMR_PAYLOAD_FORMAT_WRITE2(a, b)
       
   201 	#define AMR_PAYLOAD_FORMAT_WRITE3(a, b, c)
       
   202 	#define AMR_PAYLOAD_FORMAT_WRITE4(a, b, c, d)
       
   203 #endif
       
   204     
       
   205 #ifdef TRACE_PAYLOAD_FORMAT
       
   206     #define TRACE_PAYLOAD_FORMAT_PRINT(a) \
       
   207     	{ _LIT( KName, a); RDebug::Print(KName); }
       
   208 #else
       
   209 	#define TRACE_PAYLOAD_FORMAT_PRINT(a)
       
   210 #endif
       
   211   
       
   212 
       
   213 // DATA TYPES
       
   214 
       
   215 
       
   216 // CLASS DECLARATION
       
   217 
       
   218 
       
   219 /**
       
   220 *  The AMR payload formatter for an RTP packet.
       
   221 *
       
   222 *  @lib <TBD>
       
   223 *  @since Series-60 2.1
       
   224 */
       
   225 class CAmrPayloadFormatter : public CBase
       
   226     {
       
   227     public:  // Constructors and destructor
       
   228 
       
   229         /**
       
   230         * Destructor.
       
   231         */
       
   232         virtual ~CAmrPayloadFormatter( );
       
   233 
       
   234     public: // New functions
       
   235 
       
   236         /**
       
   237         * Initializes AMR encoder/decoder. Initialization must be done when playback
       
   238         * is started.
       
   239         * @since    Series 60 3.0
       
   240         */
       
   241         void Initialize();
       
   242         
       
   243         /**
       
   244         * Set payload buffer.
       
   245         * Calling function must have allocated memory for the buffer.
       
   246         * @since Series-60 2.1
       
   247         * @param aBuffer - [input] Descriptor of payload buffer
       
   248         * @return None
       
   249         */
       
   250         virtual void SetPayloadBuffer( TDes8& aBuffer );
       
   251 
       
   252         /**
       
   253         * Set number of audio channels.
       
   254         * @since Series-60 2.1
       
   255         * @param aChannelCount - [input] Number of audio channels
       
   256         * @return TInt - KErrNone if successful; system wide error code otherwise
       
   257         */
       
   258         virtual TInt SetChannelCount( TInt aChannelCount );
       
   259 
       
   260         /**
       
   261         * Set number of AMR frame blocks included in one RTP packet.
       
   262         * Each frame block contains frames for all audio channels.
       
   263         * @since Series-60 2.1
       
   264         * @param aFrameblockCount - [input] Number of AMR frame blocks included 
       
   265         * in one RTP packet
       
   266         * @return TInt - KErrNone if successful; system wide error code otherwise
       
   267         */
       
   268         virtual TInt SetFrameBlockCount( TInt aFrameblockCount );
       
   269 
       
   270         /**
       
   271         * Get the payload buffer.
       
   272         * @since Series-60 2.1
       
   273         * @return TDes8& - Descriptor of payload buffer
       
   274         */
       
   275         inline TDes8& PayloadBuffer( ) { return iPayloadPtr; };
       
   276         
       
   277         /**
       
   278         * Get number of speech bits in an AMR frame
       
   279         * @since Series-60 2.1
       
   280         * @param aFrameType - [input] AMR frame type
       
   281         * @return TInt - Number of speech bits in an AMR frame
       
   282         */
       
   283         TInt SpeechBitCount( enum TAmrFrameType aFrameType ) const; 
       
   284 
       
   285         /**
       
   286         * Get number of audio channels.
       
   287         * @since Series-60 2.1
       
   288         * @return TInt - Number of audio channels
       
   289         */
       
   290         inline TInt ChannelCount( ) const { return iChannelCount; };
       
   291 
       
   292         /**
       
   293         * Get number of AMR frame blocks included in one RTP packet.
       
   294         * Each frame block contains frames for all audio channels.
       
   295         * @since Series-60 2.1
       
   296         * @return TInt - Number of AMR frame blocks
       
   297         */
       
   298         inline TInt FrameBlockCount( ) const { return iFrameBlockCount; };
       
   299 
       
   300         /**
       
   301         * Get number of AMR frame included in one RTP packet.
       
   302         * @since Series-60 2.1
       
   303         * @return TInt - Number of AMR frames
       
   304         */
       
   305         inline TInt FrameCount( ) const { return iFrameCount; };
       
   306 
       
   307     protected:  // Functions
       
   308 
       
   309         /**
       
   310         * Constructor.
       
   311         */
       
   312         CAmrPayloadFormatter( TBool aIsNb );
       
   313         
       
   314         virtual void DoInitialize() = 0;
       
   315 
       
   316     protected:  // Data
       
   317 
       
   318 		// TRUE, if this is formater for narrow band AMR. FALSE for wide band.
       
   319 		TBool iIsNb;
       
   320 
       
   321         // Payload parameters
       
   322         // Number of audio channels
       
   323         TInt iChannelCount;             
       
   324         
       
   325         // Number of AMR frame blocks included in one packet
       
   326         TInt iFrameBlockCount;
       
   327         
       
   328         // Number of AMR frames included in one packet
       
   329         TInt iFrameCount;
       
   330         
       
   331         // Codec mode request
       
   332         TAmrModeRequest iModeRequest;
       
   333 
       
   334         // Frame information
       
   335         // AMR frames for one RTP packet
       
   336         TAmrFrame iFrames[KMaxFrameCountPerPacket];
       
   337 
       
   338         // Current index to `iFrames' array
       
   339         TInt iFrameIndex;
       
   340 
       
   341         // Payload data buffer info
       
   342         // Pointer descriptor for payload buffer
       
   343         TPtr8 iPayloadPtr;
       
   344         
       
   345         // Pointer to payload buffer
       
   346         TUint8* iPayload;
       
   347         
       
   348         // Size, in bytes, of payload buffer
       
   349         TInt iPayloadSize;
       
   350         
       
   351         // Frames either for redundancy detection or redundancy sending
       
   352         RPointerArray<HBufC8> iFrameDatas;
       
   353         
       
   354         #ifdef FTD_ENABLED
       
   355 		// Message queue carrying codec statistics
       
   356         RMsgQueue<TMccCodecStats> iCodecStatsQueue;
       
   357 
       
   358 		// Message queue carrying jitter buffer statistics
       
   359         RMsgQueue<TMccJBufferStats> iJBufStatsQueue;
       
   360         #endif
       
   361         
       
   362     };
       
   363 
       
   364 #endif      // __AMRPAYLOADFORMATTER_H
       
   365 
       
   366 // End of File