MP4文件结构由MPEG定义,由14496-12和14496-14具体定义。其中,14496定义了一个通用的标准和大体框架,它适用于多种文件格式的定义。14496-14是对其中一种文件格式(MP4)的具体定义。
1. 文件结构
整个文件由Box组成,所有的数据都在Box内定义。Box可以层级嵌套,例如,moov可以包含多个trak。Box结构应该不用缀述,简单列一下:
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是以8个字节开头,接着就是Box的数据。这个8个字节分别表示size和type。size是整个Box的字节数,含size和type本身。type是4个可读的字符。例如:File Type Box的type就ftyp。 标准的type都是4个字符,如果要自定义的type,则需要把type设为"uuid",然后再附上16个字符作为自定义type
还有一种FullBox结构,对Box做了一些扩展,添加了2个字段:
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. 数据类型
整数 : 以big-endian存储
unsigned int(8)
unsigned int(32)
定点小数 fixed point
template int(32) rate = 0x00010000;
其字段标明是16.16的fixed point number,表示前16位表示整数部分,后16位表示小数部分。例如,0x00010100 = 1.25 (0x0100 / 0x10000)
短阵 matrix
短阵只在MovieHeaderBox中用到,限定为3x3的矩阵,也就是9个元素。
(p q 1) * | a b u | = (m n z)
| c d v |
| x y w |
template int(32)[9] matrix =
{ 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 };
注意,{a,b,u, c,d,v, x,y,w}.中,u,v,w是2.30的fixed point,其他是16.16的fixed point,这个默认矩阵
0x00010000 0 0
0 0x00010000 0
0 0 0x40000000
换算一下就是
1 0 0
0 1 0
0 0 1
也就是说转换后坐标p' =p, q'=q,即原番不变。
template 与 pre-defined
template表示预留给其他文档用,在本文档中可以忽略(这个字段存在,值总是默认值)
pre-defined表示此字段在先前版本中出现