mmfenh/advancedaudiocontroller/audiocontrollerpluginsvariant/AdvancedAudioControllerUtility/Src/AMRAudioControllerUtility.cpp
changeset 0 71ca22bcf22a
equal deleted inserted replaced
-1:000000000000 0:71ca22bcf22a
       
     1 /*
       
     2 * Copyright (c) 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:  Class definition for the AMR utility functions.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDES
       
    20 #include "AMRAudioControllerUtility.h"
       
    21 #include "DebugMacros.h"
       
    22 #include <mmfdatabuffer.h>
       
    23 
       
    24 // CONSTANTS
       
    25 _LIT8(KAmrMagicNumber, "#!AMR\n");
       
    26 
       
    27 // The size of AMR header, header must include bits for determining frame length
       
    28 const TInt KAmrFrameHeaderSize = 1;
       
    29 
       
    30 // Frame length table (number of frame bytes)
       
    31 const TInt KAmrFrameLength[16] = {13,14,16,18,20,21,27,32,6,0,0,0,0,0,0,1};
       
    32 
       
    33 // Stuffing length table (number of stuffing bits)
       
    34 //const TInt KAmrStuffLength[16] = {4, 4, 5, 5, 7, 4, 7, 7, 4,0,0,0,0,0,0,3};
       
    35 
       
    36 // Maximum number of PCM samples in one AMR frame
       
    37 const TInt KAmrMaxSamplesPerFrame160 = 160;
       
    38 
       
    39 // constant time for each frame = 0.020 sec
       
    40 const TInt KAmrMilliSecondsPerFrame  = 20;
       
    41 
       
    42 const TInt KAmrSampleRate8K = 8000;
       
    43 const TInt KAmrBitRate = 12800;
       
    44 const TInt KAmrChannels1 = 1;
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ===============================
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CAMRAudioControllerUtility::CAMRAudioControllerUtility
       
    50 // C++ default constructor can NOT contain any code, that
       
    51 // might leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CAMRAudioControllerUtility::CAMRAudioControllerUtility()
       
    55     {
       
    56 	iSamplingRate = KAmrSampleRate8K;
       
    57 	iBitRate = KAmrBitRate;
       
    58 	iChannels = KAmrChannels1;
       
    59 	iHeaderOffset = KAmrMagicNumber().Size(); // only because recorder controller needs this
       
    60     iFrameTimeMs = KAmrMilliSecondsPerFrame;
       
    61     iLenMetaData = 0;
       
    62     iDurationUs = 0;
       
    63     iTotalFrameBytes = 0;
       
    64     iTotalFrames = 0;
       
    65     iFrameDataOffset = 0;    
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CAMRAudioControllerUtility::ConstructL
       
    70 // Symbian 2nd phase constructor can leave.
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 void CAMRAudioControllerUtility::ConstructL()
       
    74     {
       
    75 	iPosArr = new(ELeave) CArrayFixSeg<TTimePos>(1024);
       
    76 	}
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CAMRAudioControllerUtility::NewL
       
    80 // Two-phased constructor.
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C CAMRAudioControllerUtility* CAMRAudioControllerUtility::NewL()
       
    84 	{
       
    85 	DP0(_L("CAMRAudioControllerUtility::NewL"));
       
    86     CAMRAudioControllerUtility* self = new(ELeave) CAMRAudioControllerUtility;
       
    87     CleanupStack::PushL(self);
       
    88     self->ConstructL();
       
    89     CleanupStack::Pop(self);
       
    90     return self;
       
    91     }
       
    92 
       
    93 // Destructor
       
    94 EXPORT_C CAMRAudioControllerUtility::~CAMRAudioControllerUtility()
       
    95     {
       
    96 	iPosArr->Reset();
       
    97 	delete iPosArr;
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CAMRAudioControllerUtility::SeekSync
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 TInt CAMRAudioControllerUtility::SeekSync(
       
   105 	CMMFDataBuffer* aBuf,
       
   106 	TInt aBufLen)
       
   107     {
       
   108     // aBufLen is still the total buffer size.
       
   109     // we'll use the buffer position as the starting point of the seek for a frame
       
   110     // the endPtr is the ending point of the seek
       
   111     
       
   112     const TUint8* buf = aBuf->Data().Ptr() + aBuf->Position();
       
   113     const TInt KMaxFrames = 3;          // number of frames to check
       
   114     const TInt KNotFound = aBufLen;     // sync not found position
       
   115     TAudioFrameInfo frameInfo;			// frame parameters
       
   116     TInt i = 0;
       
   117     TInt syncPos = KNotFound;
       
   118     TInt maxSeek = KMaxFrames;
       
   119     const TUint8* endPtr = buf + aBufLen - aBuf->Position();
       
   120     
       
   121     //Check if start position is same as end position
       
   122     //If so, the reset the start position to the beginning of the buffer
       
   123     //Bug-Id:ESLM-7ZFAHF
       
   124     if (buf == endPtr)
       
   125         {
       
   126         buf = aBuf->Data().Ptr();
       
   127         }
       
   128 
       
   129     // Seek a valid frame candidate byte by byte until a valid frame
       
   130     // is found or all bytes have been checked.
       
   131     while (buf < endPtr  &&  syncPos == KNotFound)
       
   132     	{
       
   133         TInt seekCount = 0;
       
   134         const TUint8* framePtr = buf;
       
   135         TInt frameBufLen = aBufLen;
       
   136         syncPos = i;
       
   137         // Check the validity of this frame candidate and the nearest next
       
   138         // frames. If they are not OK, syncPos will be set to KNotFound.
       
   139         while (framePtr < endPtr  &&  syncPos != KNotFound  &&  seekCount < maxSeek)
       
   140         	{
       
   141             TInt length = FrameInfo(framePtr, frameBufLen, frameInfo);
       
   142             if (frameBufLen >= KAmrFrameHeaderSize  &&  length == 0)
       
   143             	{
       
   144 				syncPos = KNotFound;
       
   145 				}
       
   146             framePtr += length;
       
   147             frameBufLen -= length;
       
   148             seekCount++;
       
   149         	}
       
   150         buf++; aBufLen--; i++;
       
   151     	}
       
   152     return syncPos;
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CAMRAudioControllerUtility::FrameInfo
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 TInt CAMRAudioControllerUtility::FrameInfo(
       
   160 	const TUint8* aBuf,
       
   161 	TInt aBufLen,
       
   162 	TAudioFrameInfo& aInfo)
       
   163     {
       
   164     TInt length = 0;
       
   165     aInfo.iBitRate = 0;
       
   166     if (aBufLen >= KAmrFrameHeaderSize)
       
   167     	{
       
   168         // extract mode information
       
   169         const TInt mode = (aBuf[0] & 0x78) >> 3; // 1st byte 0b.MODE...
       
   170         // get length
       
   171         length = KAmrFrameLength[mode];
       
   172 
       
   173         // check start stuffing bits
       
   174         if ((aBuf[0] & 0x83) != 0)
       
   175 	        {
       
   176             length = 0; // syntax error
       
   177 	        }
       
   178 
       
   179 // SKIP CHECKING FOR STUFFING BITS
       
   180 /*
       
   181         // check end stuffing bits
       
   182         if (length > 0  &&  aBufLen >= length)
       
   183         	{
       
   184             TUint32 stuffBits = aBuf[length-1];
       
   185             stuffBits <<= (11 - KAmrStuffLength[mode]);
       
   186             if ((stuffBits & 0x0000FF) != 0)
       
   187             	{
       
   188                 length = 0; // syntax error
       
   189             	}
       
   190         	}
       
   191 */
       
   192         // update frame parameters
       
   193         aInfo.iMode = mode;
       
   194         aInfo.iBitRate = length * 400;
       
   195         aInfo.iSamplingRate = KAmrSampleRate8K;
       
   196         aInfo.iChannels = KAmrChannels1;
       
   197         aInfo.iFrameSize = length;
       
   198         aInfo.iFrameSamples = KAmrMaxSamplesPerFrame160;
       
   199         aInfo.iSamplingRateOut = aInfo.iSamplingRate;
       
   200         aInfo.iChannelsOut = aInfo.iChannels;
       
   201         aInfo.iFrameSamplesOut= aInfo.iFrameSamples;
       
   202     	}
       
   203     return length;
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CAMRAudioControllerUtility::FrameHeaderSize
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 TInt CAMRAudioControllerUtility::FrameHeaderSize()
       
   211     {
       
   212     return KAmrFrameHeaderSize;
       
   213     }
       
   214 
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CAMRAudioControllerUtility::ScanHeaderL
       
   218 // Used to scan the header information
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 void CAMRAudioControllerUtility::ScanHeaderL(
       
   222 	CMMFDataBuffer* aDataBuf)
       
   223 	{
       
   224 	DP0(_L("CAMRAudioControllerUtility::ScanHeaderL"));
       
   225 	iBitRateFrozen = EFalse; // AMR does not have bitrate in container header, so it will be averaged while playing
       
   226     const TUint8* bufferPtr = aDataBuf->Data().Ptr();
       
   227     TInt bufferSize = aDataBuf->Data().Size();
       
   228 	
       
   229 	TInt scannedBuffer = 0;
       
   230 	TAudioFrameInfo frameInfo;
       
   231 	TInt frameLen;
       
   232 	
       
   233 	if (!iHdrIsRead)
       
   234         {
       
   235         if (iLenMetaData == 0)
       
   236             {
       
   237             iSyncOffset = 0;
       
   238             iLenMetaData = ID3HeaderLength(aDataBuf);
       
   239             }
       
   240         TInt bufferRemaining = bufferSize;
       
   241         
       
   242         while (iLenMetaData > 0)
       
   243             {
       
   244             if (iLenMetaData >= bufferRemaining)
       
   245                 {
       
   246                 // this buffer contain no frame
       
   247                 iSyncOffset += bufferRemaining;
       
   248                 iLenMetaData -= bufferRemaining;
       
   249                 User::Leave(KErrNotReady);
       
   250                 }
       
   251             else
       
   252                 {
       
   253                 iSyncOffset += iLenMetaData;
       
   254                 scannedBuffer += iLenMetaData;
       
   255                 // be sure to check for following id3 tags
       
   256                 bufferRemaining = bufferSize - scannedBuffer;
       
   257                 aDataBuf->SetPosition(scannedBuffer);
       
   258                 iLenMetaData = ID3HeaderLength(aDataBuf);
       
   259                 }
       
   260             }
       
   261     
       
   262         aDataBuf->SetPosition(scannedBuffer);
       
   263         // should change seeksync to start seek from position and not start of buffer (done)
       
   264         // this is a little akward because of the implementation of seeksync and its use by the recoder controller.
       
   265         TInt seekOffset = SeekSync(aDataBuf, bufferSize); // seeksync now scans from start of buffer + Position()
       
   266         TInt realOffset = seekOffset - scannedBuffer; // we just want offset from where we were
       
   267     
       
   268         iHeaderOffset = 0;
       
   269         if (realOffset >= 6)
       
   270             {
       
   271             bufferPtr = aDataBuf->Data().Ptr() + seekOffset - 6;
       
   272             TUint32* w1p = (TUint32*)bufferPtr;
       
   273             TUint16* w2p = (TUint16*)(bufferPtr+4);
       
   274             if (((*w1p & 0x4D412123)==0x4D412123) && 
       
   275                 ((*w2p & 0x0A52)==0x0A52))
       
   276                 {
       
   277                 iHeaderOffset = 6;
       
   278                 realOffset -= 6;
       
   279                 }
       
   280             else if (seekOffset == bufferSize)
       
   281                 {
       
   282                 User::Leave(KErrNotReady);
       
   283                 }	
       
   284             }
       
   285     
       
   286         iSyncOffset += realOffset; // offset to this point from content beginning
       
   287         scannedBuffer += realOffset + iHeaderOffset; // offset to this point in this buffer
       
   288         bufferPtr = aDataBuf->Data().Ptr();
       
   289         aDataBuf->SetPosition(scannedBuffer);
       
   290         
       
   291         DP0(_L("CAMRAudioControllerUtility::ScanHeaderL - Calling FrameInfo"));
       
   292     
       
   293         //TAudioFrameInfo frameInfo;
       
   294         /*TInt*/ frameLen = FrameInfo(bufferPtr+scannedBuffer, bufferSize-scannedBuffer, frameInfo);
       
   295     
       
   296         if (frameLen == 0)
       
   297             {
       
   298             User::Leave(KErrNotReady);
       
   299             }
       
   300             
       
   301     //	scannedBuffer += frameLen;
       
   302         iBitRate = 0;
       
   303         iDurationUs = 0;
       
   304         
       
   305         iSamplingRate = frameInfo.iSamplingRate;
       
   306         iBitRate = frameInfo.iBitRate;
       
   307         iSamplesPerFrame = frameInfo.iFrameSamples;
       
   308         iChannels = frameInfo.iChannels;
       
   309         iChannelsOut = frameInfo.iChannelsOut;
       
   310     //	iSamplingRateOut = frameInfo.iSamplingRateOut;
       
   311     
       
   312         if (iSamplingRate > 0)
       
   313             {
       
   314             iFrameTimeMs = (iSamplesPerFrame*1000)/iSamplingRate;
       
   315             }
       
   316         DP0(_L("CAMRAudioControllerUtility::ScanHeaderL - setting iHdrIsRead"));
       
   317         iHdrIsRead = ETrue;
       
   318         }
       
   319 	TInt frameBytes = 0;
       
   320 	TInt frames = 0;
       
   321 
       
   322         scannedBuffer += iFrameDataOffset;
       
   323 	while (scannedBuffer < bufferSize) // buffersize is not a function of buffer.Position()
       
   324 		{
       
   325 		frameLen = FrameInfo(bufferPtr+scannedBuffer, bufferSize-scannedBuffer, frameInfo);
       
   326 		if (frameLen > 0)
       
   327 			{
       
   328 		scannedBuffer += frameLen;
       
   329 	        frameBytes += frameLen;
       
   330 	        frames++;                   
       
   331 			}
       
   332 		else
       
   333 			{
       
   334 		    scannedBuffer++; // since frameLen had to be 0 to come here, go ahead to the next byte
       
   335 	        aDataBuf->SetPosition(scannedBuffer);
       
   336 		    TInt i = SeekSync(aDataBuf, bufferSize); // still using bufferSize as total size of buffer
       
   337 		    scannedBuffer += i;
       
   338 			}
       
   339 		}
       
   340 	
       
   341 	iFrameDataOffset = scannedBuffer - bufferSize;
       
   342 	DP3(_L("scannedBuffer[%d] frameBytes[%d] iFrameDataOffset[%d]"), scannedBuffer, frameBytes, iFrameDataOffset);
       
   343 	
       
   344 	iTotalFrameBytes = iTotalFrameBytes + frameBytes;
       
   345 	iTotalFrames = iTotalFrames + frames;
       
   346 	
       
   347 	// We only leave if its not the last buffer.
       
   348 	// we will not leave with the last buffer since
       
   349 	// the AMR Controller has to populate other configuration
       
   350 	// variables.
       
   351 	
       
   352 	if(!aDataBuf->LastBuffer())
       
   353 		{ // keep reading data for calculation
       
   354 		User::Leave(KErrCompletion);  // since we have read the header already
       
   355 		}
       
   356     DP0(_L("CAMRAudioControllerUtility::ScanHeaderL - calculating bitrate"));
       
   357 
       
   358     iFrameDataOffset = 0; // reset for next time
       
   359     TInt64 num = (TInt64)iTotalFrameBytes * TInt64(8 * 1000);
       
   360     TInt64 num1 = iFrameTimeMs * iTotalFrames;
       
   361     TInt64 ans = 0;
       
   362     if (num1 != 0)
       
   363 		{
       
   364         ans = num/num1;
       
   365         ans = num > ((ans + 0.5)*num1) ? (ans + 1):ans;
       
   366 		}
       
   367     iBitRate = TInt(ans);
       
   368     iBitRateFrozen = ETrue; // bitrate has been calculated using the whole file, we can use this one now
       
   369     DP3(_L("CAMRAudioControllerUtility::ScanHeaderL - iTotalFrameBytes[%d] iTotalFrames[%d] iBitRate[%d]"),
       
   370             iTotalFrameBytes, iTotalFrames, iBitRate);
       
   371 	}
       
   372 
       
   373 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   374 
       
   375 // End of File
       
   376