如何使用 MP4SetTrackESConfiguration
http://swchoutech.blogspot.hk/2012/11/mp4settrackesconfiguration.html
在使用 MP4v2 制作 .mp4 档案时,如果你要使用的 Audio 编码格式是 AAC,那么你就需要使用 MP4SetTrackESConfiguration 这个函式来设定解码需要的资料。在网路上看到的例子都是以 FAAC 编码为居多,大多都可以参考需要的设定,设定 MP4SetTrackESConfiguration 的方式,都是先利用 FAAC 里的 faacEncGetDecoderSpecificInfo
得到想要的资料,再传给 MP4SetTrackESConfiguration
像这样
faacEncGetDecoderSpecificInfo(hEnc, &Config, &ConfigLen);
MP4SetTrackESConfiguration(hFile, AudioTrack, Config, ConfigLen);
这是刚好你用的是 FAAC library,但如果你用的是别的 library 该怎么办呢?
起初我也是试著去 Google 看看有没有人有提供这样的资料,后来在这一篇看到了有人说了大概的方式,但是我看了之后,并不了解它所说的格式怎么决定?我使用的参数不一定和它用的是一样的,所以我不能肯定要如何设定这个值。我也去
Google 了这个栏位的格式,但是没有找到,还去查了一下 Apple Qiucktime 档案格式的文件,也没发现什么。最后,我直接去看了 FAAC 的原始码,才了解怎么设定。(我应该一开始就看的...)
faacEncGetDecoderSpecificInfo 的程式码如下,可以看出我们要的格式是
5 bits | 4 bits | 4 bits | 3 bits
第一栏 第二栏 第三栏 第四栏
第一栏:AAC Object Type
第二栏:Sample Rate Index
第三栏:Channel Number
第四栏:Don't care,设 0
int FAACAPI
faacEncGetDecoderSpecificInfo(faacEncHandle hEncoder,unsignedchar**
ppBuffer,unsignedlong*
pSizeOfDecoderSpecificInfo)
{
BitStream*
pBitStream = NULL;
if((hEncoder == NULL) || (ppBuffer == NULL)
|| (pSizeOfDecoderSpecificInfo == NULL)) {
return -1;
}
if(hEncoder->config.mpegVersion
== MPEG2){
return -2; /* not supported */
}
*pSizeOfDecoderSpecificInfo = 2;
*ppBuffer =
malloc(2);
if(*ppBuffer != NULL){
memset(*ppBuffer,0,*pSizeOfDecoderSpecificInfo);
pBitStream =OpenBitStream(*pSizeOfDecoderSpecificInfo,
*ppBuffer);
PutBit(pBitStream,
hEncoder->config.aacObjectType,
5);
PutBit(pBitStream,
hEncoder->sampleRateIdx, 4);
PutBit(pBitStream,
hEncoder->numChannels, 4);
CloseBitStream(pBitStream);
return 0;
} else {
return -3;
}
}
接著,要知道怎么决定每个栏位的值,第三和第四就不用看了,只要找出第一和第二栏位就行了。从原始码里找出下面的资料:
/* AAC object types */
#defineMAIN
1
#defineLOW
2
#defineSSR
3
#defineLTP
4
/* Returns the sample rate index */
intGetSRIndex(unsignedint
sampleRate)
{
if (92017 <= sampleRate)return
0;
if (75132 <= sampleRate)return
1;
if (55426 <= sampleRate)return
2;
if (46009 <= sampleRate)return
3;
if (37566 <= sampleRate)return
4;
if (27713 <= sampleRate)return
5;
if (23004 <= sampleRate)return
6;
if (18783 <= sampleRate)return
7;
if (13856 <= sampleRate)return
8;
if (11502 <= sampleRate)return
9;
if (9391 <= sampleRate)return
10;
return 11;
}
现在,对于你自己要设定的参数值,就知道要怎么设了吧!举个例子,我使用的 AAC 是 LOW,44100 hz,Stereo,那么从上面的资料来看
第一栏:00010
第二栏:0100
第三栏:0010
第四栏:000
合起来: 00010010 00010000 => 0x12 0x10
这样就能找出你的选择,需要对映什么参数值了!