mmfenh/advancedaudiocontroller/audiocontrollerpluginsvariant/amraudioplaycontroller/Src/AMRAudioPlayControllerHwDecoder.cpp
changeset 0 71ca22bcf22a
child 12 5a06f39ad45b
child 18 2eb3b066cc7d
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:  The functions in this module implements the specific behavior
       
    15 *                for the advanced audio converter class for configuration of
       
    16 *				 hardware accelerated codec.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "AMRAudioPlayControllerDecoder.h"
       
    23 #include "DebugMacros.h"
       
    24 //#include <AmrDecHwDeviceTICIM.h>
       
    25 #include <MmfPanicCodes.h>
       
    26 
       
    27 // The size of AMR header, header must include bits for determining frame length
       
    28 const TInt KAmrFrameHeaderSize1 = 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 // Maximum number of PCM samples in one AMR frame
       
    34 const TInt KAmrSamplesPerFrame160 = 160;
       
    35 
       
    36 const TInt KAmrSampleRate8K = 8000;
       
    37 const TInt KAmrChannels1 = 1;
       
    38 
       
    39 const TInt KAmrMaxFrameSize = 32;
       
    40 const TUint KSizeOfInBuffer   = 2*KAmrMaxFrameSize;
       
    41 
       
    42 class TAudioFrameInfo
       
    43     {
       
    44 	public:
       
    45 		TInt iMode;            // encoding mode
       
    46 		TInt iBitRate;         // bitrate (bit/s)
       
    47 		TInt iSamplingRate;    // sampling frequency (Hz)
       
    48 		TInt iChannels;        // number of channels
       
    49 		TInt iFrameSize;       // encoded size (bytes)
       
    50 		TInt iFrameSamples;    // samples per frame
       
    51 		TInt iSamplingRateOut; // sampling frequency after conversion (Hz)
       
    52 		TInt iChannelsOut;     // number of audio channels after conversion (1 or 2)
       
    53 		TInt iFrameSamplesOut; // decoded size after conversion (samples per frame)
       
    54 		TInt iPadding;         // padding flag (TRUE or FALSE, TRUE if p slot exists)
       
    55 		TInt iId;              // id of algorithm (1 MPEG-1, 0 MPEG-2)
       
    56     };
       
    57 
       
    58 // ============================ MEMBER FUNCTIONS ===============================
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // C++ default constructor can NOT contain any code, that
       
    62 // might leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CAMRAudioPlayControllerDecoder::CAMRAudioPlayControllerDecoder()
       
    66     :   CAdvancedAudioDecoder(CActive::EPriorityStandard)
       
    67     {
       
    68     DP0(_L("CAMRAudioPlayControllerDecoder::CAMRAudioPlayControllerDecoder - Hardware Accelerated"));
       
    69     iMaxFrameSize = KAmrMaxFrameSize;
       
    70    	iSizeOfInBuffer = KSizeOfInBuffer;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CAMRAudioPlayControllerDecoder::ConstructL
       
    75 // Symbian 2nd phase constructor can leave.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CAMRAudioPlayControllerDecoder::ConstructL()
       
    79     {
       
    80     DP1(_L("CAMRAudioPlayControllerDecoder::ConstructL"), this);
       
    81     iFrameTable = CFrameTable::NewL();
       
    82     
       
    83 	RenderEnable();
       
    84 	UnMarkPlayEnd();
       
    85 	Enable();
       
    86 
       
    87     CActiveScheduler::Add(this);
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // C++ default constructor can NOT contain any code, that
       
    92 // might leave.
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CAMRAudioPlayControllerDecoder* CAMRAudioPlayControllerDecoder::NewL()
       
    96     {
       
    97     DP0(_L("CAMRAudioPlayControllerDecoder::NewL"));
       
    98     CAMRAudioPlayControllerDecoder* self = new(ELeave) CAMRAudioPlayControllerDecoder();
       
    99     CleanupStack::PushL(self);
       
   100     self->ConstructL();
       
   101     CleanupStack::Pop(self);
       
   102     return self;
       
   103     }
       
   104 
       
   105 // Destructor
       
   106 CAMRAudioPlayControllerDecoder::~CAMRAudioPlayControllerDecoder()
       
   107 	{
       
   108     DP0(_L("CAMRAudioPlayControllerDecoder::~CAMRAudioPlayControllerDecoder"));
       
   109     delete iFrameTable;
       
   110 	}
       
   111 
       
   112 TInt CAMRAudioPlayControllerDecoder::CodecConfig(RArray<TInt>& /*aConfigData*/)
       
   113 	{
       
   114 	TInt status = KErrNone;
       
   115 	iFrameTable->InitFrameTable(KAmrSampleRate8K, KAmrSamplesPerFrame160);
       
   116 	return status;
       
   117 	}
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CAMRAudioPlayControllerDecoder::ResetL
       
   121 // Resets configuration data and configures the codec.
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CAMRAudioPlayControllerDecoder::ResetL()
       
   125 	{
       
   126     DP0(_L ("CAMRAudioPlayControllerDecoder::Reset - Enter"));
       
   127 
       
   128 	delete[] iOutBuffer;
       
   129    	delete[] iInBuffer;
       
   130    	
       
   131    	iInBuffer = NULL;
       
   132    	iOutBuffer = NULL;
       
   133    	iInBufferCount = 0;
       
   134    	iOutBufferCount = 0;
       
   135    	iOutBufferPtr = NULL;
       
   136    	iOutFrameLength = KAmrMaxFrameSize; // since data is not decoded
       
   137    	iInBuffer = new (ELeave) TUint8[KSizeOfInBuffer];
       
   138 
       
   139     iOutBuffer = new (ELeave) TUint8[iOutFrameLength];
       
   140     iOutBufferPtr = iOutBuffer;
       
   141 
       
   142     iAccLen = 0;
       
   143     iEnabled = ETrue;
       
   144 
       
   145     DP0(_L ("CAMRAudioPlayControllerDecoder::Reset - Exit"));
       
   146 	}
       
   147 
       
   148 TCodecProcessResult CAMRAudioPlayControllerDecoder::ProcessL(CMMFBuffer& aSrc, CMMFBuffer& aDst)
       
   149 	{
       
   150     DP0(_L ("CAMRAudioPlayControllerDecoder::ProcessL"));
       
   151 	return CAdvancedAudioDecoder::ProcessHwL(aSrc, aDst);
       
   152 	}
       
   153 
       
   154 TInt CAMRAudioPlayControllerDecoder::FrameLength(const TUint8* aBuf, TInt aBufLen, TInt& aFrameLength)
       
   155 	{
       
   156 	TInt stat = KErrNone;
       
   157 	TAudioFrameInfo info;
       
   158 	TInt len = FrameInfo(aBuf, aBufLen, info);
       
   159 	if (len > 0)
       
   160 		{
       
   161 		aFrameLength = len;
       
   162 		}
       
   163 	else
       
   164 		{
       
   165 		stat = KErrUnknown;
       
   166 		}
       
   167 	return stat;
       
   168 	}
       
   169 
       
   170 TInt CAMRAudioPlayControllerDecoder::FrameInfo(
       
   171 	const TUint8* aBuf,
       
   172 	TInt aBufLen,
       
   173 	TAudioFrameInfo& aInfo)
       
   174     {
       
   175     TInt length = 0;
       
   176     aInfo.iBitRate = 0;
       
   177     if (aBufLen >= KAmrFrameHeaderSize1)
       
   178     	{
       
   179         // extract mode information
       
   180         const TInt mode = (aBuf[0] & 0x78) >> 3; // 1st byte 0b.MODE...
       
   181         // get length
       
   182         length = KAmrFrameLength[mode];
       
   183 
       
   184         // check start stuffing bits
       
   185         if ((aBuf[0] & 0x83) != 0)
       
   186 	        {
       
   187             length = 0; // syntax error
       
   188 	        }
       
   189 
       
   190 // SKIP CHECKING FOR STUFFING BITS
       
   191 /*
       
   192         // check end stuffing bits
       
   193         if (length > 0  &&  aBufLen >= length)
       
   194         	{
       
   195             TUint32 stuffBits = aBuf[length-1];
       
   196             stuffBits <<= (11 - KAmrStuffLength[mode]);
       
   197             if ((stuffBits & 0x0000FF) != 0)
       
   198             	{
       
   199                 length = 0; // syntax error
       
   200             	}
       
   201         	}
       
   202 */
       
   203         // update frame parameters
       
   204         aInfo.iMode = mode;
       
   205         aInfo.iBitRate = length * 400;
       
   206         aInfo.iSamplingRate = KAmrSampleRate8K;
       
   207         aInfo.iChannels = KAmrChannels1;
       
   208         aInfo.iFrameSize = length;
       
   209         aInfo.iFrameSamples = KAmrSamplesPerFrame160;
       
   210         aInfo.iSamplingRateOut = aInfo.iSamplingRate;
       
   211         aInfo.iChannelsOut = aInfo.iChannels;
       
   212         aInfo.iFrameSamplesOut= aInfo.iFrameSamples;
       
   213     	}
       
   214     return length;
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CAMRAudioControllerUtility::SeekSync
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 TInt CAMRAudioPlayControllerDecoder::SeekSync(TUint8* aBuf, TInt aBufLen)
       
   222     {
       
   223     const TInt KMaxFrames = 3;          // number of frames to check
       
   224     const TInt KNotFound = aBufLen;     // sync not found position
       
   225     TAudioFrameInfo frameInfo;			// frame parameters
       
   226     TInt i = 0;
       
   227     TInt syncPos = KNotFound;
       
   228     TInt maxSeek = KMaxFrames;
       
   229     const TUint8* endPtr = aBuf + aBufLen;
       
   230 
       
   231     // Seek a valid frame candidate byte by byte until a valid frame
       
   232     // is found or all bytes have been checked.
       
   233     while (aBuf < endPtr  &&  syncPos == KNotFound)
       
   234     	{
       
   235         TInt seekCount = 0;
       
   236         const TUint8* framePtr = aBuf;
       
   237         TInt frameBufLen = aBufLen;
       
   238         syncPos = i;
       
   239         // Check the validity of this frame candidate and the nearest next
       
   240         // frames. If they are not OK, syncPos will be set to KNotFound.
       
   241         while (framePtr < endPtr  &&  syncPos != KNotFound  &&  seekCount < maxSeek)
       
   242         	{
       
   243             TInt length = FrameInfo(framePtr, frameBufLen, frameInfo);
       
   244             if (frameBufLen >= KAmrFrameHeaderSize1  &&  length == 0)
       
   245             	{
       
   246 				syncPos = KNotFound;
       
   247 				}
       
   248             framePtr += length;
       
   249             frameBufLen -= length;
       
   250             seekCount++;
       
   251         	}
       
   252         aBuf++; aBufLen--; i++;
       
   253     	}
       
   254     return syncPos;
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CAMRAudioPlayControllerDecoder::IsHwAccelerated
       
   259 // Always return true when no soft codec is used.
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 TBool CAMRAudioPlayControllerDecoder::IsHwAccelerated()
       
   263 	{
       
   264 	return ETrue;
       
   265 	}
       
   266 
       
   267 TInt CAMRAudioPlayControllerDecoder::CodecCmd(TCodecCmd /*aCmd*/)
       
   268 	{
       
   269 	return KErrNotSupported;
       
   270 	}
       
   271 
       
   272 
       
   273 // End of file