videoeditorengine/audioeditorengine/codecs/AWB/src/ProcAWBInFileHandler.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 "ProcAWBInFileHandler.h"
       
    22 #include "ProcAWBFrameHandler.h"
       
    23 
       
    24 #include "AudPanic.h"
       
    25 #include "AWBConstants.h"
       
    26 #include "ProcTools.h"
       
    27 
       
    28 
       
    29 CProcAWBInFileHandler* CProcAWBInFileHandler::NewL(const TDesC& aFileName,  
       
    30                                                    RFile* aFileHandle,
       
    31                                                    CAudClip* aClip, 
       
    32                                                    TInt aReadBufferSize,
       
    33                                                    TInt aTargetSampleRate, 
       
    34                                                    TChannelMode aChannelMode) 
       
    35     {
       
    36 
       
    37     CProcAWBInFileHandler* self = NewLC(aFileName, aFileHandle, aClip, 
       
    38                     aReadBufferSize, aTargetSampleRate, aChannelMode);
       
    39     CleanupStack::Pop(self);
       
    40     return self;
       
    41     }
       
    42 
       
    43 CProcAWBInFileHandler* CProcAWBInFileHandler::NewLC(const TDesC& aFileName, 
       
    44                                                     RFile* aFileHandle,
       
    45                                                     CAudClip* aClip, 
       
    46                                                     TInt aReadBufferSize,
       
    47                                                     TInt aTargetSampleRate, 
       
    48                                                     TChannelMode aChannelMode) 
       
    49     {
       
    50     
       
    51     CProcAWBInFileHandler* self = new (ELeave) CProcAWBInFileHandler();
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL(aFileName, aFileHandle, aClip, aReadBufferSize, 
       
    54                     aTargetSampleRate, aChannelMode);
       
    55     return self;
       
    56     }
       
    57 
       
    58 CProcAWBInFileHandler::CProcAWBInFileHandler() : CProcInFileHandler() 
       
    59     {
       
    60     
       
    61     
       
    62     }
       
    63 
       
    64 void CProcAWBInFileHandler::GetPropertiesL(TAudFileProperties* aProperties) 
       
    65     {
       
    66 
       
    67     if (iProperties != 0)
       
    68         {
       
    69         *aProperties = *iProperties;
       
    70         return;
       
    71         }
       
    72     
       
    73     aProperties->iAudioTypeExtension = EAudExtensionTypeNoExtension;
       
    74     
       
    75     if (iFileOpen) 
       
    76         {
       
    77         aProperties->iFileFormat = EAudFormatAMRWB;
       
    78         
       
    79         TInt oldPos = iFilePos;
       
    80 
       
    81         TInt fileSize = 0;
       
    82         iFile.Size(fileSize);
       
    83         
       
    84         TBuf8<9> header;
       
    85 
       
    86         BufferedFileRead(0, header);
       
    87         if (header.Compare(_L8("#!AMR-WB\n")) == 0) 
       
    88             {
       
    89             aProperties->iAudioType = EAudAMRWB;
       
    90             aProperties->iSamplingRate = 16000;
       
    91             aProperties->iChannelMode = EAudSingleChannel;
       
    92             aProperties->iBitrateMode = EAudVariable;
       
    93             
       
    94             TBuf8<1> modeByte;
       
    95             BufferedFileRead(9, modeByte);
       
    96 
       
    97             TUint8 toc = modeByte[0];
       
    98             
       
    99             TUint8 mode = static_cast<TUint8>((toc >> 3) & 0x0F);
       
   100             
       
   101             aProperties->iBitrate = KAWBBitRates[mode];
       
   102             TInt durationMilli = 0;
       
   103             TInt frameAmount = 0;
       
   104             TInt frameDuration = 0;
       
   105             TInt frameSize = 0;
       
   106 
       
   107             GetFrameInfo(durationMilli, frameAmount, frameDuration, frameSize);
       
   108 
       
   109             TInt64 tmp = (TInt64)(TInt)durationMilli*1000;
       
   110             // milliseconds->microseconds
       
   111             TTimeIntervalMicroSeconds durationMicro(tmp);
       
   112             aProperties->iDuration = durationMicro;
       
   113             aProperties->iFrameCount = frameAmount;
       
   114             
       
   115             tmp = (TInt64)(TInt)frameDuration*1000;
       
   116             TTimeIntervalMicroSeconds frameDurationMicro(tmp);
       
   117             
       
   118             aProperties->iFrameDuration = frameDurationMicro;
       
   119             aProperties->iFrameLen = frameSize;
       
   120             
       
   121             
       
   122             }
       
   123         BufferedFileSetFilePos(oldPos);
       
   124         }
       
   125     else 
       
   126         {
       
   127         TAudPanic::Panic(TAudPanic::EInternal);
       
   128         }
       
   129 
       
   130     if (iProperties == 0)
       
   131         {
       
   132         iProperties = new (ELeave) TAudFileProperties;
       
   133         *iProperties = *aProperties;
       
   134         }
       
   135     
       
   136     
       
   137     
       
   138     
       
   139     }
       
   140 
       
   141 TBool CProcAWBInFileHandler::SeekAudioFrame(TInt32 aTime) 
       
   142     {
       
   143     
       
   144     if (!iFileOpen) 
       
   145         {
       
   146         TAudPanic::Panic(TAudPanic::EInternal);
       
   147         }
       
   148 
       
   149     TInt fileSize = 0;
       
   150     iFile.Size(fileSize);
       
   151 
       
   152 
       
   153     TInt firstModeByte = 9;
       
   154     TInt readSize = 0;
       
   155     TBuf8<1> modeByte;
       
   156     iCurrentTimeMilliseconds = 0;
       
   157 
       
   158     BufferedFileSetFilePos(firstModeByte);
       
   159     
       
   160     while(iCurrentTimeMilliseconds < aTime) 
       
   161         {
       
   162         
       
   163         //iFile.Read(modeByte, 1);
       
   164         BufferedFileRead(modeByte);
       
   165         if (modeByte.Size() == 0) 
       
   166             {
       
   167             return EFalse;
       
   168             }
       
   169 
       
   170         TUint8 toc = modeByte[0];
       
   171         TUint8 mode = static_cast<TUint8>((toc >> 3) & 0x0F);
       
   172 
       
   173         readSize = KAWBPacked_size[mode];
       
   174 
       
   175         if (!(BufferedFileSetFilePos(BufferedFileGetFilePos()+readSize))) 
       
   176             {
       
   177             
       
   178             // EOF
       
   179             return EFalse;
       
   180             }
       
   181         iCurrentTimeMilliseconds += 20;
       
   182 
       
   183         }
       
   184     return ETrue;
       
   185 
       
   186     }    
       
   187 
       
   188 TBool CProcAWBInFileHandler::SeekCutInFrame() 
       
   189     {
       
   190 
       
   191     iCurrentTimeMilliseconds = iCutInTime;
       
   192     return SeekAudioFrame(iCutInTime);
       
   193     }
       
   194     
       
   195 
       
   196 
       
   197 void CProcAWBInFileHandler::ConstructL(const TDesC& aFileName, 
       
   198                                        RFile* aFileHandle,
       
   199                                        CAudClip* aClip, 
       
   200                                        TInt aReadBufferSize,
       
   201                                        TInt aTargetSampleRate, 
       
   202                                        TChannelMode aChannelMode) 
       
   203     {
       
   204     
       
   205 
       
   206     iTargetSampleRate = aTargetSampleRate;
       
   207     iChannelMode = aChannelMode;
       
   208 
       
   209 
       
   210     InitAndOpenFileL(aFileName, aFileHandle, aReadBufferSize);
       
   211     
       
   212 
       
   213     iClip = aClip;
       
   214 
       
   215     if (aClip != 0)
       
   216         {
       
   217         iCutInTime = ProcTools::MilliSeconds(aClip->CutInTime());
       
   218         
       
   219         }
       
   220 
       
   221 
       
   222     TUint8 sf[18] = {0x04,0x10,0x20,0x00,0x21,
       
   223     0x1C,0x14,0xD0,0x11,0x40,0x4C,0xC1,0xA0,
       
   224     0x50,0x00,0x00,0x44,0x30};
       
   225     iSilentFrame = HBufC8::NewL(18);
       
   226     iSilentFrame->Des().Append(sf,18);
       
   227     iSilentFrameDuration = 20;
       
   228     
       
   229     
       
   230     if (iClip != 0 && iClip->Normalizing())
       
   231         {
       
   232         SetNormalizingGainL(iFrameHandler);    
       
   233         }
       
   234     
       
   235     TAudFileProperties prop;
       
   236     GetPropertiesL(&prop);
       
   237     
       
   238     iDecoder = CProcDecoder::NewL();
       
   239     
       
   240     iDecodingPossible = iDecoder->InitL(prop, aTargetSampleRate, aChannelMode);
       
   241     
       
   242     
       
   243     iFrameHandler = CProcAWBFrameHandler::NewL();
       
   244     
       
   245     if (iClip != 0 && iClip->Normalizing())
       
   246         {
       
   247         SetNormalizingGainL(iFrameHandler);    
       
   248         }
       
   249     
       
   250     }
       
   251 
       
   252 
       
   253 
       
   254 
       
   255 TBool CProcAWBInFileHandler::GetEncAudioFrameL(HBufC8*& aFrame, TInt& aSize, TInt32& aTime)
       
   256     {
       
   257     if (!iFileOpen) 
       
   258         {
       
   259         TAudPanic::Panic(TAudPanic::EInternal);
       
   260         }
       
   261 
       
   262     aFrame = NULL;
       
   263     TInt readSize = 0;
       
   264     TBuf8<1> modeByte;
       
   265 
       
   266     BufferedFileRead(modeByte);
       
   267 
       
   268     if (modeByte.Size() == 0) 
       
   269         {
       
   270         return EFalse;
       
   271         }
       
   272 
       
   273     TUint8 toc = modeByte[0];
       
   274     TUint8 mode = static_cast<TUint8>((toc >> 3) & 0x0F);
       
   275 
       
   276     readSize = KAWBPacked_size[mode];
       
   277 
       
   278 
       
   279     
       
   280     HBufC8* audioFrame = HBufC8::NewL(readSize+1);
       
   281 
       
   282     TPtr8 tmpDes((TPtr8)audioFrame->Des());
       
   283     
       
   284     BufferedFileRead((TPtr8&) tmpDes, readSize);
       
   285     audioFrame->Des().Insert(0, modeByte);
       
   286 
       
   287     aFrame = audioFrame;
       
   288 
       
   289     TRAPD(err, ManipulateGainL(aFrame));
       
   290     
       
   291     if (err != KErrNone)
       
   292         {
       
   293         // something went wrong with the gain manipulation
       
   294         // continue by returning the original frame
       
   295         }
       
   296     
       
   297 
       
   298     aSize = aFrame->Size();
       
   299     aTime = 20;
       
   300     iCurrentTimeMilliseconds += 20;
       
   301     
       
   302 
       
   303     return ETrue;
       
   304     }
       
   305 
       
   306 
       
   307 
       
   308 CProcAWBInFileHandler::~CProcAWBInFileHandler() 
       
   309     {
       
   310 
       
   311     if (iSilentFrame != 0) delete iSilentFrame;
       
   312     ResetAndCloseFile();
       
   313 
       
   314 
       
   315     delete iDecoder;
       
   316     delete iFrameHandler;
       
   317     
       
   318     }
       
   319 
       
   320 
       
   321 TBool CProcAWBInFileHandler::GetFrameInfo(TInt& aSongDuration,
       
   322                                            TInt& aFrameAmount, 
       
   323                                            TInt& aAverageFrameDuration, 
       
   324                                            TInt& aAverageFrameSize) 
       
   325     {
       
   326 
       
   327 
       
   328         
       
   329 
       
   330     if (!iFileOpen) 
       
   331         {
       
   332         TAudPanic::Panic(TAudPanic::EInternal);
       
   333         }
       
   334 
       
   335     TInt fileSize = 0;
       
   336     iFile.Size(fileSize);
       
   337 
       
   338 
       
   339     TInt firstModeByte = 9;
       
   340     TInt frameIndex = 1;
       
   341     TInt readSize = 0;
       
   342     TBuf8<1> modeByte;
       
   343 
       
   344     TInt32 frameSizeSum = 0;
       
   345     
       
   346     TInt filePos = iFilePos;
       
   347 
       
   348     BufferedFileSetFilePos(firstModeByte);
       
   349     
       
   350     while(frameIndex < 180000) // 1 hour 
       
   351         {
       
   352         
       
   353 
       
   354         BufferedFileRead(modeByte);
       
   355         if (modeByte.Size() == 0) 
       
   356             {
       
   357             break;
       
   358             }
       
   359 
       
   360         TUint8 toc = modeByte[0];
       
   361         TUint8 mode = static_cast<TUint8>((toc >> 3) & 0x0F);
       
   362 
       
   363         readSize = KAWBPacked_size[mode];
       
   364 
       
   365         frameSizeSum += readSize;
       
   366         aSongDuration += 20;
       
   367 
       
   368         if (!(BufferedFileSetFilePos(BufferedFileGetFilePos()+readSize))) 
       
   369             {
       
   370             
       
   371             break; // EOF
       
   372             }
       
   373         
       
   374         frameIndex++;
       
   375         }
       
   376 
       
   377     aAverageFrameSize = frameSizeSum/frameIndex;
       
   378     aFrameAmount = frameIndex;
       
   379     aAverageFrameDuration = aSongDuration/aFrameAmount;
       
   380     BufferedFileSetFilePos(filePos);
       
   381     return ETrue;
       
   382 
       
   383 
       
   384     }
       
   385 
       
   386 
       
   387 TBool CProcAWBInFileHandler::SetNormalizingGainL(const CProcFrameHandler* aFrameHandler)
       
   388     {
       
   389 
       
   390     HBufC8* point = 0;
       
   391     TInt siz;
       
   392     TInt32 tim = 0;
       
   393     TInt8 margin = 0;
       
   394     TInt minMargin = KMaxTInt;
       
   395     TInt tmpGain = 0;
       
   396     TInt frameCounter = 0;
       
   397     while(GetEncAudioFrameL(point, siz, tim)) 
       
   398                     
       
   399         {
       
   400         aFrameHandler->GetNormalizingMargin(point, margin);
       
   401         tmpGain += margin;
       
   402                     
       
   403         delete point;
       
   404         point = 0;
       
   405         frameCounter++;
       
   406         if (frameCounter > 1) 
       
   407             {
       
   408             tmpGain = tmpGain/3;
       
   409                         
       
   410             if (tmpGain < minMargin) 
       
   411                 {
       
   412                 minMargin = tmpGain;
       
   413                 }
       
   414                 
       
   415             frameCounter = 0;
       
   416             tmpGain = 0;
       
   417             }
       
   418         }
       
   419 
       
   420     iNormalizingMargin = static_cast<TInt8>(minMargin);
       
   421 
       
   422     return ETrue;
       
   423 
       
   424     }
       
   425     
       
   426