multimediacommscontroller/mmccshared/src/streamformatter.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2004-2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 *
       
    14 * Description:    PayloadFormat plugin capable to read RTP payload containing
       
    15 *                AMR audio.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include "streamformatter.h"
       
    24 
       
    25 // CONSTANTS
       
    26 const TInt KBitsIn4Bytes = 32;
       
    27 const TUint8 KZeroMask = 0xFF;
       
    28 const TInt KBitIndex7 = 7;
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // TStreamEncoder::Initialize
       
    34 // Initialize the stream encoder.
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 void TStreamEncoder::Initialize( TUint8* aBuffer, TInt aByteIndex, TInt aBitIndex )
       
    38     {
       
    39     iBuffer = aBuffer;
       
    40     iByteIndex = aByteIndex;
       
    41     iBitIndex = aBitIndex;
       
    42     };
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // TStreamEncoder::Encode
       
    46 // Put a number of bit values ( up to 32 bits ) into the bit-stream buffer 
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void TStreamEncoder::Encode( TUint32 aValue, TInt aBitCount )
       
    50     {
       
    51     // Bit count has to be between 1 and 32 so if number of bits is out of range
       
    52     // return from here
       
    53     if ( aBitCount < 1 || aBitCount > KBitsIn4Bytes )
       
    54         {
       
    55         return;
       
    56         }
       
    57         
       
    58     TInt bitCount = aBitCount;
       
    59     TInt vacantBits;
       
    60     TInt bitsWritten;
       
    61     TUint32 oldValue;
       
    62     TUint32 newValue;
       
    63     TUint8* p;
       
    64 
       
    65     while ( bitCount > 0 )
       
    66         {
       
    67         p = iBuffer + iByteIndex;
       
    68         vacantBits = KBitsIn1Byte - iBitIndex;
       
    69 
       
    70         if ( bitCount >= vacantBits )
       
    71             {
       
    72             // get old value and zero out vacant bits
       
    73             oldValue = ( *p ) & ( KZeroMask << vacantBits );
       
    74             
       
    75             newValue = ( ( bitCount - vacantBits ) < KBitsIn4Bytes ) ?
       
    76                  TUint8( ( aValue >> ( bitCount - vacantBits ) ) ) : 0;
       
    77                  
       
    78             bitsWritten = vacantBits;
       
    79             }
       
    80         else
       
    81             {
       
    82             oldValue = ( *p ) 
       
    83                 & ( ( KZeroMask << vacantBits ) 
       
    84                 | ( KZeroMask >> ( KBitsIn1Byte - vacantBits + bitCount ) ) );
       
    85             newValue = TUint8( ( aValue << ( vacantBits-bitCount ) ) );
       
    86             bitsWritten = bitCount;
       
    87             }
       
    88         
       
    89         // zero out unwanted bits
       
    90         newValue &= ( KZeroMask ) >> ( iBitIndex );
       
    91          // write the byte
       
    92         *p = TUint8( ( oldValue | newValue ) );
       
    93 
       
    94         // update variables
       
    95         bitCount -= bitsWritten;
       
    96         iBitIndex += bitsWritten;
       
    97         if ( iBitIndex > KBitIndex7 )
       
    98             {
       
    99             iBitIndex &= KBitIndex7;
       
   100             iByteIndex += 1;
       
   101             }
       
   102         }
       
   103     };
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // TStreamEncoder::Encode
       
   107 // Put a number of bit values into the bit-stream buffer at current location.
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void TStreamEncoder::Encode( const TUint8* aFromBuffer, TInt aFromByteIndex, 
       
   111         TInt aFromBitIndex, TInt aBitCount )
       
   112     {
       
   113     if ( aFromBitIndex == 0 && iBitIndex == 0 )
       
   114         {
       
   115         // Mem::Copy is applicable
       
   116         TUint8* desBuffer = iBuffer + iByteIndex;
       
   117         const TUint8* srcBuffer = aFromBuffer + aFromByteIndex;
       
   118         
       
   119         TInt byteLen = aBitCount / KBitsIn1Byte;
       
   120         TInt bitsLeft = aBitCount % KBitsIn1Byte;
       
   121         
       
   122         Mem::Copy( desBuffer, srcBuffer, byteLen );
       
   123         iByteIndex += byteLen;
       
   124         
       
   125         if ( bitsLeft != 0 )
       
   126             {
       
   127             TUint32 value;
       
   128             TStreamDecoder decoder;
       
   129             decoder.Initialize( aFromBuffer, aFromByteIndex + byteLen, aFromBitIndex );
       
   130             value = decoder.Decode( bitsLeft );
       
   131             Encode( value, bitsLeft );
       
   132             }
       
   133             
       
   134         return;
       
   135         }
       
   136 
       
   137     // Note that Mem::Copy is not applicable, have to do bit shifting
       
   138     TInt bitFilled = 0;
       
   139     TUint32 value;
       
   140     TStreamDecoder decoder;
       
   141     decoder.Initialize( aFromBuffer, aFromByteIndex, aFromBitIndex );
       
   142 
       
   143     while ( bitFilled < aBitCount )
       
   144         {
       
   145         if ( aBitCount - bitFilled < KBitsIn4Bytes )
       
   146             {
       
   147             value = decoder.Decode( aBitCount - bitFilled );
       
   148             Encode( value, ( aBitCount - bitFilled ) );
       
   149             bitFilled = aBitCount;
       
   150             }
       
   151         else
       
   152             {
       
   153             value = decoder.Decode( KBitsIn4Bytes );
       
   154             Encode( value, KBitsIn4Bytes );
       
   155             bitFilled += KBitsIn4Bytes;
       
   156             }
       
   157         }
       
   158     };
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // TStreamDecoder::Initialize
       
   162 // Initialize the stream decoder.
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void TStreamDecoder::Initialize( const TUint8* aBuffer, TInt aByteIndex, 
       
   166         TInt aBitIndex )
       
   167     {
       
   168     iBuffer = aBuffer;
       
   169     iByteIndex = aByteIndex;
       
   170     iBitIndex = aBitIndex;
       
   171     };
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // TStreamDecoder::Decode
       
   175 // Get a number of bit values ( up to 32 bits ) from the bit-stream buffer at 
       
   176 // current location.
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 TUint32 TStreamDecoder::Decode( TInt aBitCount )
       
   180     {
       
   181     // Bit count has to be between 1 and 32 so if number of bits is out of range
       
   182     // return from here
       
   183     if ( aBitCount < 1 || aBitCount > KBitsIn4Bytes )
       
   184         {
       
   185         return 0;
       
   186         }
       
   187 
       
   188     TInt bitCount = aBitCount;
       
   189     TInt availableBits;
       
   190     TInt bitsRead;
       
   191     TUint8* p;
       
   192     TUint32 temp;
       
   193     TUint32 value = 0;
       
   194 
       
   195     while ( bitCount > 0 )
       
   196         {
       
   197         p = const_cast<TUint8*>( iBuffer ) + iByteIndex;
       
   198         availableBits = KBitsIn1Byte - iBitIndex;
       
   199 
       
   200         // read one byte and zero out unwanted bits
       
   201         temp = ( *p ) & ( KZeroMask >> iBitIndex );
       
   202 
       
   203         // update return value
       
   204         if ( bitCount >= availableBits )
       
   205             {
       
   206             value = TUint8( temp ) | ( value<<availableBits );
       
   207             bitsRead = availableBits;
       
   208             }
       
   209         else
       
   210             {
       
   211             value = TUint8( ( temp >> ( availableBits - bitCount ) ) ) | ( value << bitCount );
       
   212             bitsRead = bitCount;
       
   213             }
       
   214 
       
   215         // update variables
       
   216         bitCount -= bitsRead;
       
   217         iBitIndex += bitsRead;
       
   218         if ( iBitIndex > KBitIndex7 )
       
   219             {
       
   220             iBitIndex &= KBitIndex7;
       
   221             iByteIndex += 1;
       
   222             }
       
   223         }
       
   224         
       
   225     return value;
       
   226     };
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // TStreamDecoder::Decode
       
   230 // Get a number of byte values from the bit-stream buffer at current location.
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void TStreamDecoder::Decode( TUint8* aToBuffer, TInt aToByteIndex, 
       
   234         TInt aToBitIndex, TInt aBitCount )
       
   235     {
       
   236     if ( aToBitIndex == 0 && iBitIndex == 0 )
       
   237         {
       
   238         // Mem::Copy is applicable
       
   239         TUint8* desBuffer = aToBuffer + aToByteIndex;
       
   240         const TUint8* srcBuffer = iBuffer + iByteIndex;
       
   241         
       
   242         TInt byteLen = aBitCount / KBitsIn1Byte;
       
   243         TInt bitsLeft = aBitCount % KBitsIn1Byte;
       
   244         
       
   245         Mem::Copy( desBuffer, srcBuffer, byteLen );
       
   246         iByteIndex += byteLen;
       
   247         
       
   248         if ( bitsLeft != 0 )
       
   249             {
       
   250             TUint32 value;
       
   251             TStreamEncoder encoder;
       
   252             encoder.Initialize( aToBuffer, aToByteIndex + byteLen, aToBitIndex );
       
   253             value = Decode( bitsLeft );
       
   254             encoder.Encode( value, bitsLeft );
       
   255             }
       
   256             
       
   257         return;
       
   258         }
       
   259 
       
   260     // Note that Mem::Copy is not applicable, have to do bit shifting
       
   261     TInt bitFilled = 0;
       
   262     TUint32 value;
       
   263     TStreamEncoder encoder;
       
   264     encoder.Initialize( aToBuffer, aToByteIndex, aToBitIndex );
       
   265 
       
   266     while ( bitFilled < aBitCount )
       
   267         {
       
   268         // Check if number of remaining bits is less than 32.
       
   269         if ( aBitCount - bitFilled < KBitsIn4Bytes )
       
   270             {
       
   271             value = Decode( aBitCount - bitFilled );
       
   272             encoder.Encode( value, ( aBitCount - bitFilled ) );
       
   273             bitFilled = aBitCount;
       
   274             }
       
   275         else
       
   276             {
       
   277             value = Decode( KBitsIn4Bytes );
       
   278             encoder.Encode( value, KBitsIn4Bytes );
       
   279             bitFilled += KBitsIn4Bytes;
       
   280             }
       
   281         }
       
   282     };
       
   283 
       
   284 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   285 
       
   286 //  End of File