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