如何使用MP4SetTrackESConfiguration

来源:互联网 发布:汉化 知乎 编辑:程序博客网 时间:2024/05/14 08:01
在使用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

這樣就能找出你的選擇,需要對映什麼參數值了!
原创粉丝点击