os/mm/mmlibs/mmfw/tsrc/mmfintegrationtest/ACLNT/WavDecodeUtility.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "mmfwavformat.h"
sl@0
    17
sl@0
    18
#include "WavDecodeUtility.h" 
sl@0
    19
sl@0
    20
sl@0
    21
CWavDecodeUtility::CWavDecodeUtility()
sl@0
    22
	{
sl@0
    23
	}
sl@0
    24
sl@0
    25
void CWavDecodeUtility::ConstructL(TDesC8& aBuffer)
sl@0
    26
	{
sl@0
    27
	iBuffer = &aBuffer;
sl@0
    28
	FindRiffChunksL();
sl@0
    29
	ProcessFormatHeaderL();
sl@0
    30
	}
sl@0
    31
sl@0
    32
CWavDecodeUtility* CWavDecodeUtility::NewL(TDesC8& aBuffer)
sl@0
    33
	{
sl@0
    34
	CWavDecodeUtility* self = new (ELeave) CWavDecodeUtility();
sl@0
    35
	CleanupStack::PushL(self);
sl@0
    36
	self->ConstructL(aBuffer);
sl@0
    37
	CleanupStack::Pop();
sl@0
    38
	return self;
sl@0
    39
	}
sl@0
    40
sl@0
    41
sl@0
    42
CWavDecodeUtility::~CWavDecodeUtility()
sl@0
    43
	{
sl@0
    44
	}
sl@0
    45
sl@0
    46
TUint16 CWavDecodeUtility::Read16(const TUint8* aPtr)
sl@0
    47
sl@0
    48
    {
sl@0
    49
	TUint16 ret = *(REINTERPRET_CAST(const TUint16*, aPtr));
sl@0
    50
	return ret;
sl@0
    51
    }
sl@0
    52
sl@0
    53
TUint32 CWavDecodeUtility::Read32(const TUint8* aPtr)
sl@0
    54
    {
sl@0
    55
    TUint32 x = *aPtr++;
sl@0
    56
    x |= *aPtr++ << 8;
sl@0
    57
    x |= *aPtr++ << 16;
sl@0
    58
    x |= *aPtr++ << 24;
sl@0
    59
    return x;
sl@0
    60
    }
sl@0
    61
void CWavDecodeUtility::AssignChunkTo(TMdaRiffChunk* aAssignedChunk)
sl@0
    62
	{
sl@0
    63
	Mem::Copy(REINTERPRET_CAST(TUint8*, aAssignedChunk), REINTERPRET_CAST(TUint8*, &iCurrent),sizeof(TMdaRiffChunk));
sl@0
    64
	aAssignedChunk->iFound=ETrue;
sl@0
    65
	}
sl@0
    66
sl@0
    67
void CWavDecodeUtility::ReadChunk(TMdaRiffChunk* aChunk)
sl@0
    68
	{
sl@0
    69
	Mem::FillZ(REINTERPRET_CAST(TUint8*, aChunk),sizeof(TMdaRiffChunk)); // Zero data
sl@0
    70
	aChunk->iPosition=iPos + KRiffChunkHeaderLength;
sl@0
    71
	aChunk->iName = Read32(iStartPtr + iPos - iLastReadPosition);
sl@0
    72
	aChunk->iLength = Read32(iStartPtr + iPos - iLastReadPosition + KRiffChunkDataLength);
sl@0
    73
	}
sl@0
    74
void CWavDecodeUtility::FindRiffChunksL()
sl@0
    75
	{
sl@0
    76
	if (!iFoundChunks)
sl@0
    77
		{
sl@0
    78
		iFoundChunks=ETrue;
sl@0
    79
//		iStartPtr=iBuffer->Data().Ptr();
sl@0
    80
		iStartPtr=iBuffer->Ptr();
sl@0
    81
		iPos=0;
sl@0
    82
		iLastReadPosition=0;//Set by CBase, but what the heck
sl@0
    83
		iDone=EFalse;
sl@0
    84
		}
sl@0
    85
	else
sl@0
    86
		{//We've done another read. If there's < chunk in the buffer then something's wrong
sl@0
    87
		if (iBuffer->Length() < STATIC_CAST(TInt, KRiffChunkHeaderLength))
sl@0
    88
			{
sl@0
    89
			if ((iFormatChunk.iFound) && (iDataChunk.iFound)) 
sl@0
    90
				{
sl@0
    91
				iDone = ETrue; //it should be ok to exit loop
sl@0
    92
				return;
sl@0
    93
				}
sl@0
    94
			else
sl@0
    95
				{
sl@0
    96
				User::Leave(KErrCorrupt);
sl@0
    97
				}
sl@0
    98
			}
sl@0
    99
		}
sl@0
   100
	
sl@0
   101
	while (!iDone)
sl@0
   102
		{
sl@0
   103
		TInt advance=0;
sl@0
   104
		
sl@0
   105
		ReadChunk(&iCurrent);
sl@0
   106
		
sl@0
   107
		if (iCurrent.iName == KRiffChunkNameRiff)//we need to look INSIDE the RIFF chunk
sl@0
   108
			{
sl@0
   109
			if(iBuffer->Length() < STATIC_CAST(TInt, KRiffContainerChunkHeaderLength))
sl@0
   110
				User::Leave(KErrCorrupt);
sl@0
   111
			iRiffChunkLength=iCurrent.iLength + KRiffChunkHeaderLength;
sl@0
   112
			advance=KRiffContainerChunkHeaderLength;
sl@0
   113
			}
sl@0
   114
		else
sl@0
   115
			{
sl@0
   116
			advance=iCurrent.iLength + KRiffChunkHeaderLength;		//... and skip all others
sl@0
   117
			}
sl@0
   118
		
sl@0
   119
		if (iCurrent.iName == KRiffChunkNameFmt_)
sl@0
   120
			AssignChunkTo(&iFormatChunk);
sl@0
   121
		
sl@0
   122
		else if (iCurrent.iName == KRiffChunkNameFact)
sl@0
   123
			AssignChunkTo(&iFactChunk);
sl@0
   124
		
sl@0
   125
		else if (iCurrent.iName == KRiffChunkNameData)
sl@0
   126
			AssignChunkTo(&iDataChunk);
sl@0
   127
		
sl@0
   128
		if (iDataChunk.iFound && iFormatChunk.iFound && iFactChunk.iFound)
sl@0
   129
			{
sl@0
   130
			iDone=ETrue;
sl@0
   131
			}
sl@0
   132
		else
sl@0
   133
			{//still have chunks to look for
sl@0
   134
			iPos+=advance;
sl@0
   135
			if (iPos & 1)
sl@0
   136
				iPos++;
sl@0
   137
			
sl@0
   138
			if ((TUint)iPos>=(TUint)iRiffChunkLength)
sl@0
   139
				{
sl@0
   140
				iDone=ETrue;//end of file
sl@0
   141
				iClipLength = iRiffChunkLength;
sl@0
   142
				}
sl@0
   143
			else
sl@0
   144
				{//make sure we have at least a chunk's worth left in the buffer
sl@0
   145
				if ((TUint)(iPos-iLastReadPosition) > 
sl@0
   146
					(TUint)(iBuffer->Length() -KRiffChunkHeaderLength))
sl@0
   147
					{
sl@0
   148
					iLastReadPosition=iPos;
sl@0
   149
					//DoReadL(iLastReadPosition);
sl@0
   150
					return;
sl@0
   151
					}	
sl@0
   152
				}
sl@0
   153
			}
sl@0
   154
		}
sl@0
   155
	
sl@0
   156
	iClipLength = iRiffChunkLength;
sl@0
   157
	if (iClipLength == 0) User::Leave(KErrNotFound);
sl@0
   158
	else if (!(iDataChunk.iFound && iFormatChunk.iFound))
sl@0
   159
		User::Leave(KErrCorrupt);
sl@0
   160
sl@0
   161
	}
sl@0
   162
sl@0
   163
void CWavDecodeUtility::ProcessFormatHeaderL()
sl@0
   164
	{
sl@0
   165
	TMdaRiffChunk* chunk = &iFormatChunk;
sl@0
   166
	
sl@0
   167
	if (!chunk)
sl@0
   168
		User::Leave(KErrCorrupt);
sl@0
   169
	
sl@0
   170
	iLastReadPosition = chunk->iPosition; // Should be beginning of fmt block
sl@0
   171
	//DoReadL(iLastReadPosition);
sl@0
   172
	
sl@0
   173
	// Set the real format
sl@0
   174
	const TUint8* rawform = iBuffer->Ptr() + iLastReadPosition;	//skip _fmt & length
sl@0
   175
	iCodecId = Read16(rawform); rawform+=2;
sl@0
   176
	iChannels = Read16(rawform); rawform+=2;
sl@0
   177
	if ((iChannels != 1)&&(iChannels != 2))		//only 1 or 2 channels allowed
sl@0
   178
		User::Leave(KErrCorrupt); 
sl@0
   179
	
sl@0
   180
	iSampleRate = Read32(rawform); rawform+=4; // Skip bytes per second estimate
sl@0
   181
	if (!iSampleRate) 	User::Leave(KErrCorrupt);
sl@0
   182
	
sl@0
   183
	iAverageBytesPerSecond = Read32(rawform); rawform+=4;
sl@0
   184
	iBlockAlign = Read16(rawform); rawform+=2;
sl@0
   185
	
sl@0
   186
	iBitsPerSample = Read16(rawform);
sl@0
   187
	rawform+=2;
sl@0
   188
	
sl@0
   189
	switch (iCodecId)
sl@0
   190
		{
sl@0
   191
		case KMMFWavFormatTypePcm:
sl@0
   192
			{
sl@0
   193
			}
sl@0
   194
			break;
sl@0
   195
		case KMMFWavFormatTypeImaAdpcm:
sl@0
   196
			{
sl@0
   197
			}
sl@0
   198
			break;
sl@0
   199
		case KMMFWavFormatTypeAlaw:
sl@0
   200
			{
sl@0
   201
			}
sl@0
   202
			break;
sl@0
   203
		case KMMFWavFormatTypeMulaw:
sl@0
   204
			{
sl@0
   205
			}
sl@0
   206
			break;
sl@0
   207
		case KMMFWavFormatTypeGSM610:
sl@0
   208
			{
sl@0
   209
			}
sl@0
   210
			break;
sl@0
   211
		default:
sl@0
   212
			User::Leave(KErrNotSupported);
sl@0
   213
		}
sl@0
   214
	
sl@0
   215
	if (iCodecId == KMMFWavFormatTypeImaAdpcm)
sl@0
   216
		{
sl@0
   217
		TUint16 extraData = Read16(rawform);
sl@0
   218
		if (extraData == 2)
sl@0
   219
			{
sl@0
   220
			rawform+=2;
sl@0
   221
			iSamplesPerBlock = Read16(rawform);
sl@0
   222
			rawform+=2;
sl@0
   223
			}
sl@0
   224
		}
sl@0
   225
	
sl@0
   226
	// Is there a fact chunk?
sl@0
   227
	if (iFactChunk.iFound)			
sl@0
   228
		iHasFactChunk = ETrue;
sl@0
   229
	
sl@0
   230
	// Find the data block
sl@0
   231
	chunk=&iDataChunk;
sl@0
   232
	iStartPosition = chunk->iPosition; 
sl@0
   233
	iDataLength = chunk->iLength;
sl@0
   234
	}