mdfdevvideoextensions/nga_mdf_postprocessor/src/NGAPostProcHwDevice.cpp
changeset 0 40261b775718
child 7 94dbab0a2133
child 13 efebd1779a59
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "NGAPostProcHwDevice.h"
       
    20 #include "NGAPostProcSessionManager.h"
       
    21 #include "NGAPostProcSurfaceHandler.h"
       
    22 #include "common.h"
       
    23 
       
    24 // post-processor info
       
    25 const TUid KUidVideoPostProcHwDevice = {KUidNGAPostProcHwDeviceImplUid};
       
    26 _LIT(KManufacturer, "Nokia Inc.");
       
    27 _LIT(KIdentifier, "Nokia S60 Video Post Processor Hardware Device Plugin");
       
    28 
       
    29 // --- Constants ---
       
    30 const TInt KMaxVBMBuffers      			= 4;
       
    31 const TInt KMinVBMInputWidth   			= 32; 
       
    32 const TInt KMinVBMInputHeight  			= 32;
       
    33 const TInt KMaxVBMInputWidth   			= 1280; 
       
    34 const TInt KMaxVBMInputHeight  			= 720;
       
    35 const TInt KRenderAhead 	     		= 50000;     
       
    36 const TInt KMaxRenderDelay     			= 50000;
       
    37 const TInt KPostingOfset       			= 0;    
       
    38 const TInt KColorConversionBuffers  	= 3;
       
    39 const TInt KMaxBuffersGceCanHold    	= 3;
       
    40 const TInt KDefPlayRate					= 100;
       
    41 const TInt KMaxAllowedSkipInNFrames 	= 40;
       
    42 #ifdef __cplusplus
       
    43 extern "C"
       
    44 {
       
    45 #endif
       
    46 
       
    47 int32 gColorConvYUVtoYUV422Int (tBaseVideoFrame *yuv420Frame, tBaseVideoFrame* yuv422Frame,
       
    48 							   uint8 outClrFmt, int16 stride); 
       
    49 
       
    50 #ifdef __cplusplus
       
    51 }
       
    52 #endif
       
    53 
       
    54 //**************************************************
       
    55 
       
    56 CMMFVideoPostProcHwDevice* CNGAPostProcHwDevice::NewL() 
       
    57 { 
       
    58    PP_DEBUG(_L("CNGAPostProcHwDevice:NewL() ++"));
       
    59 
       
    60     CNGAPostProcHwDevice* self = new (ELeave) CNGAPostProcHwDevice; 
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL(); 
       
    63     CleanupStack::Pop();
       
    64 
       
    65    PP_DEBUG(_L("CNGAPostProcHwDevice:NewL() --"));
       
    66     return (CMMFVideoPostProcHwDevice*)self; 
       
    67 }
       
    68 
       
    69 void CNGAPostProcHwDevice::ConstructL() 
       
    70 { 
       
    71    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ConstructL() ++"), this);
       
    72     
       
    73     // support for VBM buffer interface
       
    74     iVBMBufferOptions.iNumInputBuffers  = KMaxVBMBuffers; 
       
    75     iVBMBufferOptions.iBufferSize = TSize(KMaxVBMInputWidth, KMaxVBMInputHeight);
       
    76     iPostingTimer = CNGAPostProcTimer::NewL(*this);
       
    77     iWsSession.Connect();
       
    78    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ConstructL() --"), this);
       
    79 }
       
    80 
       
    81 CNGAPostProcHwDevice::CNGAPostProcHwDevice() 
       
    82         :   iProxy(NULL), 
       
    83             iInputDecoderDevice(NULL),
       
    84             iCurrentPlaybackPosition(TTimeIntervalMicroSeconds(0)),
       
    85             iPPState(EInitializing),
       
    86             iSurfaceHandler(NULL),
       
    87             iSessionManager(NULL),
       
    88             iIsInputEnded(EFalse),
       
    89             iPostingTimer(NULL),
       
    90             iFirstPictureUpdated(EFalse),
       
    91             iUsingExternalSurface(EFalse),
       
    92             iIsColorConversionNeeded(EFalse),
       
    93             iSurfaceCreatedEventPublished(EFalse),
       
    94             iOverflowPictureCounter(0),
       
    95             iVideoFrameBufSize(0),
       
    96             iResourceLost(EFalse),
       
    97             iVBMObserver(NULL),
       
    98             count(0),
       
    99             iSurfaceMask(surfaceHints::EAllowAllExternals),
       
   100             iSurfaceKey(TUid::Uid(surfaceHints::KSurfaceProtection)),
       
   101             iVideoSurfaceObserver(NULL),
       
   102             iVPObserver(NULL),
       
   103             iPicSize(0,0),
       
   104             iStepFrameCount(0),
       
   105             iPlayRate(KDefPlayRate),
       
   106             iKeyFrameMode(EFalse),
       
   107             iFPObserver(NULL)
       
   108 {
       
   109 	iRedrawSurfaceId = TSurfaceId::CreateNullId();
       
   110 	iSurfaceId 		 = TSurfaceId::CreateNullId();
       
   111 	iAttributes().iPixelFormat    = EUidPixelFormatYUV_422Interleaved;
       
   112 }
       
   113 
       
   114 CNGAPostProcHwDevice::~CNGAPostProcHwDevice()
       
   115 {
       
   116    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:~() ++"), this);
       
   117    	if (iSessionManager)
       
   118     {
       
   119         iSessionManager->CancelUpdate();
       
   120         delete iSessionManager;
       
   121         iSessionManager = NULL;
       
   122     }
       
   123    	
       
   124    	while (iSupportedInputFormats.Count()>0)
       
   125     {
       
   126 		iSupportedInputFormats.Remove(0);
       
   127     }
       
   128     
       
   129    	while (iProcessQ.Count()>0)
       
   130     {
       
   131 		iProcessQ.Remove(0);
       
   132     }
       
   133 
       
   134    	if(iPostingTimer)
       
   135     {
       
   136     	iPostingTimer->Cancel();
       
   137         delete iPostingTimer;
       
   138         iPostingTimer = NULL;
       
   139     }
       
   140     
       
   141     while (iVBMBufferReferenceQ.Count()>0)
       
   142     {
       
   143         TVideoPicture* pic = iVBMBufferReferenceQ[0];
       
   144         iVBMBufferReferenceQ.Remove(0);
       
   145 
       
   146         if (pic->iHeader) delete pic->iHeader;
       
   147         delete pic->iData.iRawData;
       
   148         delete pic;
       
   149     }
       
   150     
       
   151     iSupportedInputFormats.Reset();
       
   152     iSupportedInputFormats.Close();
       
   153     
       
   154     iVBMBufferReferenceQ.Reset();
       
   155     iVBMBufferReferenceQ.Close();
       
   156     
       
   157     iVBMBufferQ.Reset();
       
   158     iVBMBufferQ.Close();
       
   159     
       
   160     iProcessQ.Reset();
       
   161     iProcessQ.Close();
       
   162         
       
   163     iInputQ.Reset();
       
   164     iInputQ.Close();
       
   165     
       
   166     if (iSurfaceHandler)
       
   167     {
       
   168     	if(!iSurfaceId.IsNull())
       
   169     	{
       
   170     		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::UnregisterSurface"), this);
       
   171     		iWsSession.UnregisterSurface(0, iSurfaceId);
       
   172         	iSurfaceHandler->DestroySurface(iSurfaceId);
       
   173     	}
       
   174         delete iSurfaceHandler;
       
   175         iSurfaceHandler = NULL;
       
   176     }
       
   177     
       
   178     iWsSession.Close();
       
   179     iChunk.Close();
       
   180     RDebug::Printf("------ Statistics of Post Processor ------");
       
   181     RDebug::Printf("    Pictures Received : %d", iPictureCounters.iTotalPictures);
       
   182     RDebug::Printf("    Pictures Displayed: %d", iPictureCounters.iPicturesDisplayed);
       
   183     RDebug::Printf("    Pictures Skipped  : %d", iPictureCounters.iPicturesSkipped);
       
   184     RDebug::Printf("    Pictures overflow : %d", iOverflowPictureCounter);
       
   185     RDebug::Printf("------------------------------------------");
       
   186     
       
   187    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:~() --"), this);
       
   188 }
       
   189 
       
   190 void CNGAPostProcHwDevice::SetInputFormatL(const TUncompressedVideoFormat&  aFormat) 
       
   191 { 
       
   192    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputFormatL() Pattern= %x ++"), this, aFormat.iYuvFormat.iPattern);
       
   193     if (iPPState != EInitializing)
       
   194     {
       
   195 				PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputFormatL() FAILED: Unexpected state"), this);
       
   196         User::Leave(KErrNotReady);
       
   197 	  }
       
   198 
       
   199 
       
   200 		if( ((aFormat.iYuvFormat.iPattern == EYuv420Chroma1) ||
       
   201 			(aFormat.iYuvFormat.iPattern == EYuv420Chroma2) ||
       
   202     		(aFormat.iYuvFormat.iPattern == EYuv420Chroma3) ))
       
   203 		{
       
   204 #if defined __WINSCW__				
       
   205 				iIsColorConversionNeeded = ETrue; 
       
   206 #else
       
   207 				iAttributes().iPixelFormat = EUidPixelFormatYUV_420Planar;
       
   208 #endif //__WINSCW__
       
   209 		}	
       
   210 
       
   211    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputFormatL() WARNING: -- Not Found!"), this);
       
   212 }
       
   213 
       
   214 
       
   215 void CNGAPostProcHwDevice::SetInputDevice(CMMFVideoDecodeHwDevice* aDevice) 
       
   216 { 
       
   217    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputDevice() ++"), this);
       
   218 
       
   219     if (iPPState != EInitializing)
       
   220     {
       
   221 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputDevice() FAILED: unexpected state"), this);
       
   222         return;
       
   223 	}
       
   224 
       
   225     iInputDecoderDevice = aDevice;
       
   226 
       
   227    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputDevice() --"), this);
       
   228 }
       
   229 
       
   230 void CNGAPostProcHwDevice::GetOutputFormatListL(RArray<TUncompressedVideoFormat>& ) 
       
   231 { 
       
   232    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:GetOutputFormatListL() ++"), this);
       
   233 
       
   234 
       
   235 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:GetOutputFormatListL() --"), this);
       
   236 }
       
   237 
       
   238 void CNGAPostProcHwDevice::SetOutputFormatL(const TUncompressedVideoFormat&  ) 
       
   239 {
       
   240    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetOutputFormatL() ++"), this);
       
   241 
       
   242 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetOutputFormatL() --"), this);
       
   243 }
       
   244 
       
   245 void CNGAPostProcHwDevice::SetClockSource(MMMFClockSource* aClock) 
       
   246 { 
       
   247    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetClockSource() ++"), this);
       
   248     
       
   249     if (iPPState != EInitializing)
       
   250     {
       
   251 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetClockSource() FAILED: Unexpected state"), this);
       
   252         return;
       
   253 	}
       
   254     iClockSource = aClock;
       
   255 
       
   256    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetClockSource() --"), this);
       
   257 }
       
   258 
       
   259 void CNGAPostProcHwDevice::SetVideoDestScreenL(TBool /*aScreen*/) 
       
   260 { 
       
   261    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetVideoDestScreenL() ++"), this);
       
   262 
       
   263    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetVideoDestScreenL() --"), this);
       
   264 }
       
   265 
       
   266 void CNGAPostProcHwDevice::SetProxy(MMMFDevVideoPlayProxy& aProxy) 
       
   267 { 
       
   268    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetProxy() ++"), this);
       
   269 
       
   270     if (iPPState != EInitializing)
       
   271     {
       
   272 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetProxy() FAILED: Unexpected state"), this);
       
   273         return;
       
   274 	}
       
   275 
       
   276     iProxy = &aProxy;
       
   277 
       
   278    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetProxy() --"), this);
       
   279 }
       
   280 
       
   281 void CNGAPostProcHwDevice::Initialize() 
       
   282 {
       
   283    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize ++"));
       
   284 	TInt err = KErrNone;
       
   285 
       
   286     if (iPPState != EInitializing)
       
   287     {
       
   288 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize() FAILED: Unexpected state"), this);
       
   289         return;
       
   290 	}
       
   291 	if (!iSurfaceHandler)
       
   292     {
       
   293     	TRAP(err, iSurfaceHandler = CNGAPostProcSurfaceHandler::NewL());
       
   294     	if (err != KErrNone)
       
   295     	{
       
   296     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to create SurfaceHandler."), this);
       
   297     	    return;
       
   298     	}
       
   299     }
       
   300     if (!iSessionManager)
       
   301     {
       
   302     	TRAP(err, iSessionManager = CNGAPostProcSessionManager::NewL());
       
   303     	if (err != KErrNone)
       
   304     	{
       
   305     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to create SessionManager."), this);
       
   306     	    return;
       
   307     	}
       
   308     	iSessionManager->SetObserver(*this);
       
   309     }
       
   310 
       
   311   	if (iInputDecoderDevice)
       
   312 	{
       
   313 		MMmfVideoResourceHandler* handler = NULL;
       
   314 		handler = (MMmfVideoResourceHandler*)iInputDecoderDevice->CustomInterface(KUidMmfVideoResourceManagement);
       
   315 		if (handler)
       
   316 		{
       
   317 			handler->MmvrhSetObserver((MMmfVideoResourceObserver*)this);
       
   318 		}
       
   319 		else
       
   320 		{
       
   321 			PP_DEBUG(_L("ppHwDev[%x]:Initialize() decoder yet to implement MMmfVideoResourceHandler CI"), this);
       
   322 		}
       
   323 		
       
   324 		MMmfVideoPropertiesNotifier* VPHandler = NULL;
       
   325 		VPHandler = (MMmfVideoPropertiesNotifier*)iInputDecoderDevice->CustomInterface(KUidMmfVideoPropertiesManagement);
       
   326 		if (VPHandler)
       
   327 		{
       
   328 			PP_DEBUG(_L("ppHwDev[%x]:Initialize() Register for video property changes"), this);
       
   329 			VPHandler->MmvpnSetObserver((MMmfVideoPropertiesObserver*)this);
       
   330 		}
       
   331 		else
       
   332 		{
       
   333 			PP_DEBUG(_L("ppHwDev[%x]:Initialize() decoder yet to implement MMmfVideoPropertiesNotifier CI"), this);
       
   334 		}
       
   335 	}
       
   336 		
       
   337     // Initialize picture counters
       
   338 	iPictureCounters.iPicturesSkipped 	= 0;
       
   339 	iPictureCounters.iPicturesDisplayed = 0;
       
   340 	iPictureCounters.iTotalPictures = 0;
       
   341 	iOverflowPictureCounter = 0;
       
   342 	if (iProxy)
       
   343 	{
       
   344 		iProxy->MdvppInitializeComplete(this, err);
       
   345 	}
       
   346 	iPPState = EInitialized;	
       
   347    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize --"), this);
       
   348 }
       
   349 
       
   350 void CNGAPostProcHwDevice::WritePictureL(TVideoPicture* aPicture) 
       
   351 { 
       
   352 	PP_DEBUG(_L("CNGAPostProcHwDevice:WritePicture ++"));
       
   353 	TVideoPicture* pic;
       
   354 	if (iPPState==EInitializing || iPPState==EStopped || iIsInputEnded)
       
   355     {
       
   356 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:WritePictureL() FAILED: Unexpected state"), this);
       
   357         User::Leave(KErrNotReady);
       
   358 	}
       
   359 
       
   360     if(!aPicture)
       
   361     {
       
   362 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:WritePictureL() FAILED: Invalid argument"), this);
       
   363 		User::Leave(KErrArgument);
       
   364 	}
       
   365 	pic = aPicture;	
       
   366 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:WritePicture bufId = %d"), this,GetID(pic));
       
   367 	iPictureCounters.iTotalPictures++;
       
   368 	TInt64 delta = 0;
       
   369 	TTimeToPost iTimeToPost = (TTimeToPost)IsTimeToPost(pic, delta);
       
   370 	if(!IsGceReady())
       
   371     {  
       
   372 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:WritePictureL GCE not ready"), this );
       
   373 		if(iTimeToPost == EPostIt)
       
   374 		{
       
   375 				iTimeToPost = EDelayIt;
       
   376 		}
       
   377     }
       
   378 	switch(iTimeToPost)
       
   379 	{
       
   380 		case EDelayIt:
       
   381 		{
       
   382 			if(AddToQ(pic) != 0)
       
   383 			{
       
   384 				break;
       
   385 			}
       
   386 			iPostingTimer->Cancel();
       
   387 			SetTimer(delta);
       
   388 		}
       
   389 		break;
       
   390 		case EPostIt:
       
   391 		{
       
   392 		
       
   393 			if(iIsColorConversionNeeded)
       
   394 			{
       
   395 				TVideoPicture* ccPic;				
       
   396     			ccPic = DoColorConvert(pic); // output will be in ccPic
       
   397     			pic = ccPic;			   
       
   398 			}
       
   399 			iProcessQ.Append(pic);
       
   400 						
       
   401 			#ifdef _DUMP_YUV_FRAMES
       
   402 			captureYuv(pic);
       
   403 			#endif
       
   404 			iSessionManager->PostPicture(iSurfaceId, GetID(pic), iInfo().iBuffers, ETrue);	
       
   405 			iCurrentPlaybackPosition = pic->iTimestamp;			
       
   406 			
       
   407 			if(!iFirstPictureUpdated)
       
   408 			{
       
   409 				iFirstPictureUpdated = ETrue;
       
   410 				PublishSurfaceCreated();
       
   411 			}
       
   412 		}
       
   413 		break;
       
   414 		case ESkipIt:
       
   415 		{
       
   416 			ReleasePicture(pic); 
       
   417 			PicturesSkipped();
       
   418 		}
       
   419 		break;
       
   420 	}
       
   421 
       
   422 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:WritePicture --"), this);
       
   423 }
       
   424 
       
   425 
       
   426 CPostProcessorInfo* 
       
   427 CNGAPostProcHwDevice::PostProcessorInfoLC() 
       
   428 {
       
   429    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:PostProcessorInfoLC() ++"), this);
       
   430     TUncompressedVideoFormat yuvFormat;
       
   431     RArray<TUint32>                     SupportedPostProcess;
       
   432     TBool                               SupportedHwAcceleration = ETrue;   //Non-Accelerated ETrue, 
       
   433     TYuvToRgbCapabilities               SupportedYuvToRgbCapab; 
       
   434     TInt32                              SupportedRotations = ERotateNone; // no rotation supported
       
   435     
       
   436     TBool                               SupportedArbitraryScaling = EFalse; // no scaling supported
       
   437     RArray<TScaleFactor>                SupportedScaleFactors;
       
   438     TBool                               SupportedAntiAliasing = EFalse;
       
   439     
       
   440     //default
       
   441     yuvFormat.iDataFormat                     = EYuvRawData;
       
   442     yuvFormat.iYuvFormat.iYuv2RgbMatrix       = 0;
       
   443     yuvFormat.iYuvFormat.iRgb2YuvMatrix       = 0;
       
   444     yuvFormat.iYuvFormat.iAspectRatioNum      = 1;
       
   445     yuvFormat.iYuvFormat.iAspectRatioDenom    = 1;
       
   446     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt709Range1;
       
   447     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   448     yuvFormat.iYuvFormat.iDataLayout          = EYuvDataInterleavedBE;
       
   449     
       
   450     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   451     
       
   452     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   453     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   454     
       
   455     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt709Range0;
       
   456     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   457     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedBE;
       
   458     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   459     
       
   460     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   461     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   462     
       
   463     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt709Range0;
       
   464     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   465     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedLE;
       
   466     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   467             
       
   468 	yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   469     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   470 
       
   471     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt709Range1;
       
   472     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   473     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedLE;
       
   474     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   475     
       
   476     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   477     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   478             
       
   479     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt601Range0;
       
   480     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   481     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedBE;
       
   482     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   483     
       
   484     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   485     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   486 
       
   487     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt601Range1;
       
   488     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   489     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedBE;
       
   490     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   491     
       
   492     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   493     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   494             
       
   495     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt601Range0;
       
   496     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   497     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedLE;
       
   498     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   499     
       
   500     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   501     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   502 
       
   503     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt601Range1;
       
   504     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma1;
       
   505     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataInterleavedLE;
       
   506     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   507     
       
   508     yuvFormat.iYuvFormat.iPattern       	  = EYuv422Chroma2;
       
   509     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   510     
       
   511     //YUV 420 planar
       
   512     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt709Range1;
       
   513     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma1;
       
   514     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataPlanar;
       
   515     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   516 
       
   517     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma2;
       
   518     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   519 
       
   520     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma3;
       
   521     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   522 
       
   523     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt709Range0;
       
   524     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma1;
       
   525     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataPlanar;
       
   526     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   527 
       
   528     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma2;
       
   529     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   530 
       
   531     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma3;
       
   532     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   533 
       
   534     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt601Range1;
       
   535     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma1;
       
   536     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataPlanar;
       
   537     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   538 
       
   539     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma2;
       
   540     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   541 
       
   542     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma3;
       
   543     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   544 
       
   545     yuvFormat.iYuvFormat.iCoefficients  	  = EYuvBt601Range0;
       
   546     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma1;
       
   547     yuvFormat.iYuvFormat.iDataLayout    	  = EYuvDataPlanar;
       
   548     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   549 
       
   550     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma2;
       
   551     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   552 
       
   553     yuvFormat.iYuvFormat.iPattern       	  = EYuv420Chroma3;
       
   554     User::LeaveIfError(iSupportedInputFormats.Append(yuvFormat));
       
   555             
       
   556     CPostProcessorInfo* postProcessorInfo = CPostProcessorInfo::NewL( 
       
   557                 KUidVideoPostProcHwDevice, 
       
   558                 KManufacturer, 
       
   559                 KIdentifier, 
       
   560                 TVersion(1, 0, 0), 
       
   561                 iSupportedInputFormats.Array(),
       
   562                 SupportedPostProcess.Array(), 
       
   563                 SupportedHwAcceleration,   
       
   564                 ETrue,      //Direct Display
       
   565                 SupportedYuvToRgbCapab, 
       
   566                 SupportedRotations, 
       
   567                 SupportedArbitraryScaling,
       
   568                 SupportedScaleFactors.Array(), 
       
   569                 SupportedAntiAliasing);
       
   570                 
       
   571     CleanupStack::PushL(postProcessorInfo);            
       
   572    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:PostProcessorInfoLC() --"), this);
       
   573     return postProcessorInfo;
       
   574 }
       
   575 
       
   576 void CNGAPostProcHwDevice::MmvprcGetPlayRateCapabilitiesL(TVideoPlayRateCapabilities& aCap)
       
   577 {       
       
   578     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcGetPlayRateCapabilitiesL ++"), this);       
       
   579      aCap.iPlayForward = ETrue;       
       
   580      aCap.iPlayBackward = ETrue;       
       
   581      aCap.iStepForward = ETrue;       
       
   582      aCap.iStepBackward = ETrue;       
       
   583     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcGetPlayRateCapabilitiesL --"), this);       
       
   584 }       
       
   585 
       
   586 void CNGAPostProcHwDevice::MmvprcSetPlayRateL(const TInt aRate)
       
   587 {       
       
   588     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcSetPlayRateL ++"), this);       
       
   589      iPlayRate = aRate;
       
   590      if (iPlayRate<0)        
       
   591      {       
       
   592          iKeyFrameMode = ETrue;    
       
   593      }       
       
   594      else        
       
   595      {       
       
   596          iKeyFrameMode = EFalse;   
       
   597          ResetCountingBuffer();       
       
   598      }       
       
   599      //In fast forward go direct to key frame mode if speed >4X =     
       
   600     if (iPlayRate>KDefPlayRate*4)
       
   601      {       
       
   602          if (iFPObserver)        
       
   603          {       
       
   604              iFPObserver->MmvproKeyFrameModeRequest();       
       
   605              iKeyFrameMode=ETrue;       
       
   606          }       
       
   607      }       
       
   608     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcSetPlayRateL=%d --"), this, aRate);       
       
   609 }       
       
   610 
       
   611 TInt CNGAPostProcHwDevice::MmvprcPlayRateL()
       
   612 {       
       
   613    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcPlayRateL= ++"), this);       
       
   614    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcPlayRateL= --"), this);       
       
   615     return iPlayRate;       
       
   616 }       
       
   617 
       
   618 void CNGAPostProcHwDevice::MmvprcStepFrameL(const TInt aStep)
       
   619 {       
       
   620    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcStepFrameL= ++"), this);       
       
   621     iStepFrameCount = aStep;       
       
   622    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcStepFrameL=%d --"), this, aStep);       
       
   623 }       
       
   624 
       
   625 void CNGAPostProcHwDevice::MmvprcSetObserver(MMmfVideoPlayRateObserver& aObserver)
       
   626 {       
       
   627    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcSetObserver ++"), this);       
       
   628     iFPObserver  = &aObserver;
       
   629    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvprcSetObserver --"), this);
       
   630 } 
       
   631 
       
   632 void CNGAPostProcHwDevice::MmvsoSetSecureOutputL(TBool aSecure)
       
   633 {
       
   634 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvsoSetSecureOutputL aSecure = %d++"), this, aSecure);     
       
   635 	TInt err = KErrNone;  
       
   636     if(aSecure)
       
   637 	{
       
   638 		iSurfaceMask = surfaceHints::EAllowInternalOnly;
       
   639 	}
       
   640 	else
       
   641 	{
       
   642 		iSurfaceMask = surfaceHints::EAllowAllExternals;
       
   643 	}
       
   644 	if(!iSurfaceId.IsNull())
       
   645 	{
       
   646 		err = AddHints();
       
   647 		if(err != KErrNone)
       
   648 		{
       
   649 			PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvsoSetSecureOutputL -- leaving err = %d"), this, err);
       
   650 			User::Leave(err);
       
   651 		}
       
   652 	}
       
   653     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvsoSetSecureOutputL --"), this);
       
   654 }
       
   655 
       
   656 void CNGAPostProcHwDevice::MmavsoSetAllowedOutputL(TUint aAllowedOutputMask)
       
   657 {
       
   658    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmavsoSetAllowedOutputL aAllowedOutputMask=%d ++"), this,aAllowedOutputMask);  
       
   659    TInt err = KErrNone;
       
   660    iSurfaceMask = surfaceHints::EAllowInternalOnly;
       
   661     if (aAllowedOutputMask == EVideoAllowAll)
       
   662     {
       
   663         iSurfaceMask = surfaceHints::EAllowAllExternals;
       
   664     }
       
   665     else if (aAllowedOutputMask == EVideoAllowInternalOnly)
       
   666     {
       
   667         iSurfaceMask = surfaceHints::EAllowInternalOnly;
       
   668     }
       
   669     else 
       
   670     {
       
   671         // we hope to find some valid output prefs
       
   672         if (aAllowedOutputMask & EVideoAllowAnalog)
       
   673         {
       
   674             iSurfaceMask |= surfaceHints::EAllowAnalog;
       
   675         }
       
   676         if (aAllowedOutputMask & EVideoAllowMacroVision)
       
   677         {
       
   678             iSurfaceMask |= surfaceHints::EAllowAnalogProtectionRequired;
       
   679         }
       
   680         if (aAllowedOutputMask & EVideoAllowHDMI)
       
   681         {
       
   682             iSurfaceMask |= surfaceHints::EAllowDigital;
       
   683         }
       
   684         if (aAllowedOutputMask & EVideoAllowHdmiHdcpRequested)
       
   685         {
       
   686             iSurfaceMask |= surfaceHints::EAllowDigitalProtectionRequested;
       
   687         }
       
   688         if (aAllowedOutputMask & EVideoAllowHdmiHdcpRequired)
       
   689         {
       
   690             iSurfaceMask |= surfaceHints::EAllowDigitalProtectionRequired;
       
   691         }
       
   692     }
       
   693     
       
   694 	if((!iSurfaceId.IsNull()))
       
   695 	{
       
   696 		err = AddHints();
       
   697 		if(err != KErrNone)
       
   698 		{
       
   699 			PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmavsoSetAllowedOutputL -- leaving err = %d"), this, err);
       
   700 			User::Leave(err);
       
   701 		}
       
   702 	}
       
   703     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmavsoSetAllowedOutputL --"), this);
       
   704 }	
       
   705 
       
   706 void CNGAPostProcHwDevice::SetPostProcessTypesL(TUint32 /*aCombination*/) 
       
   707 { 
       
   708 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetPostProcessTypesL ++"), this);
       
   709 	
       
   710    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetPostProcessTypesL --"), this);
       
   711 }
       
   712 
       
   713 void CNGAPostProcHwDevice::SetInputCropOptionsL(const TRect& /*aRect*/) 
       
   714 { 
       
   715 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputCropOptionsL ++"), this);
       
   716    
       
   717 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetInputCropOptionsL --"), this);
       
   718 }    
       
   719 
       
   720 void CNGAPostProcHwDevice::SetYuvToRgbOptionsL( const TYuvToRgbOptions&  /*aOptions*/, const TYuvFormat& /*aYuvFormat*/, TRgbFormat /*aRgbFormat*/) 
       
   721 { 
       
   722 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetYuvToRgbOptionsL ++"), this);
       
   723 
       
   724 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetYuvToRgbOptionsL --"), this);
       
   725 }
       
   726 
       
   727 void CNGAPostProcHwDevice::SetYuvToRgbOptionsL(const TYuvToRgbOptions& /*aOptions*/)
       
   728 {
       
   729 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetYuvToRgbOptionsL ++"), this);
       
   730 
       
   731 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetYuvToRgbOptionsL --"), this);
       
   732 }
       
   733 
       
   734 void CNGAPostProcHwDevice::SetRotateOptionsL(TRotationType ) 
       
   735 { 
       
   736 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetRotateOptionsL ++"), this);
       
   737     
       
   738 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetRotateOptionsL --"));
       
   739 }
       
   740 
       
   741 void CNGAPostProcHwDevice::SetScaleOptionsL(const TSize& /*aTargetSize*/, TBool /*aAntiAliasFiltering*/) 
       
   742 { 
       
   743 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetScaleOptionsL ++"), this);
       
   744     
       
   745    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetScaleOptionsL --"), this);
       
   746 }
       
   747 
       
   748 void CNGAPostProcHwDevice::SetOutputCropOptionsL(const TRect& /*aRect*/) 
       
   749 { 
       
   750 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetOutputCropOptionsL ++"), this);
       
   751     
       
   752 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetOutputCropOptionsL --"), this);
       
   753 }
       
   754 
       
   755 void CNGAPostProcHwDevice::SetPostProcSpecificOptionsL(const TDesC8& ) 
       
   756 { 
       
   757     //ignore 
       
   758 }
       
   759 
       
   760 void CNGAPostProcHwDevice::CommitL() 
       
   761 { 
       
   762 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:CommitL ++"), this);
       
   763 
       
   764    	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:CommitL --"), this);
       
   765 }
       
   766 
       
   767 void CNGAPostProcHwDevice::Revert() 
       
   768 {
       
   769 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Revert ++"), this);
       
   770     
       
   771 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Revert --"), this);
       
   772 }
       
   773 
       
   774 void CNGAPostProcHwDevice::StartDirectScreenAccessL( const TRect& /*aVideoRect*/, CFbsScreenDevice& /*aScreenDevice*/, const TRegion& /*aClipRegion*/) 
       
   775 { 
       
   776    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:StartDSA ++"), this);
       
   777     
       
   778    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:StartDSA --"), this);
       
   779 }
       
   780 
       
   781 void CNGAPostProcHwDevice::AbortDirectScreenAccess() 
       
   782 { 
       
   783    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AbortDSA ++"), this);
       
   784 
       
   785    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AbortDSA --"), this);
       
   786 }
       
   787 
       
   788 void CNGAPostProcHwDevice::SetScreenClipRegion(const TRegion& /*aRegion*/) 
       
   789 { 
       
   790    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetScreenClipRegion ++"), this);
       
   791     
       
   792     
       
   793    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetScreenClipRegion --"), this);
       
   794 }		    
       
   795 
       
   796 void CNGAPostProcHwDevice::SetPauseOnClipFail(TBool ) 
       
   797 { 
       
   798     //ignore. Post Processor will always behave as aPause==False. 
       
   799 }
       
   800 
       
   801 TBool CNGAPostProcHwDevice::IsPlaying()
       
   802 {
       
   803 	if( iPPState == EPlaying)
       
   804 	{
       
   805     	return ETrue; 
       
   806     }
       
   807     else
       
   808     {
       
   809     	return EFalse;
       
   810     }
       
   811 }
       
   812 
       
   813 void CNGAPostProcHwDevice::Redraw() 
       
   814 { 
       
   815 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Redraw ++"), this);
       
   816 	TInt err = KErrNone;
       
   817 	if(iResourceLost)
       
   818 	{
       
   819 		err = RegisterSurface(iSurfaceId);
       
   820 		if (err != KErrNone)
       
   821 		{
       
   822 		   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvroResourcesLost -- failed to Register Surface %d"), 
       
   823 		   				this, err);
       
   824 		   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
   825 	   		iSurfaceId = TSurfaceId::CreateNullId();
       
   826 			iProxy->MdvppFatalError(this, err);	
       
   827 			return;   				
       
   828 		}
       
   829         err = AddHints();
       
   830         if (err != KErrNone)
       
   831 		{
       
   832 		   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvroResourcesLost -- failed to AddHints %d"), 
       
   833 		   				this, err);
       
   834 			iProxy->MdvppFatalError(this, err);	
       
   835 			return;   				
       
   836 		}
       
   837 		iSessionManager->PostPicture(iSurfaceId, 0, 1, EFalse);
       
   838 	   	PublishSurfaceCreated();
       
   839 	   	iResourceLost = EFalse;
       
   840 	}
       
   841 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Redraw --"), this);
       
   842 }
       
   843 
       
   844 void CNGAPostProcHwDevice::Start() 
       
   845 {  
       
   846 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Start ++"), this);
       
   847 	iPPState = EPlaying;
       
   848 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Start --"), this);
       
   849 }
       
   850 
       
   851 void CNGAPostProcHwDevice::Stop() 
       
   852 { 
       
   853 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Stop ++"), this);
       
   854     iPPState = EStopped;
       
   855 	iPostingTimer->Cancel();
       
   856 	ReleaseInputQ();
       
   857 	
       
   858 	RDebug::Printf("------ Statistics of Post Processor ------");
       
   859     RDebug::Printf("    Pictures Received : %d", iPictureCounters.iTotalPictures);
       
   860     RDebug::Printf("    Pictures Displayed: %d", iPictureCounters.iPicturesDisplayed);
       
   861     RDebug::Printf("    Pictures Skipped  : %d", iPictureCounters.iPicturesSkipped);
       
   862     RDebug::Printf("------------------------------------------");
       
   863     
       
   864 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Stop --"), this);
       
   865 }
       
   866 
       
   867 void CNGAPostProcHwDevice::Pause()
       
   868 {
       
   869 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Pause ++"), this);
       
   870 	iPPState = EPaused;
       
   871     iPostingTimer->Cancel();
       
   872    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Pause --"), this);
       
   873 }
       
   874 
       
   875 void CNGAPostProcHwDevice::Resume()
       
   876 {
       
   877 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Resume ++"), this);
       
   878 	iPPState = EPlaying;
       
   879 	AttemptToPost();
       
   880    	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Resume --"), this);
       
   881 }
       
   882 
       
   883 void CNGAPostProcHwDevice::SetPosition(const TTimeIntervalMicroSeconds& aPlaybackPosition) 
       
   884 {
       
   885    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetPosition ++"), this);
       
   886     
       
   887     if (iPPState == EInitializing)
       
   888     { 
       
   889 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetPosition FAILED: Unexpected state"), this);
       
   890         return;
       
   891     }
       
   892     
       
   893     iCurrentPlaybackPosition = aPlaybackPosition;  
       
   894     
       
   895     ReleaseInputQ();
       
   896 
       
   897    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetPosition --"), this);
       
   898 }
       
   899 
       
   900 void CNGAPostProcHwDevice::FreezePicture(const TTimeIntervalMicroSeconds& ) 
       
   901 { 
       
   902     //TODO:
       
   903 }
       
   904 
       
   905 void CNGAPostProcHwDevice::ReleaseFreeze(const TTimeIntervalMicroSeconds&  ) 
       
   906 { 
       
   907     //TODO:
       
   908 }
       
   909 
       
   910 TTimeIntervalMicroSeconds 
       
   911 CNGAPostProcHwDevice::PlaybackPosition() 
       
   912 { 
       
   913 	if (iPPState == EInitializing)
       
   914     {
       
   915         return TTimeIntervalMicroSeconds(0);
       
   916     }
       
   917     
       
   918     return iCurrentPlaybackPosition; 
       
   919 }
       
   920 
       
   921 TUint CNGAPostProcHwDevice::PictureBufferBytes() 
       
   922 {   //TODO 
       
   923     return 0; 
       
   924 }
       
   925 
       
   926 void CNGAPostProcHwDevice::GetPictureCounters( CMMFDevVideoPlay::TPictureCounters&  aCounters) 
       
   927 { 
       
   928 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:GetPictureCounters ++"), this);
       
   929 	
       
   930 	if (iPPState == EInitializing)
       
   931 		return;
       
   932 	aCounters = iPictureCounters;
       
   933 	
       
   934    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:GetPictureCounters --"), this);
       
   935 }
       
   936 
       
   937 void CNGAPostProcHwDevice::SetComplexityLevel(TUint ) 
       
   938 { 
       
   939     //not required 
       
   940 }
       
   941 
       
   942 TUint CNGAPostProcHwDevice::NumComplexityLevels() 
       
   943 { 
       
   944     //not required 
       
   945     return 1; 
       
   946 }
       
   947 
       
   948 void CNGAPostProcHwDevice::GetComplexityLevelInfo(TUint , CMMFDevVideoPlay::TComplexityLevelInfo& ) 
       
   949 { 
       
   950     //not required 
       
   951 }
       
   952 
       
   953 void CNGAPostProcHwDevice::ReturnPicture(TVideoPicture* ) 
       
   954 { 
       
   955 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReturnPicture +-"), this);
       
   956     //not required for direct rendering 
       
   957 }
       
   958 
       
   959 void CNGAPostProcHwDevice::InputEnd() 
       
   960 { 
       
   961    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:InputEnd ++"), this);
       
   962     
       
   963 	if (iPPState!=EPlaying && iPPState!=EPaused)
       
   964     {
       
   965 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:InputEnd FAILED: Unexpected state"), this);
       
   966         return;
       
   967 	}
       
   968     iIsInputEnded = ETrue;
       
   969     
       
   970   if( (iProcessQ.Count() == 1) && (iInputQ.Count() == 0))
       
   971 	{
       
   972 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:InputEnd() Stream end"), this);
       
   973 		iProxy->MdvppStreamEnd();
       
   974 	}
       
   975     
       
   976    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:InputEnd --"), this);
       
   977 }
       
   978 
       
   979 TAny* CNGAPostProcHwDevice::CustomInterface(TUid aInterface)
       
   980 {
       
   981    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:CustomInterface UID = %d ++"), this, aInterface.iUid);
       
   982 	
       
   983 	if (aInterface == KUidMmfVideoBufferManagement)
       
   984     {
       
   985       return (MMmfVideoBufferManagement *)this;
       
   986     }
       
   987 	if (aInterface == KUidMMFVideoSurfaceSupport)
       
   988 	{
       
   989       return (MMMFVideoSurfaceSupport *)this;
       
   990     }
       
   991 	if (aInterface == KUidMMFVideoSurfaceHandleControl)
       
   992 	{
       
   993     	return (MMmfVideoSurfaceHandleControl *)this;
       
   994   }    
       
   995     if (aInterface == KUidMmfVideoPlayRateControl)
       
   996     {
       
   997       return (MMmfVideoPlayRateControl *)this;
       
   998     } 
       
   999     if (aInterface == KMmfVideoAdvancedSecureOutputUid)
       
  1000     {
       
  1001       return (MMmfAdvancedVideoSecureOutput *)this;
       
  1002     }
       
  1003     if (aInterface == KUidMmfVideoResourceManagement)
       
  1004     {
       
  1005       return (MMmfVideoResourceObserver *)this;
       
  1006     } 
       
  1007    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:CustomInterface --"), this);
       
  1008     return NULL;
       
  1009 }
       
  1010 
       
  1011 void CNGAPostProcHwDevice::BufferAvailable(TInt aBufId, TInt aStatus)
       
  1012 { 
       
  1013    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:CNGAPostProcHwDevice::BufferAvailable aStatus = %d aBufId = %d++"), this, aStatus, aBufId);
       
  1014     TVideoPicture* pic = NULL;
       
  1015     if((aStatus != KErrNone) && (aStatus != KErrOverflow) && (aStatus != KErrNotVisible))
       
  1016 	{
       
  1017 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:BufferAvailable FAILED: aStatus = %d"), this, aStatus);
       
  1018 		iProxy->MdvppFatalError(this, aStatus);
       
  1019 	}
       
  1020 
       
  1021 	if(aStatus == KErrOverflow)
       
  1022 	{
       
  1023 		iOverflowPictureCounter++;
       
  1024 		PicturesSkipped();
       
  1025 	}
       
  1026     	
       
  1027     if (iVBMEnabled)
       
  1028     {
       
  1029 		for(TInt i=0; i < iProcessQ.Count(); i++)
       
  1030 		{
       
  1031 			if(iVBMBufferReferenceQ[aBufId] == iProcessQ[i])
       
  1032 			{
       
  1033 				pic = iProcessQ[i];
       
  1034 				iProcessQ.Remove(i);				
       
  1035 				ReturnPicToDecoder(pic);
       
  1036 				if (iIsColorConversionNeeded)
       
  1037 				{
       
  1038 				    AddPictureToColorConversionQ(pic);
       
  1039 				}
       
  1040 				else
       
  1041 				{
       
  1042 				    AddPictureToVBMQ(pic);
       
  1043 				}
       
  1044 				break;
       
  1045 			}
       
  1046 		} 
       
  1047 	}
       
  1048 	else if (iUsingExternalSurface)
       
  1049 	{
       
  1050 	    for(TInt i=0; i < iProcessQ.Count(); i++)
       
  1051 		{
       
  1052 			if (aBufId == GetExternalBufferID(iProcessQ[i]))
       
  1053 			{
       
  1054 				pic = iProcessQ[i];
       
  1055 				iProcessQ.Remove(i);
       
  1056 				ReturnPicToDecoder(pic);
       
  1057 				break;
       
  1058 			}
       
  1059 		} 
       
  1060 	}
       
  1061 	
       
  1062 	if(aStatus == KErrNone)
       
  1063 	{
       
  1064 		if (!iKeyFrameMode && iPlayRate>KDefPlayRate)     
       
  1065 		{   
       
  1066 		 	if (iSkippedFramesCountingBuffer[iCurrentPosInFramesCountingBuffer]==1)        
       
  1067 		 	{       
       
  1068 		 		iSkippedFramesCountingBuffer[iCurrentPosInFramesCountingBuffer] = 0;       
       
  1069 		 		iSkippedFramesInLast64Frames--;       
       
  1070 		 	}       
       
  1071 		 	iCurrentPosInFramesCountingBuffer = ++iCurrentPosInFramesCountingBuffer%64;       
       
  1072 		} 
       
  1073 		iPictureCounters.iPicturesDisplayed++;
       
  1074 		if (iStepFrameCount != 0)
       
  1075         {       
       
  1076         	iStepFrameCount > 0 ? iStepFrameCount-- : iStepFrameCount++;		            	      
       
  1077             if (iStepFrameCount==0 && iFPObserver)        
       
  1078             {       
       
  1079             	iFPObserver->MmvproStepFrameComplete(pic->iTimestamp);       
       
  1080             }       
       
  1081         }
       
  1082 	}
       
  1083 	
       
  1084 	if(iPPState == EPlaying)
       
  1085 	{
       
  1086 		AttemptToPost();
       
  1087 	}
       
  1088 
       
  1089 	if( iIsInputEnded && (iProcessQ.Count() == 1)  && (iInputQ.Count() == 0))
       
  1090 	{
       
  1091 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:BufferAvailable() Stream end"), this);
       
  1092 		iProxy->MdvppStreamEnd();
       
  1093 	}
       
  1094 	
       
  1095    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:CNGAPostProcHwDevice::BufferAvailable --"), this);
       
  1096 }
       
  1097 
       
  1098 //=== MMmfVideoBufferManagement ===
       
  1099 void CNGAPostProcHwDevice::MmvbmSetObserver(MMmfVideoBufferManagementObserver* aObserver)
       
  1100 {
       
  1101    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetObserver() ++"), this);
       
  1102 
       
  1103     if (iPPState != EInitializing)
       
  1104     {
       
  1105 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetObserver FAILED: Unexpected state"), this);
       
  1106         iProxy->MdvppFatalError(this, KErrNotReady);
       
  1107 	}
       
  1108 
       
  1109     iVBMObserver  = aObserver;
       
  1110 
       
  1111    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetObserver() --"), this);
       
  1112 }
       
  1113     
       
  1114 
       
  1115 void CNGAPostProcHwDevice::MmvbmEnable(TBool aEnable)
       
  1116 {
       
  1117     if (iPPState != EInitializing)
       
  1118     {
       
  1119 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmEnable FAILED: Unexpected state"), this);
       
  1120         iProxy->MdvppFatalError(this, KErrNotReady);
       
  1121 	}
       
  1122 
       
  1123     iVBMEnabled = aEnable;
       
  1124 }
       
  1125     
       
  1126 void CNGAPostProcHwDevice::MmvbmSetBufferOptionsL(const TBufferOptions& aOptions)
       
  1127 {
       
  1128    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetBufferOptionsL ++"), this);
       
  1129 
       
  1130     if (iPPState != EInitializing)
       
  1131     {
       
  1132 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetBufferOptionsL FAILED: Unexpected state"), this);
       
  1133         User::Leave(KErrNotReady);
       
  1134 	}
       
  1135 
       
  1136     if (aOptions.iNumInputBuffers > KMaxVBMBuffers 
       
  1137         || aOptions.iNumInputBuffers <= 1)          //at least two buffers
       
  1138     {
       
  1139 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetBufferOptionsL FAILED: Input buffer count limit"), this);
       
  1140         User::Leave(KErrNotSupported);
       
  1141 	}
       
  1142 
       
  1143     if (aOptions.iNumInputBuffers == 0 
       
  1144         || aOptions.iBufferSize.iWidth <= KMinVBMInputWidth 
       
  1145         || aOptions.iBufferSize.iHeight <= KMinVBMInputHeight  
       
  1146         || aOptions.iBufferSize.iWidth > KMaxVBMInputWidth 
       
  1147         || aOptions.iBufferSize.iHeight > KMaxVBMInputHeight)
       
  1148     {
       
  1149 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetBufferOptionsL FAILED: Unexpected buffer size"), this);
       
  1150         User::Leave(KErrArgument);
       
  1151 	}
       
  1152 
       
  1153     iVBMBufferOptions.iNumInputBuffers  = aOptions.iNumInputBuffers;
       
  1154     iVBMBufferOptions.iBufferSize       = aOptions.iBufferSize;
       
  1155 
       
  1156    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmSetBufferOptionsL --"), this);
       
  1157 }
       
  1158 
       
  1159 void CNGAPostProcHwDevice::MmvbmGetBufferOptions(TBufferOptions& aOptions)
       
  1160 {
       
  1161     if (iPPState == EInitializing)
       
  1162     {
       
  1163     	aOptions = iVBMBufferOptions;
       
  1164     }
       
  1165     else
       
  1166     {
       
  1167 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferOptions FAILED: Unexpected state"), this);
       
  1168         iProxy->MdvppFatalError(this, KErrNotReady);
       
  1169 	}
       
  1170 }
       
  1171     
       
  1172 TVideoPicture* CNGAPostProcHwDevice::MmvbmGetBufferL(const TSize& aSize)
       
  1173     {
       
  1174    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferL() ++"), this);
       
  1175     
       
  1176     TInt err = KErrNone;
       
  1177     TVideoPicture* lPic = NULL;
       
  1178 
       
  1179     if (iPPState == EInitializing)
       
  1180     {
       
  1181 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferL() FAILED: Unexpected state"), this);
       
  1182         User::Leave(KErrNotReady);
       
  1183 	  }
       
  1184 
       
  1185     if (aSize.iWidth <= KMinVBMInputWidth 
       
  1186         || aSize.iHeight <= KMinVBMInputHeight
       
  1187         || aSize.iWidth > iVBMBufferOptions.iBufferSize.iWidth 
       
  1188         || aSize.iHeight > iVBMBufferOptions.iBufferSize.iHeight)
       
  1189   	{
       
  1190 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferL() FAILED: Unexpected buffer size w=%d h=%d "), this,aSize.iWidth,aSize.iHeight );
       
  1191         User::Leave(KErrNotSupported);
       
  1192 		}
       
  1193 		
       
  1194 		if(iVBMBufferReferenceQ.Count() == 0)
       
  1195 		{
       
  1196 				err = SetupSurface(aSize);
       
  1197 				if(err)
       
  1198 				{
       
  1199 						PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferL() Surface Setup Failed %d"), this, err);
       
  1200         		return NULL;
       
  1201 				}
       
  1202 		}
       
  1203 		
       
  1204     if(!iVBMBufferQ.Count())
       
  1205     {
       
  1206        PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferL() WARNING: Queue buffer count zero"), this);
       
  1207         return NULL;
       
  1208     }
       
  1209 
       
  1210     lPic    = iVBMBufferQ[0];
       
  1211     iVBMBufferQ.Remove(0);
       
  1212 
       
  1213    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmGetBufferL() -- %d"), this, lPic);
       
  1214     return lPic;
       
  1215 }
       
  1216     
       
  1217 void CNGAPostProcHwDevice::MmvbmReleaseBuffer(TVideoPicture* aBuffer)
       
  1218 {
       
  1219    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmReleaseBuffer() ++"), this);
       
  1220 
       
  1221     if(!aBuffer)
       
  1222     {
       
  1223 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmReleaseBuffer() FAILED: Invalid buffer ptr"), this);
       
  1224     	iProxy->MdvppFatalError(this, KErrArgument);
       
  1225 	}
       
  1226 
       
  1227     TInt err = iVBMBufferQ.Append(aBuffer);
       
  1228     if (err)
       
  1229     {
       
  1230 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmReleaseBuffer() FAILED: Failed to append"), this);
       
  1231 		iProxy->MdvppFatalError(this, err);
       
  1232 	}
       
  1233 
       
  1234    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvbmReleaseBuffer() --"), this);
       
  1235 }
       
  1236 
       
  1237 //=== MMMFVideoSurfaceSupport ===
       
  1238 
       
  1239 void CNGAPostProcHwDevice::MmvssUseSurfaces()
       
  1240 {
       
  1241 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssUseSurfaces() ++"), this);
       
  1242 	// do nothing
       
  1243 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssUseSurfaces() --"), this);
       
  1244 }
       
  1245 
       
  1246 void CNGAPostProcHwDevice::MmvssSetObserver(MMMFVideoSurfaceObserver& aObserver)
       
  1247 {
       
  1248 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssSetObserver() ++"), this);
       
  1249 	iVideoSurfaceObserver = &aObserver;
       
  1250 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssSetObserver() --"), this);
       
  1251 }
       
  1252 
       
  1253 void CNGAPostProcHwDevice::MmvssGetSurfaceParametersL(TSurfaceId& aSurfaceId, 
       
  1254 						TRect& aCropRect, TVideoAspectRatio& aPixelAspectRatio)
       
  1255 {
       
  1256 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssGetSurfaceParametersL() ++"), this);
       
  1257 
       
  1258 	iSurfaceHandler->SurfaceInfo(iSurfaceId, iInfo);
       
  1259 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssGetSurfaceParametersL() \
       
  1260 		surfaceWidth = %d surfaceHeight = %d SurfaceId = 0x%x --"), 
       
  1261 		this, iInfo().iSize.iWidth, iInfo().iSize.iHeight, iSurfaceId);
       
  1262 	aSurfaceId = iSurfaceId;
       
  1263 		aCropRect = TRect(0, 0, iInfo().iSize.iWidth, iInfo().iSize.iHeight);
       
  1264 		if((iPicSize.iWidth > 0) && (iPicSize.iHeight > 0))
       
  1265 		{
       
  1266 			aCropRect.Intersection( iPicSize);
       
  1267 		}
       
  1268 	aPixelAspectRatio = TVideoAspectRatio(1,1);
       
  1269 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssGetSurfaceParametersL()--  \
       
  1270 		cropRectWidth = %d cropRectHeight = %d --"), this, aCropRect.Width(), aCropRect.Height());
       
  1271 
       
  1272 }
       
  1273 
       
  1274 void CNGAPostProcHwDevice::MmvssSurfaceRemovedL(const TSurfaceId& aSurfaceId)
       
  1275 {
       
  1276 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssSurfaceRemovedL()++"), this);
       
  1277 	if(iSurfaceId == aSurfaceId && !aSurfaceId.IsNull())
       
  1278 	{
       
  1279 		if(iInfo().iBuffers > 1) // No need to return temprory redraw surface buffer to decoder
       
  1280 		{
       
  1281 			ReleaseProcessQ();
       
  1282 		}
       
  1283 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssSurfaceRemovedL(): UnregisterSurface ID = 0x%x"), this, aSurfaceId );
       
  1284 		iWsSession.UnregisterSurface(0, iSurfaceId);
       
  1285 		iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1286 		iSurfaceId = TSurfaceId::CreateNullId();
       
  1287 	}
       
  1288 		
       
  1289 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvssSurfaceRemovedL() --"), this);
       
  1290 }
       
  1291 
       
  1292 // === MMmfVideoPropertiesObserver ===
       
  1293     
       
  1294 void CNGAPostProcHwDevice::MmvpoUpdateVideoProperties(const TYuvFormat& aYuvFormat, const TSize& aPictureSize)
       
  1295 {
       
  1296 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvpoUpdateVideoProperties ++"), this);
       
  1297 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvpoUpdateVideoProperties PAR \
       
  1298 		iAspectRatioNum = %d, iAspectRatioDenom = %d"), this,
       
  1299 					 aYuvFormat.iAspectRatioNum,aYuvFormat.iAspectRatioDenom);
       
  1300 					 iPicSize = aPictureSize;
       
  1301 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvpoUpdateVideoProperties Picture Size \
       
  1302 		iWidth = %d, iHeight = %d, iSurfaceCreatedEventPublished = %d"), 
       
  1303 		this, iPicSize.iWidth,iPicSize.iHeight, iSurfaceCreatedEventPublished?1:0);
       
  1304 			 
       
  1305 	if(iVPObserver)
       
  1306 	{
       
  1307 		iVPObserver->MmvpoUpdateVideoProperties(aYuvFormat, aPictureSize);
       
  1308 	} 
       
  1309 	if(iVideoSurfaceObserver && iSurfaceCreatedEventPublished)
       
  1310 	{
       
  1311     	iVideoSurfaceObserver->MmvsoSurfaceParametersChanged(); 
       
  1312     }
       
  1313 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvpoUpdateVideoProperties --"), this);
       
  1314 }
       
  1315 
       
  1316 // === MMmfVideoResourceObserver ===
       
  1317 
       
  1318 void CNGAPostProcHwDevice::MmvroResourcesLost(TUid )
       
  1319 {
       
  1320     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvroResourcesLost ++"), this);
       
  1321     if(!iRedrawSurfaceId.IsNull())
       
  1322 	{
       
  1323 		iResourceLost = ETrue;
       
  1324 		Pause();
       
  1325 		ReleaseInputQ();
       
  1326 		iSessionManager->CancelUpdate();
       
  1327 		ReleaseProcessQ();
       
  1328 		iWsSession.UnregisterSurface(0, iRedrawSurfaceId);
       
  1329 		iSurfaceHandler->DestroySurface(iRedrawSurfaceId);
       
  1330 		iRedrawSurfaceId = TSurfaceId::CreateNullId();
       
  1331 	}
       
  1332 	else
       
  1333 	{
       
  1334 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvroResourcesLost ERROR iRedrawSurfaceId NULL"), this);
       
  1335 	}
       
  1336     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvroResourcesLost --"), this);
       
  1337 }
       
  1338 
       
  1339 // === MMmfVideoPropertiesNotifier ===
       
  1340     
       
  1341 void CNGAPostProcHwDevice::MmvpnSetObserver(MMmfVideoPropertiesObserver* aObserver)
       
  1342 {
       
  1343 	PP_DEBUG(_L("ppHwDev[%x]::MmvpnSetObserver ++"), this);
       
  1344 	iVPObserver = aObserver;
       
  1345 	PP_DEBUG(_L("ppHwDev[%x]::MmvpnSetObserver --"), this);
       
  1346 }
       
  1347 
       
  1348 void CNGAPostProcHwDevice::MmvroResourcesRestored(TUid )
       
  1349 {
       
  1350    	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvroResourcesRestored ++"), this);
       
  1351 	iFirstPictureUpdated = EFalse;
       
  1352 	iResourceLost = EFalse;
       
  1353    	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvroResourcesRestored state=%d --"), 
       
  1354    			this, iPPState);
       
  1355 }
       
  1356 
       
  1357 void CNGAPostProcHwDevice::MmvshcSetSurfaceHandle(const TSurfaceId &aSurfaceID)
       
  1358 {
       
  1359     
       
  1360     SetupExternalSurface(aSurfaceID);
       
  1361     
       
  1362 }
       
  1363 
       
  1364 void CNGAPostProcHwDevice::MmvshcRedrawBufferToSurface(TPtrC8& aRedrawBuffer)
       
  1365 {
       
  1366     PP_DEBUG(_L("CNGAPostProcHwDevice::MmvshcRedrawBufferToSurface ++"), this);
       
  1367 	
       
  1368 	TSize 			surfaceSize; 
       
  1369     TUint8*         lPtr;
       
  1370     TInt 			offset;
       
  1371     if(iPicSize.iWidth > iPicSize.iHeight)
       
  1372     {
       
  1373     	surfaceSize.iWidth = iPicSize.iWidth;
       
  1374     	surfaceSize.iHeight = iPicSize.iWidth;
       
  1375     }
       
  1376     else
       
  1377     {
       
  1378     	surfaceSize.iWidth = iPicSize.iHeight;
       
  1379     	surfaceSize.iHeight = iPicSize.iHeight;
       
  1380     }
       
  1381 
       
  1382    	TInt err = KErrNone;
       
  1383 	SetSurfaceAttributes(surfaceSize, 1); 
       
  1384 	
       
  1385 	iRedrawSurfaceId = iSurfaceId;
       
  1386 	
       
  1387   	err = iSurfaceHandler->CreateSurface(iAttributes, iSurfaceId);
       
  1388   	if (err != KErrNone)
       
  1389 	{
       
  1390 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvshcRedrawBufferToSurface -- failed to create Surface %d"), 
       
  1391 	   				this, err);
       
  1392 		iProxy->MdvppFatalError(this, err);	   				
       
  1393 	    return;
       
  1394 	}
       
  1395 	err = iSurfaceHandler->MapSurface(iSurfaceId, iChunk);
       
  1396 	if (err != KErrNone)
       
  1397 	{
       
  1398 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvshcRedrawBufferToSurface -- failed to map Surface %d"), 
       
  1399 	   				this, err);
       
  1400 	   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1401 	   	iSurfaceId = TSurfaceId::CreateNullId();
       
  1402 		iProxy->MdvppFatalError(this, err);	   				
       
  1403 	    return;
       
  1404 	}
       
  1405 	err = iSurfaceHandler->SurfaceInfo(iSurfaceId, iInfo);
       
  1406 	if (err != KErrNone)
       
  1407 	{
       
  1408 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvshcRedrawBufferToSurface -- failed to get Surface info %d"), 
       
  1409 	   				this, err);
       
  1410 	   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1411 	   	iSurfaceId = TSurfaceId::CreateNullId();
       
  1412 		iProxy->MdvppFatalError(this, err);	   				
       
  1413 	    return;
       
  1414 	}
       
  1415 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:MmvshcRedrawBufferToSurface() \
       
  1416 		surfaceWidth = %d surfaceHeight = %d --"), this, iInfo().iSize.iWidth, iInfo().iSize.iHeight);
       
  1417 
       
  1418     if((err = iSurfaceHandler->GetBufferOffset(iSurfaceId, 0, offset)) != KErrNone)
       
  1419     {
       
  1420     	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvshcRedrawBufferToSurface offset query failed %d"), this, err);
       
  1421     	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1422 	   	iSurfaceId = TSurfaceId::CreateNullId();
       
  1423     	iProxy->MdvppFatalError(this, err);
       
  1424     	return;
       
  1425     }
       
  1426     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvshcRedrawBufferToSurface offset = %d aRedrawBuffer.Size()= %d  --"), this, offset, aRedrawBuffer.Size());
       
  1427     
       
  1428 	lPtr = reinterpret_cast<TUint8*>(iChunk.Base() + offset);
       
  1429 	memcpy((TAny *)lPtr, (TAny *)aRedrawBuffer.Ptr(), aRedrawBuffer.Size());
       
  1430 	
       
  1431     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::MmvshcRedrawBufferToSurface error = %d --"), this, err);
       
  1432 }
       
  1433 
       
  1434 TInt CNGAPostProcHwDevice::SetupExternalSurface(const TSurfaceId &aSurfaceID)
       
  1435 {
       
  1436     TInt err = KErrNone;
       
  1437     
       
  1438     if(!iSurfaceId.IsNull())
       
  1439     {
       
  1440     	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface Cleaning ReDraw Surface"), this);
       
  1441 	    iWsSession.UnregisterSurface(0, iSurfaceId);
       
  1442 		iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1443 	}
       
  1444     
       
  1445     iSurfaceId            = aSurfaceID;
       
  1446     iUsingExternalSurface = ETrue;
       
  1447     
       
  1448     
       
  1449     // Create the surface handler if it doesn't exist.
       
  1450     if (!iSurfaceHandler)
       
  1451     {
       
  1452         TRAP(err, iSurfaceHandler = CNGAPostProcSurfaceHandler::NewL());
       
  1453         if (err != KErrNone)
       
  1454         {
       
  1455            PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface -- failed to create SurfaceHandler."), this);
       
  1456             return err;
       
  1457         }
       
  1458     }
       
  1459     
       
  1460     err = iSurfaceHandler->OpenSurface(iSurfaceId);
       
  1461     if (err != KErrNone)
       
  1462 	{
       
  1463 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface -- failed OpenSurface %d"), 
       
  1464 	   				this, err);
       
  1465 	    return err;
       
  1466 	}
       
  1467 	err = RegisterSurface(iSurfaceId);
       
  1468 	if (err != KErrNone)
       
  1469 	{
       
  1470 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface -- failed RegisterSurface %d"), 
       
  1471 	   				this, err);
       
  1472 	   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1473 	   	iSurfaceId = TSurfaceId::CreateNullId();
       
  1474 	    return err;
       
  1475 	}
       
  1476 	err = AddHints();
       
  1477 	if (err != KErrNone)
       
  1478 	{
       
  1479 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface -- failed to AddHints %d"), 
       
  1480 	   				this, err);
       
  1481 	    return err;
       
  1482 	}
       
  1483 	err = iSurfaceHandler->SurfaceInfo(iSurfaceId, iInfo);
       
  1484 	if (err != KErrNone)
       
  1485 	{
       
  1486 	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface -- failed to get Surface info %d"), 
       
  1487 	   				this, err);
       
  1488 	    return err;
       
  1489 	}		
       
  1490 	  
       
  1491   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetupExternalSurface err=%d"), this, err);
       
  1492    return err;
       
  1493 }
       
  1494 
       
  1495 //=== Internal ===
       
  1496 TVideoPicture* CNGAPostProcHwDevice::CreateBuffersL(TInt aBufId)
       
  1497 {
       
  1498 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::CreateBuffersL ++"), this);
       
  1499 	
       
  1500 	TVideoPicture*          lVideoPicture = NULL;
       
  1501     TUint8*                 lPtr;
       
  1502     TPtr8*                  lTemp;
       
  1503     TInt 					offset;
       
  1504     
       
  1505 	lVideoPicture = new (ELeave) TVideoPicture;
       
  1506     CleanupStack::PushL(lVideoPicture);
       
  1507     if(TInt err = iSurfaceHandler->GetBufferOffset(iSurfaceId, aBufId, offset) != KErrNone)
       
  1508     {
       
  1509     	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::CreateBuffersL offset query failed %d"), this, err);
       
  1510     }
       
  1511     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::CreateBuffersL offset = %d id =%d --"), this, offset, aBufId);
       
  1512     
       
  1513 	lPtr = reinterpret_cast<TUint8*>(iChunk.Base() + offset);
       
  1514 
       
  1515     lTemp = new (ELeave) TPtr8(lPtr, 0, (iVideoFrameBufSize ));
       
  1516     CleanupStack::PushL(lTemp);
       
  1517 
       
  1518     lVideoPicture->iData.iRawData   = lTemp;
       
  1519     lVideoPicture->iHeader = NULL ;
       
  1520     lVideoPicture->iLayerBitRates = NULL ;
       
  1521     
       
  1522     CleanupStack::Pop(2);
       
  1523     
       
  1524    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::CreateVBMBuffersL --"), this);
       
  1525     return lVideoPicture;
       
  1526 }
       
  1527 
       
  1528 void CNGAPostProcHwDevice::CreateVBMBuffersL()
       
  1529 {
       
  1530 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::CreateVBMBuffersL ++"), this);
       
  1531     
       
  1532     TInt err = KErrNone;
       
  1533     TVideoPicture* pic = NULL;
       
  1534     iVBMBufferReferenceQ.Reset();
       
  1535     iVBMBufferQ.Reset();
       
  1536     iColorConversionQ.Reset();
       
  1537 
       
  1538     for(TInt i = 0; i < iVBMBufferOptions.iNumInputBuffers; i++)
       
  1539     {
       
  1540         TRAP(err, pic = CreateBuffersL(i));
       
  1541 	    	if (err != KErrNone)
       
  1542 	    	{
       
  1543 	    	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to create VBM buffer %d"), this, err);
       
  1544 	    	    User::Leave(err);
       
  1545 	    	}
       
  1546 
       
  1547         // This will hold the references which will be used in destructor
       
  1548         User::LeaveIfError(iVBMBufferReferenceQ.Append(pic));
       
  1549         User::LeaveIfError(iVBMBufferQ.Append(pic));
       
  1550     }
       
  1551     if(iIsColorConversionNeeded)
       
  1552     {
       
  1553 		    for(TInt i = iVBMBufferOptions.iNumInputBuffers ; 
       
  1554 		    				 i < (iVBMBufferOptions.iNumInputBuffers + KColorConversionBuffers ); i++)
       
  1555 		    {
       
  1556 		        TRAP(err, pic = CreateBuffersL(i));
       
  1557 			    	if (err != KErrNone)
       
  1558 			    	{
       
  1559 			    	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to create VBM buffer %d"), this, err);
       
  1560 			    	    User::Leave(err);
       
  1561 			    	}
       
  1562 		
       
  1563 		        // This will hold the references which will be used in destructor
       
  1564 		        User::LeaveIfError(iVBMBufferReferenceQ.Append(pic));
       
  1565 		        User::LeaveIfError(iColorConversionQ.Append(pic));
       
  1566 		    }
       
  1567 		}
       
  1568 	    
       
  1569 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]::CreateVBMBuffersL --"), this);
       
  1570 }
       
  1571 
       
  1572 void CNGAPostProcHwDevice::ReturnPicToDecoder(TVideoPicture* aPic)
       
  1573 {
       
  1574    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReturnPicToDecoder ++"), this);
       
  1575 	if (aPic == NULL)
       
  1576 	{
       
  1577 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseInputPicture FAILED: Invalid pic ptr."), this);
       
  1578 		return;
       
  1579 	}
       
  1580 	
       
  1581    	if (iInputDecoderDevice)
       
  1582     {
       
  1583        PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseInputPicture .. before return picture. 2"), this);
       
  1584         iInputDecoderDevice->ReturnPicture(aPic);
       
  1585     }
       
  1586 	
       
  1587 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReturnPicToDecoder --"), this);
       
  1588 }
       
  1589 
       
  1590 TInt CNGAPostProcHwDevice::AttemptToPost()
       
  1591 {
       
  1592    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AttemptToPost ++ Q:%d"), this, iInputQ.Count());
       
  1593     TInt err = KErrNotReady;
       
  1594     TInt count = iInputQ.Count();
       
  1595     TBool bDone = EFalse;
       
  1596     TVideoPicture* pic = PeekQ();   		
       
  1597   	while(pic && !bDone)
       
  1598   	{
       
  1599 	    if(!IsGceReady())
       
  1600 		{  
       
  1601 			PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AttemptToPost GCE not ready"), this );
       
  1602 			return err; //no need to catch this error
       
  1603 		}
       
  1604 		TInt64 delta = 0;
       
  1605 		TTimeToPost	iTimeToPost = (TTimeToPost)IsTimeToPost(pic, delta);
       
  1606 		switch(iTimeToPost)
       
  1607 		{
       
  1608 			case EDelayIt:
       
  1609 			{
       
  1610 				iPostingTimer->Cancel();
       
  1611 				SetTimer(delta);
       
  1612 				bDone = ETrue;
       
  1613 			}
       
  1614 			break;
       
  1615 			case EPostIt:
       
  1616 			{
       
  1617 				RemoveFromQ(); // remove the pic that was returned by PeekQ				
       
  1618 				if(iIsColorConversionNeeded)
       
  1619 				{
       
  1620 				    TVideoPicture* ccPic;
       
  1621     				ccPic = DoColorConvert(pic); // output will be in ccPic
       
  1622     				pic = ccPic;
       
  1623 			    }
       
  1624 				iProcessQ.Append(pic);
       
  1625                 #ifdef _DUMP_YUV_FRAMES
       
  1626                     captureYuv(pic);
       
  1627                 #endif
       
  1628 				iSessionManager->PostPicture(iSurfaceId, GetID(pic), iInfo().iBuffers, ETrue); 
       
  1629 				iCurrentPlaybackPosition = pic->iTimestamp;						 					
       
  1630 				if(!iFirstPictureUpdated)
       
  1631 				{
       
  1632 					iFirstPictureUpdated = ETrue;
       
  1633 					PublishSurfaceCreated();
       
  1634 				}	
       
  1635 			}	// end of postit
       
  1636 			break;
       
  1637 			case ESkipIt: 
       
  1638 			{
       
  1639 				RemoveFromQ();
       
  1640 				ReleasePicture(pic);
       
  1641 				PicturesSkipped();				
       
  1642 			}
       
  1643 			break;
       
  1644 		} // end of switch
       
  1645 		
       
  1646 		// get the next picture
       
  1647 		pic = PeekQ();	
       
  1648     } // end of while
       
  1649     
       
  1650    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AttemptToPost --"), this);
       
  1651  	return err;
       
  1652 }
       
  1653 
       
  1654 TInt CNGAPostProcHwDevice::IsTimeToPost(TVideoPicture* frame, TInt64& delta)
       
  1655 {
       
  1656    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:IsTimeToPost ++"), this);
       
  1657 
       
  1658     if (!frame)
       
  1659     {
       
  1660 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:IsTimeToPost FAILED: Invalid frame ptr."), this);
       
  1661 		return KErrGeneral;
       
  1662 	}
       
  1663 
       
  1664     TInt resp = EPostIt;
       
  1665     
       
  1666     if (iClockSource)
       
  1667     {
       
  1668         // Frame presentation time
       
  1669         TInt64 uPresTime = frame->iTimestamp.Int64();
       
  1670         // The time to sync with.
       
  1671         TInt64 uSyncTime = iClockSource->Time().Int64();
       
  1672         
       
  1673         delta = uPresTime - uSyncTime;
       
  1674         if (( delta > KRenderAhead ) &&  (iPlayRate > 0))	// Delay condition not checked for 
       
  1675         {													// backward playback
       
  1676         	resp = EDelayIt;  //wait
       
  1677         }
       
  1678         else if ( (delta < (-KMaxRenderDelay) && (iPlayRate > 0))
       
  1679           		||  ((delta > KMaxRenderDelay) && (iPlayRate < 0)))
       
  1680         {
       
  1681             resp = ESkipIt;  //drop
       
  1682         }
       
  1683        PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:IsTimeToPost .. Tfm=%d, Tcs=%d, delta=%d"), this, (TInt)uPresTime, (TInt)uSyncTime, (TInt)delta);
       
  1684     }       
       
  1685    
       
  1686    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:IsTimeToPost -- %d"), this, resp);
       
  1687     return resp;
       
  1688 }
       
  1689 
       
  1690 void CNGAPostProcHwDevice::ReleaseInputQ()
       
  1691 {
       
  1692 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseInputQ ++ Q = %d"), this, iInputQ.Count());
       
  1693     while (iInputQ.Count()>0)
       
  1694     {
       
  1695     	ReleasePicture(iInputQ[0]);
       
  1696         iInputQ.Remove(0);
       
  1697     }
       
  1698    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseInputQ --"), this);
       
  1699 }
       
  1700 
       
  1701 void CNGAPostProcHwDevice::ReleaseProcessQ()
       
  1702 {
       
  1703 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseProcessQ ++ Q = %d"), this, iProcessQ.Count() );
       
  1704 	TVideoPicture* pic = NULL;
       
  1705 	
       
  1706     while (iProcessQ.Count()>0)
       
  1707     {
       
  1708 		pic = iProcessQ[0];
       
  1709 		iProcessQ.Remove(0);
       
  1710 		ReturnPicToDecoder(pic);
       
  1711     }
       
  1712    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseProcessQ --"), this);
       
  1713 }
       
  1714 
       
  1715 void CNGAPostProcHwDevice::ReleasePicture(TVideoPicture *pic)
       
  1716 {
       
  1717    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleasePicture ++"), this);
       
  1718 	if (pic == NULL)
       
  1719 	{
       
  1720 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseInputPicture FAILED: Invalid pic ptr."), this);
       
  1721 		return;
       
  1722 	}
       
  1723 	
       
  1724    	if (iInputDecoderDevice)
       
  1725     {
       
  1726        PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleaseInputPicture .. before return picture. 2"), this);
       
  1727         iInputDecoderDevice->ReturnPicture(pic);
       
  1728     }
       
  1729 	if (iVBMEnabled)
       
  1730     {
       
  1731         iVBMBufferQ.Append(pic);
       
  1732 
       
  1733         if ( !iIsInputEnded && iPPState != EStopped )
       
  1734         {
       
  1735             iVBMObserver->MmvbmoNewBuffers();
       
  1736         }
       
  1737 	}
       
  1738 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ReleasePicture --"), this);
       
  1739 }
       
  1740 
       
  1741 void CNGAPostProcHwDevice::PublishSurfaceCreated()
       
  1742 {
       
  1743 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:PublishSurfaceCreated ++"), this);   
       
  1744 	if(iVideoSurfaceObserver)
       
  1745 	{
       
  1746 		iVideoSurfaceObserver->MmvsoSurfaceCreated();
       
  1747 		iSurfaceCreatedEventPublished = ETrue;
       
  1748 	}
       
  1749     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:PublishSurfaceCreated --"), this);
       
  1750 }
       
  1751 
       
  1752 TInt CNGAPostProcHwDevice::SetupSurface(const TSize& aSize)
       
  1753 {
       
  1754 	TInt err = KErrNone;
       
  1755 	if(iVBMEnabled && iVBMObserver)
       
  1756     {
       
  1757     	SetSurfaceAttributes(aSize, KMaxVBMBuffers);
       
  1758 	    iVBMBufferOptions.iNumInputBuffers    = KMaxVBMBuffers;
       
  1759 	    
       
  1760 	    //Decoder needs the buffer size to fit in yuv422 decoded output.
       
  1761 	    iVBMBufferOptions.iBufferSize = aSize;
       
  1762     	
       
  1763 	  	err = iSurfaceHandler->CreateSurface(iAttributes, iSurfaceId);
       
  1764 	  	if (err != KErrNone)
       
  1765     	{
       
  1766     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to create Surface %d"), this, err);
       
  1767     	    return err;
       
  1768     	}
       
  1769     	err = iSurfaceHandler->MapSurface(iSurfaceId, iChunk);
       
  1770     	if (err != KErrNone)
       
  1771     	{
       
  1772     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to map Surface %d"), this, err);
       
  1773     	  	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1774 	   		iSurfaceId = TSurfaceId::CreateNullId();
       
  1775     	    return err;
       
  1776     	}
       
  1777     	err = iSurfaceHandler->SurfaceInfo(iSurfaceId, iInfo);
       
  1778     	if (err != KErrNone)
       
  1779     	{
       
  1780     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to get Surface info %d"), this, err);
       
  1781     	   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1782 	   		iSurfaceId = TSurfaceId::CreateNullId();
       
  1783     	    return err;
       
  1784     	}
       
  1785     	TRAP(err, CreateVBMBuffersL());
       
  1786     	if (err != KErrNone)
       
  1787     	{
       
  1788     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to create VBM buffer %d"), this, err);
       
  1789     	   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1790 	   		iSurfaceId = TSurfaceId::CreateNullId();
       
  1791     	    return err;
       
  1792     	}
       
  1793     	err = RegisterSurface(iSurfaceId);
       
  1794     	if (err != KErrNone)
       
  1795     	{
       
  1796     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to RegisterSurface %d"), this, err);
       
  1797     	   	iSurfaceHandler->DestroySurface(iSurfaceId);
       
  1798 	   		iSurfaceId = TSurfaceId::CreateNullId();
       
  1799     	    return err;
       
  1800     	}
       
  1801    	    err = AddHints();
       
  1802    	    if (err != KErrNone)
       
  1803     	{
       
  1804     	   PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:Initialize -- failed to AddHints %d"), this, err);
       
  1805     	    return err;
       
  1806     	}
       
  1807     }
       
  1808     return err;
       
  1809 } 
       
  1810 
       
  1811 void CNGAPostProcHwDevice::SetSurfaceAttributes(const TSize& aSize, TInt aNumBuf)
       
  1812 {
       
  1813 	iAttributes().iSize                   = aSize; // size of the video frame
       
  1814     iAttributes().iBuffers            	  = aNumBuf;
       
  1815     
       
  1816     /* The stride needs to be calculated for the surface manager to know
       
  1817     how much memory to allocate */
       
  1818     
       
  1819     if(iAttributes().iPixelFormat == EUidPixelFormatYUV_420Planar)
       
  1820     	{
       
  1821     		iAttributes().iStride       = aSize.iWidth * 3/2;
       
  1822     		iVideoFrameBufSize			= aSize.iWidth * aSize.iHeight * 3/2;
       
  1823     	}
       
  1824     	else
       
  1825     	{
       
  1826     		iAttributes().iStride       = aSize.iWidth * 2;
       
  1827     		iVideoFrameBufSize			= aSize.iWidth * aSize.iHeight * 2;
       
  1828     	}
       
  1829     		
       
  1830     if(iIsColorConversionNeeded)
       
  1831     {
       
  1832     	iAttributes().iBuffers            = aNumBuf + KColorConversionBuffers;
       
  1833     }
       
  1834     else
       
  1835     {
       
  1836     	iAttributes().iBuffers            = aNumBuf;	
       
  1837     }
       
  1838     
       
  1839     iAttributes().iOffsetToFirstBuffer    = 0;
       
  1840     iAttributes().iAlignment              = 4;
       
  1841     iAttributes().iContiguous             = ETrue;
       
  1842     iAttributes().iHintCount              = 0;
       
  1843     iAttributes().iMappable               = ETrue;
       
  1844 }
       
  1845 
       
  1846 TInt CNGAPostProcHwDevice::GetID(TVideoPicture *aPicture)
       
  1847 {
       
  1848     if (iUsingExternalSurface)
       
  1849     {
       
  1850 	    return GetExternalBufferID(aPicture);
       
  1851     }
       
  1852     else
       
  1853     {
       
  1854         TUint8* aPtr = (TUint8*) aPicture->iData.iRawData->Ptr();
       
  1855         return( (TInt) ((aPtr - iChunk.Base() - iAttributes().iOffsetToFirstBuffer) / 
       
  1856 			(iVideoFrameBufSize )));
       
  1857     }
       
  1858 }
       
  1859 
       
  1860 TInt CNGAPostProcHwDevice::GetExternalBufferID(TVideoPicture *aPicture)
       
  1861 {
       
  1862     // currently type cast the pointer as buffer ID.
       
  1863     // FIXME once the new data structure is available.
       
  1864     return( (TInt) aPicture->iData.iRawData->Ptr());
       
  1865 }
       
  1866 
       
  1867 TInt CNGAPostProcHwDevice::RegisterSurface(const TSurfaceId& aSurfaceId)
       
  1868 {
       
  1869 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:RegisterSurface(): RegisterSurface ID = 0x%x"), this, aSurfaceId);
       
  1870 	return(iWsSession.RegisterSurface(0, aSurfaceId));
       
  1871 }
       
  1872 
       
  1873 TInt CNGAPostProcHwDevice::IsGceReady()
       
  1874 {
       
  1875     if(iProcessQ.Count() >= KMaxBuffersGceCanHold)
       
  1876     {
       
  1877     		return EFalse;
       
  1878     }
       
  1879     return ETrue;
       
  1880 }
       
  1881 
       
  1882 void CNGAPostProcHwDevice::SetTimer(TInt64 aDelta)
       
  1883 {
       
  1884 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:SetTimer .. aDelta=%d"), this, (TInt)aDelta);
       
  1885 	if(aDelta <= KRenderAhead)
       
  1886 	{
       
  1887 		if(aDelta < 0)
       
  1888 		{
       
  1889 			iPostingTimer->After(aDelta * -1);
       
  1890 		}
       
  1891 		else
       
  1892 		{
       
  1893 			iPostingTimer->After((aDelta - KRenderAhead) * -1);
       
  1894 		}
       
  1895 	}
       
  1896 	else
       
  1897 	{
       
  1898 		iPostingTimer->After(aDelta - KRenderAhead - KPostingOfset);
       
  1899 	}
       
  1900 }
       
  1901 //
       
  1902 // Convert YUV420 to YUV422InterLeaved.
       
  1903 //
       
  1904 TInt CNGAPostProcHwDevice::ConvertPostProcBuffer(TVideoPicture* pSrc, TVideoPicture* pDest)
       
  1905 {
       
  1906     PP_DEBUG(_L("CMdfPostingSurfaceProxy::ConvertPostProcBuffer ++"));
       
  1907     TInt err = KErrNone;
       
  1908     if (!pDest && !pSrc)
       
  1909 	{
       
  1910 		PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:ConvertPostProcBuffer FAILED: Invalid pic pSrc %x pDest %x."), this, pSrc, pDest);
       
  1911 		return KErrArgument;
       
  1912 	}
       
  1913     
       
  1914     // --- Prepare wrappers ---
       
  1915     tBaseVideoFrame tFrame420, tFrame422;
       
  1916     TInt    frameSize = pSrc->iData.iDataSize.iWidth * pSrc->iData.iDataSize.iHeight;
       
  1917 
       
  1918    PP_DEBUG(_L("CMdfPostingSurfaceProxy::ConvertPostProcBuffer .. w=%d, h=%d"), pSrc->iData.iDataSize.iWidth, pSrc->iData.iDataSize.iHeight);
       
  1919 
       
  1920     tFrame420.width = pSrc->iData.iDataSize.iWidth;
       
  1921     tFrame420.height= pSrc->iData.iDataSize.iHeight;
       
  1922     tFrame420.lum   = (TUint8*)pSrc->iData.iRawData->Ptr();
       
  1923     tFrame420.cb    = (TUint8*)tFrame420.lum + frameSize;
       
  1924     tFrame420.cr    = (TUint8*)tFrame420.lum + (frameSize*5)/4;
       
  1925     
       
  1926     tFrame422.width = pSrc->iData.iDataSize.iWidth;
       
  1927     tFrame422.height= pSrc->iData.iDataSize.iHeight;
       
  1928     tFrame422.lum   = (TUint8*)pDest->iData.iRawData->Ptr();
       
  1929     tFrame422.cb    = 0;
       
  1930     tFrame422.cr    = 0;
       
  1931     
       
  1932     // --- Convertion to posting buffer ---
       
  1933     TInt stride     = pSrc->iData.iDataSize.iWidth * 2;
       
  1934     EBufferLayout422 layout = YUV422INT_BE;
       
  1935         
       
  1936     err = gColorConvYUVtoYUV422Int(&tFrame420, &tFrame422, layout, stride);
       
  1937     if(err != KErrNone)
       
  1938     {
       
  1939     		PP_DEBUG(_L("CNGAPostProcHwDevice::ConvertPostProcBuffer .. err= %d."), err);
       
  1940     }
       
  1941 	return err;
       
  1942 }   
       
  1943 
       
  1944 void CNGAPostProcHwDevice::AddPictureToVBMQ(TVideoPicture *pic)
       
  1945 {
       
  1946    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AddPictureToVBMQ ++"), this);
       
  1947     iVBMBufferQ.Append(pic);
       
  1948 
       
  1949     if ( !iIsInputEnded && iPPState != EStopped )
       
  1950     {
       
  1951         iVBMObserver->MmvbmoNewBuffers();
       
  1952     }
       
  1953 	
       
  1954 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AddPictureToVBMQ --"), this);
       
  1955 }
       
  1956 
       
  1957 void CNGAPostProcHwDevice::AddPictureToColorConversionQ(TVideoPicture *pic)
       
  1958 {
       
  1959 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AddPictureToColorConversionQ ++"), this);
       
  1960     iColorConversionQ.Append(pic);
       
  1961    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AddPictureToColorConversionQ --"), this);
       
  1962 }
       
  1963 
       
  1964 #ifdef _DUMP_YUV_FRAMES
       
  1965 void CNGAPostProcHwDevice::captureYuv(TVideoPicture* aPicture)
       
  1966 {
       
  1967 	char buf[128];              
       
  1968 	sprintf(buf, "c:\\fb%d.yuv", count++);          
       
  1969 	FILE *fp = ::fopen(buf, "w");
       
  1970 	TInt size = aPicture->iData.iRawData->Size();
       
  1971 	//{FILE* f1 = fopen(MY_LOG_FILE_NAME, "a+"));if(f1){fprintf(f1, "Size  %d \n"), size );fclose(f1); }}
       
  1972 
       
  1973 	::fwrite(aPicture->iData.iRawData->Ptr(), 1, size, fp);
       
  1974 	::fclose(fp);    	
       
  1975 }
       
  1976 #endif
       
  1977 
       
  1978 void CNGAPostProcHwDevice::ResetCountingBuffer()
       
  1979 {       
       
  1980 	memset(iSkippedFramesCountingBuffer,0,sizeof(iSkippedFramesCountingBuffer));
       
  1981     iSkippedFramesInLast64Frames = 0;       
       
  1982     iCurrentPosInFramesCountingBuffer = 0;       
       
  1983 } 
       
  1984 
       
  1985 void CNGAPostProcHwDevice::PicturesSkipped()
       
  1986 {       
       
  1987 	PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:PicturesSkipped ++"), this);
       
  1988 	iPictureCounters.iPicturesSkipped++;
       
  1989 	if (!iKeyFrameMode && iPlayRate>KDefPlayRate)
       
  1990     {       
       
  1991     	if (iSkippedFramesCountingBuffer[iCurrentPosInFramesCountingBuffer]==0)        
       
  1992         {       
       
  1993         	iSkippedFramesCountingBuffer[iCurrentPosInFramesCountingBuffer] = 1;       
       
  1994             iSkippedFramesInLast64Frames++;       
       
  1995             if (iSkippedFramesInLast64Frames>KMaxAllowedSkipInNFrames && iFPObserver )       
       
  1996             {       
       
  1997             	iFPObserver->MmvproKeyFrameModeRequest();       
       
  1998                 iKeyFrameMode=ETrue;       
       
  1999                 ResetCountingBuffer();       
       
  2000             }       
       
  2001         }       
       
  2002         iCurrentPosInFramesCountingBuffer = ++iCurrentPosInFramesCountingBuffer%64;       
       
  2003     }
       
  2004     PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:PicturesSkipped --"), this);   
       
  2005 }
       
  2006 
       
  2007 TVideoPicture* CNGAPostProcHwDevice::DoColorConvert(TVideoPicture* aPicture)
       
  2008 {
       
  2009     TVideoPicture *pOutPicture  = aPicture;
       
  2010     					    		
       
  2011 	if(iColorConversionQ.Count())
       
  2012     {
       
  2013 	    pOutPicture    = iColorConversionQ[0];
       
  2014 	    iColorConversionQ.Remove(0);
       
  2015 	    ConvertPostProcBuffer(aPicture, pOutPicture);
       
  2016 	    ReleasePicture(aPicture);    	    
       
  2017     }				    
       
  2018     else
       
  2019     {
       
  2020        PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:WritePictureL() FAILED: color conversion"), this);
       
  2021     }
       
  2022 
       
  2023 	return pOutPicture;
       
  2024 }
       
  2025 
       
  2026 TInt CNGAPostProcHwDevice::AddToQ(TVideoPicture* aPicture)
       
  2027 {
       
  2028 	TVideoPicture* pic = aPicture;
       
  2029 	TInt pos = -1;
       
  2030 	if(iInputQ.Count() == 0)
       
  2031 	{
       
  2032 		iInputQ.Append(pic);
       
  2033 	}
       
  2034 	else
       
  2035 	{
       
  2036 		pos = iInputQ.Count()-1;
       
  2037 		for(; pos >= 0; pos--)
       
  2038 		{
       
  2039 			if(pic->iTimestamp.Int64() > iInputQ[pos]->iTimestamp.Int64())
       
  2040 			{
       
  2041 				break;
       
  2042 			}
       
  2043 		} 
       
  2044 		if(iInputQ.Count() == pos+1)
       
  2045 		{
       
  2046 			iInputQ.Append(pic);
       
  2047 		}
       
  2048 		else
       
  2049 		{
       
  2050 			iInputQ.Insert(pic, pos+1);
       
  2051 		}
       
  2052 	}
       
  2053 	return pos+1;
       
  2054 }
       
  2055 
       
  2056 void CNGAPostProcHwDevice::RemoveFromQ()
       
  2057 {
       
  2058 	if(iInputQ.Count())
       
  2059 	{
       
  2060 		if(iPlayRate > 0)
       
  2061 		{
       
  2062 			iInputQ.Remove(0);
       
  2063 		}
       
  2064 		else
       
  2065 		{
       
  2066 			iInputQ.Remove(iInputQ.Count()-1);
       
  2067 		}
       
  2068 	}
       
  2069 }
       
  2070 
       
  2071 TVideoPicture* CNGAPostProcHwDevice::PeekQ()
       
  2072 {	
       
  2073 	TVideoPicture *pic = NULL;
       
  2074 	if(iInputQ.Count())
       
  2075 	{
       
  2076 		if(iPlayRate > 0)
       
  2077 		{			
       
  2078 			pic = iInputQ[0];
       
  2079 		}
       
  2080 		else
       
  2081 		{			
       
  2082 			pic = iInputQ[iInputQ.Count()-1];
       
  2083 		}	
       
  2084 	}
       
  2085 	return pic;
       
  2086 }
       
  2087 
       
  2088 TInt CNGAPostProcHwDevice::AddHints()
       
  2089 {
       
  2090    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AddHints ++"), this);
       
  2091    TInt err = KErrNone;
       
  2092    iHint.Set(iSurfaceKey,iSurfaceMask,ETrue);
       
  2093    err = iSurfaceHandler->AddSurfaceHint(iSurfaceId,iHint);
       
  2094    if(err == KErrAlreadyExists)
       
  2095    {
       
  2096 		err = KErrNone;
       
  2097 		err = iSurfaceHandler->SetSurfaceHint(iSurfaceId,iHint);
       
  2098    }
       
  2099    PP_DEBUG(_L("CNGAPostProcHwDevice[%x]:AddHints. err = %d --"), this,err);
       
  2100    return err;
       
  2101 }
       
  2102 
       
  2103 CNGAPostProcTimer::CNGAPostProcTimer( CNGAPostProcHwDevice& aParent )
       
  2104 :CTimer(EPriorityHigh),iParent(aParent)
       
  2105 {
       
  2106 	CActiveScheduler::Add(this);
       
  2107 }
       
  2108 
       
  2109 CNGAPostProcTimer::~CNGAPostProcTimer()
       
  2110 {
       
  2111 	PP_DEBUG(_L("CNGAPostProcTimer[%x]:~CNGAPostProcTimer ++"), this);
       
  2112 	Cancel();
       
  2113 	PP_DEBUG(_L("CNGAPostProcTimer[%x]:~CNGAPostProcTimer --"), this);
       
  2114 }
       
  2115 
       
  2116 CNGAPostProcTimer* CNGAPostProcTimer::NewL( CNGAPostProcHwDevice& aParent )
       
  2117 {
       
  2118 	CNGAPostProcTimer* self = new (ELeave)CNGAPostProcTimer(aParent);
       
  2119 	CleanupStack::PushL( self );
       
  2120 	self->ConstructL();
       
  2121 	CleanupStack::Pop( self );
       
  2122 	return self;
       
  2123 }
       
  2124 
       
  2125 void CNGAPostProcTimer::ConstructL()
       
  2126 {
       
  2127 	CTimer::ConstructL();
       
  2128 }
       
  2129 
       
  2130 void CNGAPostProcTimer::RunL()
       
  2131 {
       
  2132 	PP_DEBUG(_L("CNGAPostProcTimer[%x]:RunL ++"), this);
       
  2133 	if (iStatus ==KErrCancel)
       
  2134 	{
       
  2135 		PP_DEBUG(_L("CNGAPostProcNotifier[%x]:CNGAPostProcNotifier:RunL State canceled"), this);
       
  2136 		return;
       
  2137 	}
       
  2138 	iParent.AttemptToPost();
       
  2139 	PP_DEBUG(_L("CNGAPostProcTimer[%x]:RunL --"), this);
       
  2140 }
       
  2141