00001 /* 00002 * MPEG-4 Audio common code 00003 * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "bitstream.h" 00023 #include "mpeg4audio.h" 00024 00025 const int ff_mpeg4audio_sample_rates[16] = { 00026 96000, 88200, 64000, 48000, 44100, 32000, 00027 24000, 22050, 16000, 12000, 11025, 8000, 7350 00028 }; 00029 00030 const uint8_t ff_mpeg4audio_channels[8] = { 00031 0, 1, 2, 3, 4, 5, 6, 8 00032 }; 00033 00034 static inline int get_object_type(GetBitContext *gb) 00035 { 00036 int object_type = get_bits(gb, 5); 00037 if (object_type == 31) 00038 object_type = 32 + get_bits(gb, 6); 00039 return object_type; 00040 } 00041 00042 static inline int get_sample_rate(GetBitContext *gb, int *index) 00043 { 00044 *index = get_bits(gb, 4); 00045 return *index == 0x0f ? get_bits(gb, 24) : 00046 ff_mpeg4audio_sample_rates[*index]; 00047 } 00048 00049 int ff_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int buf_size) 00050 { 00051 GetBitContext gb; 00052 int specific_config_bitindex; 00053 00054 init_get_bits(&gb, buf, buf_size*8); 00055 c->object_type = get_object_type(&gb); 00056 c->sample_rate = get_sample_rate(&gb, &c->sampling_index); 00057 c->chan_config = get_bits(&gb, 4); 00058 c->sbr = -1; 00059 if (c->object_type == 5) { 00060 c->ext_object_type = c->object_type; 00061 c->sbr = 1; 00062 c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index); 00063 c->object_type = get_object_type(&gb); 00064 } else 00065 c->ext_object_type = 0; 00066 00067 specific_config_bitindex = get_bits_count(&gb); 00068 00069 if (c->ext_object_type != 5) { 00070 int bits_left = buf_size*8 - specific_config_bitindex; 00071 for (; bits_left > 15; bits_left--) { 00072 if (show_bits(&gb, 11) == 0x2b7) { // sync extension 00073 get_bits(&gb, 11); 00074 c->ext_object_type = get_object_type(&gb); 00075 if (c->ext_object_type == 5 && (c->sbr = get_bits1(&gb)) == 1) 00076 c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index); 00077 break; 00078 } else 00079 get_bits1(&gb); // skip 1 bit 00080 } 00081 } 00082 return specific_config_bitindex; 00083 }