https://blog.csdn.net/djhsws/article/details/53522060
当解析Mp4的时候,会碰到这个esds 的box,这个box 有很多信息。
当时是为了解决在MP4A中如何判断是acc还是 MP3
然后找到下面答案
https://wiki.multimedia.cx/index.php?title=MPEG-4_Audio
http://stackoverflow.com/questions/3987850/mp4-atom-how-to-discriminate-the-audio-codec-is-it-aac-or-mp3
现在梳理下
在Audio Trak 的esds box中
首先是
1、version 和 flag 占4个字节 不使用就跳过
get_be32(m_it); /* version + flags */
2、MP4ESDescrTag 读取 MP4ESDescrTag tag 和 len 和附属内容
tag = get_byte(m_it);
len = getTaglen();
if (tag == MP4ESDescrTag) {
get_be16(m_it); /* ID */
get_byte(m_it); /* priority */
} else
get_be16(m_it); /* ID */
3、MP4DecConfigDescrTag 读取 MP4DecConfigDescrTag tag 和 len 和附属内容
tag = get_byte(m_it);
len = getTaglen();
if (tag == MP4DecConfigDescrTag) {
int object_type_id = get_byte(m_it);
get_byte(m_it); /* stream type */
get_byte(m_it); get_byte(m_it); get_byte(m_it);
get_be32(m_it); /* max bitrate */
get_be32(m_it); /* avg bitrate */
st->codec->codec_id= codec_get_id(ff_mp4_obj_type, object_type_id);
这边的附属内容我要说下 重要的是 这边对codec 解码进行了调整,id 对应表附件1,这边决定了MP3 或者 acc等
4、 MP4DecSpecificDescrTag 读取MP4DecSpecificDescrTag 和len 和附属内容
tag = get_byte(m_it);
len=getTagLen();
if (tag == MP4DecSpecificDescrTag) {
st->codec->extradata = (uint8_t *)malloc(len);
if (st->codec->extradata) {
ReadData(st->codec->extradata, len,m_it);
st->codec->extradata_size = len;
int ad = *st->codec->extradata;
int aa =*st->codec->extradata >> 3;
if ((*st->codec->extradata >> 3) == 29) {
st->codec->codec_id = CODEC_ID_MP3ON4;
}
if (st->codec->codec_id == CODEC_ID_AAC) {
MPEG4AudioConfig cfg = {0};
avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
st->codec->extradata_size, 1);
st->codec->channels = cfg.channels;
// if (cfg.object_type == 29 && cfg.sampling_index < 3) // old mp3on4
//st->codec->sample_rate = avpriv_mpa_freq_tab[cfg.sampling_index];
// else if (cfg.ext_sample_rate)
// //st->codec->sample_rate = cfg.ext_sample_rate;
// else
// //st->codec->sample_rate = cfg.sample_rate;
// if (!(st->codec->codec_id = ff_codec_get_id(mp4_audio_types,
// cfg.object_type)))
// st->codec->codec_id = AV_CODEC_ID_AAC;
}
}
这边就严重了,这边如果是老的版本 只需如此
int aa =*st->codec->extradata >> 3;
if ((*st->codec->extradata >> 3) == 29) {
st->codec->codec_id = CODEC_ID_MP3ON4;
}
但是新的 版本 调整增加了 很多内容
The Audio Specific Config is the global header for MPEG-4 Audio:
5 bits: object type
if (object type == 31)
6 bits + 32: object type
4 bits: frequency index
if (frequency index == 15)
24 bits: frequency
4 bits: channel configuration
var bits: AOT Specific Config
主要如上 object type 读取5个bit object type列表见附件2(对codec解码器进行调整,MP3 还是ACC还是 MP3on4)
然后 frequency index 读取4个bit frequency index 列表见 附件3(对采样率进行调整)
附件1
const MP4CodecTag ff_mp4_obj_type[] = {
{ CODEC_ID_MPEG4 , 32 },
{ CODEC_ID_H264 , 33 },
{ CODEC_ID_AAC , 64 },
{ CODEC_ID_MPEG2VIDEO, 96 }, /* MPEG2 Simple */
{ CODEC_ID_MPEG2VIDEO, 97 }, /* MPEG2 Main */
{ CODEC_ID_MPEG2VIDEO, 98 }, /* MPEG2 SNR */
{ CODEC_ID_MPEG2VIDEO, 99 }, /* MPEG2 Spatial */
{ CODEC_ID_MPEG2VIDEO, 100 }, /* MPEG2 High */
{ CODEC_ID_MPEG2VIDEO, 101 }, /* MPEG2 422 */
{ CODEC_ID_AAC , 102 }, /* MPEG2 AAC Main */
{ CODEC_ID_AAC , 103 }, /* MPEG2 AAC Low */
{ CODEC_ID_AAC , 104 }, /* MPEG2 AAC SSR */
{ CODEC_ID_MP3 , 105 }, /* 13818-3 */
{ CODEC_ID_MPEG1VIDEO, 106 }, /* 11172-2 */
{ CODEC_ID_MP3 , 107 }, /* 11172-3 */
{ CODEC_ID_MJPEG , 108 }, /* 10918-1 */
{ CODEC_ID_PNG , 109 },
{ CODEC_ID_JPEG2000 , 110 }, /* 15444-1 */
{ CODEC_ID_VC1 , 163 },
{ CODEC_ID_VORBIS , 221 },
{ CODEC_ID_PCM_S16LE , 224 },
{ CODEC_ID_QCELP , 225 },
{ CODEC_ID_AC3 , 226 },
{ CODEC_ID_PCM_ALAW , 227 },
{ CODEC_ID_PCM_MULAW , 228 },
{ CODEC_ID_PCM_S16BE , 230 },
{ CODEC_ID_H263 , 242 },
{ CODEC_ID_H261 , 243 },
{ CODEC_ID_NONE, 0 },
};
附件2
MPEG-4 Audio Object Types:
● 0: Null
● 1: AAC Main
● 2: AAC LC (Low Complexity)
● 3: AAC SSR (Scalable Sample Rate)
● 4: AAC LTP (Long Term Prediction)
● 5: SBR (Spectral Band Replication)
● 6: AAC Scalable
● 7: TwinVQ
● 8: CELP (Code Excited Linear Prediction)
● 9: HXVC (Harmonic Vector eXcitation Coding)
● 10: Reserved
● 11: Reserved
● 12: TTSI (Text-To-Speech Interface)
● 13: Main Synthesis
● 14: Wavetable Synthesis
● 15: General MIDI
● 16: Algorithmic Synthesis and Audio Effects
● 17: ER (Error Resilient) AAC LC
● 18: Reserved
● 19: ER AAC LTP
● 20: ER AAC Scalable
● 21: ER TwinVQ
● 22: ER BSAC (Bit-Sliced Arithmetic Coding)
● 23: ER AAC LD (Low Delay)
● 24: ER CELP
● 25: ER HVXC
● 26: ER HILN (Harmonic and Individual Lines plus Noise)
● 27: ER Parametric
● 28: SSC (SinuSoidal Coding)
● 29: PS (Parametric Stereo)
● 30: MPEG Surround
● 31: (Escape value)
● 32: Layer-1
● 33: Layer-2
● 34: Layer-3
● 35: DST (Direct Stream Transfer)
● 36: ALS (Audio Lossless)
● 37: SLS (Scalable LosslesS)
● 38: SLS non-core
● 39: ER AAC ELD (Enhanced Low Delay)
● 40: SMR (Symbolic Music Representation) Simple
● 41: SMR Main
● 42: USAC (Unified Speech and Audio Coding) (no SBR)
● 43: SAOC (Spatial Audio Object Coding)
● 44: LD MPEG Surround
● 45: USAC
附件3
Sampling Frequencies
There are 13 supported frequencies:
● 0: 96000 Hz
● 1: 88200 Hz
● 2: 64000 Hz
● 3: 48000 Hz
● 4: 44100 Hz
● 5: 32000 Hz
● 6: 24000 Hz
● 7: 22050 Hz
● 8: 16000 Hz
● 9: 12000 Hz
● 10: 11025 Hz
● 11: 8000 Hz
● 12: 7350 Hz
● 13: Reserved
● 14: Reserved
● 15: frequency is written explictly
附件3
Channel Configurations
These are the channel configurations:
● 0: Defined in AOT Specifc Config
● 1: 1 channel: front-center
● 2: 2 channels: front-left, front-right
● 3: 3 channels: front-center, front-left, front-right
● 4: 4 channels: front-center, front-left, front-right, back-center
● 5: 5 channels: front-center, front-left, front-right, back-left, back-right
● 6: 6 channels: front-center, front-left, front-right, back-left, back-right, LFE-channel
● 7: 8 channels: front-center, front-left, front-right, side-left, side-right, back-left, back-right, LFE-channel
● 8-15: Reserved