1. 定义Box对象
使用 syntax description language(SDL) 来描述 box。在 MPEG‐4 的里面 box 的定义如下(在这里我们先不管 extended_type 的含义):
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;
}
}
box 的存储采用大端模式,如果要解析一个 MP4 file 需要按照正确的顺序解析 box 各个 field。 在文章 MP4 file format详解之二:Box 中介绍了 full box,在其 data 中包含 version 和 flags 两个 field,其定义如下:
aligned(8) class FullBox(unsigned int(32) boxtype, unsigned int(8) v, bit(24) f) extends Box(boxtype) {
unsigned int(8) version = v;
bit(24) flags = f;
}
2. fytp
2.1 Syntax
该 box 用于识别 MP4 file 符合的规范,其定义如下:
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
}
每一个 brand 都是可打印的4个字符,用于标识一个准确的规范,并且在 ISO 中注册。
- major_brand – is a brand identifier
- minor_version – is an informative integer for the minor version of the major brand
- compatible_brands – is a list, to the end of the box, of brands
2.2 定义
File Type Box的定义如下:
- Box Type: 'ftyp'
- Container: File(表示这是一个 Leaf Box)
- Mandatory: Yes
- Quantity: Exactly one (但是有例外,下面会解释)
需要注意的是符合 MPEG-4 Part 14 规范的文件都必须有个类型为 fytp
的box,但是为了兼容此规范的旧标准,一些文件可能符合本标准,但是并不包含类型为 fytp
的 box。如果读取这样的文件,则认为其有如下字段的 File Type Box。
- major_brand = 'mp41'
- minor_version = 0
- compatible_brands = ['mp41']
File Type Box 应该尽量靠近文件的开头,但是应该早于一些重要的可变大小的 boxes(Movie box, Media Data box, or Free Space)。 它表明文件最符合哪个标准,以及该标准的次要版本。
3. 例子
下图是对一个 MP4 文件进行解析得到的 File Type Box 的信息:
可以看出该 box 的 type 为
fytp
,
- major_brand = ‘isom’
- compatible_brands = ['isom', 'avc1']
所有的brand name 都应该在 mp4ra.org 注册,可以从 fytps 得到完整的 brand name
,其中包含注册和未注册的 brand name。