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 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
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) {
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 (c->ext_sample_rate == c->sample_rate)
00132 c->sbr = -1;
00133 }
00134 if (get_bits_left(&gb) > 11 && get_bits(&gb, 11) == 0x548)
00135 c->ps = get_bits1(&gb);
00136 break;
00137 } else
00138 get_bits1(&gb);
00139 }
00140 }
00141
00142
00143 if (!c->sbr)
00144 c->ps = 0;
00145
00146 if ((c->ps == -1 && c->object_type != AOT_AAC_LC) || c->channels & ~0x01)
00147 c->ps = 0;
00148
00149 return specific_config_bitindex;
00150 }
00151
00152 static av_always_inline unsigned int copy_bits(PutBitContext *pb,
00153 GetBitContext *gb,
00154 int bits)
00155 {
00156 unsigned int el = get_bits(gb, bits);
00157 put_bits(pb, bits, el);
00158 return el;
00159 }
00160
00161 int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
00162 {
00163 int five_bit_ch, four_bit_ch, comment_size, bits;
00164 int offset = put_bits_count(pb);
00165
00166 copy_bits(pb, gb, 10);
00167 five_bit_ch = copy_bits(pb, gb, 4);
00168 five_bit_ch += copy_bits(pb, gb, 4);
00169 five_bit_ch += copy_bits(pb, gb, 4);
00170 four_bit_ch = copy_bits(pb, gb, 2);
00171 four_bit_ch += copy_bits(pb, gb, 3);
00172 five_bit_ch += copy_bits(pb, gb, 4);
00173 if (copy_bits(pb, gb, 1))
00174 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, 3);
00179 for (bits = five_bit_ch*5+four_bit_ch*4; bits > 16; bits -= 16)
00180 copy_bits(pb, gb, 16);
00181 if (bits)
00182 copy_bits(pb, gb, bits);
00183 avpriv_align_put_bits(pb);
00184 align_get_bits(gb);
00185 comment_size = copy_bits(pb, gb, 8);
00186 for (; comment_size > 0; comment_size--)
00187 copy_bits(pb, gb, 8);
00188
00189 return put_bits_count(pb) - offset;
00190 }