MP4文件格式詳解(ISO-14496-12/14)
Author:Pirate Leo
Email:codeevoship@gmail.com
一、基本概念
1、 文件,由許多Box和FullBox組成。
2、 Box,每個Box由Header和Data組成。
3、 FullBox,是Box的擴展,Box結構的基礎上在Header中增加8bits version和24bits flags。
4、 Header,包含了整個Box的長度size和類型type。當size==0時,代表這是文件中最后一個Box;當size==1時,意味着Box長度需要更多bits來描述,在后面會定義一個64bits的largesize描述Box的長度;當type是uuid時,代表Box中的數據是用戶自定義擴展類型。
5、 Data,是Box的實際數據,可以是純數據也可以是更多的子Boxes。
6、 當一個Box的Data中是一系列子Box時,這個Box又可成為Container Box。
Box的結構用偽代碼表示如下:
- aligned(8) class Box (unsigned int(32) boxtype,optional unsigned int(8)[16] extended_type)
- {
- unsigned int(32) size;
- unsigned int(32) type = boxtype;
- if (size==1)
- {
- unsigned int(64) largesize;
- }
- else if (size==0)
- {
- // box extends to end of file
- }
- if (boxtype==‘uuid’)
- {
- unsigned int(8)[16] usertype = extended_type;
- }
aligned(8) class Box (unsigned int(32) boxtype,optional unsigned int(8)[16] extended_type) { unsigned int(32) size; unsigned int(32) type = boxtype; if (size==1) { unsigned int(64) largesize; } else if (size==0) { // box extends to end of file } if (boxtype==‘uuid’) { unsigned int(8)[16] usertype = extended_type; }
結構如下圖:
文件基本結構描述圖
二、MP4文件格式(ISO-14496-12/14)
MP4文件概述
MP4文件就是由各式各樣的Box組成的,下表中列出了所有必選或可選的Box類型,√代表Box必選。
具體列表:
ftyp |
|
|
|
|
|
√ |
file type and compatibility |
pdin |
|
|
|
|
|
|
progressive download information |
moov |
|
|
|
|
|
√ |
container for all the metadata |
|
mvhd |
|
|
|
|
√ |
movie header, overall declarations |
|
trak |
|
|
|
|
√ |
container for an individual track or stream |
|
|
tkhd |
|
|
|
√ |
track header, overall information about the track |
|
|
tref |
|
|
|
|
track reference container |
|
|
edts |
|
|
|
|
edit list container |
|
|
|
elst |
|
|
|
an edit list |
|
|
mdia |
|
|
|
√ |
container for the media information in a track |
|
|
|
mdhd |
|
|
√ |
media header, overall information about the media |
|
|
|
hdlr |
|
|
√ |
handler, declares the media (handler) type |
|
|
|
minf |
|
|
√ |
media information container |
|
|
|
|
vmhd |
|
|
video media header, overall information (video track only) |
|
|
|
|
smhd |
|
|
sound media header, overall information (sound track only) |
|
|
|
|
hmhd |
|
|
hint media header, overall information (hint track only) |
|
|
|
|
nmhd |
|
|
Null media header, overall information (some tracks only) |
|
|
|
|
dinf |
|
√ |
data information box, container |
|
|
|
|
|
dref |
√ |
data reference box, declares source(s) of media data in track |
|
|
|
|
stbl |
|
√ |
sample table box, container for the time/space map |
|
|
|
|
|
stsd |
√ |
sample descriptions (codec types, initialization etc.) |
|
|
|
|
|
stts |
√ |
(decoding) time-to-sample |
|
|
|
|
|
ctts |
|
(composition) time to sample |
|
|
|
|
|
stsc |
√ |
sample-to-chunk, partial data-offset information |
|
|
|
|
|
stsz |
|
sample sizes (framing) |
|
|
|
|
|
stz2 |
|
compact sample sizes (framing) |
|
|
|
|
|
stco |
√ |
chunk offset, partial data-offset information |
|
|
|
|
|
co64 |
|
64-bit chunk offset |
|
|
|
|
|
stss |
|
sync sample table (random access points) |
|
|
|
|
|
stsh |
|
shadow sync sample table |
|
|
|
|
|
padb |
|
sample padding bits |
|
|
|
|
|
stdp |
|
sample degradation priority |
|
|
|
|
|
sdtp |
|
independent and disposable samples |
|
|
|
|
|
sbgp |
|
sample-to-group |
|
|
|
|
|
sgpd |
|
sample group description |
|
|
|
|
|
subs |
|
sub-sample information |
|
mvex |
|
|
|
|
|
movie extends box |
|
|
mehd |
|
|
|
|
movie extends header box |
|
|
trex |
|
|
|
√ |
track extends defaults |
|
ipmc |
|
|
|
|
|
IPMP Control Box |
moof |
|
|
|
|
|
|
movie fragment |
|
mfhd |
|
|
|
|
√ |
movie fragment header |
|
traf |
|
|
|
|
|
track fragment |
|
|
tfhd |
|
|
|
√ |
track fragment header |
|
|
trun |
|
|
|
|
track fragment run |
|
|
sdtp |
|
|
|
|
independent and disposable samples |
|
|
sbgp |
|
|
|
|
sample-to-group |
|
|
subs |
|
|
|
|
sub-sample information |
mfra |
|
|
|
|
|
|
movie fragment random access |
|
tfra |
|
|
|
|
|
track fragment random access |
|
mfro |
|
|
|
|
√ |
movie fragment random access offset |
mdat |
|
|
|
|
|
|
media data container |
free |
|
|
|
|
|
|
free space |
skip |
|
|
|
|
|
|
free space |
|
udta |
|
|
|
|
|
user-data |
|
|
cprt |
|
|
|
|
copyright etc. |
meta |
|
|
|
|
|
|
metadata |
|
hdlr |
|
|
|
|
√ |
handler, declares the metadata (handler) type |
|
dinf |
|
|
|
|
|
data information box, container |
|
|
dref |
|
|
|
|
data reference box, declares source(s) of metadata items |
|
ipmc |
|
|
|
|
|
IPMP Control Box |
|
iloc |
|
|
|
|
|
item location |
|
ipro |
|
|
|
|
|
item protection |
|
|
sinf |
|
|
|
|
protection scheme information box |
|
|
|
frma |
|
|
|
original format box |
|
|
|
imif |
|
|
|
IPMP Information box |
|
|
|
schm |
|
|
|
scheme type box |
|
|
|
schi |
|
|
|
scheme information box |
|
iinf |
|
|
|
|
|
item information |
|
xml |
|
|
|
|
|
XML container |
|
bxml |
|
|
|
|
|
binary XML container |
|
pitm |
|
|
|
|
|
primary item reference |
|
fiin |
|
|
|
|
|
file delivery item information |
|
|
paen |
|
|
|
|
partition entry |
|
|
|
fpar |
|
|
|
file partition |
|
|
|
fecr |
|
|
|
FEC reservoir |
|
|
segr |
|
|
|
|
file delivery session group |
|
|
gitn |
|
|
|
|
group id to name |
|
|
tsel |
|
|
|
|
track selection |
meco |
|
|
|
|
|
|
additional metadata container |
|
mere |
|
|
|
|
|
metabox relation |
正式開始前先對文件的幾個重要部分宏觀介紹一下,以便諸位在后續學習時心中有數:
1、 ftypbox,在文件的開始位置,描述的文件的版本、兼容協議等;
2、 moovbox,這個box中不包含具體媒體數據,但包含本文件中所有媒體數據的宏觀描述信息,moov box下有mvhd和trak box。
>>mvhd中記錄了創建時間、修改時間、時間度量標尺、可播放時長等信息。
>>trak中的一系列子box描述了每個媒體軌道的具體信息。
3、 moofbox,這個box是視頻分片的描述信息。並不是MP4文件必須的部分,但在我們常見的可在線播放的MP4格式文件中(例如Silverlight Smooth Streaming中的ismv文件)確是重中之重。
4、 mdatbox,實際媒體數據。我們最終解碼播放的數據都在這里面。
5、 mfrabox,一般在文件末尾,媒體的索引文件,可通過查詢直接定位所需時間點的媒體數據。
附:Smooth Streaming中ismv文件結構,文件分為了多個Fragments,每個Fragment中包含moof和mdat。這樣的結構符合漸進式播放需求。(mdat及其描述信息逐步傳輸,收齊一個Fragment便可播放其中的mdat)。
文件類型ftyp(ISO-14496-12)
Author:Pirate Leo
Email:codeevoship@gmail.com
ISO 14496 - 12 定義了一種封裝媒體數據的基礎文件格式,mp4、3gp、ismv等我們常見媒體封裝格式都是以這種基礎文件格式為基礎衍生的。
如果從全局角度了解基礎文件格式,請看我之前的博文《MP4文件格式詳解——結構概述》。
本系列文檔從MP4文件入手,對文件中重要的box進行解析。
<======================================================================>
本次解析ftyp box
- aligned(8) class FileTypeBox extends Box(‘ftyp’)
- {
- unsigned int(32) major_brand;
- unsigned int(32) minor_version;
- unsigned int(32) compatible_brands[]; // to end of the box
- }
aligned(8) class FileTypeBox extends Box(‘ftyp’) { unsigned int(32) major_brand; unsigned int(32) minor_version; unsigned int(32) compatible_brands[]; // to end of the box }
什么是brands?官方是這樣描述的:
Each brand is a printable four-character code, registered with ISO, that identifies a precise specification.//在ISO注冊的4個字符。
下表來源於網絡,列出了幾種常見的基於基礎文件格式的,媒體封裝格式標識。
更多的內容可以查看http://www.ftyps.com/
Brand | Extension | Mime Type | |
MP4 | mp41, mp42 | .mp4 | video/mp4, audio/mp4, application/mp4 |
3GPP | various, e.g. 3gp4, 3gp5 | .3gp | video/3gpp, audio/3gpp |
3GPP2 | 3g2a | .3g2 | video/3gpp2, audio/3gpp2 |
Motion JPEG 2000 | mjp2 | .mj2 | video/mj2 |
QuickTime | "qt" | .mov | video/quicktime |
結合實際文件,下圖是MP4文件起始位置存放的數據
length(4字節):0x0000001c:box的長度是28字節;
boxtype(4字節):0x66747970:“ftyp”的ASCII碼,box的標識;
major_brand(4字節):0x69736f6d:“isom“的ASCII碼;
minor_version(4字節):0x00000200:ismo的版本號;
compatible_brands(12字節):說明本文件遵從(或稱兼容)ismo,iso2,mp41三種協議。
ftyp box通常放在文件的開始,通過對該box解析可以讓我們的軟件(播放器、demux、解析器)知道應該使用哪種協議對這該文件解析,是后續解讀文件基礎。