mp3_dec/arimp3decmmfcodec/src/arimp3decmmfcodec.cpp
changeset 0 bb31fbe78861
equal deleted inserted replaced
-1:000000000000 0:bb31fbe78861
       
     1 /*
       
     2 * Copyright (c) 2009 Aricent and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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 * Aricent - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Implementation of member functions of Plugin class (CAriMp3DecMmfCodec).
       
    16 *
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <ecom.h>
       
    21 #include <ImplementationProxy.h>
       
    22 // User includes
       
    23 #include "arimp3decwrapper.h"
       
    24 #include "arimp3decmmfcodec.h"
       
    25 #include "arimp3decmmfcodec_uid.hrh"
       
    26 #include "ariprint.h"
       
    27 
       
    28 //Maximum size of a single input frame in an Mp3 stream
       
    29 const TInt KMinBytesInput = 1440;
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 //  Two-phased constructor.
       
    34 //  Creates an instance of CAriMp3DecMmfCodec.
       
    35 //  Instance is not left on cleanup stack.
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 
       
    39 CMMFCodec* CAriMp3DecMmfCodec::NewL()
       
    40 	{
       
    41 	PRINT_ENTRY;
       
    42 	CAriMp3DecMmfCodec* self = new ( ELeave ) CAriMp3DecMmfCodec();
       
    43 	CleanupStack::PushL( self );
       
    44 	self->ConstructL();
       
    45 	CleanupStack::Pop( self );
       
    46 	PRINT_EXIT;
       
    47 	return ( CMMFCodec* )self;
       
    48 	}
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 //  Destructor;Destroys the decoder instance and any internal buffers
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 
       
    55 CAriMp3DecMmfCodec::~CAriMp3DecMmfCodec()
       
    56 	{
       
    57 	PRINT_ENTRY;
       
    58 	if ( iInternalInputBuffer )
       
    59 		{
       
    60 		delete iInternalInputBuffer;
       
    61 		iInternalInputBuffer = NULL;
       
    62 		}
       
    63 	if ( iInternalOutputBuffer )
       
    64 		{
       
    65 		delete iInternalOutputBuffer;
       
    66 		iInternalOutputBuffer = NULL;
       
    67 		}
       
    68 
       
    69 	if ( iCodec )
       
    70 		{
       
    71 		delete iCodec;
       
    72 		iCodec = NULL;
       
    73 		}
       
    74 	PRINT_EXIT;
       
    75 	}
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 //  From class CMMFCodec.
       
    79 //  The function Sets codec configuration.
       
    80 //  The value used for aConfigType must be KUidMmfCodecAudioSettings
       
    81 //  (defined in include\mmf\plugins\mmfCodecImplementationUIDs.hrh)
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 
       
    85  void CAriMp3DecMmfCodec::ConfigureL( TUid /*aConfigType*/,
       
    86 		const TDesC8& /*aParam*/ )
       
    87 	{
       
    88 	PRINT_ENTRY;
       
    89 	PRINT_EXIT;
       
    90 	}
       
    91 
       
    92  // --------------------------------------------------------------------------
       
    93  //  From class CMMFCodec.
       
    94  //  This function is used to flush out status information when a
       
    95  //  reposition occurs.
       
    96  //  This is used if the codec requires resetting prior to use.
       
    97  // --------------------------------------------------------------------------
       
    98  //
       
    99 
       
   100  void CAriMp3DecMmfCodec::ResetL()
       
   101 	{
       
   102 	PRINT_ENTRY;
       
   103 	iInternalInputBufferResidueLen = 0;
       
   104 	iInternalOutputBufferPos = 0;
       
   105 	iInternalOutputBufferResidueLen = 0;
       
   106 	PRINT_EXIT;
       
   107 	}
       
   108 
       
   109  //---------------------------------------------------------------------------
       
   110  //  From class CMMFCodec.
       
   111  //  This function is used to decode the given source and fill the
       
   112  //  destination buffer with the decode data.
       
   113  //  The buffers can be of any size.  Since the buffers can be of any size
       
   114  //  there is no guarantee that all the source buffer can be processed to
       
   115  //  fill the destination buffer or that the all the source buffer may be
       
   116  //  processed before the destination is full.  Therefore the ProcessL needs
       
   117  //  to return a TCodecProcessResult returing the number of source bytes
       
   118  //  processed and the number of destination bytes processed along with a
       
   119  //  process result code defined thus:
       
   120  //  - EProcessComplete: the codec processed all the source data into the
       
   121  //  sink buffer
       
   122  //  - EProcessIncomplete: the codec filled sink buffer before all the source
       
   123  //   buffer was processed
       
   124  //  - EDstNotFilled: the codec processed the source buffer but the sink
       
   125  //  buffer was not filled
       
   126  //  - EEndOfData: the codec detected the end data - all source data in
       
   127  //  processed but sink may not be full
       
   128  //  - EProcessError: the codec process error condition
       
   129  //
       
   130  //  The ProcessL should start processing the source buffer from the iPosition
       
   131  //  data member of the source data and start filling the destination buffer
       
   132  //   from its iPosition.
       
   133  //---------------------------------------------------------------------------
       
   134  //
       
   135 
       
   136 TCodecProcessResult CAriMp3DecMmfCodec::ProcessL( const CMMFBuffer& aSrc,
       
   137 													CMMFBuffer& aDst )
       
   138 	{
       
   139 	PRINT_ENTRY;
       
   140 	// total decoded bytes added to the dst buffer
       
   141 	TInt totalDstBytesAdded = 0;
       
   142 	// total src bytes added to the internal src buffer
       
   143 	TInt totalSrcBytesCopied = 0;
       
   144 	// temporary variable to use for copying the sorce or destination data
       
   145 	TInt numberOfBytesCopied;
       
   146 	// Flag for finding valid sync
       
   147 	TBool syncFound = EFalse;
       
   148 
       
   149 	/**
       
   150 	* Process the dst buffer, update the dstBufferPos and check
       
   151 	* whether dst buffer is NULL or not.
       
   152 	*/
       
   153 	CMMFDataBuffer* dst = static_cast<CMMFDataBuffer*>( &aDst );
       
   154 
       
   155 	const TInt dstMaxLen = dst->Data().MaxLength();
       
   156 	TUint8* dstPtr = const_cast<TUint8*>( dst->Data().Ptr() );
       
   157 	TInt dstBufferPos = dst->Position();
       
   158 
       
   159 	/**
       
   160 	* Process the src buffer, update srcbuffer length, position and
       
   161 	* flag for last frame. check whether src buffer is NULL or not
       
   162 	* and check src buffer contains any data
       
   163 	*/
       
   164 	const CMMFDataBuffer* src = static_cast<const CMMFDataBuffer*>( &aSrc );
       
   165 
       
   166 	TUint8* srcPtr = const_cast <TUint8*>( src->Data().Ptr() );
       
   167 	TInt srcBufferLen = src->Data().Length();
       
   168 	TInt srcBufferPos = src->Position();
       
   169 	TBool lastFrame = src->LastBuffer();
       
   170 
       
   171 	if ( srcBufferLen == 0 && iInternalInputBufferResidueLen == 0 )
       
   172 		{
       
   173 		PRINT_ERR( "source buffer length is zero" );
       
   174 		User::Leave( KErrArgument );
       
   175 		}
       
   176 
       
   177 	/**
       
   178 	* if any destination bytes from internal destination buffer is not
       
   179 	* given to the dst buffer from the previous call, give it to the
       
   180 	* dst buffer. After this block, it ensures that no bytes are remaining
       
   181 	* in the internal destination buffer.
       
   182 	*/
       
   183 	if ( iInternalOutputBufferResidueLen - iInternalOutputBufferPos > 0 )
       
   184 		{
       
   185 		numberOfBytesCopied = CopyToDstBuffer( dst, totalDstBytesAdded );
       
   186 
       
   187 		if ( iInternalOutputBufferResidueLen - iInternalOutputBufferPos > 0 )
       
   188 			{
       
   189 			PRINT_EXIT;
       
   190 			return Result( TCodecProcessResult::EProcessIncomplete,
       
   191 							totalSrcBytesCopied, totalDstBytesAdded );
       
   192 			}
       
   193 		else
       
   194 			{
       
   195 			if ( ( lastFrame ) && ( srcBufferLen - srcBufferPos == 0 )&&
       
   196 								( iInternalInputBufferResidueLen == 0 ) )
       
   197 				{
       
   198 				iInternalOutputBufferResidueLen = 0;
       
   199 				iInternalInputBufferResidueLen = 0;
       
   200 				iInternalOutputBufferPos = 0;
       
   201 				PRINT_EXIT;
       
   202 				return Result( TCodecProcessResult::EEndOfData,
       
   203 								totalSrcBytesCopied, totalDstBytesAdded );
       
   204 				}
       
   205 			iInternalOutputBufferPos = 0;
       
   206 			iInternalOutputBufferResidueLen = 0;
       
   207 			}
       
   208 		}
       
   209 
       
   210 	/**
       
   211 	* copy the src buffer data into the internal buffer till internal buffer
       
   212 	* holds minimum bytes to process i.e KMinBytesInput. After this block, it
       
   213 	* ensures that internal source buffer holds KMinBytesInput.
       
   214 	* if it is a last frame, treat remaining residual buffer as internal
       
   215 	* buffer.
       
   216 	*/
       
   217 	if ( ( KMinBytesInput - iInternalInputBufferResidueLen > 0 )
       
   218 							&& ( srcBufferLen - srcBufferPos > 0 ) )
       
   219 		{
       
   220 		numberOfBytesCopied = CopyFromSrcBuffer( src, totalSrcBytesCopied );
       
   221 		}
       
   222 
       
   223 	if ( ( KMinBytesInput > iInternalInputBufferResidueLen )
       
   224 														&& ( !lastFrame ) )
       
   225 		{
       
   226 		PRINT_EXIT;
       
   227 		return Result( TCodecProcessResult::EDstNotFilled,
       
   228 						srcBufferLen - srcBufferPos, totalDstBytesAdded );
       
   229 		}
       
   230 
       
   231 	/**
       
   232 	* process the src buffer till destination buffer or source buffer
       
   233 	* or both buffers are exhausted.
       
   234 	*/
       
   235 	do
       
   236 	{
       
   237 		/**
       
   238 		* call seeksync to find the valid fram start offset i.e syncpos.
       
   239 		* update internal buffer position to that offset position if it is
       
   240 		* success.if it is failed then there is no valid frame start in
       
   241 		* the available buffer. Go for new src buffer. all bytes present
       
   242 		* in the internal buffer are discarded.
       
   243 		*/
       
   244 		TInt syncpos;
       
   245 		TInt srcBufferRemainingBytes = srcBufferLen
       
   246 									  - srcBufferPos
       
   247 									  - totalSrcBytesCopied;
       
   248 		TInt dstBufferRemainingBytes = dstMaxLen
       
   249 									   - dstBufferPos
       
   250 									   - totalDstBytesAdded;
       
   251 		TInt internalInputBufferPos = 0;
       
   252 
       
   253 		if ( KErrNone != iCodec->SeekSync(
       
   254 				&iInternalInputBuffer[internalInputBufferPos],
       
   255 				( iInternalInputBufferResidueLen - internalInputBufferPos ),
       
   256 					syncpos ) )
       
   257 			{
       
   258 			TCodecProcessResult result = GetNewData( src, totalSrcBytesCopied,
       
   259 														totalDstBytesAdded );
       
   260 
       
   261 			if ( result.iStatus == TCodecProcessResult::EDstNotFilled ||
       
   262 					 result.iStatus == TCodecProcessResult::EEndOfData )
       
   263 				{
       
   264 				return result;
       
   265 				}
       
   266 			}
       
   267 		else
       
   268 			{
       
   269 			syncFound = ETrue;
       
   270 			internalInputBufferPos += syncpos;
       
   271 			}
       
   272 		/**
       
   273 		* call GetFrameInfo to find whether valid frame is present in the
       
   274 		* availabel buffer.if it is success and framelength is 0 then
       
   275 		* there is no valid frame is present,  discard the present buffer
       
   276 		* till sync position and add new buffer.
       
   277 		*/
       
   278 		if ( syncFound )
       
   279 			{
       
   280 			TInt frameLen;
       
   281 			TMp3FrameInfo frameInfo;
       
   282 			if ( KErrNone != iCodec->GetFrameInfo(
       
   283 				&iInternalInputBuffer[internalInputBufferPos],
       
   284 				( iInternalInputBufferResidueLen - internalInputBufferPos ),
       
   285 				frameLen, frameInfo ) )
       
   286 				{
       
   287 				PRINT_ERR( "Decoder Getframeinfo failed" );
       
   288 				User::Leave( KErrGeneral );
       
   289 				}
       
   290 			if ( frameLen == 0 )
       
   291 				{
       
   292 				TCodecProcessResult result = GetNewData( src,
       
   293 									totalSrcBytesCopied, totalDstBytesAdded );
       
   294 
       
   295 				if ( result.iStatus == TCodecProcessResult::EDstNotFilled ||
       
   296 						 result.iStatus == TCodecProcessResult::EEndOfData )
       
   297 					{
       
   298 					return result;
       
   299 					}
       
   300 				}
       
   301 
       
   302 			/**
       
   303 			* if the buffer has less than framelen then fill the internal
       
   304 			* sorce buffer with KMinBytesInput bytes.
       
   305 			*/
       
   306 			if ( frameLen   > ( iInternalInputBufferResidueLen
       
   307 								- internalInputBufferPos ) )
       
   308 				{
       
   309 				if( lastFrame )
       
   310 					{
       
   311 					iInternalInputBufferResidueLen = 0;
       
   312 					iInternalOutputBufferResidueLen = 0;
       
   313 					iInternalOutputBufferPos = 0;
       
   314 					PRINT_EXIT;
       
   315 					return Result(
       
   316 							TCodecProcessResult::EEndOfData,
       
   317 							totalSrcBytesCopied, totalDstBytesAdded );
       
   318 					}
       
   319 
       
   320 				ShiftData( internalInputBufferPos, 0 );
       
   321 				numberOfBytesCopied = CopyFromSrcBuffer( src,
       
   322 														totalSrcBytesCopied );
       
   323 				internalInputBufferPos = 0;
       
   324 
       
   325 				if ( iInternalInputBufferResidueLen < KMinBytesInput )
       
   326 					{
       
   327 					PRINT_EXIT;
       
   328 					return Result(
       
   329 							TCodecProcessResult::EDstNotFilled,
       
   330 							totalSrcBytesCopied, totalDstBytesAdded );
       
   331 					}
       
   332 				}
       
   333 
       
   334 			/**
       
   335 			* initialize the variables like srcUsed and dstLen accordingly.
       
   336 			* call Decode.
       
   337 			*/
       
   338 
       
   339 			TInt srcUsed = iInternalInputBufferResidueLen
       
   340 							- internalInputBufferPos;
       
   341 			TInt dstLen  = iOutFrameSize;
       
   342 
       
   343 			TInt error = iCodec->Decode(
       
   344 						   &iInternalInputBuffer[internalInputBufferPos],
       
   345 									srcUsed, iInternalOutputBuffer, dstLen );
       
   346 
       
   347 			if ( KErrNone != error )
       
   348 				{
       
   349 				iInternalInputBufferResidueLen = 0;
       
   350 				PRINT_ERR( error );
       
   351 				return Result(
       
   352 						TCodecProcessResult::EProcessError,
       
   353 						totalSrcBytesCopied, totalDstBytesAdded );
       
   354 				}
       
   355 
       
   356 			/**
       
   357 			* Fill Destination Buffer
       
   358 			*/
       
   359 
       
   360 			iInternalOutputBufferResidueLen = dstLen;
       
   361 			numberOfBytesCopied = CopyToDstBuffer( dst, totalDstBytesAdded );
       
   362 			dstBufferRemainingBytes -= numberOfBytesCopied;
       
   363 
       
   364 			/***
       
   365 			* Fill Sorce Buffer
       
   366 			*/
       
   367 
       
   368 			internalInputBufferPos += srcUsed ;
       
   369 			ShiftData( internalInputBufferPos, 0 );
       
   370 			numberOfBytesCopied = CopyFromSrcBuffer( src,
       
   371 													totalSrcBytesCopied );
       
   372 			srcBufferRemainingBytes -= numberOfBytesCopied;
       
   373 			internalInputBufferPos = 0;
       
   374 
       
   375 			/***
       
   376 			* check four conditions if else for src and if else for dst
       
   377 			*/
       
   378 
       
   379 			// src buffer has available bytes to decode
       
   380 			if ( srcBufferRemainingBytes > 0 )
       
   381 				{
       
   382 				if ( dstBufferRemainingBytes == 0 )
       
   383 					{
       
   384 					PRINT_EXIT;
       
   385 					return Result( TCodecProcessResult::EProcessIncomplete,
       
   386 							totalSrcBytesCopied, totalDstBytesAdded );
       
   387 					}
       
   388 				}
       
   389 
       
   390 			else
       
   391 				{
       
   392 				// dst buffer has availabe space for decoded bytes
       
   393 				if ( dstBufferRemainingBytes > 0 )
       
   394 					{
       
   395 					// last frame of the input stream
       
   396 					if ( lastFrame )
       
   397 						{
       
   398 						if ( iInternalInputBufferResidueLen == 0 )
       
   399 							{
       
   400 							PRINT_EXIT;
       
   401 							return Result( TCodecProcessResult::EEndOfData,
       
   402 									totalSrcBytesCopied, totalDstBytesAdded );
       
   403 							}
       
   404 						}
       
   405 					else
       
   406 						{
       
   407 						PRINT_EXIT;
       
   408 						return Result( TCodecProcessResult::EDstNotFilled,
       
   409 								totalSrcBytesCopied, totalDstBytesAdded );
       
   410 						}
       
   411 					}
       
   412 				else
       
   413 					{
       
   414 					/**
       
   415 					 *internal output buffer has decoded bytes which is not
       
   416 					 *given to dst buffer.
       
   417 					 */
       
   418 					if ( iInternalOutputBufferResidueLen
       
   419 										- iInternalOutputBufferPos > 0 )
       
   420 						{
       
   421 						PRINT_EXIT;
       
   422 						return Result(
       
   423 									TCodecProcessResult::EProcessIncomplete,
       
   424 									totalSrcBytesCopied, totalDstBytesAdded );
       
   425 						}
       
   426 					// last frame of the input stream
       
   427 					else if ( lastFrame )
       
   428 						{
       
   429 						// if internal buffer has available bytes to decode
       
   430 						if ( iInternalInputBufferResidueLen > 0 )
       
   431 							{
       
   432 							PRINT_EXIT;
       
   433 							return Result(
       
   434 									TCodecProcessResult::EProcessIncomplete,
       
   435 									totalSrcBytesCopied, totalDstBytesAdded );
       
   436 							}
       
   437 						else
       
   438 							{
       
   439 							iInternalInputBufferResidueLen = 0;
       
   440 							PRINT_EXIT;
       
   441 							return Result(
       
   442 									TCodecProcessResult::EEndOfData,
       
   443 									totalSrcBytesCopied, totalDstBytesAdded );
       
   444 							}
       
   445 						}
       
   446 					else
       
   447 						{
       
   448 						PRINT_EXIT;
       
   449 						return Result(
       
   450 								TCodecProcessResult::EProcessComplete,
       
   451 								totalSrcBytesCopied, totalDstBytesAdded );
       
   452 						}
       
   453 					}
       
   454 				}
       
   455 			}
       
   456 	}while ( 1 );
       
   457 	}
       
   458 
       
   459 //----------------------------------------------------------------------------
       
   460 //  Default constructor for performing 1st stage construction.
       
   461 //  Should not contain any code that could leave.
       
   462 //----------------------------------------------------------------------------
       
   463 //
       
   464 
       
   465 CAriMp3DecMmfCodec::CAriMp3DecMmfCodec():
       
   466 iCodec( NULL ), iInternalInputBufferResidueLen( 0 ),
       
   467 iInternalOutputBufferResidueLen( 0 ), iOutFrameSize( 0 ),
       
   468 iInternalInputBuffer( NULL ), iInternalOutputBuffer( NULL ),
       
   469 iInternalOutputBufferPos( 0 )
       
   470 	{
       
   471 	PRINT_ENTRY;
       
   472 	PRINT_EXIT;
       
   473 	}
       
   474 
       
   475 //----------------------------------------------------------------------------
       
   476 //  Second phase of the two-phased constructor.
       
   477 //  Creates an instance of decoder
       
   478 //----------------------------------------------------------------------------
       
   479 //
       
   480 
       
   481 void CAriMp3DecMmfCodec::ConstructL()
       
   482 	{
       
   483 	PRINT_ENTRY;
       
   484 	TMp3FrameInfo frameInfo;
       
   485 
       
   486 	TRAPD( creatErr,iCodec = CAriMp3DecWrapper::NewL() );
       
   487 	if( KErrNone != creatErr )
       
   488 		{
       
   489 		PRINT_ERR( creatErr );
       
   490 		User::Leave( creatErr );
       
   491 		}
       
   492 
       
   493 	User::LeaveIfError( iCodec->Reset() );
       
   494 
       
   495 	TInt frameLength;
       
   496 
       
   497 	User::LeaveIfError( iCodec->GetMaxFrameInfo( frameLength, frameInfo ) );
       
   498 
       
   499 	iOutFrameSize = frameInfo.iBufferSize;
       
   500 
       
   501 	iInternalInputBuffer = new( ELeave ) TUint8[KMinBytesInput];
       
   502 	if ( !iInternalInputBuffer )
       
   503 		{
       
   504 		PRINT_ERR( "iInternalInputBuffer is not created" );
       
   505 		User::Leave( KErrNoMemory );
       
   506 		}
       
   507 
       
   508 	iInternalOutputBuffer = new( ELeave ) TUint8[iOutFrameSize];
       
   509 	if ( !iInternalOutputBuffer )
       
   510 		{
       
   511 		PRINT_ERR( "iInternalOutputBuffer is not created" );
       
   512 		User::Leave( KErrNoMemory );
       
   513 		}
       
   514 	}
       
   515 
       
   516 //----------------------------------------------------------------------------
       
   517 //	Updates the result of the processing
       
   518 //----------------------------------------------------------------------------
       
   519 //
       
   520 TCodecProcessResult CAriMp3DecMmfCodec::Result(
       
   521 					TCodecProcessResult::TCodecProcessResultStatus aStatus,
       
   522  					TInt aSrcBytesConsumed, TInt aDstBytesAdded )
       
   523 
       
   524 	 {
       
   525 	 PRINT_ENTRY;
       
   526 	 TCodecProcessResult result;
       
   527 	 // update destination bytes
       
   528 	 result.iDstBytesAdded = aDstBytesAdded;
       
   529 	 // update source bytes
       
   530 	 result.iSrcBytesProcessed = aSrcBytesConsumed;
       
   531 
       
   532 	 // update status
       
   533 	 switch ( aStatus )
       
   534 		 {
       
   535 		 case TCodecProcessResult::EProcessComplete:
       
   536 			result.iStatus = TCodecProcessResult::EProcessComplete;
       
   537 			break;
       
   538 		 case TCodecProcessResult::EProcessIncomplete:
       
   539 		 	result.iStatus = TCodecProcessResult::EProcessIncomplete;
       
   540 		 	break;
       
   541 		 case TCodecProcessResult::EEndOfData:
       
   542 		 	result.iStatus = TCodecProcessResult::EEndOfData;
       
   543 		 	break;
       
   544 		 case TCodecProcessResult::EDstNotFilled:
       
   545 		 	result.iStatus = TCodecProcessResult::EDstNotFilled;
       
   546 		 	break;
       
   547 		 case TCodecProcessResult::EProcessError:
       
   548 		 	result.iStatus = TCodecProcessResult::EProcessError;
       
   549 		 	break;
       
   550 		 default:
       
   551 			result.iStatus = TCodecProcessResult::EProcessError;
       
   552 			break;
       
   553 		 }
       
   554 	 PRINT_MSG( LEVEL_HIGH, ( "result.iSrcBytesProcessed = %d",
       
   555 								result.iSrcBytesProcessed ) );
       
   556 	 PRINT_MSG( LEVEL_HIGH, ( "result.iDstBytesAdded = %d",
       
   557 								result.iDstBytesAdded ) );
       
   558 	 PRINT_MSG( LEVEL_HIGH, ( "result.iStatus = %d",
       
   559 								result.iStatus ) );
       
   560 	 PRINT_EXIT;
       
   561 	 return result;
       
   562 	 }
       
   563 
       
   564 //----------------------------------------------------------------------------
       
   565 // Copy the bytes to destination buffer from the internal buffer
       
   566 // first checks whether the number of bytes to be copied is lesser of the
       
   567 // destination buffer reamining bytes and internal input internal remaining
       
   568 // remaining bytes and then copies that many bytes.
       
   569 //----------------------------------------------------------------------------
       
   570 //
       
   571  TInt CAriMp3DecMmfCodec::CopyToDstBuffer( CMMFDataBuffer* aDst,
       
   572 											 TInt &aDstBytesConsumed )
       
   573 	{
       
   574 	PRINT_ENTRY;
       
   575 	TInt numberOfBytesToBeCopied;
       
   576 	const TInt dstMaxLen = aDst->Data().MaxLength();
       
   577 	TUint8* dstPtr = const_cast<TUint8*>( aDst->Data().Ptr() );
       
   578 	TInt dstBufferPos = aDst->Position();
       
   579 
       
   580 	// destination buffer remaining bytes
       
   581 	TInt dstBufferRemainingBytes = dstMaxLen
       
   582 								   - dstBufferPos
       
   583 								   - aDstBytesConsumed;
       
   584 	// internal output buffer remaining bytes
       
   585 	TInt internalOutputBufferRemainingBytes =
       
   586 										 iInternalOutputBufferResidueLen
       
   587 										 - iInternalOutputBufferPos;
       
   588 
       
   589 	if ( internalOutputBufferRemainingBytes > dstBufferRemainingBytes )
       
   590 		{
       
   591 		numberOfBytesToBeCopied = dstBufferRemainingBytes;
       
   592 		}
       
   593 	else
       
   594 		{
       
   595 		numberOfBytesToBeCopied = internalOutputBufferRemainingBytes;
       
   596 		iInternalOutputBufferResidueLen = 0;
       
   597 		}
       
   598 
       
   599 	// copy data to destination buffer from internal ouput buffer
       
   600 	Mem::Copy( dstPtr + dstBufferPos + aDstBytesConsumed,
       
   601 		iInternalOutputBuffer + iInternalOutputBufferPos,
       
   602 		numberOfBytesToBeCopied );
       
   603 
       
   604 	// update internal output buffer position
       
   605 	if( iInternalOutputBufferResidueLen )
       
   606 		{
       
   607 		iInternalOutputBufferPos += dstBufferRemainingBytes;
       
   608 		}
       
   609 	else
       
   610 		{
       
   611 		iInternalOutputBufferPos = 0;
       
   612 		}
       
   613 
       
   614 	aDstBytesConsumed += numberOfBytesToBeCopied;
       
   615 	aDst->Data().SetLength( dstBufferPos +  aDstBytesConsumed );
       
   616 	PRINT_EXIT;
       
   617 	return numberOfBytesToBeCopied;
       
   618 	}
       
   619 
       
   620 //---------------------------------------------------------------------------
       
   621 // Copy the bytes from the source buffer to the internal input buffer.
       
   622 // first it checks number of bytes to be copied is lesser of the source buffer
       
   623 // remaining bytes or internal input buffer remaining bytes and then copies
       
   624 // that many bytes.
       
   625 //---------------------------------------------------------------------------
       
   626 //
       
   627  TInt CAriMp3DecMmfCodec::CopyFromSrcBuffer( const CMMFDataBuffer* aSrc,
       
   628 												 TInt &aSrcBytesConsumed )
       
   629 	{
       
   630 	PRINT_ENTRY;
       
   631 	TInt numberOfBytesToBeCopied;
       
   632 	TUint8* srcPtr = const_cast <TUint8*>( aSrc->Data().Ptr() );
       
   633 	TInt srcBufferLen = aSrc->Data().Length();
       
   634 	TInt srcBufferPos = aSrc->Position();
       
   635 
       
   636 	// calculate the source buffer remaining bytes
       
   637 	TInt srcBufferRemainingBytes = srcBufferLen - srcBufferPos
       
   638 								   - aSrcBytesConsumed;
       
   639 
       
   640 	// calculate internal input buffer remaining bytes
       
   641 	TInt internalInputBufferRemaingBytes = KMinBytesInput
       
   642 										   - iInternalInputBufferResidueLen;
       
   643 
       
   644 	if ( internalInputBufferRemaingBytes > srcBufferRemainingBytes )
       
   645 		{
       
   646 		numberOfBytesToBeCopied = srcBufferRemainingBytes;
       
   647 		}
       
   648 	else
       
   649 		{
       
   650 		numberOfBytesToBeCopied = internalInputBufferRemaingBytes;
       
   651 		}
       
   652 
       
   653 	// copy data from source buffer to internal input buffer
       
   654 	Mem::Copy( iInternalInputBuffer + iInternalInputBufferResidueLen,
       
   655 				srcPtr + srcBufferPos + aSrcBytesConsumed,
       
   656 				numberOfBytesToBeCopied );
       
   657 
       
   658 	// update internal input buffer residue length
       
   659 	iInternalInputBufferResidueLen += numberOfBytesToBeCopied;
       
   660 	aSrcBytesConsumed += numberOfBytesToBeCopied;
       
   661 	PRINT_EXIT;
       
   662 	return numberOfBytesToBeCopied;
       
   663 	}
       
   664 //---------------------------------------------------------------------------
       
   665 // Moves the data of the internal input buffer to the start position
       
   666 //---------------------------------------------------------------------------
       
   667 //
       
   668 void CAriMp3DecMmfCodec::ShiftData( TInt aFromPos, TInt aToPos )
       
   669 	{
       
   670 	PRINT_ENTRY;
       
   671 	for ( TInt i = aToPos; i < ( iInternalInputBufferResidueLen - aFromPos );
       
   672 																	i++ )
       
   673 		{
       
   674 		iInternalInputBuffer[i] = iInternalInputBuffer[i + aFromPos];
       
   675 		}
       
   676 	iInternalInputBufferResidueLen -= aFromPos;
       
   677 	PRINT_EXIT;
       
   678 	}
       
   679 //---------------------------------------------------------------------------
       
   680 // Gets the new data from the source buffer.
       
   681 // checks whether it is last buffer from the source buffer and copies the data
       
   682 // and then checks if source buffer remianing bytes are not and returns the
       
   683 // result of the processing.
       
   684 //---------------------------------------------------------------------------
       
   685 //
       
   686 TCodecProcessResult CAriMp3DecMmfCodec::GetNewData(
       
   687 							const CMMFDataBuffer* aSrc,
       
   688 							TInt &aSrcBytesConsumed, TInt &aDstBytesConsumed )
       
   689 	{
       
   690 	PRINT_ENTRY;
       
   691 	TUint8* srcPtr = const_cast <TUint8*>( aSrc->Data().Ptr() );
       
   692 	TInt srcBufferLen = aSrc->Data().Length();
       
   693 	TInt srcBufferPos = aSrc->Position();
       
   694 	TBool lastFrame = aSrc->LastBuffer();
       
   695 	// calculate source buffer remaining bytes
       
   696 	TInt srcBufferRemainingBytes = srcBufferLen - srcBufferPos
       
   697 								   - aSrcBytesConsumed;
       
   698 
       
   699 	TCodecProcessResult result;
       
   700 	result.iStatus = TCodecProcessResult::EProcessError;
       
   701 
       
   702 	// if it is last frame return end of data
       
   703 	if ( ( lastFrame ) && ( srcBufferRemainingBytes == 0 ) )
       
   704 		{
       
   705 		iInternalInputBufferResidueLen = 0;
       
   706 		iInternalOutputBufferResidueLen = 0;
       
   707 		iInternalOutputBufferPos = 0;
       
   708 		PRINT_EXIT;
       
   709 		return Result( TCodecProcessResult::EEndOfData,
       
   710 						aSrcBytesConsumed, aDstBytesConsumed );
       
   711 		}
       
   712 	else
       
   713 	{
       
   714 		iInternalInputBufferResidueLen = 0;
       
   715 		TInt numberOfBytesToBeCopied = CopyFromSrcBuffer( aSrc,
       
   716 														 aSrcBytesConsumed );
       
   717 
       
   718 		if ( iInternalInputBufferResidueLen < KMinBytesInput )
       
   719 			{
       
   720 			PRINT_EXIT;
       
   721 			return Result( TCodecProcessResult::EDstNotFilled,
       
   722 							aSrcBytesConsumed, aDstBytesConsumed );
       
   723 			}
       
   724 	}
       
   725 	PRINT_EXIT;
       
   726 	return result;
       
   727 	}
       
   728 /* __________________________________________________________________________
       
   729 * Exported proxy for instantiation method resolution
       
   730 * Define the interface UIDs
       
   731 */
       
   732 
       
   733 const TImplementationProxy ImplementationTable[] =
       
   734 	{
       
   735 	IMPLEMENTATION_PROXY_ENTRY( KUidMp3DecCodecImplUid,
       
   736 								CAriMp3DecMmfCodec::NewL )
       
   737 	};
       
   738 
       
   739 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(
       
   740 														TInt& aTableCount )
       
   741 {
       
   742 	PRINT_ENTRY;
       
   743 	aTableCount = sizeof( ImplementationTable )
       
   744 					/ sizeof( TImplementationProxy );
       
   745 	PRINT_EXIT;
       
   746 	return ImplementationTable;
       
   747 }
       
   748 
       
   749 //---------------------------------------------------------------------------
       
   750 //  End of File
       
   751 //---------------------------------------------------------------------------