MP4V2--如何使用 MP4SetTrackESConfiguration .

 

如何使用 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

這樣就能找出你的選擇,需要對映什麼參數值了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章