os/mm/mmlibs/mmfw/Recogniser/src/mpeg2parser.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmlibs/mmfw/Recogniser/src/mpeg2parser.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,299 @@
     1.4 +// Copyright (c) 2006-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 "parsers.h"
    1.20 +
    1.21 +static const TUint8 KISOEndCode = 0xB9;
    1.22 +static const TUint8 KPackStartCode = 0xBA;
    1.23 +static const TUint8 KVideoElementaryStream = 0xE0;
    1.24 +
    1.25 +#define KMPEG1PackHeaderLen		8
    1.26 +#define KMPEG2PackHeaderLen		10
    1.27 +
    1.28 +#define KMPEG1PackHeaderID		0x21	// 0010xxx1. See Doc Link 2.
    1.29 +#define KMPEG2PackHeaderID		0x44	// 01xxx1xx. See Doc Link 2.
    1.30 +
    1.31 +#define KStartCodeMask			0xffffff00
    1.32 +#define KStartCodeIntro			0x00000100
    1.33 +
    1.34 +#define KMPEG2StartCode1Bit	KBit1
    1.35 +#define KMPEG2StartCode2Bit	KBit2
    1.36 +#define KMPEG2VideoBit		KBit3
    1.37 +#define KMPEG2MPEG1Bit		KBit4
    1.38 +
    1.39 +//
    1.40 +// Mapping flags to a confidence level.
    1.41 +//
    1.42 +// A: extension match
    1.43 +// B: start-code1
    1.44 +// C: start-code2
    1.45 +//
    1.46 +// C B A -> Confidence
    1.47 +// -------------------
    1.48 +// 0 0 0 -> ENotRecognised
    1.49 +// 0 0 1 -> EPossible
    1.50 +// 0 1 0 -> EPossible
    1.51 +// 0 1 1 -> EProbable
    1.52 +// 1 0 0 -> ENotRecognised
    1.53 +// 1 0 1 -> ENotRecognised
    1.54 +// 1 1 0 -> EProbable
    1.55 +// 1 1 1 -> ECertain
    1.56 +//
    1.57 +static const TInt KMPEG2FlagsToConfidence[] =
    1.58 +	{
    1.59 +	KConfNotRecognised,
    1.60 +	KConfPossible,
    1.61 +	KConfPossible,
    1.62 +	KConfProbable,
    1.63 +	KConfNotRecognised,
    1.64 +	KConfNotRecognised,
    1.65 +	KConfProbable,
    1.66 +	KConfCertain
    1.67 +	};
    1.68 +
    1.69 +#define KMPEG2ConfidenceMask	0x07	// 00000111
    1.70 +#define KMPEG2MimeMask	 		0x18	// 00011000
    1.71 +#define KMPEG2MimeShift			0x03	
    1.72 +
    1.73 +static const TInt KMPEG2ExtOnlyIndex = 1;
    1.74 +	
    1.75 +//
    1.76 +// The 'ED' bits of the flags member are used as an index into
    1.77 +// this table of possible MIME-types.
    1.78 +//
    1.79 +// E: MPEG1 instead of MPEG2
    1.80 +// D: Video content present
    1.81 +//
    1.82 +// E D -> Mime
    1.83 +// -----------
    1.84 +// 0 0 -> audio/mpeg2
    1.85 +// 0 1 -> video/mpeg2
    1.86 +// 1 0 -> audio/mpeg1
    1.87 +// 1 1 -> video/mpeg1
    1.88 +//
    1.89 +static const TText8* const KMPEG2Mimes[] = 
    1.90 +	{
    1.91 +	KMimeMPEG2_A,
    1.92 +	KMimeMPEG2_V,
    1.93 +	KMimeMPEG1_A,
    1.94 +	KMimeMPEG1_V
    1.95 +	};
    1.96 +
    1.97 +//
    1.98 +// A list of known MPEG2 file extensions.
    1.99 +// MPEG1 file extensions are also listed.
   1.100 +//
   1.101 +typedef struct
   1.102 +	{
   1.103 +	const TText* iExt;
   1.104 +	const TText8* iMime;
   1.105 +	}
   1.106 +TMPEG2Types;
   1.107 +
   1.108 +static const TMPEG2Types KMPEG2Types[] = 
   1.109 +	{
   1.110 +		{ KExtMPEG_1, KMimeMPEG2_V },
   1.111 +		{ KExtMPEG_2, KMimeMPEG2_V },
   1.112 +		{ KExtMPEG_3, KMimeMPEG2_V },
   1.113 +		{ KExtMPEG_4, KMimeMPEG2_V },
   1.114 +		{ KExtMPEG_5, KMimeMPEG2_V },
   1.115 +		{ KExtMPEG_6, KMimeMPEG2_V },
   1.116 +		{ KExtMPEG_7, KMimeMPEG1_V },
   1.117 +		{ KExtMPEG_8, KMimeMPEG2_A }
   1.118 +	};
   1.119 +	
   1.120 +#define KMPEG2ExtCount sizeof(KMPEG2Types) / sizeof(TMPEG2Types)
   1.121 +	
   1.122 +
   1.123 +//
   1.124 +//
   1.125 +//
   1.126 +TMPEG2Parser::TMPEG2Parser(CReader& aReader, TFlags& aFlags)
   1.127 + :	iReader(aReader),
   1.128 + 	iFlags(aFlags)
   1.129 +	{
   1.130 +	}
   1.131 +
   1.132 +
   1.133 +//
   1.134 +// Match the file's extension to known MPEG2 file extensions.
   1.135 +//
   1.136 +const TText8* TMPEG2Parser::MatchExtension(const TDesC& aExt)
   1.137 +	{
   1.138 +	if (aExt.Length() > 0)
   1.139 +		{
   1.140 +		for (TInt i = 0; i < KMPEG2ExtCount; i++)
   1.141 +			{
   1.142 +			if (aExt.MatchF(TPtrC(KMPEG2Types[i].iExt)) != KErrNotFound)
   1.143 +				{
   1.144 +				iFlags.SetExtensionFlag();
   1.145 +				return KMPEG2Types[i].iMime;
   1.146 +				}
   1.147 +			}
   1.148 +		}
   1.149 +	
   1.150 +	return NULL;
   1.151 +	}
   1.152 +
   1.153 +
   1.154 +//
   1.155 +//
   1.156 +//
   1.157 +void TMPEG2Parser::DoRecognise(const TDesC& aExt, CReader& aReader, TMatch& aMatch)
   1.158 +	{
   1.159 +	TFlags flags;
   1.160 +	TMPEG2Parser parser(aReader, flags);
   1.161 +	
   1.162 +	const TText8* extMime = parser.MatchExtension(aExt);
   1.163 +	TRAP_IGNORE(parser.ParseL());
   1.164 +	
   1.165 +	TInt confIndex = flags.GetBitField(KMPEG2ConfidenceMask);
   1.166 +	aMatch.iConfidence = KMPEG2FlagsToConfidence[confIndex];
   1.167 +	if (aMatch.iConfidence != KConfNotRecognised)
   1.168 +		{
   1.169 +		if (confIndex == KMPEG2ExtOnlyIndex)
   1.170 +			{
   1.171 +			// The content is corrupt, but the extension was recognised.
   1.172 +			aMatch.iMime = extMime;
   1.173 +			}
   1.174 +		else
   1.175 +			{
   1.176 +			TInt mimeIndex = flags.GetBitField(KMPEG2MimeMask, KMPEG2MimeShift);
   1.177 +			aMatch.iMime = KMPEG2Mimes[mimeIndex];
   1.178 +			}
   1.179 +		}
   1.180 +	}
   1.181 +
   1.182 +
   1.183 +//
   1.184 +// Attempts to parse an MPEG2 file by looking for start-codes.
   1.185 +//
   1.186 +void TMPEG2Parser::ParseL()
   1.187 +	{
   1.188 +	TBool finished;
   1.189 +	
   1.190 +	// Assume there's video content if we only have a buffer.
   1.191 +	if (iReader.Type() == CReader::EBuffer)
   1.192 +		{
   1.193 +		iFlags.SetBit(KMPEG2VideoBit);
   1.194 +		}
   1.195 +	
   1.196 +	do
   1.197 +		{
   1.198 +		finished = NextStartCodeL();
   1.199 +		}
   1.200 +	while (!finished);
   1.201 +	}
   1.202 +
   1.203 +
   1.204 +//
   1.205 +// Skips over the current start-code box.
   1.206 +//
   1.207 +void TMPEG2Parser::SkipL()
   1.208 +	{
   1.209 +	TUint16 size;
   1.210 +	
   1.211 +	iReader.Read16L(size);
   1.212 +	iReader.SeekL((TInt)size);
   1.213 +	}
   1.214 +
   1.215 +
   1.216 +/*
   1.217 +** Expects an MPEG2 Pack Header at the current location.
   1.218 +** The Pack Header is used to determine between MPEG1 and MPEG2.
   1.219 +*/
   1.220 +void TMPEG2Parser::ReadPackHeaderL()
   1.221 +	{
   1.222 +	TUint8 byte;
   1.223 +	TBuf8<KMPEG2PackHeaderLen> header;	// Size is of whichever is larger.
   1.224 +	
   1.225 +	iReader.ReadByteL(byte);
   1.226 +	
   1.227 +	if ((byte & KMPEG1PackHeaderID) == KMPEG1PackHeaderID)
   1.228 +		{
   1.229 +		iFlags.SetBit(KMPEG2MPEG1Bit);
   1.230 +		header.SetLength(KMPEG1PackHeaderLen - 1);	// We've already read a byte.
   1.231 +		iReader.ReadBytesL(header);
   1.232 +		}
   1.233 +	else if ((byte & KMPEG2PackHeaderID) == KMPEG2PackHeaderID)
   1.234 +		{
   1.235 +		header.SetLength(KMPEG2PackHeaderLen - 1);	// We've already read a byte.
   1.236 +		iReader.ReadBytesL(header);
   1.237 +		
   1.238 +		// The lowest 3 bits of the last byte say how much stuffing is present.
   1.239 +		TInt stuffing = header[8] & 0x07; // 00000111
   1.240 +		if (stuffing)
   1.241 +			{
   1.242 +			iReader.SeekL(stuffing);
   1.243 +			}
   1.244 +		}
   1.245 +	else
   1.246 +		{
   1.247 +		User::Leave(KErrCorrupt);
   1.248 +		}
   1.249 +		
   1.250 +	}
   1.251 +
   1.252 +//
   1.253 +// Start codes are bit patterns that do not occur in the video stream.
   1.254 +// The start-code sequence is expected to be at the current CReader position.
   1.255 +//
   1.256 +TBool TMPEG2Parser::NextStartCodeL()
   1.257 +	{
   1.258 +	TUint32 data;
   1.259 +
   1.260 +	iReader.Read32L(data);
   1.261 +	
   1.262 +	// Start codes must begin with 0x000001ss, where 'ss' is the start code.
   1.263 +	if ((data & KStartCodeMask) != KStartCodeIntro)
   1.264 +		{
   1.265 +		User::Leave(KErrCorrupt);
   1.266 +		}
   1.267 +		
   1.268 +	if (!iFlags.GetBitField(KMPEG2StartCode1Bit))
   1.269 +		{
   1.270 +		iFlags.SetBit(KMPEG2StartCode1Bit);
   1.271 +		}
   1.272 +	else
   1.273 +		{
   1.274 +		if (!iFlags.GetBitField(KMPEG2StartCode2Bit))
   1.275 +			{
   1.276 +			iFlags.SetBit(KMPEG2StartCode2Bit);
   1.277 +			}
   1.278 +		}
   1.279 +		
   1.280 +	// Try to identify the start code.
   1.281 +	switch (LOW_BYTE(data))
   1.282 +		{
   1.283 +		case KPackStartCode:
   1.284 +			ReadPackHeaderL();
   1.285 +			break;
   1.286 +			
   1.287 +		case KVideoElementaryStream:
   1.288 +			iFlags.SetBit(KMPEG2VideoBit);
   1.289 +			return ETrue;
   1.290 +			
   1.291 +		case KISOEndCode:
   1.292 +			// This code should occur at the end of the file and
   1.293 +			// it cannot be skipped over.
   1.294 +			return ETrue;
   1.295 +			
   1.296 +		default:
   1.297 +			SkipL();
   1.298 +		}
   1.299 +		
   1.300 +	return EFalse;
   1.301 +	}
   1.302 +