Matroska文件解析之SimpleBlock

分析二进制文件是一件痛苦的事情,最近在做一个关于流媒体的项目研究,涉及到webm这种开源视频文件,它其实就是用Matroska(也就是大家熟悉的MKV容器格式)包裹VP8和VBRIS,没什么特别的。

因为要研究视频流的传输过程,所以我需要把IMediaSample保存下来,下面是一个SimpleBlock十六进制显示。

0xA3, 0xB6, 0x81, 0x00, 0x28, 0x00, 0xD1, 0x05, 0x00, 0x05, 0x10, 0xA4, 0x00, 0x18, 0x00, 0x18, 
0x58, 0x2F, 0xF4, 0x00, 0x08, 0x80, 0x04, 0x00, 0x00, 0x80, 0x09, 0x67, 0x31, 0xD7, 0x6A, 0xB4, 
0x61, 0x8C, 0x65, 0xBA, 0x8D, 0x16, 0x53, 0x0D, 0x66, 0x9B, 0x41, 0x90, 0xBF, 0x59, 0xA5, 0x0E, 
0x63, 0xAE, 0xD5, 0x68, 0xC3, 0x14, 0x00, 0x00

A3是SimpleBlock的标志位,B6=10110110,所以计算这段SimpleBlock大小的使用了一个字节去计算(0+1),大小为:00110110=54,故有效数据为54字节,实际在webm filter中截取的IMediaSample包中,只有50个字节。B6后面的0x81, 0x00, 0x28, 0x00这四个字节是SimpleBlock Header。

关于SimpleBlock Header的相关信息如下:

 

SimpleBlock Header
OffsetPlayerDescription
0x00+ must Track Number (Track Entry). It is coded in EBML like form (1 octet if the value is < 0x80, 2 if < 0x4000, etc) (most significant bits set to increase the range).
0x01+ must Timecode (relative to Cluster timecode, signed int16)
0x03+ -
Flags
BitPlayerDescription
0 - Keyframe, set when the Block contains only keyframes
1-3 - Reserved, set to 0
4 - Invisible, the codec should decode this frame but not display it
5-6 must Lacing
  • 00 : no lacing
  • 01 : Xiph lacing
  • 11 : EBML lacing
  • 10 : fixed-size lacing
7 - Discardable, the frames of the Block can be discarded during playing if needed
Lace (when lacing bit is set)
0x00 must Number of frames in the lace-1 (uint8)
0x01 / 0xXX must* Lace-coded size of each frame of the lace, except for the last one (multiple uint8). *This is not used with Fixed-size lacing as it is calculated automatically from (total size of lace) / (number of frames in lace).
(possibly) Laced Data
0x00 must Consecutive laced frames

简单解释下,因为我的SimpleBlock Header中第一个字节为0x81大于0x80,所以我的这条视频的Track Number(轨道号)为:2,也就是说这段SimpleBlock是音频。紧接着的0x00, 0x28表示的是时间码,因为时间码用两个字节来表示的,其值为0.040s。最后的0x00大家可以对照上图“0x03+”看看具体含义。

注:上面的那段SimpleBlock总共有56个字节,减去标志位A3和计算SimpleBlock大小的B6,剩下54个字节,再减去SimpleBlock Header所占用的四个字节,正好是50个字节。这里我做了个SimpleBlock模块解析说明,Matroska文件格式其他模块解析过程类似,其他的可以参考官方文档:http://www.matroska.org/technical/specs/index.html

知识共享许可协议

本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

原文地址:https://www.cnblogs.com/tangdoudou/p/2499063.html