MP4 file format详解之二:Box

197 阅读1分钟

1. 什么是box?

一个 MP4 封装格式的文件,其基本的数据单元就是 box。每一个 box 都有 typesize 两个 fields,叫做 box 的 header。header 后面是该 box 的 data。type 表示该 box 的类型,是一个无符号类型的32位整数,可以转换成4个 ASCII 编码的字符。size 表示该 box 所占的字节数,用一个无符号的32位整数表示。下图展示了一个 MP4 文件的可能的布局:

未命名文件.png

2. Box的分类

根据 box 是否包含另一个 box,可以将 box 分为两类:

  1. Container box 包含其它 box
  2. Leaf box 不包含其它 box

如下图所示:红色背景的为 Leaf box,而蓝色背景的为 Container box,其中包含了两个 Leaf box。

未命名文件 (1).png

3. Full Box

有些 box 有 version 和 flags 两个 fields,这两个 fields 不包括在 box 的 header 部分,而在box 的 data 部分,我们把这种 box 叫做 full box。如下图所示就是一个包含一个 full box 的 MP4 file 的结构:

未命名文件 (4).png

  • version is an integer that specifies the version of this format of the box
  • flags is a map of flags

4. Extended Size

当 box 的大小超过了该 box size field 的表示范围时,可以将 size 设置为 1。表明该 box 有一个 extended size field 在 box 的 data 部分。一个包含 extended size 的 box 如下图所示:

未命名文件 (5).png