videoeditorengine/audioeditorengine/codecs/mp3/src/ProcMP3FrameHandler.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 /*-- Project Headers. --*/
       
    21 #include "ProcMP3FrameHandler.h"
       
    22 #include "ProcMP3InFileHandler.h"
       
    23 #include "ProcFrameHandler.h"
       
    24 
       
    25 #include "MP3API.h"
       
    26 
       
    27 
       
    28 //mixing files
       
    29 
       
    30 
       
    31 
       
    32 CProcMP3FrameHandler* 
       
    33 CProcMP3FrameHandler::NewL() 
       
    34 {    
       
    35   CProcMP3FrameHandler* self = NewLC();
       
    36   CleanupStack::Pop(self);
       
    37   return self;
       
    38 }
       
    39 
       
    40 CProcMP3FrameHandler* 
       
    41 CProcMP3FrameHandler::NewLC() 
       
    42 {
       
    43   CProcMP3FrameHandler* self = new (ELeave) CProcMP3FrameHandler();
       
    44   CleanupStack::PushL(self);
       
    45   self->ConstructL();
       
    46   return self;
       
    47 }
       
    48 
       
    49 CProcMP3FrameHandler::~CProcMP3FrameHandler() 
       
    50 {
       
    51 
       
    52     if (iMp3Edit != 0) delete iMp3Edit;
       
    53 
       
    54     if (iMpAud != 0)
       
    55         {
       
    56         
       
    57         delete iMpAud;
       
    58         }
       
    59     if (iTHandle != 0)
       
    60         delete iTHandle;
       
    61 
       
    62     
       
    63 }
       
    64 
       
    65 CProcMP3FrameHandler::CProcMP3FrameHandler() : decInitialized(EFalse)
       
    66 {
       
    67     
       
    68 }
       
    69 
       
    70 void CProcMP3FrameHandler::
       
    71 ConstructL() 
       
    72 {
       
    73 
       
    74     iMp3Edit = CMp3Edit::NewL();
       
    75     
       
    76     iTHandle = new (ELeave) TMpTransportHandle();
       
    77 
       
    78     iMp3Edit->InitTransport(iTHandle);
       
    79 
       
    80     iMpAud = CMPAudDec::NewL();
       
    81     iMpAud->Init(iTHandle);
       
    82     
       
    83 
       
    84 }
       
    85 
       
    86 TBool CProcMP3FrameHandler::
       
    87 ManipulateGainL(const HBufC8* aFrameIn, HBufC8*& aFrameOut, TInt8 aGain) 
       
    88 {
       
    89 
       
    90     aFrameOut = HBufC8::NewLC(aFrameIn->Size());
       
    91 
       
    92     aFrameOut->Des().Copy(aFrameIn->Ptr(), aFrameIn->Size());
       
    93 
       
    94 
       
    95     RArray<TInt> gains;
       
    96     CleanupClosePushL(gains);
       
    97     TInt aMaxGain = 0;
       
    98     TBitStream bs;
       
    99     GetMP3Gains(aFrameOut, gains, aMaxGain, bs);
       
   100     TUint8* globalGains = new (ELeave) TUint8[gains.Count()/2];
       
   101     CleanupStack::PushL(globalGains);
       
   102     TUint* globalGainPos = new (ELeave) TUint[gains.Count()/2];
       
   103     CleanupStack::PushL(globalGainPos);
       
   104 
       
   105 
       
   106     //for (TInt a = 0 ; a < gains.Count(); a++)
       
   107     for (TInt a = 0 ; a < gains.Count()/2; a++)
       
   108         {
       
   109 
       
   110         
       
   111         TInt newGain = aGain*500;
       
   112         newGain = newGain/1500;
       
   113 
       
   114         if (gains[a]+newGain < 0) globalGains[a] = 0;
       
   115         else if(gains[a]+newGain > 255) globalGains[a] = 255;
       
   116         else globalGains[a] = static_cast<TUint8>(gains[a*2]+newGain);
       
   117         globalGainPos[a] = gains[a*2+1];
       
   118         }
       
   119     
       
   120 //SetMPGlobalGains(BitStream *bs, uint8 numGains, uint8 *globalGain, uint32 *gainPos);
       
   121 
       
   122 
       
   123     iMp3Edit->SetMPGlobalGains(&bs, static_cast<TUint8>(gains.Count()/2), globalGains, globalGainPos);
       
   124     
       
   125 
       
   126     //delete[] globalGains;
       
   127     //delete[] globalGainPos;
       
   128     CleanupStack::PopAndDestroy(globalGainPos);
       
   129     CleanupStack::PopAndDestroy(globalGains);
       
   130     CleanupStack::PopAndDestroy(&gains);
       
   131     CleanupStack::Pop(); // aFrameOut
       
   132     return ETrue;
       
   133     
       
   134 }
       
   135 
       
   136 TBool CProcMP3FrameHandler::
       
   137 GetGainL(const HBufC8* aFrame, RArray<TInt>& aGains, TInt& aMaxGain) const
       
   138     {
       
   139     RArray<TInt> gains;
       
   140     TBitStream bs;
       
   141     GetMP3Gains(aFrame, gains, aMaxGain, bs);
       
   142     
       
   143 
       
   144     for (TInt a = 0 ; a < gains.Count(); a++)
       
   145         {
       
   146         TInt ga = gains[a]-120;
       
   147 
       
   148         if (ga < 0) ga = 0;
       
   149 
       
   150         ga = ga*ga;
       
   151         ga = ga/50;
       
   152 
       
   153         aGains.Append(ga);
       
   154         a++;
       
   155         }
       
   156 
       
   157     gains.Reset();
       
   158     aMaxGain = 120;
       
   159     return ETrue;
       
   160     }
       
   161 
       
   162 TBool CProcMP3FrameHandler::
       
   163 GetMP3Gains(const HBufC8* aFrame, RArray<TInt>& aGains, TInt& aMaxGain, TBitStream& aBs) const
       
   164     {
       
   165 
       
   166     TInt16 readBytes, frameBytes, headerBytes = 0;
       
   167 
       
   168     if (!decInitialized)
       
   169         {
       
   170         iMp3Edit->SeekSync(iTHandle, const_cast<unsigned char*>(aFrame->Ptr()), 
       
   171         aFrame->Length(), &readBytes, 
       
   172         &frameBytes, &headerBytes, (uint8) 1);
       
   173         iMpAud->Init(iTHandle);
       
   174         decInitialized = ETrue;
       
   175         }
       
   176 
       
   177 
       
   178     int16 i;
       
   179     uint32 globalGainPos[4];
       
   180     uint8 globalGain[4], nBufs;
       
   181     TBitStream bsTmp;
       
   182 
       
   183     const TInt headerLength = 4;
       
   184 
       
   185     TUint8* buf = const_cast<TUint8*>(aFrame->Right(aFrame->Size()-headerLength).Ptr());
       
   186     BsInit(&aBs, buf, aFrame->Size() - headerLength);
       
   187     BsSaveBufState(&aBs, &bsTmp);
       
   188 
       
   189     //BsSaveBufState(&bsIn, &bsTmp);
       
   190 
       
   191     nBufs = iMp3Edit->GetMPGlobalGains(&bsTmp, iTHandle, globalGain, globalGainPos);
       
   192 
       
   193     for(i = 0; i < nBufs; i++)
       
   194         {
       
   195         TInt gg = globalGain[i];
       
   196         TInt ggp = globalGainPos[i];
       
   197 
       
   198         aGains.Append(gg);
       
   199         aGains.Append(ggp);
       
   200         }
       
   201     aMaxGain = 255;
       
   202 
       
   203     return (EFalse);
       
   204     
       
   205     }
       
   206 
       
   207 TBool CProcMP3FrameHandler::
       
   208 GetNormalizingMargin(const HBufC8* /*aFrame*/, TInt8& /*aMargin*/) const
       
   209 {
       
   210   return (EFalse);
       
   211 }
       
   212 
       
   213 TBool CProcMP3FrameHandler::IsMixingAvailable() const
       
   214 {
       
   215     return EFalse;
       
   216 }
       
   217