videoeditorengine/audioeditorengine/codecs/AAC/src/ProcADTSInFileHandler.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <e32base.h>
       
    22 #include <f32file.h>
       
    23 #include "AudCommon.h"
       
    24 #include "ProcConstants.h"
       
    25 #include "ProcADTSInFileHandler.h"
       
    26 //#include "mp4aud.h"
       
    27 #include "mp4config.h"
       
    28 #include "audconstants.h"
       
    29 
       
    30 #include "ProcTools.h"
       
    31 #include "ProcAACFrameHandler.h"
       
    32 
       
    33 
       
    34  
       
    35 CProcADTSInFileHandler* CProcADTSInFileHandler::NewL(const TDesC& aFileName, RFile* aFileHandle,
       
    36                                                      CAudClip* aClip, TInt aReadBufferSize, 
       
    37                                                      TInt aTargetSampleRate, TChannelMode aChannelMode)
       
    38     {
       
    39   
       
    40   CProcADTSInFileHandler* self = NewLC(aFileName, aFileHandle, aClip, aReadBufferSize, aTargetSampleRate, aChannelMode);
       
    41   CleanupStack::Pop(self);
       
    42   return self;
       
    43 
       
    44     }
       
    45     
       
    46 CProcADTSInFileHandler* CProcADTSInFileHandler::NewLC(const TDesC& aFileName, RFile* aFileHandle,
       
    47                                                       CAudClip* aClip, TInt aReadBufferSize, TInt aTargetSampleRate,
       
    48                                                       TChannelMode aChannelMode)
       
    49 
       
    50     {
       
    51 
       
    52   CProcADTSInFileHandler* self = new (ELeave) CProcADTSInFileHandler();
       
    53   CleanupStack::PushL(self);
       
    54   self->ConstructL(aFileName, aFileHandle, aClip, aReadBufferSize, aTargetSampleRate, aChannelMode);
       
    55   return self;
       
    56 
       
    57     }
       
    58 
       
    59 void CProcADTSInFileHandler::GetPropertiesL(TAudFileProperties* aProperties)
       
    60     {
       
    61 
       
    62     if (iProperties != 0)
       
    63         {
       
    64         *aProperties = *iProperties;
       
    65         return;
       
    66         }
       
    67 
       
    68     aProperties->iDuration = 0;
       
    69     aProperties->iSamplingRate = 0;
       
    70     aProperties->iBitrate = 0;
       
    71     aProperties->iChannelMode = EAudChannelModeNotRecognized;
       
    72     aProperties->iFrameLen = 0;
       
    73     aProperties->iFrameCount = 0;
       
    74 
       
    75     aProperties->iAudioType = EAudTypeUnrecognized;
       
    76     aProperties->iAudioTypeExtension = EAudExtensionTypeNoExtension;
       
    77     aProperties->iFileFormat = EAudFormatUnrecognized;
       
    78     aProperties->iBitrateMode = EAudBitrateModeNotRecognized;
       
    79     aProperties->iFrameDuration = 0;
       
    80     aProperties->iNumFramesPerSample = 1;
       
    81     aProperties->iChannelModeExtension = EAudChannelModeNotRecognized;
       
    82     aProperties->iAACObjectType = EAudAACObjectTypeNone;
       
    83     
       
    84     if (!iSupportedFile)
       
    85         {
       
    86         User::Leave(KErrNotSupported);
       
    87         return;
       
    88         }
       
    89 
       
    90     ReadHeaderInformation(aProperties);
       
    91     ReadOtherInformationL(aProperties);
       
    92 
       
    93     aProperties->iBitrateMode = EAudConstant;
       
    94 
       
    95     if (iProperties == 0)
       
    96         {
       
    97         iProperties = new (ELeave) TAudFileProperties;
       
    98         *iProperties = *aProperties;
       
    99         }
       
   100 
       
   101     }
       
   102 
       
   103 
       
   104 TBool CProcADTSInFileHandler::SeekAudioFrame(TInt32 aTime)
       
   105     {
       
   106     
       
   107     if (!iSupportedFile)
       
   108         {
       
   109         return EFalse;
       
   110         }
       
   111 
       
   112     if (aTime == 0) 
       
   113         {
       
   114         FindFirstAACFrame();
       
   115         iCurrentTimeMilliseconds = aTime;
       
   116         return ETrue;
       
   117         }
       
   118     
       
   119     else FindFirstAACFrame();
       
   120 
       
   121     TBuf8<6> header;
       
   122 
       
   123     if(BufferedFileRead(header, 6) != 6)
       
   124         {
       
   125         return EFalse;
       
   126         }
       
   127 
       
   128     if (header.Length() != 6)
       
   129         {
       
   130         return EFalse;
       
   131         }
       
   132 
       
   133     TUint8 byte2 = header[1];
       
   134     TBuf8<8> byte2b;
       
   135     ProcTools::Dec2Bin(byte2, byte2b);
       
   136     // lets confirm that we have found a legal AAC header
       
   137     if (header[0] == 0xFF &&
       
   138         byte2b[0] == '1' &&
       
   139         byte2b[1] == '1' &&
       
   140         byte2b[2] == '1' &&
       
   141         byte2b[3] == '1' &&
       
   142         //byte2b[4] == '1' &&
       
   143         byte2b[5] == '0' &&
       
   144         byte2b[6] == '0')
       
   145         {
       
   146         }    
       
   147     else
       
   148         {
       
   149         return EFalse;
       
   150         }
       
   151 
       
   152     TUint8 byte4 = header[3];
       
   153     TUint8 byte5 = header[4];
       
   154     TUint8 byte6 = header[5];
       
   155 
       
   156     TUint frameLen = CalculateAACFrameLength(byte4, byte5, byte6);    
       
   157     //aFrame = HBufC8::NewL(frameLen);
       
   158 
       
   159     TInt dur = 0;
       
   160     
       
   161     while (BufferedFileSetFilePos(iFilePos-6+frameLen))
       
   162         {
       
   163 
       
   164         dur = dur + ProcTools::MilliSeconds(iProperties->iFrameDuration);
       
   165         if (dur > aTime)
       
   166             {
       
   167             break;
       
   168             }
       
   169 
       
   170         if(BufferedFileRead(header, 6) != 6)
       
   171             {
       
   172             break;
       
   173             }
       
   174 
       
   175         if (header.Length() != 6)
       
   176             {
       
   177             break;
       
   178             }
       
   179 
       
   180         if (header[0] != 0xFF) break;
       
   181         byte4 = header[3];
       
   182         byte5 = header[4];
       
   183         byte6 = header[5];
       
   184 
       
   185         frameLen = CalculateAACFrameLength(byte4, byte5, byte6);    
       
   186     
       
   187         }
       
   188 
       
   189 
       
   190     aTime = dur;
       
   191     iCurrentTimeMilliseconds = aTime;
       
   192     return ETrue;
       
   193     }
       
   194 
       
   195 TBool CProcADTSInFileHandler::SeekCutInFrame()
       
   196     {
       
   197     iCurrentTimeMilliseconds = iCutInTime;
       
   198     return SeekAudioFrame(iCutInTime);
       
   199     }
       
   200 
       
   201 
       
   202 
       
   203 
       
   204 TBool CProcADTSInFileHandler::SetNormalizingGainL(const CProcFrameHandler* aFrameHandler)
       
   205     {
       
   206     
       
   207     
       
   208     HBufC8* point = 0;
       
   209     TInt siz;
       
   210     TInt32 tim = 0;
       
   211     TInt maxGain = 0;
       
   212     RArray<TInt> gains;
       
   213     TInt maxAverage = 0;
       
   214     TInt tmpGain = 0;
       
   215     TInt gainCounter = 0;
       
   216     TInt timeNow = 0;
       
   217     while(GetEncAudioFrameL(point, siz, tim)) 
       
   218     {
       
   219         CleanupStack::PushL(point);
       
   220         
       
   221         aFrameHandler->GetGainL(point, gains, maxGain);
       
   222         timeNow += tim;
       
   223         
       
   224         for (TInt a = 0 ; a < gains.Count() ; a++)
       
   225         {
       
   226             tmpGain += gains[a];
       
   227             gainCounter++;
       
   228         }
       
   229         gains.Reset();
       
   230         
       
   231         if (timeNow > 1000)
       
   232         {
       
   233             if (tmpGain/gainCounter > maxAverage)
       
   234             {
       
   235                 maxAverage = tmpGain/gainCounter;
       
   236             }
       
   237             
       
   238             timeNow = 0;
       
   239             tmpGain = 0;
       
   240             gainCounter = 0;
       
   241         }
       
   242         
       
   243         CleanupStack::PopAndDestroy(point);
       
   244         
       
   245     }
       
   246     
       
   247     // bigger value makes normalizing more efficient, but makes
       
   248     // dynamic compression more likely to occur
       
   249     TInt NormalizingFactor = 175;
       
   250     if (iProperties->iBitrate > 20000 && iProperties->iBitrate < 40000)
       
   251     {
       
   252         
       
   253         // 32 kBit/s
       
   254         NormalizingFactor = 170;
       
   255         
       
   256     }
       
   257     else if (iProperties->iBitrate > 80000 && iProperties->iBitrate < 110000)
       
   258     {
       
   259         // 96 kBit/s
       
   260         NormalizingFactor = 170;
       
   261         
       
   262     }
       
   263     
       
   264     
       
   265     else if (iProperties->iBitrate > 110000 && iProperties->iBitrate < 140000)
       
   266     {
       
   267         // 128 kBit/s
       
   268         if (iProperties->iChannelMode == EAudSingleChannel)
       
   269             NormalizingFactor = 160;
       
   270         else
       
   271             NormalizingFactor = 170;
       
   272         
       
   273     }
       
   274     else if (iProperties->iBitrate > 150000)
       
   275     {
       
   276         // 256 kBit/s
       
   277         if (iProperties->iChannelMode == EAudSingleChannel)
       
   278             NormalizingFactor = 150;
       
   279         else
       
   280             NormalizingFactor = 165;
       
   281         
       
   282     }
       
   283     else
       
   284     {
       
   285         
       
   286         if (iProperties->iChannelMode == EAudSingleChannel)
       
   287             NormalizingFactor = 170;
       
   288         
       
   289     }
       
   290     
       
   291     
       
   292     TInt gainBoost = (NormalizingFactor-maxAverage)*3;
       
   293     
       
   294     if (gainBoost < 0) gainBoost = 0;
       
   295     
       
   296     iNormalizingMargin = static_cast<TInt8>(gainBoost);
       
   297     
       
   298     return ETrue;
       
   299 
       
   300     }
       
   301 
       
   302 
       
   303 TBool CProcADTSInFileHandler::ReadAudioDecoderSpecificInfoL(HBufC8*& aBytes, TInt aBufferSize)
       
   304     {
       
   305 
       
   306     if (aBufferSize < iMP4DecSpecInfo->Size()) return EFalse;
       
   307 
       
   308     if (iMP4DecSpecInfo != 0)
       
   309         {    
       
   310         aBytes = HBufC8::NewL(iMP4DecSpecInfo->Size());    
       
   311         aBytes->Des().Append(iMP4DecSpecInfo->Des());
       
   312         return ETrue;
       
   313         }
       
   314     else 
       
   315         {
       
   316         aBytes = 0;
       
   317         return EFalse;
       
   318         }    
       
   319 
       
   320     }
       
   321 
       
   322 
       
   323 CProcADTSInFileHandler::~CProcADTSInFileHandler()
       
   324     {
       
   325     ResetAndCloseFile();
       
   326 
       
   327     if (iSilentFrame != 0)
       
   328         {
       
   329         delete iSilentFrame;
       
   330         }
       
   331 
       
   332     if (iMP4DecSpecInfo != 0)
       
   333         {
       
   334         delete iMP4DecSpecInfo;
       
   335 
       
   336         }
       
   337 
       
   338 
       
   339     if (iFrameInfo != 0)
       
   340         {
       
   341         delete iFrameInfo;
       
   342 
       
   343         }
       
   344     
       
   345     delete iFrameHandler;
       
   346         
       
   347     delete iDecoder;
       
   348     
       
   349     }
       
   350 
       
   351 
       
   352 void CProcADTSInFileHandler::ConstructL(const TDesC& aFileName, 
       
   353                                         RFile* aFileHandle,
       
   354                                         CAudClip* aClip, 
       
   355                                         TInt aReadBufferSize,
       
   356                                         TInt aTargetSampleRate,
       
   357                                         TChannelMode aChannelMode)
       
   358     {
       
   359 
       
   360     
       
   361 
       
   362     iTargetSampleRate = aTargetSampleRate;
       
   363     iChannelMode = aChannelMode;
       
   364 
       
   365     InitAndOpenFileL(aFileName, aFileHandle, aReadBufferSize);
       
   366     
       
   367     if (aClip != 0)
       
   368         {
       
   369         iCutInTime = ProcTools::MilliSeconds(aClip->CutInTime());
       
   370         
       
   371         }
       
   372     
       
   373     TBool frameFound = FindFirstAACFrame();
       
   374     
       
   375     if (!frameFound)
       
   376         {
       
   377         iSupportedFile = EFalse;
       
   378         return;
       
   379         }
       
   380     
       
   381     iClip = aClip;
       
   382 
       
   383     TAudFileProperties prop;
       
   384     iFrameInfo = new (ELeave) TAACFrameHandlerInfo;
       
   385     GetPropertiesL(&prop);
       
   386 
       
   387     if (prop.iAudioTypeExtension != EAudExtensionTypeNoExtension)
       
   388         {
       
   389         // AACPlus frame duration is calculated from sampling rate
       
   390         // the actual sampling rate of AAC plus is twice as high as
       
   391         // the AAC part
       
   392         iProperties->iFrameDuration = iProperties->iFrameDuration.Int64()/2;
       
   393         
       
   394         }
       
   395 
       
   396     // fill in aacInfo for retrieving decoder specific information
       
   397     //AACStreamInfo aacInfo;
       
   398     int16 frameLen = 0;
       
   399     int32 sampleRate = 0;
       
   400     uint8 profile = 0;
       
   401     uint8 nChannels = 0;
       
   402 
       
   403     // generate a silent frame ------------------>
       
   404 
       
   405     if (iProperties->iChannelMode == EAudSingleChannel)
       
   406         {
       
   407 
       
   408         iSilentFrame = HBufC8::NewL(KSilentMonoAACFrameLenght);    
       
   409         
       
   410         iSilentFrame->Des().Append(KSilentMonoAACFrame, KSilentMonoAACFrameLenght);
       
   411 
       
   412         nChannels = 1;
       
   413         }
       
   414     else if (iProperties->iChannelMode == EAudStereo)
       
   415         {
       
   416 
       
   417         iSilentFrame = HBufC8::NewL(KSilentStereoAACFrameLenght);    
       
   418         iSilentFrame->Des().Append(KSilentStereoAACFrame, KSilentStereoAACFrameLenght);
       
   419         
       
   420         nChannels = 2;
       
   421         }
       
   422     else
       
   423         {
       
   424         User::Leave(KErrNotSupported);
       
   425         }
       
   426     
       
   427     // Check that the sample rate is supported    
       
   428     if( (iProperties->iSamplingRate != KAedSampleRate8kHz) &&
       
   429         (iProperties->iSamplingRate != KAedSampleRate11kHz) &&
       
   430         (iProperties->iSamplingRate != KAedSampleRate16kHz) &&
       
   431         (iProperties->iSamplingRate != KAedSampleRate22kHz) &&
       
   432         (iProperties->iSamplingRate != KAedSampleRate24kHz) &&
       
   433         (iProperties->iSamplingRate != KAedSampleRate32kHz) &&
       
   434         (iProperties->iSamplingRate != KAedSampleRate44kHz) &&
       
   435         (iProperties->iSamplingRate != KAedSampleRate48kHz) )
       
   436         {
       
   437         User::Leave(KErrNotSupported);
       
   438         }
       
   439          
       
   440 
       
   441 
       
   442     uint8 decSpecInfo[16];
       
   443     int16 nConfigBytes;
       
   444   
       
   445     
       
   446     frameLen = 1024;
       
   447     sampleRate = iProperties->iSamplingRate;
       
   448     if (iFrameInfo->iProfileID == 3)
       
   449         {
       
   450         profile = LTP_OBJECT;
       
   451         }
       
   452     else if (iFrameInfo->iProfileID == 1)
       
   453         {
       
   454         profile = LC_OBJECT;
       
   455         }
       
   456     else
       
   457         {
       
   458         User::Leave(KErrNotSupported);
       
   459         }
       
   460 
       
   461     //nConfigBytes = AACGetMP4ConfigInfo(&aacInfo, decSpecInfo, 16);
       
   462     nConfigBytes = AACGetMP4ConfigInfo(sampleRate, profile, 
       
   463         nChannels, frameLen, decSpecInfo, 16);
       
   464 
       
   465 
       
   466     if (nConfigBytes > 0)
       
   467         {
       
   468 
       
   469         iMP4DecSpecInfo = HBufC8::NewL(nConfigBytes);
       
   470         iMP4DecSpecInfo->Des().Append(decSpecInfo, nConfigBytes);
       
   471 
       
   472         }
       
   473 
       
   474 
       
   475     mp4_u32 frameDurationMilli = ProcTools::MilliSeconds(iProperties->iFrameDuration);
       
   476     
       
   477     iSilentFrameDuration = frameDurationMilli;
       
   478     
       
   479 
       
   480     // <------------------generate a silent frame
       
   481 
       
   482     // Generate a decoder ----------------------->
       
   483 
       
   484     iDecoder = CProcDecoder::NewL();
       
   485     
       
   486     iDecodingPossible = iDecoder->InitL(*iProperties, aTargetSampleRate, aChannelMode);
       
   487     
       
   488     // <----------------------- Generate a decoder 
       
   489     
       
   490     // Create a frame handler --------------------->
       
   491     
       
   492     iFrameHandler = CProcAACFrameHandler::NewL(*iFrameInfo);
       
   493     
       
   494     if (iClip != 0 && iClip->Normalizing())
       
   495         {
       
   496         SetNormalizingGainL(iFrameHandler);    
       
   497         }
       
   498     
       
   499 
       
   500     }
       
   501 
       
   502 
       
   503 
       
   504 
       
   505 TBool CProcADTSInFileHandler::GetEncAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime)
       
   506     {
       
   507 
       
   508     if (!iSupportedFile)
       
   509         {
       
   510         return EFalse;
       
   511         }
       
   512         
       
   513         
       
   514     TBuf8<7> header;
       
   515 
       
   516     if(BufferedFileRead(header, 7) != 7)
       
   517         {
       
   518         return EFalse;
       
   519         }
       
   520 
       
   521     if (header.Length() != 7)
       
   522         {
       
   523         return EFalse;
       
   524         }
       
   525 
       
   526     if (header[0] != 0xFF) return EFalse;
       
   527     TUint8 byte4 = header[3];
       
   528     TUint8 byte5 = header[4];
       
   529     TUint8 byte6 = header[5];
       
   530 //    TUint8 byte7 = header[6];
       
   531 
       
   532     
       
   533     TInt frameLen = CalculateAACFrameLength(byte4, byte5, byte6);    
       
   534     aFrame = HBufC8::NewL(frameLen);
       
   535 
       
   536     BufferedFileSetFilePos(iFilePos-7);
       
   537     TPtr8 tmpDes((TPtr8)aFrame->Des());
       
   538 
       
   539     if (BufferedFileRead((TPtr8&)tmpDes, frameLen) != frameLen)
       
   540         {
       
   541         delete aFrame;
       
   542         aFrame = 0;
       
   543         return EFalse;
       
   544         }
       
   545         
       
   546     aSize = frameLen;
       
   547     aTime = ProcTools::MilliSeconds(iProperties->iFrameDuration);
       
   548     
       
   549     // remove ADTS header -------------------------------------------->
       
   550     
       
   551     TInt headerLength = 0;  //  because we need to remove header in case of AAC frame from .aac files
       
   552     //check Header length to be removed from the frame before writting to 3gp file
       
   553     TUint8* dtPtr = (TUint8*)(aFrame->Ptr());
       
   554     if((dtPtr[1]&& 0x01) == 0x00)    //based on bytes decide header length to remove
       
   555         {
       
   556             headerLength =9;
       
   557         }
       
   558     else if((dtPtr[1]&& 0x01)== 0x01)
       
   559         {
       
   560             headerLength =7;
       
   561         }
       
   562         else
       
   563         {
       
   564             headerLength =0;
       
   565         }
       
   566     
       
   567     aFrame->Des().Delete(0,headerLength);
       
   568     aSize -= headerLength;
       
   569  
       
   570     // <--------------------------------------------- remove ADTS header
       
   571     
       
   572     iCurrentTimeMilliseconds += ProcTools::MilliSeconds(iProperties->iFrameDuration);
       
   573     
       
   574     
       
   575     // Fix for synchronizing problem ---------------------------------->
       
   576     // If the accurate frame length cannot be represented in milliseconds
       
   577     // store the remainder and increase the output frame lenght by one ms 
       
   578     // when needed. Accuracy depends on sampling rate
       
   579     
       
   580     TReal accurateFrameLen = TReal(1024000)/iProperties->iSamplingRate;
       
   581     
       
   582     iAACFrameLenRemainderMilli += accurateFrameLen - aTime;
       
   583         
       
   584     if (iAACFrameLenRemainderMilli > 1)
       
   585         {
       
   586         aTime += 1;
       
   587         iCurrentTimeMilliseconds += 1;
       
   588         iAACFrameLenRemainderMilli -= 1;
       
   589         }
       
   590 
       
   591     
       
   592     // <---------------------------------- Fix for synchronizing problem
       
   593     
       
   594     
       
   595     if (iProperties->iAudioTypeExtension == EAudExtensionTypeNoExtension)
       
   596         {
       
   597          
       
   598           
       
   599         // AACPlus is handled after decoding
       
   600                 
       
   601         TRAPD(err, ManipulateGainL(aFrame));
       
   602         
       
   603         if (err != KErrNone)
       
   604             {
       
   605             // something went wrong with the gain manipulation
       
   606             // continue by returning the original frame
       
   607             }    
       
   608         }
       
   609     
       
   610         
       
   611     aSize = aFrame->Size();
       
   612     
       
   613     return ETrue;
       
   614 
       
   615     
       
   616     }
       
   617 
       
   618 CProcADTSInFileHandler::CProcADTSInFileHandler()  : CProcInFileHandler(), iFrameInfo(0),
       
   619                                                     iSupportedFile(ETrue)
       
   620     {
       
   621     
       
   622 
       
   623     }
       
   624 
       
   625 
       
   626 TBool CProcADTSInFileHandler::FindFirstAACFrame()
       
   627     {    
       
   628 
       
   629     TBuf8<8> byte2b;
       
   630     TUint8 byte2;
       
   631     TUint8 byte1;
       
   632 
       
   633     TBool fileEnded = EFalse;
       
   634     TUint ind = 0;
       
   635     TBool frameFound = EFalse;
       
   636 
       
   637   BufferedFileSetFilePos(0);
       
   638 
       
   639     while (!fileEnded) 
       
   640         {
       
   641         if (BufferedFileReadChar(ind, byte1) == 0) 
       
   642             {
       
   643             fileEnded = ETrue;
       
   644             }
       
   645         ind++;
       
   646         
       
   647         if (byte1 != 0xFF) continue;
       
   648 
       
   649 
       
   650         if (BufferedFileReadChar(ind, byte2) == 0) 
       
   651             {
       
   652             fileEnded = ETrue;
       
   653             }
       
   654         ind++;
       
   655         ProcTools::Dec2Bin(byte2, byte2b);
       
   656         
       
   657     
       
   658         if (byte1 == 0xFF &&
       
   659             byte2b[0] == '1' &&
       
   660             byte2b[1] == '1' &&
       
   661             byte2b[2] == '1' &&
       
   662             byte2b[3] == '1' &&
       
   663             //byte2b[4] == '1' &&
       
   664             byte2b[5] == '0' &&
       
   665             byte2b[6] == '0') 
       
   666             {
       
   667                 
       
   668             // this looks like AAC frame header, let's confirm...    
       
   669             TUint8 byte3, byte4, byte5, byte6;
       
   670             BufferedFileReadChar(ind, byte3);    
       
   671             BufferedFileReadChar(ind+1, byte4);
       
   672             BufferedFileReadChar(ind+2, byte5);
       
   673             BufferedFileReadChar(ind+3, byte6);
       
   674 
       
   675             ind++;
       
   676 
       
   677             TUint frameLength = CalculateAACFrameLength(byte4, byte5, byte6);
       
   678             
       
   679             TUint8 testByte1, testByte2, testByte3;
       
   680             BufferedFileReadChar(ind+frameLength-3, testByte1);
       
   681             BufferedFileReadChar(ind+frameLength-2, testByte2);
       
   682             BufferedFileReadChar(ind+frameLength-1, testByte3);
       
   683             
       
   684             if (byte1 == testByte1 && byte2 == testByte2
       
   685                 && byte3 == testByte3) 
       
   686                 {
       
   687                 // this must be the header...
       
   688                 iFirstFrame = ind-3;
       
   689                 //FillInHeaderValues(byte2, byte3, byte4);
       
   690                 frameFound = ETrue;
       
   691                 break;
       
   692                 }
       
   693     
       
   694             }
       
   695 
       
   696         }
       
   697     if (frameFound)
       
   698         {
       
   699         BufferedFileSetFilePos(iFirstFrame);
       
   700         }
       
   701 
       
   702     return frameFound;
       
   703     }
       
   704 
       
   705 
       
   706 TUint CProcADTSInFileHandler::CalculateAACFrameLength(TUint8 byte4, TUint8 byte5, TUint8 byte6) 
       
   707     {
       
   708 
       
   709     TBuf8<13> headerLengthB;
       
   710     TBuf8<8> byte4b, byte5b, byte6b;
       
   711     ProcTools::Dec2Bin(byte4, byte4b);
       
   712     ProcTools::Dec2Bin(byte5, byte5b);
       
   713     ProcTools::Dec2Bin(byte6, byte6b);
       
   714     
       
   715     headerLengthB.Append(byte4b[6]);
       
   716     headerLengthB.Append(byte4b[7]);
       
   717     headerLengthB.Append(byte5b);
       
   718     headerLengthB.Append(byte6b[0]);
       
   719     headerLengthB.Append(byte6b[1]);
       
   720     headerLengthB.Append(byte6b[2]);
       
   721 
       
   722 
       
   723     TUint headerLength = 0;
       
   724     TLex8 leks(headerLengthB);
       
   725 
       
   726     if (leks.Val(headerLength, EBinary) != KErrNone) 
       
   727         {
       
   728         return 0;
       
   729         }
       
   730 
       
   731     return headerLength;
       
   732 
       
   733 }
       
   734 
       
   735 
       
   736 TBool CProcADTSInFileHandler::ReadHeaderInformation(TAudFileProperties* aProperties)
       
   737     {
       
   738 
       
   739     TInt origFilePos = iFilePos;
       
   740     
       
   741     if (aProperties == 0)
       
   742         return EFalse;
       
   743 
       
   744     TBuf8<4> header;
       
   745 
       
   746     if(BufferedFileRead(header, 4) != 4)
       
   747         {
       
   748         BufferedFileSetFilePos(origFilePos);
       
   749         return EFalse;
       
   750         }
       
   751 
       
   752     if (header.Length() != 4)
       
   753         {
       
   754         BufferedFileSetFilePos(origFilePos);
       
   755         return EFalse;
       
   756         }
       
   757 
       
   758     TUint8 byte2 = header[1];
       
   759     TBuf8<8> byte2b;
       
   760     ProcTools::Dec2Bin(byte2, byte2b);
       
   761     // lets confirm that we have found a legal AAC header
       
   762     if (header[0] == 0xFF &&
       
   763         byte2b[0] == '1' &&
       
   764         byte2b[1] == '1' &&
       
   765         byte2b[2] == '1' &&
       
   766         byte2b[3] == '1' &&
       
   767         //byte2b[4] == '1' &&
       
   768         byte2b[5] == '0' &&
       
   769         byte2b[6] == '0')
       
   770         {
       
   771         // OK
       
   772         aProperties->iFileFormat = EAudFormatAAC_ADTS;
       
   773     //    aProperties->iFrameDuration = 23000;
       
   774         }    
       
   775     else
       
   776         {
       
   777         return EFalse;
       
   778         }
       
   779     /*
       
   780     if (byte2b[4] == '1')
       
   781         {
       
   782         aProperties->iAudioType = EAudAAC_MPEG2;
       
   783         }
       
   784     else
       
   785         {
       
   786         aProperties->iAudioType = EAudAAC_MPEG4;
       
   787         }
       
   788     */
       
   789     
       
   790     // NOTE: call all MPeg audio EAudAAC_MPEG4
       
   791     aProperties->iAudioType = EAudAAC_MPEG4;
       
   792     
       
   793     
       
   794     TUint8 byte3 = header[2];
       
   795     TUint8 byte4 = header[3];
       
   796 
       
   797     TBuf8<8> byte3b;
       
   798     TBuf8<8> byte4b;
       
   799 
       
   800     ProcTools::Dec2Bin(byte2, byte2b);
       
   801     ProcTools::Dec2Bin(byte3, byte3b);
       
   802     ProcTools::Dec2Bin(byte4, byte4b);
       
   803 
       
   804     TBuf8<2> profileId(byte3b.Left(2));
       
   805     TUint proID = 0;
       
   806     ProcTools::Bin2Dec(profileId, proID);
       
   807     
       
   808     iFrameInfo->iProfileID = static_cast<TUint8>(proID);
       
   809     aProperties->iAACObjectType = TAudAACObjectType(iFrameInfo->iProfileID);
       
   810 
       
   811 
       
   812     TBuf8<4> samplingRateIndexB;
       
   813     samplingRateIndexB.Append(byte3b[2]);
       
   814     samplingRateIndexB.Append(byte3b[3]);
       
   815     samplingRateIndexB.Append(byte3b[4]);
       
   816     samplingRateIndexB.Append(byte3b[5]);
       
   817 
       
   818     TUint srIndex = 0;
       
   819     TLex8 lek(samplingRateIndexB);
       
   820 
       
   821     if (lek.Val(srIndex, EBinary) != KErrNone) 
       
   822         {
       
   823         BufferedFileSetFilePos(origFilePos);
       
   824         return EFalse;
       
   825         }
       
   826     // aac sampling rates
       
   827         
       
   828     const TInt KAAC_SAMPLING_RATES[16] = {96000,88200,64000,48000,44100,32000,24000
       
   829             ,22050,16000,12000,11025,8000,0,0,0,0};
       
   830 
       
   831     iFrameInfo->iSampleRateID = static_cast<TUint8>(srIndex);
       
   832     aProperties->iSamplingRate = KAAC_SAMPLING_RATES[srIndex];
       
   833     samplingRateIndexB.Delete(0, samplingRateIndexB.Length());
       
   834         
       
   835     // channel configuration
       
   836 
       
   837 
       
   838     TUint aacChannels = 0;
       
   839     TBuf8<3> channelB;
       
   840     channelB.Append(byte3b[7]);
       
   841     channelB.Append(byte4b[0]);
       
   842     channelB.Append(byte4b[1]);
       
   843 
       
   844     TLex8 leks2(channelB);
       
   845     if (leks2.Val(aacChannels, EBinary) != KErrNone) 
       
   846         {
       
   847         BufferedFileSetFilePos(origFilePos);
       
   848         return EFalse;
       
   849         }
       
   850         
       
   851     if (aacChannels == 1)
       
   852         {
       
   853         aProperties->iChannelMode = EAudSingleChannel;
       
   854         }
       
   855     else if (aacChannels == 2)
       
   856         {
       
   857         aProperties->iChannelMode = EAudStereo;
       
   858         }
       
   859     
       
   860     iFrameInfo->iNumChannels = static_cast<TUint8>(aacChannels);
       
   861     iFrameInfo->iNumCouplingChannels = 0;
       
   862     iFrameInfo->iIs960 = 0;
       
   863     
       
   864 
       
   865     BufferedFileSetFilePos(origFilePos);
       
   866     return ETrue;
       
   867 
       
   868     }
       
   869 
       
   870 
       
   871 TBool CProcADTSInFileHandler::ReadOtherInformationL(TAudFileProperties* aProperties)
       
   872     {
       
   873 
       
   874     TInt origFilePos = iFilePos;
       
   875 
       
   876     FindFirstAACFrame();
       
   877 
       
   878     TBuf8<7> header;
       
   879 
       
   880     if(BufferedFileRead(header, 7) != 7)
       
   881         {
       
   882         return EFalse;
       
   883         }
       
   884 
       
   885     if (header.Length() != 7)
       
   886         {
       
   887         return EFalse;
       
   888         }
       
   889 
       
   890     TUint8 byte2 = header[1];
       
   891     TBuf8<8> byte2b;
       
   892     ProcTools::Dec2Bin(byte2, byte2b);
       
   893     // lets confirm that we have found a legal AAC header
       
   894     if (header[0] == 0xFF &&
       
   895         byte2b[0] == '1' &&
       
   896         byte2b[1] == '1' &&
       
   897         byte2b[2] == '1' &&
       
   898         byte2b[3] == '1' &&
       
   899     //    byte2b[4] == '1' &&
       
   900         byte2b[5] == '0' &&
       
   901         byte2b[6] == '0')
       
   902         {
       
   903         // OK
       
   904         //aProperties->iAudioType = EAudAAC;
       
   905         //aProperties->iFileFormat = EAudFormatAAC_ADTS;
       
   906         }    
       
   907     else
       
   908         {
       
   909         return EFalse;
       
   910         }
       
   911 
       
   912     TUint8 byte4 = header[3];
       
   913     TUint8 byte5 = header[4];
       
   914     TUint8 byte6 = header[5];
       
   915 
       
   916     aProperties->iFrameDuration = ((1024*1000)/(aProperties->iSamplingRate))*1000;
       
   917 
       
   918     TInt frameLen = CalculateAACFrameLength(byte4, byte5, byte6);
       
   919     
       
   920     
       
   921     
       
   922 
       
   923      /*-- Allocate resources and read 1st frame. --*/
       
   924     HBufC8 *aFrame = HBufC8::NewL(frameLen);
       
   925     CleanupStack::PushL(aFrame);
       
   926 
       
   927     TPtr8 tmpDes((TPtr8)aFrame->Des());
       
   928 
       
   929     BufferedFileRead((TPtr8&)tmpDes, frameLen - 7);
       
   930 
       
   931     
       
   932     const TInt KShortestAACFrame = 20;
       
   933 
       
   934     if (frameLen > KShortestAACFrame) // a silent frame?
       
   935         {
       
   936         
       
   937         
       
   938 
       
   939         /*
       
   940          * Get frame parameters for eAAC+ codec. It is possible that the bitstream
       
   941         * is plain AAC but we don't know it before the 1st frame is parsed!
       
   942         */
       
   943 
       
   944         TUint8* buf = const_cast<TUint8*>(aFrame->Right(aFrame->Size()).Ptr());
       
   945         CProcAACFrameHandler::GetEnhancedAACPlusParametersL(buf, frameLen - 7, aProperties, iFrameInfo);
       
   946 
       
   947         }
       
   948     else
       
   949         {
       
   950         iFrameInfo->iIsParametricStereo = EFalse;
       
   951         iFrameInfo->isSBR = EFalse;
       
   952             
       
   953         }
       
   954 
       
   955     CleanupStack::Pop(aFrame);
       
   956     delete aFrame;
       
   957     aFrame = 0;
       
   958 
       
   959 
       
   960         
       
   961 
       
   962 
       
   963     if(BufferedFileRead(header, 7) != 7)
       
   964         {
       
   965         return EFalse;
       
   966         }
       
   967 
       
   968     if (header.Length() != 7)
       
   969         {
       
   970         return EFalse;
       
   971         }
       
   972 
       
   973     byte4 = header[3];
       
   974     byte5 = header[4];
       
   975     byte6 = header[5];
       
   976 
       
   977     TInt dur = ProcTools::MilliSeconds(aProperties->iFrameDuration);
       
   978     TInt bytes = frameLen;
       
   979     TInt frames = 1;
       
   980 
       
   981     frameLen = CalculateAACFrameLength(byte4, byte5, byte6);
       
   982 
       
   983     while (BufferedFileSetFilePos(iFilePos-7+frameLen))
       
   984         {
       
   985 
       
   986         dur += ProcTools::MilliSeconds(aProperties->iFrameDuration);
       
   987         bytes += frameLen;
       
   988         frames++;
       
   989 
       
   990         if(BufferedFileRead(header, 7) != 7)
       
   991             {
       
   992             break;
       
   993             }
       
   994 
       
   995         if (header.Length() != 7)
       
   996             {
       
   997             break;
       
   998             }
       
   999 
       
  1000         if (header[0] != 0xFF) break;
       
  1001         byte4 = header[3];
       
  1002         byte5 = header[4];
       
  1003         byte6 = header[5];
       
  1004 
       
  1005         frameLen = CalculateAACFrameLength(byte4, byte5, byte6);    
       
  1006     
       
  1007         }
       
  1008     
       
  1009     TTimeIntervalMicroSeconds tmp((TInt64)(TInt)dur*1000);
       
  1010     aProperties->iDuration = tmp;
       
  1011 
       
  1012     aProperties->iFrameCount = frames;
       
  1013     aProperties->iFrameLen = bytes/frames;
       
  1014     aProperties->iBitrate = (iFilePos*1000/(dur))*8;
       
  1015 
       
  1016 
       
  1017     BufferedFileSetFilePos(origFilePos);
       
  1018 
       
  1019     return ETrue;
       
  1020     }
       
  1021 
       
  1022 TBool CProcADTSInFileHandler::GetInfoForFrameHandler(TAACFrameHandlerInfo& aAACInfo)
       
  1023     {
       
  1024     
       
  1025     aAACInfo = *iFrameInfo;
       
  1026     return ETrue;
       
  1027     }
       
  1028