资讯详情

MP4格式分析

介绍

mp四件格式也叫MPEG-4 Part 14,出?MPEG-4标准第14部分 。它是一种多媒体格式容器,广泛应用于包装视频和频数据流、海报、字幕和元数据。(对了,流行的视频编码格式AVC/H264 定义在MPEG-4 Part 10)。 mp基于4件格式Apple公司的QuickTime格式,所以,QuickTime File Format Specification 也可作为我们的研究mp4的重要参考 MP4文件结构的资料 mp4box大杀器

概述

mp4?件由box组成,每个box分为Header和Data。其中Header部分包含了box类型和大小,Data包含 了子box或者数据,box可以嵌套?box。 下图是典型的mp四件的基本结构 在这里插入图片描述 MP4文件的基本组成单元是box,也就是说MP四是各种各样的零件。box组成的,有parent box,还有 children box。因此,这些boxes之间存在?定的层次关系,总结如下表所示,表中标记出了各个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)
hmhd hint media header, overall information (hint track only)
nmhd Null media header, overall information (some tracks only)
dinf data information box, container 数据信息box,容器
dref data reference box, declares source(s) of media data in track 如何定位媒体信息
stbl sample table box, container for the time/space map 包含了track中的sample的所有时间和位置信息,以及sample的编解码等信息。利⽤这个表 可以解析sample的时序、类型、大小以及在各自存储容器中的位置。
stsd sample descriptions (codec types, initialization etc.) 如果是视频,包含:编码类型、宽⾼、⻓度等 信息; 如果是⾳频,包含:声道、采样率等信息
stts (decoding) time-to-sample 描述了sample时序的映射⽅法,我们可以通过 它找到任何时间的sample。
ctts (composition) time to sample
stsc sample-to-chunk, partial data-offset information ⽤chunk组织sample可以⽅便优化数据获取, ⼀个chunk包含⼀个或多个sample。
stsz sample sizes (framing) 每个sample的大小。 虽然这⾥没有打勾,但对于mp4还是非常必要的。
stz2 compact sample sizes (framing)
stco chunk offset, partial data-offset information 定义了每个chunk在媒体流中的偏移位置
co6 64-bit chunk offset
stss sync sample table (random access points) ⽤于确定media中的关键帧
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

本文使用mediainfo和mp4box进⾏分析 图中看到mp4⽂件由几个主要组成部分,分析案例:

2_audio_track_5s

ftyp — File Type Box

⼀般在文件的开始位置,描述的⽂件的版本、兼容协议等。

moov — Movie Box

Movie Box,包含本⽂件中所有媒体数据的宏观描述信息以及每路媒体轨道的具体信息。⼀般位于放在⽂ 件末尾,但如果为了⽀持http边下载边播放则需要将moov提前。 注意,当改变moov位置时,内部⼀些值 需要重新计算。 moov⾥⾯的box才是我们主要分析的box

mdat — Media Data Box

存放具体的媒体数据。

Moov Insider

mp4的媒体数据信息主要存放在Moov Box中,是我们需要分析的重点。moov的主要组成部分如下:

mvhd — Movie Header Box

Movie Header Box,记录整个媒体⽂件的描述信息,如创建时间、修改时间、时间度量标尺、可播放时 ⻓等。 下图示例中,可以获取⽂件信息如时⻓为 Duration: 5016 ms秒 文件持续播放时间:Duration/Time scale=5.016秒

14B2D6  Movie header (108 bytes)
14B2D6   Header (8 bytes)
14B2D6    Size:                                108 (0x0000006C)
14B2DA    Name:                                mvhd
14B2DE   Version:                              0 (0x00)
14B2DF   Flags:                                0 (0x000000)
14B2E2   Creation time:                        0 (0x00000000) - 
14B2E6   Modification time:                    0 (0x00000000) - 
14B2EA   Time scale:                           1000 (0x000003E8) - 1000 Hz
14B2EE   Duration:                             5016 (0x00001398) - 5016 ms
14B2F2   Preferred rate:                       65536 (0x00010000) - 1.000
14B2F6   Preferred volume:                     256 (0x0100) - 1.000
14B2F8   Reserved:                             (10 bytes)
14B302   Matrix structure (36 bytes)
14B302    a (width scale):                     1.000
14B306    b (width rotate):                    0.000
14B30A    u (width angle):                     0.000
14B30E    c (height rotate):                   0.000
14B312    d (height scale):                    1.000
14B316    v (height angle):                    0.000
14B31A    x (position left):                   0.000
14B31E    y (position top):                    0.000
14B322    w (divider):                         1.000
14B326   Preview time:                         0 (0x00000000)
14B32A   Preview duration:                     0 (0x00000000)
14B32E   Poster time:                          0 (0x00000000)
14B332   Selection time:                       0 (0x00000000)
14B336   Selection duration:                   0 (0x00000000)
14B33A   Current time:                         0 (0x00000000)
14B33E   Next track ID:                        4 (0x00000004)

udta — User Data Box

⾃定义数据

track — Track Box

记录媒体流信息,⽂件中可以存在⼀个或多个track,它们之间是相互独立的。 每个track包含以下⼏个组成部分:

tkhd — Track Header Box

包含关于媒体流的头信息。 下图示例中,可以看到流信息如视频流宽度720,长度1280。

视频的tkhd
14CEA6   Track Header - 3 (0x3) - 4875 (0x130B) ms (92 bytes)
14CEA6    Header (8 bytes)
14CEA6     Size:                               92 (0x0000005C)
14CEAA     Name:                               tkhd
14CEAE    Version:                             0 (0x00)
14CEAF    Flags:                               3 (0x000003)
14CEB2    Track Enabled:                       Yes
14CEB2    Track in Movie:                      2 (0x0000000000000002)
14CEB2    Track in Preview:                    0 (0x0000000000000000)
14CEB2    Track in Poster:                     0 (0x0000000000000000)
14CEB2    Creation time:                       0 (0x00000000) - 
14CEB6    Modification time:                   0 (0x00000000) - 
14CEBA    Track ID:                            3 (0x00000003)
14CEBE    Reserved:                            0 (0x00000000)
14CEC2    Duration:                            4875 (0x0000130B) - 4875 (0x130B) ms
14CEC6    Reserved:                            0 (0x00000000)
14CECA    Reserved:                            0 (0x00000000)
14CECE    Layer:                               0 (0x0000)
14CED0    Alternate group:                     2 (0x0002)
14CED2    Volume:                              0 (0x0000) - 0.000
14CED4    Reserved:                            0 (0x0000)
14CED6    Matrix structure (36 bytes)
14CED6     a (width scale):                    1.000
14CEDA     b (width rotate):                   0.000
14CEDE     u (width angle):                    0.000
14CEE2     c (height rotate):                  0.000
14CEE6     d (height scale):                   1.000
14CEEA     v (height angle):                   0.000
14CEEE     x (position left):                  0.000
14CEF2     y (position top):                   0.000
14CEF6     w (divider):                        1.000
14CEFA    Track width:                         1920.000
14CEFE    Track height:                        800.000

音频的tkhd
14B34A   Track Header - 1 (0x1) - 5016 (0x1398) ms (92 bytes)
14B34A    Header (8 bytes)
14B34A     Size:                               92 (0x0000005C)
14B34E     Name:                               tkhd
14B352    Version:                             0 (0x00)
14B353    Flags:                               3 (0x000003)
14B356    Track Enabled:                       Yes
14B356    Track in Movie:                      2 (0x0000000000000002)
14B356    Track in Preview:                    0 (0x0000000000000000)
14B356    Track in Poster:                     0 (0x0000000000000000)
14B356    Creation time:                       0 (0x00000000) - 
14B35A    Modification time:                   0 (0x00000000) - 
14B35E    Track ID:                            1 (0x00000001)
14B362    Reserved:                            0 (0x00000000)
14B366    Duration:                            5016 (0x00001398) - 5016 (0x1398) ms
14B36A    Reserved:                            0 (0x00000000)
14B36E    Reserved:                            0 (0x00000000)
14B372    Layer:                               0 (0x0000)
14B374    Alternate group:                     0 (0x0000)
14B376    Volume:                              256 (0x0100) - 1.000
14B378    Reserved:                            0 (0x0000)
14B37A    Matrix structure (36 bytes)
14B37A     a (width scale):                    1.000
14B37E     b (width rotate):                   0.000
14B382     u (width angle):                    0.000
14B386     c (height rotate):                  0.000
14B38A     d (height scale):                   1.000
14B38E     v (height angle):                   0.000
14B392     x (position left):                  0.000
14B396     y (position top):                   0.000
14B39A     w (divider):                        1.000
14B39E    Track width:                         0.000
14B3A2    Track height:                        0.000

mdia — Media Box

这是⼀个包含track媒体数据信息的container box。 子box包括:

  1. mdhd:Media Header Box,存放视频流创建时间,⻓度等信息。
  2. hdlr:Handler ReferenceBox,媒体的播放过程信息。
  3. minf:Media InformationBox,解释track媒体数据的handler-specific信息。minf同样是个containerbox,其内部需要关注的内容是stbl,这也是moov中最复杂的部分。stbl包含了媒体流每⼀个sample在⽂件中的offset,pts,duration等信息。想要播放⼀个mp4⽂件,必须根据stbl正确找到每个sample并送给 解码器。 mdia展开如下图所示
mdhd – Media Header Box

存放视频流创建时间,⻓度等信息。

视频的mdhd

音频的mdhd

⾳频的mdhd,也类似视频,但要注意Time scale,我们在计算时间戳的时候都要使⽤该Time scale,对 应我们流⾥⾯的AVStream->time_base

hdlr — Handler Reference Box

媒体的播放过程信息。

视频的hdlr

重点Component subtype:

音频的hdlr

minf — Media Information Box

解释track媒体数据的handler-specific信息。minf同样是个container box,其内部需要关注的内容是stbl,这也是moov中最复杂的部分。stbl包含了媒体流每⼀个sample在⽂ 件中的offset,pts,duration等信息。想要播放⼀个mp4⽂件,必须根据stbl正确找到每个sample并送给 解码器。 ⽽且需要注意的是,minf⾥⾯的⼦容器,⾳频和视频轨是有区别的

  1. 视频轨:vmhd
  2. 音频轨则为: smhd

Stbl Insider — Sample Table Box

上⽂提到mdia中最主要的部分是存放⽂件中每个sample信息的stbl。在解析stbl 前,我们需要区分chunk和sample这两个概念。 在mp4⽂件中,sample是⼀个媒体流的基本单元,例如视频流的⼀个sample代表实际的nal数据。chunk 是数据存储的基本单位,它是⼀系列sample数据的集合,⼀个chunk中可以包含⼀个或多的sample。 stbl⽤来描述每个sample的信息,包含以下⼏个主要的⼦box:

stsd — Sample Description Box

存放解码必须的描述信息。 下图示例中,对于h264的视频流,其具体类型为 avc1 ,extensions中其中存放有sps,pps等解码必要信息。

视频的stsd

⾥⾯包含了avc1,avc1⾥⾯⼜包含了avcC和pasp

  1. avc1:包含了视频Width、Height
  2. avcC:包含了视频编码器相关的信息,包括sps、pps等信息
14CFDF       Video (158 bytes)
14CFDF        Header (8 bytes)
14CFDF         Size:                           158 (0x0000009E)
14CFE3         Name:                           avc1
14CFE7        Reserved:                        0 (0x0000000000000000)
14CFED        Data reference index:            1 (0x0001)
14CFEF        Version:                         0 (0x0000)
14CFF1        Revision level:                  0 (0x0000)
14CFF3        Vendor:                          
14CFF7        Temporal quality:                0 (0x00000000)
14CFFB        Spatial quality:                 0 (0x00000000)
14CFFF        Width:                           1920 (0x0780)
14D001        Height:                          800 (0x0320)
14D003        Horizontal resolution:           4718592 (0x00480000)
14D007        Vertical resolution:             4718592 (0x00480000)
14D00B        Data size:                       0 (0x00000000)
14D00F        Frame count:                     1 (0x0001)
14D011        Compressor name size:            0 (0x00)
14D012        Padding:                         (31 bytes)
14D031        Depth:                           24 (0x0018)
14D033        Color table ID:                  65535 (0xFFFF)
14D035        AVC decode (56 bytes)
14D035         Header (8 bytes)
14D035          Size:                          56 (0x00000038)
14D039          Name:                          avcC
14D03D         Version:                        1 (0x01)
14D03E         Specific (47 bytes)
14D03E          Profile:                       100 (0x64)
14D03F          Compatible profile:            0 (0x00)
14D040          Level:                         40 (0x28)
14D041          Reserved:                      63 (0x3F) - (6 bits)
14D041          Size of NALU length minus 1:   3 (0x3) - (2 bits)
14D042          Reserved:                      7 (0x7) - (3 bits)
14D042          seq_parameter_set count:       1 (0x01) - (5 bits)
14D043          seq_parameter_set (30 bytes)
14D043           Size:                         28 (0x001C)
14D045           nal_ref_idc:                  3 (0x3) - (2 bits)
14D045           nal_unit_type:                7 
        标签: 2472d05po光电传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台