os/mm/mmlibs/mmfw/Recogniser/src/asfparser.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) 2006-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 "constants.h"
sl@0
    17
#include "parsers.h"
sl@0
    18
sl@0
    19
#define KASFAudioMedia				"\xF8\x69\x9E\x40\x5B\x4D\x11\xCF\xA8\xFD\x00\x80\x5F\x5C\x44\x2B"
sl@0
    20
#define KASFVideoMedia				"\xBC\x19\xEF\xC0\x5B\x4D\x11\xCF\xA8\xFD\x00\x80\x5F\x5C\x44\x2B"
sl@0
    21
#define KASFHeaderObject 			"\x75\xB2\x26\x30\x66\x8E\x11\xCF\xA6\xD9\x00\xAA\x00\x62\xCE\x6C"
sl@0
    22
#define KASFStreamPropertiesObject	"\xB7\xDC\x07\x91\xA9\xB7\x11\xCF\x8E\xE6\x00\xC0\x0C\x20\x53\x65"
sl@0
    23
#define KASFCodecListObject			"\x86\xD1\x52\x40\x31\x1D\x11\xD0\xA3\xA4\x00\xA0\xC9\x03\x48\xF6"
sl@0
    24
sl@0
    25
sl@0
    26
static const TInt KGUIDLen = 16; 				// 128-bit
sl@0
    27
static const TInt KObjectLen = KGUIDLen + 8; 	// GUID followed by 64-bit size.
sl@0
    28
static const TInt KMinObjectLen = 30; 			// From documentation
sl@0
    29
sl@0
    30
sl@0
    31
typedef struct
sl@0
    32
	{
sl@0
    33
	const TText* iExt;
sl@0
    34
	const TText8* iVideoMime;
sl@0
    35
	const TText8* iAudioMime;
sl@0
    36
	}
sl@0
    37
TASFType;
sl@0
    38
sl@0
    39
sl@0
    40
//
sl@0
    41
// Various ASF container MIME-types.
sl@0
    42
//
sl@0
    43
static const TASFType KASFTypes[] =
sl@0
    44
	{
sl@0
    45
		{KExtWMA,	KMimeWMA,	KMimeWMA},
sl@0
    46
		{KExtWMV,	KMimeWMV,	KMimeWMV},
sl@0
    47
		{KExtASF,	KMimeASF_V,	KMimeASF_A}
sl@0
    48
	};
sl@0
    49
sl@0
    50
#define KASFTypesCount	sizeof(KASFTypes) / sizeof(TASFType)
sl@0
    51
sl@0
    52
sl@0
    53
#define KASFHeaderObjectBit		KBit1	// 00000001
sl@0
    54
#define KASFStreamHeaderBit		KBit2	// 00000010
sl@0
    55
#define KASFVideoBit			KBit3	// 00000100
sl@0
    56
#define KASFConfidenceMask		0x07	// 00000111
sl@0
    57
sl@0
    58
//
sl@0
    59
// Flags mapped to confidence levels.
sl@0
    60
//
sl@0
    61
// A: Extension identified.
sl@0
    62
// B: HeaderObject GUID
sl@0
    63
// C: StreamProperties GUID
sl@0
    64
//
sl@0
    65
// C B A -> Confidence
sl@0
    66
// -------------------
sl@0
    67
// 0 0 0 -> ENotRecognised
sl@0
    68
// 0 0 1 -> EPossible
sl@0
    69
// 0 1 0 -> EPossible
sl@0
    70
// 0 1 1 -> EProbable
sl@0
    71
// 1 0 0 -> ENotRecognised (StreamProperties occurs within HeaderObject)
sl@0
    72
// 1 0 1 -> ENotRecognised (StreamProperties occurs within HeaderObject)
sl@0
    73
// 1 1 0 -> EProbable
sl@0
    74
// 1 1 1 -> ECertain
sl@0
    75
//
sl@0
    76
static const TInt KASFFlagsToConfidence[8] = 
sl@0
    77
	{
sl@0
    78
	KConfNotRecognised,
sl@0
    79
	KConfPossible,
sl@0
    80
	KConfPossible,
sl@0
    81
	KConfProbable,
sl@0
    82
	KConfNotRecognised,
sl@0
    83
	KConfNotRecognised,
sl@0
    84
	KConfProbable,
sl@0
    85
	KConfCertain
sl@0
    86
	};
sl@0
    87
sl@0
    88
sl@0
    89
//
sl@0
    90
//
sl@0
    91
//
sl@0
    92
TASFParser::TASFParser(CReader& aReader, TFlags& aFlags)
sl@0
    93
 :	iReader(aReader),
sl@0
    94
 	iFlags(aFlags)
sl@0
    95
	{
sl@0
    96
	}
sl@0
    97
sl@0
    98
sl@0
    99
//
sl@0
   100
// Sets the mime-type the file extension implies.
sl@0
   101
//
sl@0
   102
const TText8* TASFParser::MatchExtension(const TDesC& aExt, TBool aVideo)
sl@0
   103
	{
sl@0
   104
	for (TInt i = 0; i < KASFTypesCount; i++)
sl@0
   105
		{
sl@0
   106
		if (aExt.MatchF(TPtrC(KASFTypes[i].iExt)) != KErrNotFound)
sl@0
   107
			{
sl@0
   108
			return (aVideo ? KASFTypes[i].iVideoMime : KASFTypes[i].iAudioMime);
sl@0
   109
			}
sl@0
   110
		}
sl@0
   111
		
sl@0
   112
	return NULL;
sl@0
   113
	}
sl@0
   114
sl@0
   115
sl@0
   116
//
sl@0
   117
//
sl@0
   118
//
sl@0
   119
void TASFParser::DoRecognise(const TDesC& aExt, CReader& aReader, TMatch& aMatch)
sl@0
   120
	{
sl@0
   121
	TFlags flags;
sl@0
   122
	TASFParser parser(aReader, flags);
sl@0
   123
	
sl@0
   124
	// We need to parse first to determine if there's video content present.
sl@0
   125
	TRAPD(err, parser.ParseL());
sl@0
   126
	if (err == KErrCorrupt)
sl@0
   127
		{
sl@0
   128
		// Unrecognised content. However the extension may allow
sl@0
   129
		// correct identification so assume there's video content.
sl@0
   130
		flags.SetBit(KASFVideoBit);
sl@0
   131
		}
sl@0
   132
		
sl@0
   133
	const TText8* extMime = parser.MatchExtension(aExt, flags.GetBitField(KASFVideoBit));
sl@0
   134
	if (extMime != NULL)
sl@0
   135
		{
sl@0
   136
		// The extension was recognised.
sl@0
   137
		flags.SetExtensionFlag();
sl@0
   138
		}
sl@0
   139
		
sl@0
   140
	TInt confIndex = flags.GetBitField(KASFConfidenceMask);
sl@0
   141
	aMatch.iConfidence = KASFFlagsToConfidence[confIndex];
sl@0
   142
	if (aMatch.iConfidence != KConfNotRecognised)
sl@0
   143
		{
sl@0
   144
		// Trust the mime-type the extension maps to.
sl@0
   145
		// If the extension wasn't recognised, but the content was
sl@0
   146
		// then return the generic ASF mime type. ASF format files
sl@0
   147
		// can't be identified from their content; just whether they
sl@0
   148
		// contain video or not.
sl@0
   149
		aMatch.iMime = extMime;
sl@0
   150
		if (aMatch.iMime == NULL)
sl@0
   151
			{
sl@0
   152
			aMatch.iMime = (flags.GetBitField(KASFVideoBit) ? KMimeASF_V : KMimeASF_A);
sl@0
   153
			}
sl@0
   154
		}
sl@0
   155
	}
sl@0
   156
sl@0
   157
sl@0
   158
//
sl@0
   159
//
sl@0
   160
//
sl@0
   161
void TASFParser::ParseL()
sl@0
   162
	{
sl@0
   163
	// ASF files are logically composed of three types of top-level objects:
sl@0
   164
	// the Header Object, the Data Object, and the Index Object(s).
sl@0
   165
	// The Header Object is mandatory and must be placed at the beginning of every
sl@0
   166
	// ASF file. The Data Object is also mandatory and must follow the Header Object.
sl@0
   167
	// The Index Object(s) are optional, but they are useful in providing time-based
sl@0
   168
	// random access into ASF files. When present, the Index Object(s) must be the
sl@0
   169
	// last object in the ASF file. 
sl@0
   170
	
sl@0
   171
	TBuf8<KGUIDLen> guid;
sl@0
   172
	TInt64 size;
sl@0
   173
	const TBool useLittleEndian = ETrue;
sl@0
   174
	
sl@0
   175
	// Assume there's video content present if we only have buffer data.
sl@0
   176
	if (iReader.Type() == CReader::EBuffer)
sl@0
   177
		{
sl@0
   178
		iFlags.SetBit(KASFVideoBit);
sl@0
   179
		}
sl@0
   180
		
sl@0
   181
	ReadObjectL(guid, size);
sl@0
   182
	if (guid == MAKE_TPtrC8(KASFHeaderObject))
sl@0
   183
		{
sl@0
   184
		TUint32 objectCount;
sl@0
   185
		
sl@0
   186
		if (size < KMinObjectLen)
sl@0
   187
			{
sl@0
   188
			User::Leave(KErrCorrupt);
sl@0
   189
			}
sl@0
   190
			
sl@0
   191
		iFlags.SetBit(KASFHeaderObjectBit);
sl@0
   192
		// We need to find out how many objects there are.
sl@0
   193
		iReader.Read32L(objectCount, useLittleEndian);
sl@0
   194
		iReader.SeekL(2); // Ignore reserved values (two bytes).
sl@0
   195
		
sl@0
   196
		const TDesC8& streamPropertiesGUID = MAKE_TPtrC8(KASFStreamPropertiesObject);
sl@0
   197
		const TDesC8& videoMediaGUID = MAKE_TPtrC8(KASFVideoMedia);
sl@0
   198
		
sl@0
   199
		for (TInt i = 0; i < objectCount; i++)
sl@0
   200
			{
sl@0
   201
			ReadObjectL(guid, size);
sl@0
   202
			
sl@0
   203
			// We want the stream properties object.
sl@0
   204
			if (guid == streamPropertiesGUID)
sl@0
   205
				{
sl@0
   206
				// There may be more than one present.
sl@0
   207
				iFlags.SetBit(KASFStreamHeaderBit);
sl@0
   208
				
sl@0
   209
				ReadGUIDL(guid);
sl@0
   210
				if (guid == videoMediaGUID)
sl@0
   211
					{
sl@0
   212
					iFlags.SetBit(KASFVideoBit);
sl@0
   213
					}
sl@0
   214
				iReader.SeekL(size - KObjectLen - KGUIDLen);
sl@0
   215
				}
sl@0
   216
			else
sl@0
   217
				{
sl@0
   218
				iReader.SeekL(size - KObjectLen);
sl@0
   219
				}
sl@0
   220
			}
sl@0
   221
		}
sl@0
   222
	else
sl@0
   223
		{
sl@0
   224
		User::Leave(KErrCorrupt);
sl@0
   225
		}
sl@0
   226
	}
sl@0
   227
	
sl@0
   228
//
sl@0
   229
//
sl@0
   230
//
sl@0
   231
void TASFParser::ReadObjectL(TDes8& aGUID, TInt64& aSize)
sl@0
   232
	{
sl@0
   233
	//The base unit of organization for ASF files is called the ASF object.
sl@0
   234
	//It consists of a 128-bit GUID for the object, a 64-bit integer object size,
sl@0
   235
	//and the variable-length object data. The value of the object size field is
sl@0
   236
	//the sum of 24 bytes plus the size of the object data in bytes.
sl@0
   237
	
sl@0
   238
	TUint32 word1;
sl@0
   239
	TUint32 word2;
sl@0
   240
	const TBool useLittleEndian = ETrue;
sl@0
   241
	
sl@0
   242
	aGUID.SetLength(KGUIDLen);
sl@0
   243
	ReadGUIDL(aGUID);
sl@0
   244
	
sl@0
   245
	iReader.Read32L(word2, useLittleEndian);
sl@0
   246
	iReader.Read32L(word1, useLittleEndian);
sl@0
   247
	
sl@0
   248
	aSize = MAKE_TINT64(word1, word2);
sl@0
   249
	}
sl@0
   250
sl@0
   251
sl@0
   252
//
sl@0
   253
//
sl@0
   254
//
sl@0
   255
void TASFParser::ReadGUIDL(TDes8& aGUID)
sl@0
   256
	{
sl@0
   257
	TUint8 byte;
sl@0
   258
	
sl@0
   259
	if (aGUID.Length() != KGUIDLen)
sl@0
   260
		{
sl@0
   261
		User::Leave(KErrUnderflow);
sl@0
   262
		}
sl@0
   263
	
sl@0
   264
	// Parts of the GUID are stored in big-endian order.
sl@0
   265
	// They're converted to little-endian order here.
sl@0
   266
	iReader.ReadByteL(byte);	aGUID[3] = byte;
sl@0
   267
	iReader.ReadByteL(byte);	aGUID[2] = byte;
sl@0
   268
	iReader.ReadByteL(byte);	aGUID[1] = byte;
sl@0
   269
	iReader.ReadByteL(byte);	aGUID[0] = byte;
sl@0
   270
	
sl@0
   271
	iReader.ReadByteL(byte);	aGUID[5] = byte;
sl@0
   272
	iReader.ReadByteL(byte);	aGUID[4] = byte;
sl@0
   273
	
sl@0
   274
	iReader.ReadByteL(byte);	aGUID[7] = byte;
sl@0
   275
	iReader.ReadByteL(byte);	aGUID[6] = byte;
sl@0
   276
	
sl@0
   277
	for (TInt i = 8; i < KGUIDLen; i++)
sl@0
   278
		{
sl@0
   279
		iReader.ReadByteL(byte);
sl@0
   280
		aGUID[i] = byte; 
sl@0
   281
		}
sl@0
   282
	}
sl@0
   283