元数据moov(二)tkhd box(ISO-14496-12)
Author:Pirate Leo
Email:codeevoship@gmail.com
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;
- }<strong>
- </strong>
类似我们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只选择一个进行播放。
|