os/mm/mmlibs/mmfw/Recogniser/src/rmparser.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "parsers.h"
    17 
    18 static const TUint32 KRMFileHeaderId = MAKE_INT32('.', 'R', 'M', 'F');
    19 static const TUint32 KRMMediaPropertiesId = MAKE_INT32('M', 'D', 'P', 'R');
    20 static const TInt KRMMaxMimeTypeLen = 255;	// Specified by an 8-bit field
    21 
    22 #define KRMExtensionBit			KBit0
    23 #define KRMFileHeaderBit		KBit1
    24 #define KRMMediaPropertiesBit	KBit2
    25 #define KRMVideoBit				KBit3
    26 
    27 #define KRMVideoMask			0x08	// 0000 1000
    28 #define KRMConfidenceMask 		0x07	// 0000 0111
    29 
    30 //
    31 // This truth table maps the following flags to a confidence level.
    32 // -----------------------------------------------------------------
    33 // A: MediaProperties chunk found
    34 // B: FileHeader signature found
    35 // C: Extension recognised.
    36 //
    37 // A B C -> Confidence
    38 // ===================
    39 // 0 0 0 -> ENotRecognized
    40 // 0 0 1 -> EPossible
    41 // 0 1 0 -> EPossible (EPossible beacuse the header is ASCII and may match plaintext)
    42 // 0 1 1 -> ECertain
    43 // 1 0 0 -> ENotRecognised (Can't have MediaProperties without FileHeader)
    44 // 1 0 1 -> ENotRecognised (Can't have MediaProperties without FileHeader)
    45 // 1 1 0 -> EProbable
    46 // 1 1 1 -> ECertain
    47 //
    48 static const TInt KRMFlagsToConfidence[] =
    49 	{
    50 	KConfNotRecognised,
    51 	KConfPossible,
    52 	KConfPossible,
    53 	KConfCertain,
    54 	KConfNotRecognised,
    55 	KConfNotRecognised,
    56 	KConfProbable,
    57 	KConfCertain
    58 	};
    59 
    60 
    61 //
    62 // Constructor.
    63 //
    64 TRMParser::TRMParser(CReader& aReader, TFlags& aFlags)
    65  :	iReader(aReader),
    66 	iFlags(aFlags)
    67 	{
    68 	}
    69 
    70 
    71 //
    72 // The main RealMedia parsing function.
    73 //
    74 void TRMParser::DoRecognise(const TDesC& aFileExt, CReader& aReader, TMatch& aMatch)
    75 	{
    76 	TFlags flags;
    77 	TRMParser parser(aReader, flags);
    78 
    79 	parser.MatchExtension(aFileExt);
    80 
    81 	TRAP_IGNORE(parser.ParseL());
    82 
    83 	TInt confIndex = flags.GetBitField(KRMConfidenceMask);
    84 	aMatch.iConfidence = KRMFlagsToConfidence[confIndex];
    85 
    86 	if (aMatch.iConfidence != KConfNotRecognised)
    87 		{
    88 		aMatch.iMime = (flags.GetBitField(KRMVideoMask) ? KMimeRM_V : KMimeRM_A);
    89 		}
    90 	}
    91 
    92 
    93 //
    94 // Match the file extension given by AppArc against known
    95 // extensions for this format.
    96 //
    97 void TRMParser::MatchExtension(const TDesC& aFileExt)
    98 	{
    99 	TBool match;
   100 
   101 	match = (aFileExt.MatchF(TPtrC(KExtRMF)) != KErrNotFound);
   102 	match |= (aFileExt.MatchF(TPtrC(KExtRM)) != KErrNotFound);
   103 
   104 	if (match)
   105 		{
   106 		iFlags.SetExtensionFlag();
   107 		}
   108 	}
   109 
   110 //
   111 // RealMedia files are divided into chunks. Chunks begins with a 32-bit identifier
   112 // which is followed by a 32-bit unsigned field that specifies the size of the chunk
   113 // in bytes. Since the size field is unsigned, it must be cast to a TInt64 in order
   114 // for SeekL() to work reliably. The size field also includes the length of the chunk
   115 // identifier and the length of the size field itself, so these must be subtracted
   116 // from any seek operation.
   117 //
   118 void TRMParser::ParseL()
   119 	{
   120 	TUint32 objectId;
   121 	TUint32 size;
   122 	const TInt KChunkHeaderSize = sizeof(TUint32) * 2; // sizeof objectId and size fields.
   123 
   124 	// The first chunk must be the FileHeader.
   125 	ReadChunkHeaderL(objectId, size, ETrue);
   126 
   127 	// Assume there's video content if we only have buffer data.
   128 	if (iReader.Type() == CReader::EBuffer)
   129 		{
   130 		iFlags.SetBit(KRMVideoBit);
   131 		}
   132 
   133 	iReader.SeekL(TInt64(size - KChunkHeaderSize));
   134 
   135 	// The other chunks can occur in any order.
   136 	TBool complete = EFalse;
   137 	do
   138 		{
   139 		ReadChunkHeaderL(objectId, size);
   140 
   141 		if (objectId == KRMMediaPropertiesId)
   142 			{
   143 			iFlags.SetBit(KRMMediaPropertiesBit);
   144 			if (ReadMediaPropertiesL())
   145 				{
   146 				// Exit early if the video flag has been set by ReadMediaPropertiesL.
   147 				complete = ETrue;
   148 				}
   149 			}
   150 		else
   151 			{
   152 			// Skip over the unneeded chunk.
   153 			iReader.SeekL(TInt64(size - KChunkHeaderSize));
   154 			}
   155 		}
   156 	while (!complete);
   157 	}
   158 
   159 
   160 //
   161 // Reads the chunk identifier and size.
   162 //
   163 void TRMParser::ReadChunkHeaderL(TUint32& aObjectId, TUint32& aSize, TBool aFirstChunk)
   164 	{
   165 	iReader.Read32L(aObjectId);
   166 
   167 	if (aFirstChunk)
   168 		{
   169 		if (aObjectId == KRMFileHeaderId)
   170 			{
   171 			iFlags.SetBit(KRMFileHeaderBit);
   172 			}
   173 		else
   174 			{
   175 			User::Leave(KErrCorrupt);
   176 			}
   177 		}
   178 
   179 	iReader.Read32L(aSize);
   180 	if (aSize == 0)
   181 		{
   182 		User::Leave(KErrCorrupt);
   183 		}
   184 	}
   185 
   186 
   187 //
   188 // The MediaProperties chunk describes a stream in the RM file. There
   189 // may be several MediaProperties chunks in one file. In order to determine
   190 // if video content is present we must look at the MIME-type that may
   191 // be present in each MediaProperties chunk.
   192 // Return ETrue if this function set the video bit.
   193 //
   194 TBool TRMParser::ReadMediaPropertiesL()
   195 	{
   196 	const TInt KBytesToFieldSize = 30;
   197 	
   198 	// ObjectId and size fields have already been read.
   199 	TBool setVideo = EFalse;
   200 	TUint16 objectVersion;
   201 
   202 	iReader.Read16L(objectVersion);
   203 
   204 	if (objectVersion == 0)
   205 		{
   206 		TUint8 fieldSize;
   207 		TBuf8<KRMMaxMimeTypeLen> mimeType;
   208 		_LIT8(KVideoStar, "video/*");
   209 
   210 		// Skip unneeded information.
   211 		iReader.SeekL(KBytesToFieldSize);
   212 
   213 		// Read stream_name_size so we can skip over it.
   214 		iReader.ReadByteL(fieldSize);
   215 		iReader.SeekL(fieldSize);
   216 
   217 		// Read the mime_type_size. Because it's 8-bit, it has a max length of 256 bytes.
   218 		iReader.ReadByteL(fieldSize);
   219 
   220 		mimeType.SetLength(fieldSize);
   221 		iReader.ReadBytesL(mimeType);
   222 
   223 		// Now check if the mime-type field indicates video content.
   224 		if (mimeType.MatchF(KVideoStar) != KErrNotFound)
   225 			{
   226 			iFlags.SetBit(KRMVideoBit);
   227 			setVideo = ETrue;
   228 			}
   229 
   230 		// Read the type_specific_len so we can skip over it.
   231 		TUint32 typeSpecificLen;
   232 		iReader.Read32L(typeSpecificLen);
   233 		iReader.SeekL(TInt64(typeSpecificLen));
   234 		}
   235 
   236 	return setVideo;
   237 	}
   238