MP4文件格式详解(ISO-14496-12/14)

Author:Pirate Leo

Email:codeevoship@gmail.com

一、基本概念

1、 文件,由许多Box和FullBox组成。

2、 Box,每个Box由Header和Data组成。

3、 FullBox,是Box的扩展,Box结构的基础上在Header中增加8bits version和24bits flags。

4、 Header,包含了整个Box的长度size和类型type。当size==0时,代表这是文件中最后一个Box;当size==1时,意味着Box长度需要更多bits来描述,在后面会定义一个64bits的largesize描述Box的长度;当type是uuid时,代表Box中的数据是用户自定义扩展类型。

5、 Data,是Box的实际数据,可以是纯数据也可以是更多的子Boxes。

6、 当一个Box的Data中是一系列子Box时,这个Box又可成为Container Box。

Box的结构用伪代码表示如下:

  1. aligned(8) class Box (unsigned int(32) boxtype,optional unsigned int(8)[16] extended_type)
  2. {
  3. unsigned int(32) size;
  4. unsigned int(32) type = boxtype;
  5. if (size==1)
  6. {
  7. unsigned int(64) largesize;
  8. }
  9. else if (size==0)
  10. {
  11. // box extends to end of file
  12. }
  13. if (boxtype==‘uuid’)
  14. {
  15. unsigned int(8)[16] usertype = extended_type;
  16. }
aligned(8) class Box (unsigned int(32) boxtype,optional unsigned int(8)[16] extended_type)
{
unsigned int(32) size;
unsigned int(32) type = boxtype;
if (size==1)
{
unsigned int(64) largesize;
}
else if (size==0)
{
// box extends to end of file
}
if (boxtype==‘uuid’)
{
unsigned int(8)[16] usertype = extended_type;
}

结构如下图:

文件基本结构描述图

二、MP4文件格式(ISO-14496-12/14)

MP4文件概述

MP4文件就是由各式各样的Box组成的,下表中列出了所有必选或可选的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)

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

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

正式开始前先对文件的几个重要部分宏观介绍一下,以便诸位在后续学习时心中有数:

1、  ftypbox,在文件的开始位置,描述的文件的版本、兼容协议等;

2、  moovbox,这个box中不包含具体媒体数据,但包含本文件中所有媒体数据的宏观描述信息,moov box下有mvhd和trak box。

>>mvhd中记录了创建时间、修改时间、时间度量标尺、可播放时长等信息。

>>trak中的一系列子box描述了每个媒体轨道的具体信息。

3、  moofbox,这个box是视频分片的描述信息。并不是MP4文件必须的部分,但在我们常见的可在线播放的MP4格式文件中(例如Silverlight Smooth Streaming中的ismv文件)确是重中之重。

4、  mdatbox,实际媒体数据。我们最终解码播放的数据都在这里面。

5、  mfrabox,一般在文件末尾,媒体的索引文件,可通过查询直接定位所需时间点的媒体数据。

附:Smooth Streaming中ismv文件结构,文件分为了多个Fragments,每个Fragment中包含moof和mdat。这样的结构符合渐进式播放需求。(mdat及其描述信息逐步传输,收齐一个Fragment便可播放其中的mdat)。

文件类型ftyp(ISO-14496-12)

Author:Pirate Leo

Email:codeevoship@gmail.com

ISO 14496 - 12 定义了一种封装媒体数据的基础文件格式,mp4、3gp、ismv等我们常见媒体封装格式都是以这种基础文件格式为基础衍生的。

如果从全局角度了解基础文件格式,请看我之前的博文《MP4文件格式详解——结构概述》。

本系列文档从MP4文件入手,对文件中重要的box进行解析。

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

本次解析ftyp box

  1. aligned(8) class FileTypeBox extends Box(‘ftyp’)
  2. {
  3. unsigned int(32) major_brand;
  4. unsigned int(32) minor_version;
  5. unsigned int(32) compatible_brands[];  // to end of the box
  6. }
aligned(8) class FileTypeBox extends Box(‘ftyp’)
{
unsigned int(32) major_brand;
unsigned int(32) minor_version;
unsigned int(32) compatible_brands[]; // to end of the box
}

什么是brands?官方是这样描述的:

Each brand is a printable four-character code, registered with ISO, that identifies a precise specification.//在ISO注册的4个字符。

下表来源于网络,列出了几种常见的基于基础文件格式的,媒体封装格式标识。

更多的内容可以查看http://www.ftyps.com/

  Brand Extension Mime Type
MP4 mp41, mp42 .mp4 video/mp4, audio/mp4, application/mp4
3GPP various, e.g. 3gp4, 3gp5 .3gp video/3gpp, audio/3gpp
3GPP2 3g2a .3g2 video/3gpp2, audio/3gpp2
Motion JPEG 2000 mjp2 .mj2 video/mj2
QuickTime "qt" .mov video/quicktime
 

结合实际文件,下图是MP4文件起始位置存放的数据

length(4字节):0x0000001c:box的长度是28字节;

boxtype(4字节):0x66747970:“ftyp”的ASCII码,box的标识;

major_brand(4字节):0x69736f6d:“isom“的ASCII码;

minor_version(4字节):0x00000200:ismo的版本号;

compatible_brands(12字节):说明本文件遵从(或称兼容)ismo,iso2,mp41三种协议。

ftyp box通常放在文件的开始,通过对该box解析可以让我们的软件(播放器、demux、解析器)知道应该使用哪种协议对这该文件解析,是后续解读文件基础。

MP4文件格式的更多相关文章

  1. mp4文件格式之fragment mp4

    目前网络上对mp4文件格式的总结已经相当多了,我就不在这里抄别人总结的东西了,想入门的话百度一下会有许多这方面的文章.这里介绍一下其他文章中很少涉及的一种mp4文件结构,即fragment mp4,也 ...

  2. MP4文件格式具体解释——结构概述

    MP4文件格式具体解释(ISO-14496-12/14) Author:Pirate Leo Email:codeevoship@gmail.com 一.基本概念 1. 文件,由很多Box和FullB ...

  3. mp4文件格式解析

    目前MP4的概念被炒得很火,也很乱.最开始MP4指的是音频(MP3的升级版),即MPEG-2 AAC标准.随后MP4概念被转移到视频上,对应的是MPEG-4标准.而现在我们流行的叫法,多半是指能播放M ...

  4. MP4文件格式的解析

    MP4文件格式的解析,以及MP4文件的分割算法 mp4应该算是一种比较复杂的媒体格式了,起源于QuickTime.以前研究的时候就花了一番的功夫,尤其是如何把它完美的融入到视频点播应用中,更是费尽了心 ...

  5. mp4文件格式解析(转载)

    mp4文件格式解析 原作:http://blog.sina.com.cn/s/blog_48f93b530100jz4b.html 目前MP4的概念被炒得很火,也很乱.最开始MP4指的是音频(MP3的 ...

  6. mp4文件格式解析二

    目前MP4的概念被炒得很火,也很乱.最开始MP4指的是音频(MP3的升级版),即MPEG-2 AAC标准.随后MP4概念被转移到视频上,对应的是MPEG-4标准.而现在我们流行的叫法,多半是指能播放M ...

  7. mp4文件格式解析(转)

    mp4文件格式解析 MP4文件格式带数据详解 MP4文件格式的解析,以及MP4文件的分割算法

  8. MP4文件格式分析及分割实现(附源码)

    MP4文件格式分析                   MP4(MPEG-4 Part 14)是一种常见的多媒体容器格式,它是在“ISO/IEC 14496-14”标准文件中定义的,属于MPEG-4的 ...

  9. 5分钟入门MP4文件格式

    写在前面 本文主要内容包括,什么是MP4.MP4文件的基本结构.Box的基本结构.常见且重要的box介绍.普通MP4与fMP4的区别.如何通过代码解析MP4文件 等. 写作背景:最近经常回答团队小伙伴 ...

随机推荐

  1. topcoder srm 520 div1

    problem1 link 设$f[i][j][k]$表示考虑了前$i$道题,剩下时间为$j$,剩下技能为$k$的最大得分. 从小到大计算二元组$(j,k)$的话,在存储上可以省略掉$i$这一维. p ...

  2. topcoder srm 703 div1 -3

    1.给出一个包含$n$个元素的数组$x$,构造出一个有向无环图满足从节点$i$出发可以访问到的节点数为$x_{i}$. 思路:按照$x$从小到大排序.然后从前向后处理,当前节点依次与前面已经处理的节点 ...

  3. linux内核中的fuse是什么?

    答: 一个用户态文件系统框架,属于内核的一种特性. 1.组成部分 fuse.ko(内核模块) + libfuse.*(用户空间库) + fusemount(挂载工具) 2.参考资料 fuse.txt

  4. 如何自己烧制全文RSS(打造自己RSS源)

    烧制RSS源 到Feed43注册一个账号,虽说不注册也能用,但是为了方便修改自己烧制的RSS,最好还是注册一个账号来管理 到主页点击Create new feed 输入网址点击reload 可以看到请 ...

  5. P3211 [HNOI2011]XOR和路径

    思路 看到异或,容易联想到二进制位之间是相互独立的,所以可以把问题变成每个二进制位为1的概率再乘上(1<<pos)的值 假设现在考虑到pos位,设f[i]为第i个节点期望的异或和第pos位 ...

  6. C#数字前面如何补0

    PadLeft()函数:向左补齐PadRight()函数:向右补齐 class Program { static void Main(string[] args) { ; Console.WriteL ...

  7. 【BZOJ 5125】小Q的书架

    Problem Description 小 \(Q\) 有 \(n\) 本书,每本书有一个独一无二的编号,现在它们正零乱地在地上排成了一排. 小 \(Q\) 希望把这一排书分成恰好 \(k\) 段,使 ...

  8. PHP feof()函数

    feof()函数检查是否已经到达文件末尾(EOF) EOF == end  of  file 如果出错或者文件指针到了文件末尾(EOF)则返回true,否则返回false 语法: feof(file) ...

  9. springboot读取properties(yml)的几种常用方式

    boot项目中一些秘钥等不常变动的信息大多存储在配置文件中,那么我们怎么获取配置文件中的属性呢? 以获取server端口号为例讲解几种方法:配置信息如下 一:使用@Value注解 @Value(&qu ...

  10. springboot + mybatis 的项目,实现简单的CRUD

    以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD  上图是项目的 ...