"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/thirdparty/faad2-2.0/common/mp4av/aac.cpp" (31 May 2008, 4218 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * The contents of this file are subject to the Mozilla Public
    3  * License Version 1.1 (the "License"); you may not use this file
    4  * except in compliance with the License. You may obtain a copy of
    5  * the License at http://www.mozilla.org/MPL/
    6  * 
    7  * Software distributed under the License is distributed on an "AS
    8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
    9  * implied. See the License for the specific language governing
   10  * rights and limitations under the License.
   11  * 
   12  * The Original Code is MPEG4IP.
   13  * 
   14  * The Initial Developer of the Original Code is Cisco Systems Inc.
   15  * Portions created by Cisco Systems Inc. are
   16  * Copyright (C) Cisco Systems Inc. 2000-2002.  All Rights Reserved.
   17  * 
   18  * Contributor(s): 
   19  *      Dave Mackie     dmackie@cisco.com
   20  */
   21 
   22 /* 
   23  * Notes:
   24  *  - file formatted with tabstops == 4 spaces 
   25  */
   26 
   27 #include <mp4av_common.h>
   28 
   29 /*
   30  * AAC Config in ES:
   31  *
   32  * AudioObjectType          5 bits
   33  * samplingFrequencyIndex   4 bits
   34  * if (samplingFrequencyIndex == 0xF)
   35  *  samplingFrequency   24 bits 
   36  * channelConfiguration     4 bits
   37  * GA_SpecificConfig
   38  *  FrameLengthFlag         1 bit 1024 or 960
   39  *  DependsOnCoreCoder      1 bit (always 0)
   40  *  ExtensionFlag           1 bit (always 0)
   41  */
   42 
   43 extern "C" u_int8_t MP4AV_AacConfigGetSamplingRateIndex(u_int8_t* pConfig)
   44 {
   45     return ((pConfig[0] << 1) | (pConfig[1] >> 7)) & 0xF;
   46 }
   47 
   48 extern "C" u_int32_t MP4AV_AacConfigGetSamplingRate(u_int8_t* pConfig)
   49 {
   50     u_int8_t index =
   51         MP4AV_AacConfigGetSamplingRateIndex(pConfig);
   52 
   53     if (index == 0xF) {
   54         return (pConfig[1] & 0x7F) << 17
   55             | pConfig[2] << 9
   56             | pConfig[3] << 1
   57             | (pConfig[4] >> 7);
   58     }
   59     return AdtsSamplingRates[index];
   60 }
   61 
   62 extern "C" u_int16_t MP4AV_AacConfigGetSamplingWindow(u_int8_t* pConfig)
   63 {
   64     u_int8_t adjust = 0;
   65 
   66     if (MP4AV_AacConfigGetSamplingRateIndex(pConfig) == 0xF) {
   67         adjust = 3;
   68     }
   69 
   70     if ((pConfig[1 + adjust] >> 2) & 0x1) {
   71         return 960;
   72     }
   73     return 1024;
   74 }
   75 
   76 extern "C" u_int8_t MP4AV_AacConfigGetChannels(u_int8_t* pConfig)
   77 {
   78     u_int8_t adjust = 0;
   79 
   80     if (MP4AV_AacConfigGetSamplingRateIndex(pConfig) == 0xF) {
   81         adjust = 3;
   82     }
   83     return (pConfig[1 + adjust] >> 3) & 0xF;
   84 }
   85 
   86 extern "C" bool MP4AV_AacGetConfigurationFromAdts(
   87     u_int8_t** ppConfig,
   88     u_int32_t* pConfigLength,
   89     u_int8_t* pHdr)
   90 {
   91     return MP4AV_AacGetConfiguration(
   92         ppConfig,
   93         pConfigLength,
   94         MP4AV_AdtsGetProfile(pHdr),
   95         MP4AV_AdtsGetSamplingRate(pHdr),
   96         MP4AV_AdtsGetChannels(pHdr));
   97 }
   98 
   99 extern "C" bool MP4AV_AacGetConfiguration(
  100     u_int8_t** ppConfig,
  101     u_int32_t* pConfigLength,
  102     u_int8_t profile,
  103     u_int32_t samplingRate,
  104     u_int8_t channels)
  105 {
  106     /* create the appropriate decoder config */
  107 
  108     u_int8_t* pConfig = (u_int8_t*)malloc(2);
  109 
  110     if (pConfig == NULL) {
  111         return false;
  112     }
  113 
  114     u_int8_t samplingRateIndex = 
  115         MP4AV_AdtsFindSamplingRateIndex(samplingRate);
  116 
  117     pConfig[0] =
  118         ((profile + 1) << 3) | ((samplingRateIndex & 0xe) >> 1);
  119     pConfig[1] =
  120         ((samplingRateIndex & 0x1) << 7) | (channels << 3);
  121 
  122     /* LATER this option is not currently used in MPEG4IP
  123     if (samplesPerFrame == 960) {
  124         pConfig[1] |= (1 << 2);
  125     }
  126     */
  127 
  128     *ppConfig = pConfig;
  129     *pConfigLength = 2;
  130 
  131     return true;
  132 }
  133 
  134 
  135 extern "C" bool MP4AV_AacGetConfiguration_SBR(
  136     u_int8_t** ppConfig,
  137     u_int32_t* pConfigLength,
  138     u_int8_t profile,
  139     u_int32_t samplingRate,
  140     u_int8_t channels)
  141 {
  142     /* create the appropriate decoder config */
  143 
  144     u_int8_t* pConfig = (u_int8_t*)malloc(5);
  145     pConfig[0] = 0;
  146     pConfig[1] = 0;
  147     pConfig[2] = 0;
  148     pConfig[3] = 0;
  149     pConfig[4] = 0;
  150 
  151     if (pConfig == NULL) {
  152         return false;
  153     }
  154 
  155     u_int8_t samplingRateIndex = 
  156         MP4AV_AdtsFindSamplingRateIndex(samplingRate);
  157 
  158     pConfig[0] =
  159         ((profile + 1) << 3) | ((samplingRateIndex & 0xe) >> 1);
  160     pConfig[1] =
  161         ((samplingRateIndex & 0x1) << 7) | (channels << 3);
  162 
  163     /* pConfig[0] & pConfig[1] now contain the backward compatible
  164        AudioSpecificConfig
  165     */
  166 
  167     /* SBR stuff */
  168     const u_int16_t syncExtensionType = 0x2B7;
  169     u_int8_t extensionSamplingRateIndex = 
  170         MP4AV_AdtsFindSamplingRateIndex(2*samplingRate);
  171 
  172     pConfig[2] = (syncExtensionType >> 3) & 0xFF;
  173     pConfig[3] = ((syncExtensionType & 0x7) << 5) | 5 /* ext ot id */;
  174     pConfig[4] = ((1 & 0x1) << 7) | (extensionSamplingRateIndex << 3);
  175 
  176     *ppConfig = pConfig;
  177     *pConfigLength = 5;
  178 
  179     return true;
  180 }