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 avpriv_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 avpriv_mpeg4audio_sample_rates[*index]; 00077 } 00078 00079 int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, 00080 int bit_size, int sync_extension) 00081 { 00082 GetBitContext gb; 00083 int specific_config_bitindex; 00084 00085 if(bit_size<=0) 00086 return AVERROR_INVALIDDATA; 00087 00088 init_get_bits(&gb, buf, bit_size); 00089 c->object_type = get_object_type(&gb); 00090 c->sample_rate = get_sample_rate(&gb, &c->sampling_index); 00091 c->chan_config = get_bits(&gb, 4); 00092 if (c->chan_config < FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) 00093 c->channels = ff_mpeg4audio_channels[c->chan_config]; 00094 c->sbr = -1; 00095 c->ps = -1; 00096 if (c->object_type == AOT_SBR || (c->object_type == AOT_PS && 00097 // check for W6132 Annex YYYY draft MP3onMP4 00098 !(show_bits(&gb, 3) & 0x03 && !(show_bits(&gb, 9) & 0x3F)))) { 00099 if (c->object_type == AOT_PS) 00100 c->ps = 1; 00101 c->ext_object_type = AOT_SBR; 00102 c->sbr = 1; 00103 c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index); 00104 c->object_type = get_object_type(&gb); 00105 if (c->object_type == AOT_ER_BSAC) 00106 c->ext_chan_config = get_bits(&gb, 4); 00107 } else { 00108 c->ext_object_type = AOT_NULL; 00109 c->ext_sample_rate = 0; 00110 } 00111 specific_config_bitindex = get_bits_count(&gb); 00112 00113 if (c->object_type == AOT_ALS) { 00114 skip_bits(&gb, 5); 00115 if (show_bits_long(&gb, 24) != MKBETAG('0円','A','L','S')) 00116 skip_bits_long(&gb, 24); 00117 00118 specific_config_bitindex = get_bits_count(&gb); 00119 00120 if (parse_config_ALS(&gb, c)) 00121 return -1; 00122 } 00123 00124 if (c->ext_object_type != AOT_SBR && sync_extension) { 00125 while (get_bits_left(&gb) > 15) { 00126 if (show_bits(&gb, 11) == 0x2b7) { // sync extension 00127 get_bits(&gb, 11); 00128 c->ext_object_type = get_object_type(&gb); 00129 if (c->ext_object_type == AOT_SBR && (c->sbr = get_bits1(&gb)) == 1) 00130 c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index); 00131 if (get_bits_left(&gb) > 11 && get_bits(&gb, 11) == 0x548) 00132 c->ps = get_bits1(&gb); 00133 break; 00134 } else 00135 get_bits1(&gb); // skip 1 bit 00136 } 00137 } 00138 00139 //PS requires SBR 00140 if (!c->sbr) 00141 c->ps = 0; 00142 //Limit implicit PS to the HE-AACv2 Profile 00143 if ((c->ps == -1 && c->object_type != AOT_AAC_LC) || c->channels & ~0x01) 00144 c->ps = 0; 00145 00146 return specific_config_bitindex; 00147 } 00148 00149 static av_always_inline unsigned int copy_bits(PutBitContext *pb, 00150 GetBitContext *gb, 00151 int bits) 00152 { 00153 unsigned int el = get_bits(gb, bits); 00154 put_bits(pb, bits, el); 00155 return el; 00156 } 00157 00158 int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb) 00159 { 00160 int five_bit_ch, four_bit_ch, comment_size, bits; 00161 int offset = put_bits_count(pb); 00162 00163 copy_bits(pb, gb, 10); //Tag, Object Type, Frequency 00164 five_bit_ch = copy_bits(pb, gb, 4); //Front 00165 five_bit_ch += copy_bits(pb, gb, 4); //Side 00166 five_bit_ch += copy_bits(pb, gb, 4); //Back 00167 four_bit_ch = copy_bits(pb, gb, 2); //LFE 00168 four_bit_ch += copy_bits(pb, gb, 3); //Data 00169 five_bit_ch += copy_bits(pb, gb, 4); //Coupling 00170 if (copy_bits(pb, gb, 1)) //Mono Mixdown 00171 copy_bits(pb, gb, 4); 00172 if (copy_bits(pb, gb, 1)) //Stereo Mixdown 00173 copy_bits(pb, gb, 4); 00174 if (copy_bits(pb, gb, 1)) //Matrix Mixdown 00175 copy_bits(pb, gb, 3); 00176 for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16) 00177 copy_bits(pb, gb, 16); 00178 if (bits) 00179 copy_bits(pb, gb, bits); 00180 avpriv_align_put_bits(pb); 00181 align_get_bits(gb); 00182 comment_size = copy_bits(pb, gb, 8); 00183 for (; comment_size > 0; comment_size--) 00184 copy_bits(pb, gb, 8); 00185 00186 return put_bits_count(pb) - offset; 00187 }