• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/mpeg4audio.c

Go to the documentation of this file.
00001 /*
00002  * MPEG-4 Audio common code
00003  * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
00004  * Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "get_bits.h"
00024 #include "put_bits.h"
00025 #include "mpeg4audio.h"
00026 
00033 static int parse_config_ALS(GetBitContext *gb, MPEG4AudioConfig *c)
00034 {
00035     if (get_bits_left(gb) < 112)
00036         return -1;
00037 
00038     if (get_bits_long(gb, 32) != MKBETAG('A','L','S','\0'))
00039         return -1;
00040 
00041     // override AudioSpecificConfig channel configuration and sample rate
00042     // which are buggy in old ALS conformance files
00043     c->sample_rate = get_bits_long(gb, 32);
00044 
00045     // skip number of samples
00046     skip_bits_long(gb, 32);
00047 
00048     // read number of channels
00049     c->chan_config = 0;
00050     c->channels    = get_bits(gb, 16) + 1;
00051 
00052     return 0;
00053 }
00054 
00055 const int ff_mpeg4audio_sample_rates[16] = {
00056     96000, 88200, 64000, 48000, 44100, 32000,
00057     24000, 22050, 16000, 12000, 11025, 8000, 7350
00058 };
00059 
00060 const uint8_t ff_mpeg4audio_channels[8] = {
00061     0, 1, 2, 3, 4, 5, 6, 8
00062 };
00063 
00064 static inline int get_object_type(GetBitContext *gb)
00065 {
00066     int object_type = get_bits(gb, 5);
00067     if (object_type == AOT_ESCAPE)
00068         object_type = 32 + get_bits(gb, 6);
00069     return object_type;
00070 }
00071 
00072 static inline int get_sample_rate(GetBitContext *gb, int *index)
00073 {
00074     *index = get_bits(gb, 4);
00075     return *index == 0x0f ? get_bits(gb, 24) :
00076         ff_mpeg4audio_sample_rates[*index];
00077 }
00078 
00079 int ff_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int buf_size)
00080 {
00081     GetBitContext gb;
00082     int specific_config_bitindex;
00083 
00084     init_get_bits(&gb, buf, buf_size*8);
00085     c->object_type = get_object_type(&gb);
00086     c->sample_rate = get_sample_rate(&gb, &c->sampling_index);
00087     c->chan_config = get_bits(&gb, 4);
00088     if (c->chan_config < FF_ARRAY_ELEMS(ff_mpeg4audio_channels))
00089         c->channels = ff_mpeg4audio_channels[c->chan_config];
00090     c->sbr = -1;
00091     c->ps  = -1;
00092     if (c->object_type == AOT_SBR || (c->object_type == AOT_PS &&
00093         // check for W6132 Annex YYYY draft MP3onMP4
00094         !(show_bits(&gb, 3) & 0x03 && !(show_bits(&gb, 9) & 0x3F)))) {
00095         if (c->object_type == AOT_PS)
00096             c->ps = 1;
00097         c->ext_object_type = AOT_SBR;
00098         c->sbr = 1;
00099         c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index);
00100         c->object_type = get_object_type(&gb);
00101         if (c->object_type == AOT_ER_BSAC)
00102             c->ext_chan_config = get_bits(&gb, 4);
00103     } else {
00104         c->ext_object_type = AOT_NULL;
00105         c->ext_sample_rate = 0;
00106     }
00107     specific_config_bitindex = get_bits_count(&gb);
00108 
00109     if (c->object_type == AOT_ALS) {
00110         skip_bits(&gb, 5);
00111         if (show_bits_long(&gb, 24) != MKBETAG('\0','A','L','S'))
00112             skip_bits_long(&gb, 24);
00113 
00114         specific_config_bitindex = get_bits_count(&gb);
00115 
00116         if (parse_config_ALS(&gb, c))
00117             return -1;
00118     }
00119 
00120     if (c->ext_object_type != AOT_SBR) {
00121         while (get_bits_left(&gb) > 15) {
00122             if (show_bits(&gb, 11) == 0x2b7) { // sync extension
00123                 get_bits(&gb, 11);
00124                 c->ext_object_type = get_object_type(&gb);
00125                 if (c->ext_object_type == AOT_SBR && (c->sbr = get_bits1(&gb)) == 1)
00126                     c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index);
00127                 if (get_bits_left(&gb) > 11 && get_bits(&gb, 11) == 0x548)
00128                     c->ps = get_bits1(&gb);
00129                 break;
00130             } else
00131                 get_bits1(&gb); // skip 1 bit
00132         }
00133     }
00134     return specific_config_bitindex;
00135 }
00136 
00137 static av_always_inline unsigned int copy_bits(PutBitContext *pb,
00138                                                GetBitContext *gb,
00139                                                int bits)
00140 {
00141     unsigned int el = get_bits(gb, bits);
00142     put_bits(pb, bits, el);
00143     return el;
00144 }
00145 
00146 int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
00147 {
00148     int five_bit_ch, four_bit_ch, comment_size, bits;
00149     int offset = put_bits_count(pb);
00150 
00151     copy_bits(pb, gb, 10);                  //Tag, Object Type, Frequency
00152     five_bit_ch  = copy_bits(pb, gb, 4);    //Front
00153     five_bit_ch += copy_bits(pb, gb, 4);    //Side
00154     five_bit_ch += copy_bits(pb, gb, 4);    //Back
00155     four_bit_ch  = copy_bits(pb, gb, 2);    //LFE
00156     four_bit_ch += copy_bits(pb, gb, 3);    //Data
00157     five_bit_ch += copy_bits(pb, gb, 4);    //Coupling
00158     if (copy_bits(pb, gb, 1))               //Mono Mixdown
00159         copy_bits(pb, gb, 4);
00160     if (copy_bits(pb, gb, 1))               //Stereo Mixdown
00161         copy_bits(pb, gb, 4);
00162     if (copy_bits(pb, gb, 1))               //Matrix Mixdown
00163         copy_bits(pb, gb, 3);
00164     for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16)
00165         copy_bits(pb, gb, 16);
00166     if (bits)
00167         copy_bits(pb, gb, bits);
00168     align_put_bits(pb);
00169     align_get_bits(gb);
00170     comment_size = copy_bits(pb, gb, 8);
00171     for (; comment_size > 0; comment_size--)
00172         copy_bits(pb, gb, 8);
00173 
00174     return put_bits_count(pb) - offset;
00175 }

Generated on Fri Sep 16 2011 17:17:40 for FFmpeg by  doxygen 1.7.1