1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmlibs/mmfw/tsrc/mmfintegrationtest/ACLNT/WavDecodeUtility.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,234 @@
1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "mmfwavformat.h"
1.20 +
1.21 +#include "WavDecodeUtility.h"
1.22 +
1.23 +
1.24 +CWavDecodeUtility::CWavDecodeUtility()
1.25 + {
1.26 + }
1.27 +
1.28 +void CWavDecodeUtility::ConstructL(TDesC8& aBuffer)
1.29 + {
1.30 + iBuffer = &aBuffer;
1.31 + FindRiffChunksL();
1.32 + ProcessFormatHeaderL();
1.33 + }
1.34 +
1.35 +CWavDecodeUtility* CWavDecodeUtility::NewL(TDesC8& aBuffer)
1.36 + {
1.37 + CWavDecodeUtility* self = new (ELeave) CWavDecodeUtility();
1.38 + CleanupStack::PushL(self);
1.39 + self->ConstructL(aBuffer);
1.40 + CleanupStack::Pop();
1.41 + return self;
1.42 + }
1.43 +
1.44 +
1.45 +CWavDecodeUtility::~CWavDecodeUtility()
1.46 + {
1.47 + }
1.48 +
1.49 +TUint16 CWavDecodeUtility::Read16(const TUint8* aPtr)
1.50 +
1.51 + {
1.52 + TUint16 ret = *(REINTERPRET_CAST(const TUint16*, aPtr));
1.53 + return ret;
1.54 + }
1.55 +
1.56 +TUint32 CWavDecodeUtility::Read32(const TUint8* aPtr)
1.57 + {
1.58 + TUint32 x = *aPtr++;
1.59 + x |= *aPtr++ << 8;
1.60 + x |= *aPtr++ << 16;
1.61 + x |= *aPtr++ << 24;
1.62 + return x;
1.63 + }
1.64 +void CWavDecodeUtility::AssignChunkTo(TMdaRiffChunk* aAssignedChunk)
1.65 + {
1.66 + Mem::Copy(REINTERPRET_CAST(TUint8*, aAssignedChunk), REINTERPRET_CAST(TUint8*, &iCurrent),sizeof(TMdaRiffChunk));
1.67 + aAssignedChunk->iFound=ETrue;
1.68 + }
1.69 +
1.70 +void CWavDecodeUtility::ReadChunk(TMdaRiffChunk* aChunk)
1.71 + {
1.72 + Mem::FillZ(REINTERPRET_CAST(TUint8*, aChunk),sizeof(TMdaRiffChunk)); // Zero data
1.73 + aChunk->iPosition=iPos + KRiffChunkHeaderLength;
1.74 + aChunk->iName = Read32(iStartPtr + iPos - iLastReadPosition);
1.75 + aChunk->iLength = Read32(iStartPtr + iPos - iLastReadPosition + KRiffChunkDataLength);
1.76 + }
1.77 +void CWavDecodeUtility::FindRiffChunksL()
1.78 + {
1.79 + if (!iFoundChunks)
1.80 + {
1.81 + iFoundChunks=ETrue;
1.82 +// iStartPtr=iBuffer->Data().Ptr();
1.83 + iStartPtr=iBuffer->Ptr();
1.84 + iPos=0;
1.85 + iLastReadPosition=0;//Set by CBase, but what the heck
1.86 + iDone=EFalse;
1.87 + }
1.88 + else
1.89 + {//We've done another read. If there's < chunk in the buffer then something's wrong
1.90 + if (iBuffer->Length() < STATIC_CAST(TInt, KRiffChunkHeaderLength))
1.91 + {
1.92 + if ((iFormatChunk.iFound) && (iDataChunk.iFound))
1.93 + {
1.94 + iDone = ETrue; //it should be ok to exit loop
1.95 + return;
1.96 + }
1.97 + else
1.98 + {
1.99 + User::Leave(KErrCorrupt);
1.100 + }
1.101 + }
1.102 + }
1.103 +
1.104 + while (!iDone)
1.105 + {
1.106 + TInt advance=0;
1.107 +
1.108 + ReadChunk(&iCurrent);
1.109 +
1.110 + if (iCurrent.iName == KRiffChunkNameRiff)//we need to look INSIDE the RIFF chunk
1.111 + {
1.112 + if(iBuffer->Length() < STATIC_CAST(TInt, KRiffContainerChunkHeaderLength))
1.113 + User::Leave(KErrCorrupt);
1.114 + iRiffChunkLength=iCurrent.iLength + KRiffChunkHeaderLength;
1.115 + advance=KRiffContainerChunkHeaderLength;
1.116 + }
1.117 + else
1.118 + {
1.119 + advance=iCurrent.iLength + KRiffChunkHeaderLength; //... and skip all others
1.120 + }
1.121 +
1.122 + if (iCurrent.iName == KRiffChunkNameFmt_)
1.123 + AssignChunkTo(&iFormatChunk);
1.124 +
1.125 + else if (iCurrent.iName == KRiffChunkNameFact)
1.126 + AssignChunkTo(&iFactChunk);
1.127 +
1.128 + else if (iCurrent.iName == KRiffChunkNameData)
1.129 + AssignChunkTo(&iDataChunk);
1.130 +
1.131 + if (iDataChunk.iFound && iFormatChunk.iFound && iFactChunk.iFound)
1.132 + {
1.133 + iDone=ETrue;
1.134 + }
1.135 + else
1.136 + {//still have chunks to look for
1.137 + iPos+=advance;
1.138 + if (iPos & 1)
1.139 + iPos++;
1.140 +
1.141 + if ((TUint)iPos>=(TUint)iRiffChunkLength)
1.142 + {
1.143 + iDone=ETrue;//end of file
1.144 + iClipLength = iRiffChunkLength;
1.145 + }
1.146 + else
1.147 + {//make sure we have at least a chunk's worth left in the buffer
1.148 + if ((TUint)(iPos-iLastReadPosition) >
1.149 + (TUint)(iBuffer->Length() -KRiffChunkHeaderLength))
1.150 + {
1.151 + iLastReadPosition=iPos;
1.152 + //DoReadL(iLastReadPosition);
1.153 + return;
1.154 + }
1.155 + }
1.156 + }
1.157 + }
1.158 +
1.159 + iClipLength = iRiffChunkLength;
1.160 + if (iClipLength == 0) User::Leave(KErrNotFound);
1.161 + else if (!(iDataChunk.iFound && iFormatChunk.iFound))
1.162 + User::Leave(KErrCorrupt);
1.163 +
1.164 + }
1.165 +
1.166 +void CWavDecodeUtility::ProcessFormatHeaderL()
1.167 + {
1.168 + TMdaRiffChunk* chunk = &iFormatChunk;
1.169 +
1.170 + if (!chunk)
1.171 + User::Leave(KErrCorrupt);
1.172 +
1.173 + iLastReadPosition = chunk->iPosition; // Should be beginning of fmt block
1.174 + //DoReadL(iLastReadPosition);
1.175 +
1.176 + // Set the real format
1.177 + const TUint8* rawform = iBuffer->Ptr() + iLastReadPosition; //skip _fmt & length
1.178 + iCodecId = Read16(rawform); rawform+=2;
1.179 + iChannels = Read16(rawform); rawform+=2;
1.180 + if ((iChannels != 1)&&(iChannels != 2)) //only 1 or 2 channels allowed
1.181 + User::Leave(KErrCorrupt);
1.182 +
1.183 + iSampleRate = Read32(rawform); rawform+=4; // Skip bytes per second estimate
1.184 + if (!iSampleRate) User::Leave(KErrCorrupt);
1.185 +
1.186 + iAverageBytesPerSecond = Read32(rawform); rawform+=4;
1.187 + iBlockAlign = Read16(rawform); rawform+=2;
1.188 +
1.189 + iBitsPerSample = Read16(rawform);
1.190 + rawform+=2;
1.191 +
1.192 + switch (iCodecId)
1.193 + {
1.194 + case KMMFWavFormatTypePcm:
1.195 + {
1.196 + }
1.197 + break;
1.198 + case KMMFWavFormatTypeImaAdpcm:
1.199 + {
1.200 + }
1.201 + break;
1.202 + case KMMFWavFormatTypeAlaw:
1.203 + {
1.204 + }
1.205 + break;
1.206 + case KMMFWavFormatTypeMulaw:
1.207 + {
1.208 + }
1.209 + break;
1.210 + case KMMFWavFormatTypeGSM610:
1.211 + {
1.212 + }
1.213 + break;
1.214 + default:
1.215 + User::Leave(KErrNotSupported);
1.216 + }
1.217 +
1.218 + if (iCodecId == KMMFWavFormatTypeImaAdpcm)
1.219 + {
1.220 + TUint16 extraData = Read16(rawform);
1.221 + if (extraData == 2)
1.222 + {
1.223 + rawform+=2;
1.224 + iSamplesPerBlock = Read16(rawform);
1.225 + rawform+=2;
1.226 + }
1.227 + }
1.228 +
1.229 + // Is there a fact chunk?
1.230 + if (iFactChunk.iFound)
1.231 + iHasFactChunk = ETrue;
1.232 +
1.233 + // Find the data block
1.234 + chunk=&iDataChunk;
1.235 + iStartPosition = chunk->iPosition;
1.236 + iDataLength = chunk->iLength;
1.237 + }