Tuesday, November 30, 2010

MP3 Frame Size and Frame Length

最近在閱讀有關 MP3stego相關論文, 由於 MP3stego 基本上是將機密藏在 Frame Length 之中, 因此上 Google 搜尋了一些 MP3 規格書的相關訊息。由於 MP3 規格書 (MPEG I Audio: ISO/IEC 11172-3, MPEG II Audio: ISO/IEC 13818-3) 必須要付費購買才拿得到, 所以只好從其他網頁中尋求相關訊息。

MPEG AUDIO FRAME HEADER 這個網頁之中, 有一段文章非常值得參考, 如下:
How to calculate frame length

First, let's distinguish two terms frame size and frame length. Frame size is the number of samples contained in a frame. It is constant and always 384 samples for Layer I and 1152 samples for Layer II and Layer III. Frame length is length of a frame when compressed. It is calculated in slots. One slot is 4 bytes long for Layer I, and one byte long for Layer II and Layer III. When you are reading MPEG file you must calculate this to be able to find each consecutive frame. Remember, frame length may change from frame to frame due to padding or bitrate switching.

從上文中, 我們知道一個 MP3 frame 中, 固定有 1152 個 samples。

接下來, 如果我們從 Header 中讀到 Audio 的 BitRate = 128000 Bit/Sec, SampleRate = 44100 Sample/Sec. 我們就可以去大概計算出平均一個 frame 被壓縮成多少個 byte?
每秒取樣 44100 個 samples, 每個 frame 有 1152 個 samples,
所以, 一秒鐘有 44100 / 1152 = 38.28125 個 frame。
每秒鐘只能壓縮成 128000 bits = 16000 bytes。
因此, 每個 frame 平均可以分配到 16000 Bytes / 38.28125 frames = 417.96 Bytes。
Frame Length = 417.96 Bytes。
化成公式的模樣就是:
FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
有關公式中的 Padding, 指的是額外加的一些 Bytes, 目的是使得 Frame Length 剛好符合 BitRate 的設定。在 網頁 中有一段說明如下:
Padding is used to fit the bit rates exactly.
For an example: 128k 44.1kHz layer II uses a lot of 418 bytes and some of 417 bytes long frames to get the exact 128k bitrate.
For Layer I slot is 32 bits long, for Layer II and Layer III slot is 8 bits long.

 
 

No comments:

Post a Comment