Libav 0.7.1
|
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 Libav. 00007 * 00008 * Libav 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 * Libav 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 Libav; 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 00135 //PS requires SBR 00136 if (!c->sbr) 00137 c->ps = 0; 00138 //Limit implicit PS to the HE-AACv2 Profile 00139 if ((c->ps == -1 && c->object_type != AOT_AAC_LC) || c->channels & ~0x01) 00140 c->ps = 0; 00141 00142 return specific_config_bitindex; 00143 } 00144 00145 static av_always_inline unsigned int copy_bits(PutBitContext *pb, 00146 GetBitContext *gb, 00147 int bits) 00148 { 00149 unsigned int el = get_bits(gb, bits); 00150 put_bits(pb, bits, el); 00151 return el; 00152 } 00153 00154 int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb) 00155 { 00156 int five_bit_ch, four_bit_ch, comment_size, bits; 00157 int offset = put_bits_count(pb); 00158 00159 copy_bits(pb, gb, 10); //Tag, Object Type, Frequency 00160 five_bit_ch = copy_bits(pb, gb, 4); //Front 00161 five_bit_ch += copy_bits(pb, gb, 4); //Side 00162 five_bit_ch += copy_bits(pb, gb, 4); //Back 00163 four_bit_ch = copy_bits(pb, gb, 2); //LFE 00164 four_bit_ch += copy_bits(pb, gb, 3); //Data 00165 five_bit_ch += copy_bits(pb, gb, 4); //Coupling 00166 if (copy_bits(pb, gb, 1)) //Mono Mixdown 00167 copy_bits(pb, gb, 4); 00168 if (copy_bits(pb, gb, 1)) //Stereo Mixdown 00169 copy_bits(pb, gb, 4); 00170 if (copy_bits(pb, gb, 1)) //Matrix Mixdown 00171 copy_bits(pb, gb, 3); 00172 for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16) 00173 copy_bits(pb, gb, 16); 00174 if (bits) 00175 copy_bits(pb, gb, bits); 00176 align_put_bits(pb); 00177 align_get_bits(gb); 00178 comment_size = copy_bits(pb, gb, 8); 00179 for (; comment_size > 0; comment_size--) 00180 copy_bits(pb, gb, 8); 00181 00182 return put_bits_count(pb) - offset; 00183 }