MP4文件格式詳解——元數據moov(二)tkhd box

元數據moov(二)tkhd box(ISO-14496-12)

Author:Pirate Leo

Email:[email protected]

ISO 14496 - 12 定義了一種封裝媒體數據的基礎文件格式,mp4、3gp、ismv等我們常見媒體封裝格式都是以這種基礎文件格式爲基礎衍生的。

如果從全局角度瞭解基礎文件格式,請看我之前的博文《MP4文件格式詳解——結構概述》。

本系列文檔從MP4文件入手,對文件中重要的box進行解析。

<======================================================================>

本次繼續解析moov box,關於moov的解析推薦從我上一篇博文《MP4文件格式詳解——元數據moov(一)》看起。

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


前面我們已經知道每個文件是由多個Track(軌道)組成的,每個Track都對應了自身trak box,其中存放了本track的元數據信息。

本次繼續解析trak box的一系列子box:

1)tkhd box

aligned(8) class TrackHeaderBox extends FullBox(‘tkhd’, version, flags)
{
    if (version==1) 
    { 
      unsigned int(64) creation_time; 
      unsigned int(64) modification_time; 
      unsigned int(32) track_ID; 
      const unsigned int(32)  reserved = 0; 
      unsigned int(64) duration; 
    } 
    else 
    {   // version==0 
        unsigned int(32) creation_time; 
        unsigned int(32) modification_time; 
        unsigned int(32) track_ID; 
        const unsigned int(32)  reserved = 0; 
        unsigned int(32) duration; 
    } 
    const unsigned int(32)[2]  reserved = 0; 
    template int(16) layer = 0; 
    template int(16) alternate_group = 0; 
    template int(16)  volume = {if track_is_audio 0x0100 else 0}; 
    const unsigned int(16)  reserved = 0; 
    template int(32)[9]  matrix= { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; 
    // unity matrix 
    unsigned int(32) width; 
    unsigned int(32) height; 
}
類似我們moov中的mvhd box,但tkhd僅僅描述的單一Track的特性。


上圖是實際媒體中的tkhd的數據:

0x5c是tkhd box長度,0x746b6864是“tkhd”的ASCII碼。

0x00 00 00 0f是使用了Full box中的flag位(Full box 8bits version + 24bits flag,詳見我第一篇日誌),這裏flag= 0xf,即1111b。

這4位從低到高分別代表:

Track_enabled: Indicates that the track is enabled.  若此位爲0,則該track內容無需播放(比如我們用一些非線編軟件<如Sony Vegas>做視頻剪輯時,有些Track僅爲我們參考與模仿用,在輸出時將該Track關掉)。
Track_in_movie: Indicates that the track is used in the presentation. 
Track_in_preview: Indicates that the track is used when previewing the presentation.

Track_in_poster: Indicates that the track is used in movie's poster.

/* -------------------------------------------------------------------------------------------------------*/

important:我們知道,MP4文件格式是ISO-14496-12基礎文件格式的衍生品,14496-14中對-12協議進行了擴充與進一步定義。

重要的是該“14496-12 基礎文件格式”協議如果認祖歸宗,我們發現這種文件格式最初是由Apple公司的QuickTime媒體格式發展而來的。

即,mov格式發展出了“ISO 14496 - 12協議”,再由該協議衍生出了mp4,f4v,ismv,3gp等我們常見的媒體封裝格式。

因此上述標誌位的poster位,在14496-12中並沒有見到描述,而在Apple的協議中卻看到了準確定義。

詳見 https://developer.apple.com/library/mac/#documentation/QuickTime/QTFF/QTFFChap2/qtff2.html

/* -------------------------------------------------------------------------------------------------------*/


兩個0xc5268eb6 是track的創建時間與最後修改時間;

緊隨其後的0x00000002,代表track ID =2,Track ID是非0的,唯一的,不可重複使用的標識track的方式;

後面32bit全0是保留位;

0x0009d97c是本track的時長,需要配合mvhd box中的timescale 計算實際的持續時間。

後續一些寫死的字段不再分析,有些與mvhd重複,可以參見之前的文章。我們看兩個關鍵字段:

layer,類似photoshop中圖層的概念,數值小的在播放時更貼近用戶(上層圖層)。

alternate_group,track的備用分組ID,當該值爲0時,意味着本track內容無備份;否則本track會可能會有零到多個備份track。當播放時相同group ID的track只選擇一個進行播放。


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