- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# mp4.c

    00001 /*
    00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
    00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
    00004 **
    00005 ** This program is free software; you can redistribute it and/or modify
    00006 ** it under the terms of the GNU General Public License as published by
    00007 ** the Free Software Foundation; either version 2 of the License, or
    00008 ** (at your option) any later version.
    00009 **
    00010 ** This program is distributed in the hope that it will be useful,
    00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013 ** GNU General Public License for more details.
    00014 **
    00015 ** You should have received a copy of the GNU General Public License
    00016 ** along with this program; if not, write to the Free Software
    00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00018 **
    00019 ** Any non-GPL usage of this software or parts of this software is strictly
    00020 ** forbidden.
    00021 **
    00022 ** Software using this code must display the following message visibly in the
    00023 ** software:
    00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
    00025 ** in, for example, the about-box or help/startup screen.
    00026 **
    00027 ** Commercial non-GPL licensing of this software is possible.
    00028 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
    00029 **
    00030 ** $Id: mp4.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
    00031 **/
    00032
    00033 #include "common.h"
    00034 #include "structs.h"
    00035
    00036 #include <stdlib.h>
    00037
    00038 #include "bits.h"
    00039 #include "mp4.h"
    00040 #include "syntax.h"
    00041
    00042 /* defines if an object type can be decoded by this library or not */
    00043 static uint8_t ObjectTypesTable[32] = {
    00044     0, /*  0 NULL */
    00045 #ifdef MAIN_DEC
    00046     1, /*  1 AAC Main */
    00047 #else
    00048     0, /*  1 AAC Main */
    00049 #endif
    00050     1, /*  2 AAC LC */
    00051 #ifdef SSR_DEC
    00052     1, /*  3 AAC SSR */
    00053 #else
    00054     0, /*  3 AAC SSR */
    00055 #endif
    00056 #ifdef LTP_DEC
    00057     1, /*  4 AAC LTP */
    00058 #else
    00059     0, /*  4 AAC LTP */
    00060 #endif
    00061 #ifdef SBR_DEC
    00062     1, /*  5 SBR */
    00063 #else
    00064     0, /*  5 SBR */
    00065 #endif
    00066     0, /*  6 AAC Scalable */
    00067     0, /*  7 TwinVQ */
    00068     0, /*  8 CELP */
    00069     0, /*  9 HVXC */
    00070     0, /* 10 Reserved */
    00071     0, /* 11 Reserved */
    00072     0, /* 12 TTSI */
    00073     0, /* 13 Main synthetic */
    00074     0, /* 14 Wavetable synthesis */
    00075     0, /* 15 General MIDI */
    00076     0, /* 16 Algorithmic Synthesis and Audio FX */
    00077
    00078     /* MPEG-4 Version 2 */
    00079 #ifdef ERROR_RESILIENCE
    00080     1, /* 17 ER AAC LC */
    00081     0, /* 18 (Reserved) */
    00082 #ifdef LTP_DEC
    00083     1, /* 19 ER AAC LTP */
    00084 #else
    00085     0, /* 19 ER AAC LTP */
    00086 #endif
    00087     0, /* 20 ER AAC scalable */
    00088     0, /* 21 ER TwinVQ */
    00089     0, /* 22 ER BSAC */
    00090 #ifdef LD_DEC
    00091     1, /* 23 ER AAC LD */
    00092 #else
    00093     0, /* 23 ER AAC LD */
    00094 #endif
    00095     0, /* 24 ER CELP */
    00096     0, /* 25 ER HVXC */
    00097     0, /* 26 ER HILN */
    00098     0, /* 27 ER Parametric */
    00099 #else /* No ER defined */
    00100     0, /* 17 ER AAC LC */
    00101     0, /* 18 (Reserved) */
    00102     0, /* 19 ER AAC LTP */
    00103     0, /* 20 ER AAC scalable */
    00104     0, /* 21 ER TwinVQ */
    00105     0, /* 22 ER BSAC */
    00106     0, /* 23 ER AAC LD */
    00107     0, /* 24 ER CELP */
    00108     0, /* 25 ER HVXC */
    00109     0, /* 26 ER HILN */
    00110     0, /* 27 ER Parametric */
    00111 #endif
    00112     0, /* 28 (Reserved) */
    00113     0, /* 29 (Reserved) */
    00114     0, /* 30 (Reserved) */
    00115     0  /* 31 (Reserved) */
    00116 };
    00117
    00118 /* Table 1.6.1 */
    00119 int8_t NEAACDECAPI NeAACDecAudioSpecificConfig(uint8_t *pBuffer,
    00120                                                uint32_t buffer_size,
    00121                                                mp4AudioSpecificConfig *mp4ASC)
    00122 {
    00123     return AudioSpecificConfig2(pBuffer, buffer_size, mp4ASC, NULL);
    00124 }
    00125
    00126 int8_t AudioSpecificConfig2(uint8_t *pBuffer,
    00127                             uint32_t buffer_size,
    00128                             mp4AudioSpecificConfig *mp4ASC,
    00129                             program_config *pce)
    00130 {
    00131     bitfile ld;
    00132     int8_t result = 0;
    00133 #ifdef SBR_DEC
    00134     int8_t bits_to_decode = 0;
    00135 #endif
    00136
    00137     if (pBuffer == NULL)
    00138         return -7;
    00139     if (mp4ASC == NULL)
    00140         return -8;
    00141
    00142     memset(mp4ASC, 0, sizeof(mp4AudioSpecificConfig));
    00143
    00144     faad_initbits(&ld, pBuffer, buffer_size);
    00145     faad_byte_align(&ld);
    00146
    00147     mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(&ld, 5
    00148         DEBUGVAR(1,1,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
    00149
    00150     mp4ASC->samplingFrequencyIndex = (uint8_t)faad_getbits(&ld, 4
    00151         DEBUGVAR(1,2,"parse_audio_decoder_specific_info(): SamplingFrequencyIndex"));
    00152
    00153     mp4ASC->channelsConfiguration = (uint8_t)faad_getbits(&ld, 4
    00154         DEBUGVAR(1,3,"parse_audio_decoder_specific_info(): ChannelsConfiguration"));
    00155
    00156     mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
    00157
    00158     if (ObjectTypesTable[mp4ASC->objectTypeIndex] != 1)
    00159     {
    00160         faad_endbits(&ld);
    00161         return -1;
    00162     }
    00163
    00164     if (mp4ASC->samplingFrequency == 0)
    00165     {
    00166         faad_endbits(&ld);
    00167         return -2;
    00168     }
    00169
    00170     if (mp4ASC->channelsConfiguration > 7)
    00171     {
    00172         faad_endbits(&ld);
    00173         return -3;
    00174     }
    00175
    00176 #if (defined(PS_DEC) || defined(DRM_PS))
    00177     /* check if we have a mono file */
    00178     if (mp4ASC->channelsConfiguration == 1)
    00179     {
    00180         /* upMatrix to 2 channels for implicit signalling of PS */
    00181         mp4ASC->channelsConfiguration = 2;
    00182     }
    00183 #endif
    00184
    00185 #ifdef SBR_DEC
    00186     mp4ASC->sbr_present_flag = -1;
    00187     if (mp4ASC->objectTypeIndex == 5)
    00188     {
    00189         uint8_t tmp;
    00190
    00191         mp4ASC->sbr_present_flag = 1;
    00192         tmp = (uint8_t)faad_getbits(&ld, 4
    00193             DEBUGVAR(1,5,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
    00194         /* check for downsampled SBR */
    00195         if (tmp == mp4ASC->samplingFrequencyIndex)
    00196             mp4ASC->downSampledSBR = 1;
    00197         mp4ASC->samplingFrequencyIndex = tmp;
    00198         if (mp4ASC->samplingFrequencyIndex == 15)
    00199         {
    00200             mp4ASC->samplingFrequency = (uint32_t)faad_getbits(&ld, 24
    00201                 DEBUGVAR(1,6,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
    00202         } else {
    00203             mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
    00204         }
    00205         mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(&ld, 5
    00206             DEBUGVAR(1,7,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
    00207     }
    00208 #endif
    00209
    00210     /* get GASpecificConfig */
    00211     if (mp4ASC->objectTypeIndex == 1 || mp4ASC->objectTypeIndex == 2 ||
    00212         mp4ASC->objectTypeIndex == 3 || mp4ASC->objectTypeIndex == 4 ||
    00213         mp4ASC->objectTypeIndex == 6 || mp4ASC->objectTypeIndex == 7)
    00214     {
    00215         result = GASpecificConfig(&ld, mp4ASC, pce);
    00216
    00217 #ifdef ERROR_RESILIENCE
    00218     } else if (mp4ASC->objectTypeIndex >= ER_OBJECT_START) { /* ER */
    00219         result = GASpecificConfig(&ld, mp4ASC, pce);
    00220         mp4ASC->epConfig = (uint8_t)faad_getbits(&ld, 2
    00221             DEBUGVAR(1,143,"parse_audio_decoder_specific_info(): epConfig"));
    00222
    00223         if (mp4ASC->epConfig != 0)
    00224             result = -5;
    00225 #endif
    00226
    00227     } else {
    00228         result = -4;
    00229     }
    00230
    00231 #ifdef SSR_DEC
    00232     /* shorter frames not allowed for SSR */
    00233     if ((mp4ASC->objectTypeIndex == 4) && mp4ASC->frameLengthFlag)
    00234         return -6;
    00235 #endif
    00236
    00237
    00238 #ifdef SBR_DEC
    00239     bits_to_decode = (int8_t)(buffer_size*8 - faad_get_processed_bits(&ld));
    00240
    00241     if ((mp4ASC->objectTypeIndex != 5) && (bits_to_decode >= 16))
    00242     {
    00243         int16_t syncExtensionType = (int16_t)faad_getbits(&ld, 11
    00244             DEBUGVAR(1,9,"parse_audio_decoder_specific_info(): syncExtensionType"));
    00245
    00246         if (syncExtensionType == 0x2b7)
    00247         {
    00248             mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(&ld, 5
    00249                 DEBUGVAR(1,10,"parse_audio_decoder_specific_info(): extensionAudioObjectType"));
    00250
    00251             if (mp4ASC->objectTypeIndex == 5)
    00252             {
    00253                 mp4ASC->sbr_present_flag = (uint8_t)faad_get1bit(&ld
    00254                     DEBUGVAR(1,11,"parse_audio_decoder_specific_info(): sbr_present_flag"));
    00255
    00256                 if (mp4ASC->sbr_present_flag)
    00257                 {
    00258                     uint8_t tmp;
    00259                     tmp = (uint8_t)faad_getbits(&ld, 4
    00260                         DEBUGVAR(1,12,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
    00261
    00262                     /* check for downsampled SBR */
    00263                     if (tmp == mp4ASC->samplingFrequencyIndex)
    00264                         mp4ASC->downSampledSBR = 1;
    00265                     mp4ASC->samplingFrequencyIndex = tmp;
    00266
    00267                     if (mp4ASC->samplingFrequencyIndex == 15)
    00268                     {
    00269                         mp4ASC->samplingFrequency = (uint32_t)faad_getbits(&ld, 24
    00270                             DEBUGVAR(1,13,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
    00271                     } else {
    00272                         mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
    00273                     }
    00274                 }
    00275             }
    00276         }
    00277     }
    00278
    00279     /* no SBR signalled, this could mean either implicit signalling or no SBR in this file */
    00280     /* MPEG specification states: assume SBR on files with samplerate <= 24000 Hz */
    00281     if (mp4ASC->sbr_present_flag == -1)
    00282     {
    00283         if (mp4ASC->samplingFrequency <= 24000)
    00284         {
    00285             mp4ASC->samplingFrequency *= 2;
    00286             mp4ASC->forceUpSampling = 1;
    00287         } else /* > 24000*/ {
    00288             mp4ASC->downSampledSBR = 1;
    00289         }
    00290     }
    00291 #endif
    00292
    00293     faad_endbits(&ld);
    00294
    00295     return result;
    00296 }