videoeditorengine/mp3aacManipLib/MP3Gain/src/mpaud.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   mpaud.cpp - MPEG-1, MPEG-2 LSF and MPEG-2.5 layer III bitstream decoder.
       
    22 
       
    23   Author(s): Juha Ojanpera
       
    24   Copyright (c) 1999-2004 by Nokia Research Center, Speech and Audio Systems.
       
    25   *************************************************************************/
       
    26 
       
    27 /*-- project headers --*/
       
    28 #include "mpaud.h"
       
    29 #include "mpheader.h"
       
    30 #include "mp3tool.h"
       
    31 
       
    32 /**************************************************************************
       
    33   Internal Objects
       
    34   *************************************************************************/
       
    35 
       
    36 /**************************************************************************
       
    37   Title        : L3BitReservoir
       
    38 
       
    39   Purpose      : Layer III bit reservoir subroutine.
       
    40 
       
    41   Usage        : y = L3BitReservoir(mp)
       
    42 
       
    43   Input        : mp - mp3 stream parameters
       
    44 
       
    45   Output       : y - # of bytes discarded from the bit reservoir buffer.
       
    46                      Value -1 indicates that an error has occured during
       
    47                      decoding (invalid bitstream syntax found)
       
    48 
       
    49   Explanation  : Return value -1 should be regarded as a fatal error in the
       
    50                  sense that decoding of current frame should not be continued.
       
    51                  Instead, the decoder should be re-initialized.
       
    52 
       
    53   Author(s)    : Juha Ojanpera
       
    54   *************************************************************************/
       
    55 
       
    56 INLINE int16
       
    57 L3BitReservoir(CMPAudDec *mp)
       
    58 {
       
    59   int16 bytes_to_discard;
       
    60   int16 flush_main;
       
    61   int32 bits_read;
       
    62   TBitStream *br;
       
    63 
       
    64   br = &mp->br;
       
    65 
       
    66   /*------------ Start of bit reservoir processing. ------------------*/
       
    67 
       
    68   if(mp->WasSeeking == FALSE)
       
    69   {
       
    70     /*-- Byte alignment. --*/
       
    71     bits_read = BsGetBitsRead(br);
       
    72     flush_main = (int16) (bits_read & 7);
       
    73     if(flush_main)
       
    74       BsSkipBits(br, (int16) (8 - flush_main));
       
    75 
       
    76     /*
       
    77      * Determine how many bits were left from the previous frame.
       
    78      */
       
    79     if(mp->SkipBr == FALSE)
       
    80     {
       
    81       BsClearBitsRead(br);
       
    82       BsSetBitsRead(br, (mp->PrevSlots << 3) - bits_read);
       
    83     }
       
    84 
       
    85     /*
       
    86      * Determine how many bytes need to be discarded from the previous
       
    87      * frame to find the start of next frame.
       
    88      */
       
    89     bytes_to_discard = (int16) ((BsGetBitsRead(br) >> 3) - mp->side_info->main_data_begin);
       
    90     
       
    91     /*-- Reset the bit reservoir bit counter. --*/
       
    92     BsClearBitsRead(br);
       
    93     
       
    94     /*-- # of slots available for this frame. --*/
       
    95     mp->PrevSlots = (int16) (mp->mpFileFormat->mainDataSlots + mp->side_info->main_data_begin);
       
    96     
       
    97     if(bytes_to_discard < 0)
       
    98     {
       
    99       BsClearBitsRead(br);
       
   100       BsSetBitsRead(br, mp->mpFileFormat->mainDataSlots << 3);
       
   101       mp->SkipBr = TRUE;
       
   102       return (-1);
       
   103     }
       
   104 
       
   105     mp->SkipBr = FALSE;
       
   106 
       
   107     if(bytes_to_discard)
       
   108     {
       
   109       mp->PrevSlots = (int16) (mp->PrevSlots + bytes_to_discard);
       
   110       BsSkipNBits(br, bytes_to_discard << 3);
       
   111     }
       
   112   }
       
   113   else
       
   114   {
       
   115     bytes_to_discard = 0;
       
   116 
       
   117     /*-- # of slots available for this frame. --*/
       
   118     mp->PrevSlots = (int16) (mp->mpFileFormat->mainDataSlots + mp->side_info->main_data_begin);
       
   119 
       
   120     mp->SkipBr = FALSE;
       
   121     mp->WasSeeking = FALSE;
       
   122     
       
   123     if(mp->side_info->main_data_begin)
       
   124       BsRewindNBits(br, mp->side_info->main_data_begin << 3);
       
   125 
       
   126     /*-- Reset the bit reservoir bit counter. --*/
       
   127     BsClearBitsRead(br);
       
   128   }
       
   129   /*-------------- End of bit reservoir processing. ------------------*/
       
   130 
       
   131   return (bytes_to_discard);
       
   132 }
       
   133 
       
   134 /**************************************************************************
       
   135   Title        : DeleteMPAudDec
       
   136 
       
   137   Purpose      : Releases resources allocated to the mp3 decoder core.
       
   138 
       
   139   Usage        : y = DeleteMPAudDec(mpAud)
       
   140 
       
   141   Input        : mpAud - Layer I/II/III decoder core
       
   142 
       
   143   Output       : y     - NULL
       
   144 
       
   145   Author(s)    : Juha Ojanpera
       
   146   *************************************************************************/
       
   147 
       
   148 
       
   149 INLINE uint8
       
   150 GetLayer3Version(TMpTransportHandle *tHandle)
       
   151 {
       
   152   uint8 mp_version;
       
   153   
       
   154   /*
       
   155    * Which version of mp3 ?
       
   156    */
       
   157   if(version(&tHandle->header))
       
   158     mp_version = 1; /*-- MPEG-1   --*/
       
   159   else if(!mp25version(&tHandle->header))
       
   160     mp_version = 2; /*-- MPEG-2   --*/
       
   161   else
       
   162     mp_version = 3; /*-- MPEG-2.5 --*/
       
   163 
       
   164   return (mp_version);
       
   165 }