videoeditorengine/mp3aacManipLib/src/Mp3API.cpp
branchRCL_3
changeset 3 e0b5df5c0969
parent 0 951a5db380a0
child 5 4c409de21d23
equal deleted inserted replaced
0:951a5db380a0 3:e0b5df5c0969
     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 "Mp3API.h"
       
    22 #include "mpif.h"
       
    23 #include "mpaud.h"
       
    24 #include "mpheader.h"
       
    25 #include "mp3tool.h"
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 // Exception handler for FLOATS
       
    31 void handler(TExcType /*aType*/)
       
    32 {
       
    33 #ifndef __WINS__
       
    34 //    CMp3Mix::WriteL(_L("Exception cought!!!"));
       
    35 #endif
       
    36 
       
    37 }
       
    38 
       
    39 //const int16 Kmp3BufSize = 8;
       
    40 
       
    41 EXPORT_C CMp3Edit* CMp3Edit::NewL()
       
    42     {
       
    43 
       
    44     CMp3Edit* self = new (ELeave) CMp3Edit();
       
    45     CleanupStack::PushL(self);
       
    46     self->ConstructL();
       
    47     CleanupStack::Pop(self);
       
    48     return self;
       
    49 
       
    50     }
       
    51 
       
    52 void CMp3Edit::ConstructL()
       
    53     {
       
    54     
       
    55       }
       
    56 
       
    57 
       
    58 EXPORT_C CMp3Edit::~CMp3Edit()
       
    59     {
       
    60 
       
    61     }
       
    62 
       
    63 CMp3Edit::CMp3Edit()
       
    64     {
       
    65 
       
    66     }
       
    67 
       
    68 
       
    69 EXPORT_C uint32
       
    70 CMp3Edit::FileLengthInMs(TMpTransportHandle *tHandle, int32 fileSize) const
       
    71     {
       
    72     return MP_FileLengthInMs(tHandle, fileSize);
       
    73     }
       
    74 
       
    75 EXPORT_C int32
       
    76 CMp3Edit::GetSeekOffset(TMpTransportHandle *tHandle, int32 seekPos) const
       
    77     {
       
    78     return MP_GetSeekOffset(tHandle, seekPos);
       
    79     }
       
    80 
       
    81 EXPORT_C int32
       
    82 CMp3Edit::GetFrameTime(TMpTransportHandle *tHandle) const
       
    83     {
       
    84     return MP_GetFrameTime(tHandle);
       
    85     }
       
    86 
       
    87 EXPORT_C void 
       
    88 CMp3Edit::InitTransport(TMpTransportHandle *tHandle) const
       
    89     {
       
    90 
       
    91     mpInitTransport(tHandle);
       
    92     }
       
    93 
       
    94 EXPORT_C int16
       
    95 CMp3Edit::SeekSync(TMpTransportHandle *tHandle, uint8 *syncBuf, 
       
    96             uint32 syncBufLen, int16 *readBytes, 
       
    97             int16 *frameBytes, int16 *headerBytes,
       
    98             uint8 initMode) const
       
    99 
       
   100     {
       
   101 
       
   102     return MP_SeekSync(tHandle, syncBuf, 
       
   103             syncBufLen, readBytes, 
       
   104             frameBytes, headerBytes,
       
   105             initMode);
       
   106     }
       
   107 
       
   108 EXPORT_C int16
       
   109 CMp3Edit::FreeMode(TMpTransportHandle *tHandle, uint8 *syncBuf, 
       
   110             uint32 syncBufLen, int16 *readBytes, 
       
   111             int16 *frameBytes, int16 *headerBytes) const
       
   112     {
       
   113     return MP_FreeMode(tHandle, syncBuf, 
       
   114             syncBufLen, readBytes, 
       
   115             frameBytes, headerBytes);
       
   116     }
       
   117 
       
   118 EXPORT_C int16
       
   119 CMp3Edit::EstimateBitrate(TMpTransportHandle *tHandle, uint8 isVbr) const
       
   120     {
       
   121     return MP_EstimateBitrate(tHandle, isVbr) ;
       
   122 
       
   123     }
       
   124 
       
   125 
       
   126 
       
   127 /*
       
   128  * Saves modified 'global_gain' bitstream elements to specified layer I/II/III data buffer.
       
   129  */
       
   130 EXPORT_C void
       
   131 CMp3Edit::SetMPGlobalGains(TBitStream *bs, uint8 numGains, uint8 *globalGain, uint32 *gainPos)
       
   132 {
       
   133   int16 i;
       
   134 
       
   135   /*-- Store the gain element back to bitstream. --*/
       
   136   for(i = 0; i < numGains; i++)
       
   137   {
       
   138     BsSkipNBits(bs, gainPos[i]); 
       
   139     BsPutBits(bs, 8, globalGain[i]);
       
   140   }
       
   141 }
       
   142 
       
   143 
       
   144 
       
   145 EXPORT_C uint8
       
   146 CMp3Edit::GetMPGlobalGains(TBitStream *bs, TMpTransportHandle *tHandle, uint8 *globalGain, uint32 *gainPos)
       
   147 {
       
   148   uint32 bufBitOffset;
       
   149   uint8 numGains, nVersion, nChannels;
       
   150 
       
   151   numGains = 0;
       
   152   nVersion = (uint8) version(&tHandle->header);
       
   153   nChannels =(uint8)  channels(&tHandle->header);
       
   154 
       
   155   bufBitOffset = BsGetBitsRead(bs);
       
   156 
       
   157   /*-- Get the gain for each channel and granule. --*/
       
   158   switch(nVersion)
       
   159   {
       
   160     /*-- MPEG-1. --*/
       
   161     case 1:
       
   162       switch(nChannels)
       
   163       {
       
   164         /*
       
   165          * Mono frame consists of 2 granules.
       
   166          */
       
   167         case 1:
       
   168           numGains = 2;
       
   169 
       
   170           /*-- Left channel, granules 0 and granule 1. --*/
       
   171           BsSkipNBits(bs, 0x27);
       
   172           gainPos[0] = BsGetBitsRead(bs) - bufBitOffset;
       
   173           globalGain[0] = (uint8) BsGetBits(bs, 8);
       
   174           BsSkipNBits(bs, 0x33);
       
   175           gainPos[1] = 0x33;
       
   176           globalGain[1] = (uint8) BsGetBits(bs, 8);
       
   177           break;
       
   178 
       
   179         /*
       
   180          * Two channels, 2 granules.
       
   181          */
       
   182         case 2:
       
   183           numGains = 4;
       
   184 
       
   185           /*-- Left and right channel, granule 0. --*/
       
   186           BsSkipNBits(bs, 0x29);
       
   187           gainPos[0] = BsGetBitsRead(bs) - bufBitOffset;
       
   188           globalGain[0] = (uint8) BsGetBits(bs, 8);
       
   189           BsSkipNBits(bs, 0x33); 
       
   190           gainPos[1] = 0x33;
       
   191           globalGain[1] = (uint8) BsGetBits(bs, 8);
       
   192 
       
   193           /*-- Left and right channel, granule 1. --*/
       
   194           BsSkipNBits(bs, 0x33); 
       
   195           gainPos[2] = 0x33;
       
   196           globalGain[2] = (uint8) BsGetBits(bs, 8);
       
   197           BsSkipNBits(bs, 0x33); 
       
   198           gainPos[3] = 0x33;
       
   199           globalGain[3] = (uint8) BsGetBits(bs, 8);
       
   200           break;
       
   201       }
       
   202       break;
       
   203 
       
   204     /*-- MPEG-2 LSF and MPEG-2.5. --*/
       
   205     default:
       
   206       switch(nChannels)
       
   207       {
       
   208         /*
       
   209          * Mono channel, 1 granule.
       
   210          */
       
   211         case 1:
       
   212           numGains = 1;
       
   213           BsSkipNBits(bs, 0x1E); 
       
   214           gainPos[0] = BsGetBitsRead(bs) - bufBitOffset;
       
   215           globalGain[0] = (uint8) BsGetBits(bs, 8);
       
   216           break;
       
   217 
       
   218         /*
       
   219          * Two channels, 1 granule.
       
   220          */
       
   221         case 2:
       
   222           numGains = 2;
       
   223           BsSkipNBits(bs, 0x1F); 
       
   224           gainPos[0] = BsGetBitsRead(bs) - bufBitOffset;
       
   225           globalGain[0] = (uint8) BsGetBits(bs, 8);
       
   226           BsSkipNBits(bs, 0x37); 
       
   227           gainPos[1] = 0x37;
       
   228           globalGain[1] = (uint8) BsGetBits(bs, 8);
       
   229           break;
       
   230       }
       
   231       break;
       
   232   }
       
   233 
       
   234 
       
   235   return (numGains);
       
   236 }
       
   237 
       
   238 EXPORT_C CMPAudDec* CMPAudDec::NewL()
       
   239     {
       
   240     CMPAudDec* self = new (ELeave) CMPAudDec();
       
   241     CleanupStack::PushL(self);
       
   242     self->ConstructL();
       
   243     CleanupStack::Pop(self);
       
   244     return self;
       
   245 
       
   246 
       
   247     }
       
   248     
       
   249 CMPAudDec::CMPAudDec()
       
   250     {
       
   251 
       
   252 
       
   253     }
       
   254 
       
   255 EXPORT_C CMPAudDec::~CMPAudDec()
       
   256     {
       
   257 
       
   258   
       
   259     int16 i;
       
   260 
       
   261     /* --Scalefactors. --*/
       
   262     if (frame->scale_factors != 0) delete[] frame->scale_factors;
       
   263     
       
   264     /*-- Quantized samples. --*/
       
   265     if (frame->quant != 0) delete[] frame->quant;
       
   266 
       
   267     /*-- Huffman codebooks. --*/
       
   268     if (huffman != 0) delete[] huffman;
       
   269 
       
   270     /*-- Layer III side info. --*/
       
   271     if(side_info)
       
   272         {
       
   273         for(i = 0; i < MAX_CHANNELS; i++)
       
   274             {
       
   275             int16 j;
       
   276 
       
   277             if(side_info->ch_info[i])
       
   278                 {
       
   279                 if (side_info->ch_info[i]->scale_fac != 0) delete side_info->ch_info[i]->scale_fac;
       
   280           
       
   281                 for(j = 0; j < 2; j++)
       
   282                     if (side_info->ch_info[i]->gr_info[j] != 0) delete side_info->ch_info[i]->gr_info[j];
       
   283                 }      
       
   284             if (side_info->ch_info[i] != 0) delete side_info->ch_info[i];
       
   285             }
       
   286     
       
   287         if (side_info->s_mode_long != 0) delete[] side_info->s_mode_long;
       
   288     
       
   289         for(i = 0; i < 3; i++)
       
   290             if (side_info->s_mode_short[i] != 0) delete[] side_info->s_mode_short[i];
       
   291     
       
   292         if (side_info != 0) delete side_info;
       
   293         }
       
   294   
       
   295     if (frame != 0) delete frame;
       
   296     if (bitReserv != 0) delete[] bitReserv;
       
   297 
       
   298     
       
   299 
       
   300     }
       
   301 
       
   302 void CMPAudDec::ConstructL()
       
   303     {
       
   304      
       
   305     int16 i, j, groups;
       
   306 
       
   307     /*-- Create handle. --*/
       
   308     
       
   309     /*-- Create bit reservoir buffer. --*/
       
   310     bitReserv = new (ELeave) uint8[4 * MAX_BITRESER_SIZE];
       
   311     ZERO_MEMORY(bitReserv, 4 * MAX_BITRESER_SIZE * sizeof(uint8));
       
   312 
       
   313     BsInit(&br, bitReserv, 4 * MAX_BITRESER_SIZE);
       
   314   
       
   315     /*-- Create frame parameters. --*/
       
   316     frame = new (ELeave) TMPEG_Frame;
       
   317   
       
   318     /*-- Create side info parameters. --*/
       
   319     side_info = CIII_Side_Info::NewL();
       
   320   
       
   321     /*-- Create scalefactors. --*/
       
   322     frame->scale_factors = new (ELeave) uint8[MAX_CHANNELS * SBLIMIT * 3];
       
   323     ZERO_MEMORY(frame->scale_factors, MAX_CHANNELS * SBLIMIT * 3 * sizeof(uint8)); 
       
   324 
       
   325 
       
   326     /*-- Create L3 side info. --*/
       
   327     for(i = 0; i < MAX_CHANNELS; i++)
       
   328         {
       
   329     
       
   330         side_info->ch_info[i] = CIII_Channel_Info::NewL();
       
   331     
       
   332         for(j = 0; j < 2; j++)
       
   333             {
       
   334                 side_info->ch_info[i]->gr_info[j] = new (ELeave) TGranule_Info;
       
   335       
       
   336             }
       
   337         }
       
   338   
       
   339     side_info->s_mode_long = new (ELeave) StereoMode[22];
       
   340     
       
   341     ZERO_MEMORY(side_info->s_mode_long, 22 * sizeof(StereoMode));
       
   342 
       
   343     for(i = 0; i < 3; i++)
       
   344         {
       
   345         side_info->s_mode_short[i] =  new (ELeave) StereoMode[13]; //GET_CHUNK(13 * sizeof(StereoMode));
       
   346         ZERO_MEMORY(side_info->s_mode_short[i], 13 * sizeof(StereoMode));
       
   347         }
       
   348   
       
   349     /*-- Initialize scalefactors. --*/
       
   350     for(i = j = 0; i < MAX_CHANNELS; i++)
       
   351         {
       
   352         uint8 idx[] = {0, 23, 36, 49, 62, 85, 98, 111};
       
   353 
       
   354         CIII_Scale_Factors *scale_fac;
       
   355 
       
   356         side_info->ch_info[i]->scale_fac = new (ELeave) CIII_Scale_Factors();
       
   357       
       
   358         scale_fac = side_info->ch_info[i]->scale_fac;
       
   359 
       
   360         scale_fac->scalefac_long     = frame->scale_factors + idx[j++];
       
   361         scale_fac->scalefac_short[0] = frame->scale_factors + idx[j++];
       
   362         scale_fac->scalefac_short[1] = frame->scale_factors + idx[j++];
       
   363         scale_fac->scalefac_short[2] = frame->scale_factors + idx[j++];
       
   364     }
       
   365 
       
   366     groups = MAX_MONO_SAMPLES * MAX_CHANNELS;
       
   367 
       
   368     /*-- Create buffer for quantized samples. --*/
       
   369     frame->quant = new (ELeave) int16[groups + 10];//(int16 *) GET_CHUNK((groups + 10) * sizeof(int16));
       
   370 
       
   371     ZERO_MEMORY(frame->quant, (groups + 10) * sizeof(int16));
       
   372 
       
   373     for(i = 0; i < MAX_CHANNELS; i++)
       
   374         frame->ch_quant[i] = frame->quant + i * MAX_MONO_SAMPLES;
       
   375 
       
   376     /*-- Create Huffman handle. --*/
       
   377     huffman = new (ELeave) CHuffman[33];// *) GET_CHUNK(33 * sizeof(CHuffman)); 
       
   378     
       
   379     /*-- Get the Huffman codebooks. --*/
       
   380     InitL3Huffman(huffman);
       
   381 
       
   382     }
       
   383 
       
   384     /*-- Initializes MPEG Layer I/II/III decoder handle. --*/
       
   385 EXPORT_C void
       
   386 CMPAudDec::Init(TMpTransportHandle *aMpFileFormat)
       
   387 {
       
   388   mpFileFormat = aMpFileFormat;
       
   389 
       
   390   /*-- MPEG-1 --*/
       
   391   if(version(&mpFileFormat->header) == MPEG_AUDIO_ID)
       
   392     {
       
   393     side_info->lsf = FALSE;
       
   394     side_info->max_gr = 2;
       
   395     }
       
   396 
       
   397     /*-- MPEG-2 LSF or MPEG-2.5 --*/
       
   398     else
       
   399     {
       
   400         side_info->lsf = TRUE;
       
   401         side_info->max_gr = 1;
       
   402     }
       
   403 
       
   404     /*-- Get the scalefactor band related parameters. --*/
       
   405     III_SfbDataInit(side_info->sfbData, &mpFileFormat->header);
       
   406 }
       
   407 
       
   408 
       
   409