MP4格式分析之元數據moov(一)

movie box —— container box whose sub-boxes define the metadata for a presentation (‘moov’) 

moov包含的一系列次級box中存儲着媒體播放所需的元數據(metadata)。

兩點疑問:什麼是元數據?moov有哪些次級box?


1)元數據:描述數據的數據。針對媒體文件而言元數據都有哪些呢?爲了讓大家直觀瞭解:


上圖是使用“格式工廠”獲取某MP4文件的媒體信息,這些媒體信息基本都包含在moov中。

視頻包括編碼等級、分辨率、色域、碼率、幀率、位深、時長等等……

音頻又包括聲道、採樣率等音頻特有屬性。

這些元數據對於我們的價值在於:我們的系統(比如PC播放器,高清播放機)可以通過對moov box的解析,自動適配運行在某種模式下去播放影片。在嵌入式領域,由於DSP或ARM的Ram空間有限,經常需要動態加載本次播放所需的解碼器(算法程序),通過自適配可以用最廉價的CPU,完成一款支持多碼率多格式的全能播放器。


2)moov有哪些次級box用來保存這些信息?


 

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

先對兩個重要的box作功能上的描述,每個box具體組成細節在以後的篇幅中將詳細介紹。

mvhd ——This box defines overall information which is media-independent, and relevant to the entire presentation.

全文件唯一的(一個文件中只能包含一個mvhd box),對整個文件所包含的媒體數據作全面的全局的描述。包含了媒體的創建與修改時間時間刻度、默認音量、色域、時長等信息。

 

  1. aligned(8) class MovieHeaderBox extends FullBox(‘mvhd’, version, 0)   
  2. {   
  3.     if (version==1)   
  4.     {   
  5.         unsigned int(64) creation_time;   
  6.         unsigned int(64) modification_time;   
  7.         unsigned int(32) timescale;   
  8.         unsigned int(64) duration;   
  9.     }   
  10.     else   
  11.     { // version==0   
  12.         unsigned int(32) creation_time;   
  13.         unsigned int(32) modification_time;   
  14.         unsigned int(32) timescale;   
  15.         unsigned int(32) duration;   
  16.     }   
  17.     template int(32)  rate = 0x00010000; // typically 1.0   
  18.     template int(16)  volume = 0x0100;  // typically, full volume   
  19.     const bit(16)  reserved = 0;   
  20.     const unsigned int(32)[2]  reserved = 0;   
  21.     template int(32)[9]  matrix = { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 };   
  22.     // Unity matrix   
  23.     bit(32)[6]  pre_defined = 0;   
  24.     unsigned int(32) next_track_ID;   
  25. }   
首先mvhd是個Full Box,引用我第一篇文中解釋:“ FullBox,是Box的擴展,Box結構的基礎上在Header中增加8bits version和24bits flags。”,見下圖:

圖中標紅的是moov box的長度與標識(其實這種box結構類似TLV,稱爲LTV更直觀)。

標藍的是mvhd的長度0x6c,表黃的是mvhd的標識與內容:

0x6D766864 是mvhd的ASCII標識;

0x00000000 是FullBox擴展出來的標識位,這裏是全0,Version和flags都是0,參見前面的結構定義可知後面的時間與時長採用了32bit表示方式。

第一個0x7C25B080是創建時間,第二個0x7C25B080是最後修改時間。可見媒體未被修改過。這兩個數值是怎麼描述具體時間的呢?

 time is an integer that declares the creation time of the presentation (in seconds since midnight, Jan. 1, 1904, in UTC time) 

即,從UTC時間的1904年1月1日0點至今的秒數。我們手動算一下:

0x7C25B080 = 2082844800秒,大概是66.0465年(每年按365天算,不考慮閏年),1904+66=1970年中。看來這個媒體文件生成的並不規範,沒有按照ISO的規範填寫創建與修改時間(by the way,該文件是一年前使用iKu轉碼生成)。不過這裏的時間並不影響播放器識別並播放影片。 

我們假設4字節的時間描述取其最大值0xFFFFFFFF,通過計算最多支持到2040年。如果,假設該參數是播放視頻所必備的參數,那麼到2040年後,也許所有的MP4文件一夜間就無法播放了~(0 == Version這種)。

0x000003E8 是timescale,該數值表示本文件的所有時間描述所採用的單位。0x3E8 = 1000,即將1s平均分爲1000份,每份1ms。

0x000A06A2 是duration,媒體可播放時長,0xA06A2 =  657058,這個數值的單位與實際時間的對應關係就要通過上面的timescale參數。

duration / timescale = 可播放時長(s)。這裏算出該視頻能播放657.058s。使用MPC打開,時長與我們計算的一致。


timescale時間刻度貫穿在整個文件中,所有對於時間的描述都要以其爲參照,例如解碼時間DTS,展示時間PTS等最重要的時間描述。

0x00010000 媒體速率,這個值代表原始倍速。

0x0100 媒體音量,這個值代表滿音量。

接下來的一系列值都是結構中的預定義值,參見結構定義即可。


trak——This is a container box for a single track of a presentation. A presentation consists of one or more tracks. Each track is independent of the other tracks in the presentation and carries its own temporal and spatial information. Each track will contain its associated Media Box.

其次級box中包含了單個媒體軌道(Track)所特有的描述信息。

我們知道一部視頻一般都有多個軌道組成。

例如,《讓子彈飛》的正版DVD,1)有一條視頻軌用於電影畫面。2)至少有兩條音頻軌分別提供了普通話與四川話版,實際上爲了營造更加逼真的現場效果,爲了配合多聲道家庭影院該影片還獨有一條音效軌。3)多條字幕軌,簡體中文,繁體中文,英文……。從中我們可以理解爲什麼trak box可以有多個:每個track都是獨立的,具有自我特徵與屬性的,因此需要各自描述互不干涉。


我們一般所看的MP4文件中moov下都至少是mvhd-trak-trak-trak...這種構成。


後續將對trak下的一系列子box進行解析

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