00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00042
00043 c->sample_rate = get_bits_long(gb, 32);
00044
00045
00046 skip_bits_long(gb, 32);
00047
00048
00049 c->chan_config = 0;
00050 c->channels = get_bits(gb, 16) + 1;
00051
00052 return 0;
00053 }
00054
00055
00056
00057 const int avpriv_mpeg4audio_sample_rates[16] = {
00058 96000, 88200, 64000, 48000, 44100, 32000,
00059 24000, 22050, 16000, 12000, 11025, 8000, 7350
00060 };
00061
00062 const uint8_t ff_mpeg4audio_channels[8] = {
00063 0, 1, 2, 3, 4, 5, 6, 8
00064 };
00065
00066 static inline int get_object_type(GetBitContext *gb)
00067 {
00068 int object_type = get_bits(gb, 5);
00069 if (object_type == AOT_ESCAPE)
00070 object_type = 32 + get_bits(gb, 6);
00071 return object_type;
00072 }
00073
00074 static inline int get_sample_rate(GetBitContext *gb, int *index)
00075 {
00076 *index = get_bits(gb, 4);
00077 return *index == 0x0f ? get_bits(gb, 24) :
00078 avpriv_mpeg4audio_sample_rates[*index];
00079 }
00080
00081 int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf,
00082 int bit_size, int sync_extension)
00083 {
00084 GetBitContext gb;
00085 int specific_config_bitindex;
00086
00087 if(bit_size<=0)
00088 return AVERROR_INVALIDDATA;
00089
00090 init_get_bits(&gb, buf, bit_size);
00091 c->object_type = get_object_type(&gb);
00092 c->sample_rate = get_sample_rate(&gb, &c->sampling_index);
00093 c->chan_config = get_bits(&gb, 4);
00094 if (c->chan_config < FF_ARRAY_ELEMS(ff_mpeg4audio_channels))
00095 c->channels = ff_mpeg4audio_channels[c->chan_config];
00096 c->sbr = -1;
00097 c->ps = -1;
00098 if (c->object_type == AOT_SBR || (c->object_type == AOT_PS &&
00099
00100 !(show_bits(&gb, 3) & 0x03 && !(show_bits(&gb, 9) & 0x3F)))) {
00101 if (c->object_type == AOT_PS)
00102 c->ps = 1;
00103 c->ext_object_type = AOT_SBR;
00104 c->sbr = 1;
00105 c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index);
00106 c->object_type = get_object_type(&gb);
00107 if (c->object_type == AOT_ER_BSAC)
00108 c->ext_chan_config = get_bits(&gb, 4);
00109 } else {
00110 c->ext_object_type = AOT_NULL;
00111 c->ext_sample_rate = 0;
00112 }
00113 specific_config_bitindex = get_bits_count(&gb);
00114
00115 if (c->object_type == AOT_ALS) {
00116 skip_bits(&gb, 5);
00117 if (show_bits_long(&gb, 24) != MKBETAG('\0','A','L','S'))
00118 skip_bits_long(&gb, 24);
00119
00120 specific_config_bitindex = get_bits_count(&gb);
00121
00122 if (parse_config_ALS(&gb, c))
00123 return -1;
00124 }
00125
00126 if (c->ext_object_type != AOT_SBR && sync_extension) {
00127 while (get_bits_left(&gb) > 15) {
00128 if (show_bits(&gb, 11) == 0x2b7) {
00129 get_bits(&gb, 11);
00130 c->ext_object_type = get_object_type(&gb);
00131 if (c->ext_object_type == AOT_SBR && (c->sbr = get_bits1(&gb)) == 1) {
00132 c->ext_sample_rate = get_sample_rate(&gb, &c->ext_sampling_index);
00133 if (c->ext_sample_rate == c->sample_rate)
00134 c->sbr = -1;
00135 }
00136 if (get_bits_left(&gb) > 11 && get_bits(&gb, 11) == 0x548)
00137 c->ps = get_bits1(&gb);
00138 break;
00139 } else
00140 get_bits1(&gb);
00141 }
00142 }
00143
00144
00145 if (!c->sbr)
00146 c->ps = 0;
00147
00148 if ((c->ps == -1 && c->object_type != AOT_AAC_LC) || c->channels & ~0x01)
00149 c->ps = 0;
00150
00151 return specific_config_bitindex;
00152 }
00153
00154 static av_always_inline unsigned int copy_bits(PutBitContext *pb,
00155 GetBitContext *gb,
00156 int bits)
00157 {
00158 unsigned int el = get_bits(gb, bits);
00159 put_bits(pb, bits, el);
00160 return el;
00161 }
00162
00163 int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
00164 {
00165 int five_bit_ch, four_bit_ch, comment_size, bits;
00166 int offset = put_bits_count(pb);
00167
00168 copy_bits(pb, gb, 10);
00169 five_bit_ch = copy_bits(pb, gb, 4);
00170 five_bit_ch += copy_bits(pb, gb, 4);
00171 five_bit_ch += copy_bits(pb, gb, 4);
00172 four_bit_ch = copy_bits(pb, gb, 2);
00173 four_bit_ch += copy_bits(pb, gb, 3);
00174 five_bit_ch += copy_bits(pb, gb, 4);
00175 if (copy_bits(pb, gb, 1))
00176 copy_bits(pb, gb, 4);
00177 if (copy_bits(pb, gb, 1))
00178 copy_bits(pb, gb, 4);
00179 if (copy_bits(pb, gb, 1))
00180 copy_bits(pb, gb, 3);
00181 for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16)
00182 copy_bits(pb, gb, 16);
00183 if (bits)
00184 copy_bits(pb, gb, bits);
00185 avpriv_align_put_bits(pb);
00186 align_get_bits(gb);
00187 comment_size = copy_bits(pb, gb, 8);
00188 for (; comment_size > 0; comment_size--)
00189 copy_bits(pb, gb, 8);
00190
00191 return put_bits_count(pb) - offset;
00192 }