mp4文件格式系列
mp4文件格式系列1???-???綜述
Overview and Introduction
Core Concepts
MP4文件格式中,所有的內容存在一個稱為movie的容器中。一個movie可以由多個tracks組成。每個track就是一個隨時間變化的媒體序列,例如,視頻幀序列。track里的每個時間單位是一個sample,它可以是一幀視頻,或者音頻。sample按照時間順序排列。注意,一幀音頻可以分解成多個音頻sample,所以音頻一般用sample作為單位,而不用幀。MP4文件格式的定義里面,用sample這個單詞表示一個時間幀或者數據單元。每個track會有一個或者多個sample descriptions。track里面的每個sample通過引用關聯到一個sample description。這個sample descriptions定義了怎樣解碼這個sample,例如使用的壓縮算法。
與其他的多媒體文件格式不同的是,MP4文件格式經常使用幾個不同的概念,理解其不同是理解這個文件格式的關鍵。
這個文件的物理格式沒有限定媒體本身的格式。例如,許多文件格式將媒體數據分成幀,頭部或者其他數據緊緊跟隨每一幀視頻,!!!TODO(例如MPEG2)。而MP4文件格式不是如此。
文件的物理格式和媒體數據的排列都不受媒體的時間順序的限制。視頻幀不需要在文件按時間順序排列。這就意味著如果文件中真的存在這樣的一些幀,那么就有一些文件結構來描述媒體的排列和對應的時間信息。
MP4文件中所有的數據都封裝在一些box中(以前叫atom)。所有的metadata(媒體描述元數據),包括定義媒體的排列和時間信息的數據都包含在這樣的一些結構box中。MP4文件格式定義了這些這些box的格式。Metadata對媒體數據(例如,視頻幀)引用說明。媒體數據可以包含在同一個的一個或多個box里,也可以在其他文件中,metadata允許使用URLs來引用其他的文件,而媒體數據在這些引用文件中的排列關系全部在第一個主文件中的metadata描述。其他的文件不一定是MP4文件格式,例如,可能就沒有一個box。
有很多種類的track,其中有三個最重要,video track包含了視頻sample;audio track包含了audio sample;hint track稍有不同,它描述了一個流媒體服務器如何把文件中的媒體數據組成符合流媒體協議的數據包。 如果文件只是本地播放,可以忽略hint track,他們只與流媒體有關系。
Physical structure of the media
Box定義了如何在sample table中找到媒體數據的排列。這包括data reference(數據引用), the sample size table, the sample to chunk table, and the chunk offset table. 這些表就可以找到track中每個sample在文件中的位置和大小。
data reference允許在第二個媒體文件中找到媒體的位置。這樣,一部電影就可以由一個媒體數據庫中的多個不同文件組成,而且不用把它們全部拷貝到另一個新文件中。例如,對視頻編輯就很有幫助。
為了節約空間,這些表都很緊湊。另外,interleave不是sample by sample,而是把單個track的幾個samples組合到一起,然后另外幾個sample又進行新的組合,等等。一個track的連續幾個sample組成的單元就被稱為chunk。每個chunk在文件中有一個偏移量,這個偏移量是從文件開頭算起的,在這個chunk內,sample是連續存儲的。
這樣,如果一個chunk包含兩個sample,第二個sample的位置就是chunk的偏移量加上第一個sample的大小。chunk offset table說明了每個chunk的偏移量,sample to chunk table說明了sample序號和chunk序號的映射關系。
注意chunk之間可能會有死區,沒有任何媒體數據引用到這部分區域,但是chunk內部不會有這樣的死區。這樣,如果在節目編輯的時候,不需要一些媒體數據,就可以簡單的留在那里,而不用引用,這樣就不用刪除它們了。類似的,如果媒體存放在第二個文件中,但是格式不同于MP4文件格式,這個陌生文件的頭部或者其他文件格式都可以簡單忽略掉。
Temporal structure of the media
文件中的時間可以理解為一些結構。電影以及每個track都有一個timescale。它定義了一個時間軸來說明每秒鐘有多少個ticks。合理的選擇這個數目,就可以實現準確的計時。一般來說,對于audio track,就是audio的sampling rate。對于video track,情況稍微復雜,需要合理選擇。例如,如果一個media TimeScale是30000,media sample durations是1001,就準確的定義了NTSC video的時間格式(雖然不準確,但一般就是29.97),and provide 19.9 hours of time in 32 bits.
Track的時間結構受一個edit list影響,有兩個用途:全部電影中的一個track的一部分時間片斷變化(有可能是重用);空白時間的插入,也就是空的edits。特別注意的是如果一個track不是從節目開頭部分開始,edit list的第一個edit就一定是空的edit。
每個track的全部duration定義在文件頭部,這就是對track的總結,每個sample有一個規定的duration。一個sample的準確描述時間,也就是他的時間戳(time-stamp)就是以前的sample的duration之和。
Interleave
文件的時間和物理結構可以是對齊的,這表明媒體數據在容器中的物理順序就是時間順序。另外,如果多個track的媒體數據包含在同一個文件中,這個媒體數據可以是interleaved。一般來說,為了方便讀取一個track的媒體數據,同時保證每個表緊湊,以一個合適的時間間隔(例如1秒)做一次interleave,而不是sample by sample。這樣就可以減少chunk的數據,減小chunk offset table的大小。
Composition
如果多個audio track包含在同一個文件中,他們有可能被混合在一起進行播放,并且由一個總track volume和左/右balance控制。
類似的,video track也可以根據各自的層次序列號(從后向前)和合成模式進行混合。另外,每個track可以用一個matrix進行變換,也可以全部電影用一個matrix進行變換。這樣既可以進行簡單操作(例如放大圖像,校正90o 旋轉),也可以做更復雜的操作(例如shearing, arbitrary rotation)。
這個混合方法只是非常簡單,是一個缺省的方法,MPEG4的另一份文檔會定義更強有力的方法(例如MPEG-4 BIFS)。
mp4文件格式系列2?-?mp4文件例子
Darwin Streaming Server里面有一些很好的工具,可以幫助分析mp4文件格式。
但是如果可以自己逐字節的parse文件,可以更好的了解mp4文件格式。這里我就逐字節的分析文件結構。文件例子是DSS里面包含的sample_100kbit.mp4
mp4文件格式系列3?-?Movie?Atom?-?MOOV
Movie atom定義了一部電影的數據信息。它的類型是'moov',是一個容器atom,至少必須包含三種atom中的一種—movie header atom('mvhd'), compressed movie atom('cmov')和reference movie atom ('rmra')。沒有壓縮的 movie header atom必須至少包含movie header atom 和reference movie atom中的一種。也可以包含其他的atom,例如一個clipping atom ('clip'),一個或幾個track atoms ('trak'),一個color table atom ('ctab'),和一個user data atom ('udta')。其中movie header atom定義了整部電影的time scale,duration信息以及display characteristics。track atom定義了電影中一個track的信息。Track就是電影中可以獨立操作的媒體單位,例如一個聲道就是一個track。
Compressed movie atoms 和reference movie atoms 不太使用,不在本文討論范圍內。本文主要討論uncompressed movie atoms。
?
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個movie header atom的字節數 |
| 類型 | 4 | moov |
?
以下是實際的sample_100kbit.mp4的部分字節,可以看到結果是
?
主要包含四個子atom,movie header atom(mvhd), 一個audio track atom(trak),一個video track atom(trak)。
mp4文件格式系列4?-?Movie?Header?Atoms?-?MVHD
?
Movie header atom定義了整個movie的特性,例如time scale和duration,它的atom類型是'mvhd'。
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個movie header atom的字節數 |
| 類型 | 4 | Mvhd |
| 版本 | 1 | 這個movie header atom的版本 |
| 標志 | 3 | 擴展的movie header標志,這里為0 |
| 生成時間 | 4 | Movie atom的起始時間。基準時間是1904-1-1 0:00 AM |
| 修訂時間 | 4 | Movie atom的修訂時間。基準時間是1904-1-1 0:00 AM |
| Time scale | 4 | A time value that indicates the time scale for this movie—that is, the number of time units that pass per second in its time coordinate system. A time coordinate system that measures time in sixtieths of a second, for example, has a time scale of 60. |
| Duration | 4 | A time value that indicates the duration of the movie in time scale units.Note that this property is derived from the movie’s tracks. The value of this field corresponds to the duration of the longest track in the movie. ? |
| 播放速度 | 4 | 播放此movie的速度。1.0為正常播放速度 |
| 播放音量 | 2 | 播放此movie的音量。1.0為最大音量 |
| 保留 | 10 | 這里為0 |
| 矩陣結構 | 36 | 該矩陣定義了此movie中兩個坐標空間的映射關系 |
| 預覽時間 | 4 | 開始預覽此movie的時間 |
| 預覽duration | 4 | 以movie的time scale為單位,預覽的duration |
| Poster time | 4 | The time value of the time of the movie poster. |
| Selection time | 4 | The time value for the start time of the current selection. |
| Selection duration | 4 | The duration of the current selection in movie time scale units. |
| 當前時間 | 4 | 當前時間 |
| 下一個track ID | 4 | 下一個待添加track的ID值。0不是一個有效的ID值。 |
一個Track atom定義了movie中的一個track。一部movie可以包含一個或多個tracks,它們之間相互獨立,各自有各自的時間和空間信息。每個track atom 都有與之關聯的media atom。
Track主要用于以下目的:
·???????????????????? 包含媒體數據引用和描述(media tracks)
·???????????????????? 包含modifier tracks (tweens等)
·???????????????????? 對于流媒體協議的打包信息(hint tracks)。Hint tracks可以引用或者復制對應的媒體sample data。
Hint tracks和modifier tracks必須保證完整性,同時和至少一個media track一起存在。換句話說,即使hint tracks復制了對應的媒體sample data,media tracks 也不能從一部hinted movie中刪除。
Track atoms 的atom類型是'trak'. Track atom要求必須有一個track header atom ('tkhd') 和一個media atom ('mdia')。其他的track clipping atom ('clip'),track matte atom ('matt'),edit atom ('edts'),track reference atom ('tref'),track load settings atom ('load'),a track input map atom ('imap')以及user data atom ('udta')都是可選的。
Track atoms是一個容器atom,本身沒有特別的字段,需要子atom來進一步說明有效的內容。
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | Edts |
Audio track的值
Video track的值
每個trak都包含了一個track header atom
mp4文件格式系列6?-?Track?Header?Atoms?-?TKHD
每個trak都包含了一個track header atom. The track header atom 定義了一個track的特性,例如時間,空間和音量信息,它的類型是('tkhd').
?
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | tkhd |
| 版本 | 1 | 這個atom的版本 |
| 標志 | 3 | 有效的標志是 ·???????????????????? 0x0001 - the track is enabled ·???????????????????? 0x0002 - the track is used in the movie ·???????????????????? 0x0004 - the track is used in the movie’s preview ·???????????????????? 0x0008 - the track is used in the movie’s poster |
| 生成時間 | 4 | Movie atom的起始時間。基準時間是1904-1-1 0:00 AM |
| 修訂時間 | 4 | Movie atom的修訂時間。基準時間是1904-1-1 0:00 AM |
| Track ID | 4 | 唯一標志該track的一個非零值。 |
| 保留 | 4 | 這里為0 ? |
| Duration | 4 | The duration of this track (in the movie’s time coordinate system). Note that this property is derived from the track’s edits. The value of this field is equal to the sum of the durations of all of the track’s edits. If there is no edit list, then the duration is the sum of the sample durations, converted into the movie timescale. |
| 保留 | 8 | 這里為0 |
| Layer | 2 | The track’s spatial priority in its movie. The QuickTime Movie Toolbox uses this value to determine how tracks overlay one another. Tracks with lower layer values are displayed in front of tracks with higher layer values. |
| Alternate group | 2 | A collection of movie tracks that contain alternate data for one another. QuickTime chooses one track from the group to be used when the movie is played. The choice may be based on such considerations as playback quality, language, or the capabilities of the computer. |
| 音量 | 2 | 播放此track的音量。1.0為正常音量 |
| 保留 | 2 | 這里為0 |
| 矩陣結構 | 36 | 該矩陣定義了此track中兩個坐標空間的映射關系 |
| 寬度 | 4 | 如果該track是video track,此值為圖像的寬度 |
| 高度 | 4 | 如果該track是video track,此值為圖像的高度 |
?
Audio track的值
?
Video track的值mp4文件格式系列7?-?Edit?Atoms?-?EDTS
Edit atoms 定義了創建movie中一個track的一部分媒體。所有的edit都在一個表里面,包括每一部分的時間偏移量和長度。Edit atoms 的類型是'edts'。如果沒有該表,則此track會被立即播放。一個空的edit用來偏移track的起始時間。
如果沒有edit atom 或edit list atom,則此track使用全部媒體。
Edit atoms是一個容器atom,本身沒有特別的字段,需要子atom來進一步說明有效的內容。
?
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | Edts |
Audio track的值
?
Video track的值
?
mp4文件格式系列8?-?Edit?List?Atoms?-?ELST
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | elst |
| 版本 | 1 | 這個atom的版本 |
| 標志 | 3 | 這里為0 |
| 條目數目 | 4 | 后面的edit list表中的條目數目 |
| edit list表 | 可變 | 每一個條目包含3項,見下圖和下表 |
?
?
| 字段 | 長度(字節) | 描述 |
| Track duration | 4 | duration of this edit segment in units of the movie’s time scale. |
| 時間 | 4 | starting time within the media of this edit segment (in media timescale units)。值為-1表示是空edit。Track中的最后一個edit永遠不能為空。Any difference between the movie’s duration and the track’s duration is expressed as an implicit empty edit. |
| 速度 | 4 | relative rate at which to play the media corresponding to this edit segment。不能是0或負數。 |
Audio track的值
?
Video track的值 mp4文件格式系列9?-?Track?Reference?Atoms?-?TREFTrack reference atoms define relationships between tracks. Track reference atoms allow one track to specify how it is related to other tracks. For example, if a movie has three video tracks and three sound tracks, track references allow you to identify the related sound and video tracks. ?Track reference atoms have an atom type value of 'tref'.
Track references are uni-directional and point from the recipient track to the source track. For example, a video track may reference a time code track to indicate where its time code is stored, but the time code track would not reference the video track. The time code track is the source of time information for the video track.
A single track may reference multiple tracks. For example, a video track could reference a sound track to indicate that the two are synchronized and a time code track to indicate where its time code is stored.
A single track may also be referenced by multiple tracks. For example, both a sound and video track could reference the same time code track if they share the same timing information.
If this atom is not present, the track is not referencing any other track in any way. Note that the array of track reference type atoms is sized to fill the track reference atom. Track references with a reference index of 0 are permitted. This indicates no reference.
Each track reference atom defines relationships with tracks of a specific type. The reference type
implies a track type. Following table shows the track reference types and their descriptions.
Table: Track reference types
?
| Reference type | Description |
| tmcd | Time code. Usually references a time code track. |
| chap | Chapter or scene list. Usually references a text track. |
| sync | Synchronization. Usually between a video and sound track. Indicates that the two tracks are synchronized. The reference can be from either track to the other, or there may be two references. |
| scpt | Transcript. Usually references a text track. |
| ssrc | Nonprimary source. Indicates that the referenced track should send its data to this track, rather than presenting it. The referencing track will use the data to modify how it presents its data. See “Track Input Map Atoms” (page 51) for more information. |
| hint | The referenced tracks contain the original media for this hint track. |
Each track reference type atom contains the following data elements.
Size
A 32-bit integer that specifies the number of bytes in this track reference type atom.
Type
A 32-bit integer that identifies the atom type; this field must be set to one of the values shown
in above table.
Track IDs
A list of track ID values (32-bit integers) specifying the related tracks. Note that this is one case
where track ID values can be set to 0. Unused entries in the atom may have a track ID value
of 0. Setting the track ID to 0 may be more convenient than deleting the reference.
You can determine the number of track references stored in a track reference type atom by subtracting its header size from its overall size and then dividing by the size, in bytes, of a track ID.
mp4文件格式系列10?-?Media?Atoms?-?MDIA
Media atoms定義了track的媒體類型和sample數據,例如音頻或視頻,描述sample數據的media handler component,media timescale and track duration以及media-and-track-specific 信息,例如音量和圖形模式。它也可以包含一個引用,指明媒體數據存儲在另一個文件中。也可以包含一個sample table atoms,指明sample description, duration, and byte offset from the data reference for each media sample.
Media atom 的類型是'mdia'。它是一個容器atom,必須包含一個media header atom ('mdhd'),一個handler reference ('hdlr'),一個媒體信息引用('minf')和用戶數據atom('udta').
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | Edts |
Audio track的值
?
Video track的值 mp4文件格式系列11?-?Media?Header?Atoms?-?MDHDMedia header atom 定義了媒體的特性,例如time scale和duration。它的類型是'mdhd'.
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | mdhd |
| 版本 | 1 | 這個atom的版本 |
| 標志 | 3 | 這里為0 |
| 生成時間 | 4 | Movie atom的起始時間。基準時間是1904-1-1 0:00 AM |
| 修訂時間 | 4 | Movie atom的修訂時間。基準時間是1904-1-1 0:00 AM |
| Time scale | 4 | A time value that indicates the time scale for this media—that is, the number of time units that pass per second in its time coordinate system. |
| Duration | 4 | The duration of this media in units of its time scale. |
| 語言 | 2 | 媒體的語言碼 |
| 質量 | 2 | 媒體的回放質量???怎樣生成此質量,什么是參照點 |
mp4文件格式系列12?-?Handler?Reference?Atoms?-?HDLR
Handler reference atom 定義了描述此媒體數據的media handler component,類型是'hdlr'。在過去,handler reference atom也可以用來數據引用,但是現在,已經不允許這樣使用了。一個media atom內的handler atom解釋了媒體流的播放過程。例如,一個視頻handler處理一個video track。
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | hdlr |
| 版本 | 1 | 這個atom的版本 |
| 標志 | 3 | 這里為0 |
| Component type | 4 | handler的類型。當前只有兩種類型 ·???????????????????? 'mhlr':media handlers ·???????????????????? 'dhlr':data handlers |
| Component subtype | 4 | media handler or data handler的類型。 如果component type是mhlr,這個字段定義了數據的類型,例如,'vide'是video數據,'soun'是sound數據 如果component type是dhlr,這個字段定義了數據引用的類型,例如,'alis'是文件的別名 |
| Component manufacturer | 4 | 保留字段,缺省為0 |
| Component flags | 4 | 保留字段,缺省為0 |
| Component flags mask | 4 | 保留字段,缺省為0 |
| Component name | 可變 | 這個component的名字,也就是生成此media的media handler。該字段的長度可以為0 |
Media information atoms的類型是'minf',存儲了解釋該track的媒體數據的handler-specific的信息。media handler用這些信息將媒體時間映射到媒體數據,并進行處理。它是一個容器atom,包含其他的子atom。
這些信息是與媒體定義的數據類型特別對應的,而且media information atoms 的格式和內容也是與解釋此媒體數據流的media handler 密切相關的。其他的media handler不知道如何解釋這些信息。
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | minf |
mp4文件格式系列14?-?Video?Media?Information?Atoms
Video media information atoms是視頻媒體的第一層atoms,包含其他的定義視頻媒體數據的特性。
mp4文件格式系列15?-?Sound?Media?Information?Atoms
Sound media information atoms是音頻媒體的第一層atoms,包含其他的定義音頻媒體數據的特性。
mp4文件格式系列16?-?Video?Media?Information?Header?Atoms
Video media information header atoms 定義顏色和圖形模式信息。
?
| 字段 | 長度(字節) | 描述 |
| 尺寸 | 4 | 這個atom的字節數 |
| 類型 | 4 | vmhd |
| 版本 | 1 | 這個atom的版本 |
| 標志 | 3 | 這里總是0x000001 |
| 圖形模式 | 2 | The transfer mode. The transfer mode specifies which Boolean operation QuickDrawshould performwhen drawing or transferring an image fromone location to another. |
| Opcolor | 6 | Three 16-bit values that specify the red, green, and blue colors for the transfer mode operation indicated in the graphics mode field. |
17?-?Sound?Media?Information?Header?Atoms?-?SMHD
The sound media information header atom定義了聲音媒體的控制信息,例如均衡。
總結
- 上一篇: Android中的Intent和Inte
- 下一篇: C#接口中为什么不能像java那样使用s