【多媒体封装格式详解】---MKV【2】

上文MKV【1】已经了解了EBML基本结构和MKV文件的整体结构,接下来我们自己分析每一部分。

1.EBML Header

MKV文件的开头部分是EBML header。可能会包括以下的内容

Element Name L EBML ID Ma Mu Rng Default T 1 2 3 4 W Description
EBML Header
EBML 0 [1A][45][DF][A3] * * - - m * * * * * Set the EBML characteristics of the data to follow. Each EBML document has to start with this.
EBMLVersion 1 [42][86] * - - 1 u * * * * * The version of EBML parser used to create the file.
EBMLReadVersion 1 [42][F7] * - - 1 u * * * * * The minimum EBML version a parser has to support to read this file.
EBMLMaxIDLength 1 [42][F2] * - - 4 u * * * * * The maximum length of the IDs you'll find in this file (4 or less in Matroska).
EBMLMaxSizeLength 1 [42][F3] * - - 8 u * * * * * The maximum length of the sizes you'll find in this file (8 or less in Matroska). This does not override the element size indicated at the beginning of an element. Elements that have an indicated size which is larger than what is allowed by EBMLMaxSizeLength shall be considered invalid.
DocType 1 [42][82] * - - matroska s * * * * * A string that describes the type of document that follows this EBML header. 'matroska' in our case or 'webm' for webm files.
DocTypeVersion 1 [42][87] * - - 1 u * * * * * The version of DocType interpreter used to create the file.
DocTypeReadVersion 1 [42][85] * - - 1 u * * * * * The minimum DocType version an interpreter has to support to read this file.
 
上面是官方文档,找个例子简单看一下。下面是一个mkv文件的开头截图




按照上文EBML格式来解析
1A = 00011010  长度3+1  ID = 1A 45 DF A3
93 = 10010011  长度1  size=19
data 就是之后19个字节内容
对照上面的表可以得知整个EBML header为红线圈住的内容



按照EBML规则进一步解析内容可得知
DocType = matroska
DocTypeVersion = 1
DocTypeReadVersion = 1
和官方工具对比发现完全一致


这里要说一下容器格式的可辨识性信息,一般的容器格式开头都有自己唯一的可辨识性信息,如flv文件开头三个字节'F' 'L' 'V' .这个可以让播放器来自动辨识容器格式。
MKV头部的1A 45 DF A3就是类似这种内容,如果发现开头是这4个字节,很有可能就是MKV文件。会继续进行解析。可以看看ffmpeg中matroska_probe 
/* top-level master-IDs */
#define EBML_ID_HEADER             0x1A45DFA3

/*
 * Autodetecting...
 */
static int matroska_probe(AVProbeData *p)
{
    uint64_t total = 0;
    int len_mask = 0x80, size = 1, n = 1, i;

    /* EBML header? */
    if (AV_RB32(p->buf) != EBML_ID_HEADER)
        return 0;

2.Segment

MKV除了上面的EBML header,剩下的都属于Segment。里面包括了音视频信息、音视频数据等等。
还是用上面那个文件作为例子。用EBML方法继续解析。
ID = 18 53 80 67 
size = 01 EC D8 D4 (32299220)

查表可知 ID = 18 53 80 67 对应的就是Segment 

Element Name L EBML ID Ma Mu Rng Default T 1 2 3 4 W Description
Segment
Segment 0 [18][53][80][67] * * - - m * * * * * This element contains all other top-level (level 1) elements. Typically a Matroska file is composed of 1 segment.


Segment中可能出现的是下面这些内容。
Meta Seek Information
Segment Information
Track
Chapters
Clusters
Cueing Data
Attachment
Tagging

1.Meta Seek Information

Meta Seek Information 其实是个快速索引的信息。他可能包含Track information, Chapters, Tags, Cues, Attachments, 这些部分的位置信息。这些位置是在Segment中的相对位置。Meta Seek这部分内容比不一定必须有,但是有了它能让你快速的定位到你关心的一些关键信息的位置,而不用按照文件顺序的解析。

Meta Seek Information
SeekHead 1 [11][4D][9B][74] - * - - m * * * * * Contains the position of other level 1 elements.
Seek 2 [4D][BB] * * - - m * * * * * Contains a single seek entry to an EBML element.
SeekID 3 [53][AB] * - - - b * * * * * The binary ID corresponding to the element name.
SeekPosition 3 [53][AC] * - - - u * * * * * The position of the element in the segment in octets (0 = first level 1 element).

上文提到过了EBML元素都有自己的级别,每一个高一级的元素由若干次一级的元素组成。下图就是meta seek 结构

Meta Seek Information
SeekHead
Seek
SeekID
SeekPosition
Seek
SeekID
SeekPosition
还是以上面的MKV文件为例子。Segment中第一部分很可能就是meta seek。


红线圈里就是seek 内同,按照规范中的表解析EBML元素

11 4D 9B 74 表示SeekHead 

C0 解析后 size 64 

接下来就是SeekHead 下一级元素 

Seek

ID = 4D BB

size = 12

data = 53 AB 84 16 54 AE 6B 53 AC 82 10 03 

data里面的信息就是seekID SeekPosition

53 AB 表示seekID元素 

size = 4

seekID = 15 49 A9 66 

53 AC 表示SeekPosition元素 

size = 2

SeekPosition = 10 03  (4099)

这个表示 ID 为 15 49 A9 66  的EBML元素 所在Segment的位置为 4099处.查表可知道这个ID是

Segment Information 中的 info 

这个4099 是Segment中的相对位置,把之前24字节的EBML header  和 12字节Segment的ID和size加上 正好是4135,咱们找到文件4135(0x1027)的位置看看


正好是info 的位置。所以有了meta seek的信息我们可以快速找到一些关键EBML元素的位置。

把剩下meta seek信息同样方法解析出来就会等到如下信息:



2.Segment Information

Segment Information 包含识别文件的信息,包括 Title 、 SegmentUID,有个比较关心的文件时常信息Duration也在这一部分。


Element Name L EBML ID Ma Mu Rng Default T 1 2 3 W Description
 
Segment Information
Info 1 [15][49][A9][66] * * - - m * * * * Contains miscellaneous general information and statistics on the file.
SegmentUID 2 [73][A4] - - not 0 - b * * *
A randomly generated unique ID to identify the current segment between many others (128 bits).
SegmentFilename 2 [73][84] - - - - 8 * * *
A filename corresponding to this segment.
PrevUID 2 [3C][B9][23] - - - - b * * *
A unique ID to identify the previous chained segment (128 bits).
PrevFilename 2 [3C][83][AB] - - - - 8 * * *
An escaped filename corresponding to the previous segment.
NextUID 2 [3E][B9][23] - - - - b * * *
A unique ID to identify the next chained segment (128 bits).
NextFilename 2 [3E][83][BB] - - - - 8 * * *
An escaped filename corresponding to the next segment.
SegmentFamily 2 [44][44] - * - - b * * *
A randomly generated unique ID that all segments related to each other must use (128 bits).
ChapterTranslate 2 [69][24] - * - - m * * *
A tuple of corresponding ID used by chapter codecs to represent this segment.
ChapterTranslateEditionUID 3 [69][FC] - * - - u * * *
Specify an edition UID on which this correspondance applies. When not specified, it means for all editions found in the segment.
ChapterTranslateCodec 3 [69][BF] * - - - u * * *
The chapter codec using this ID (0: Matroska Script, 1: DVD-menu).
ChapterTranslateID 3 [69][A5] * - - - b * * *
The binary value used to represent this segment in the chapter codec data. The format depends on theChapProcessCodecID used.
TimecodeScale 2 [2A][D7][B1] * - - 1000000 u * * * * Timecode scale in nanoseconds (1.000.000 means all timecodes in the segment are expressed in milliseconds). When combined with TimecodeScaleDenominator the Timecode scale is given by the fraction TimecodeScale/TimecodeScaleDenominator in seconds.
TimecodeScaleDenominator 2 [2A][D7][B2] * - - 1000000000 u



Timecode scale numerator, seeTimecodeScale.
Duration 2 [44][89] - - > 0 - f * * * * Duration of the segment (based on TimecodeScale).
DateUTC 2 [44][61] - - - - d * * * * Date of the origin of timecode (value 0), i.e. production date.
Title 2 [7B][A9] - - - - 8 * * *
General name of the segment.
MuxingApp 2 [4D][80] * - - - 8 * * * * Muxing application or library ("libmatroska-0.4.3").
WritingApp 2 [57][41] * - - - 8 * * * * Writing application ("mkvmerge-0.3.3").


3.Track

Track包含了音视频的基本信息,如音视频解码器类型、视频分辨率、音频采样率等这。通过对Track部分的解析。我们就能得到音视频的基本信息。为选择相应解码器以及初始化这些解码器做好准备工作。每个 TrackEntry 代表着1条轨道信息。

Tracks

TrackEntry
Name
TrackNumber
TrackType
TrackEntry
Name
TrackNumber
TrackType

Element Name

L

EBML ID

Ma

Mu

Rng

Default

T

1

2

3

W

Description















Track

Tracks

1

[16][54][AE][6B]

-

*

-

-

m

*

*

*

*

A top-level block of information with many tracks described.

TrackEntry

2

[AE]

*

*

-

-

m

*

*

*

*

Describes a track with all elements.

TrackNumber

3

[D7]

*

-

not 0

-

u

*

*

*

*

The track number as used in the Block Header (using more than 127 tracks is not encouraged, though the design allows an unlimited number).

TrackUID

3

[73][C5]

*

-

not 0

-

u

*

*

*

*

A unique ID to identify the Track. This should be kept the same when making a direct stream copy of the Track to another file.

TrackType

3

[83]

*

-

1-254

-

u

*

*

*

*

A set of track types coded on 8 bits (1: video, 2: audio, 3: complex, 0x10: logo, 0x11: subtitle, 0x12: buttons, 0x20: control).

FlagEnabled

3

[B9]

*

-

0-1

1

u


*

*

*

Set if the track is usable. (1 bit)

FlagDefault

3

[88]

*

-

0-1

1

u

*

*

*

*

Set if that track (audio, video or subs) SHOULD be active if no language found matches the user preference. (1 bit)

FlagForced

3

[55][AA]

*

-

0-1

0

u

*

*

*

*

Set if that track MUST be active during playback. There can be many forced track for a kind (audio, video or subs), the player should select the one which language matches the user preference or the default + forced track. Overlay MAY happen between a forced and non-forced track of the same kind. (1 bit)

FlagLacing

3

[9C]

*

-

0-1

1

u

*

*

*

*

Set if the track may contain blocks using lacing. (1 bit)

MinCache

3

[6D][E7]

*

-

-

0

u

*

*

*


The minimum number of frames a player should be able to cache during playback. If set to 0, the reference pseudo-cache system is not used.

MaxCache

3

[6D][F8]

-

-

-

-

u

*

*

*


The maximum cache size required to store referenced frames in and the current frame. 0 means no cache is needed.

DefaultDuration

3

[23][E3][83]

-

-

not 0

-

u

*

*

*

*

Number of nanoseconds (not scaled via TimecodeScale) per frame ('frame' in the Matroska sense -- one element put into a (Simple)Block).

TrackTimecodeScale

3

[23][31][4F]

*

-

> 0

1.0

f

*

*

*


DEPRECATED, DO NOT USE. The scale to apply on this track to work at normal speed in relation with other tracks (mostly used to adjust video speed when the audio length differs).

TrackOffset

3

[53][7F]

-

-

-

0

i





A value to add to the Block's Timecode. This can be used to adjust the playback offset of a track.

MaxBlockAdditionID

3

[55][EE]

*

-

-

0

u

*

*

*


The maximum value of BlockAddID. A value 0 means there is no BlockAdditions for this track.

Name

3

[53][6E]

-

-

-

-

8

*

*

*

*

A human-readable track name.

Language

3

[22][B5][9C]

-

-

-

eng

s

*

*

*

*

Specifies the language of the track in theMatroska languages form.

CodecID

3

[86]

*

-

-

-

s

*

*

*

*

An ID corresponding to the codec, see thecodec page for more info.

CodecPrivate

3

[63][A2]

-

-

-

-

b

*

*

*

*

Private data only known to the codec.

CodecName

3

[25][86][88]

-

-

-

-

8

*

*

*

*

A human-readable string specifying the codec.

CodecSettings

3

[3A][96][97]

-

-

-

-

8





A string describing the encoding setting used.

CodecInfoURL

3

[3B][40][40]

-

*

-

-

s





A URL to find information about the codec used.

CodecDownloadURL

3

[26][B2][40]

-

*

-

-

s





A URL to download about the codec used.

CodecDecodeAll

3

[AA]

*

-

0-1

1

u


*

*


The codec can decode potentially damaged data (1 bit).

TrackOverlay

3

[6F][AB]

-

*

-

-

u

*

*

*


Specify that this track is an overlay track for the Track specified (in the u-integer). That means when this track has a gap (seeSilentTracks) the overlay track should be used instead. The order of multiple TrackOverlay matters, the first one is the one that should be used. If not found it should be the second, etc.

TrackTranslate

3

[66][24]

-

*

-

-

m

*

*

*


The track identification for the given Chapter Codec.

TrackTranslateEditionUID

4

[66][FC]

-

*

-

-

u

*

*

*


Specify an edition UID on which this translation applies. When not specified, it means for all editions found in the segment.

TrackTranslateCodec

4

[66][BF]

*

-

-

-

u

*

*

*


The chapter codec using this ID (0: Matroska Script, 1: DVD-menu).

TrackTranslateTrackID

4

[66][A5]

*

-

-

-

b

*

*

*


The binary value used to represent this track in the chapter codec data. The format depends on the ChapProcessCodecID used.

Video

3

[E0]

-

-

-

-

m

*

*

*

*

Video settings.

FlagInterlaced

4

[9A]

*

-

0-1

0

u


*

*

*

Set if the video is interlaced. (1 bit)

StereoMode

4

[53][B8]

-

-

-

0

u



*

*

Stereo-3D video mode (0: mono, 1: side by side (left eye is first), 2: top-bottom (right eye is first), 3: top-bottom (left eye is first), 4: checkboard (right is first), 5: checkboard (left is first), 6: row interleaved (right is first), 7: row interleaved (left is first), 8: column interleaved (right is first), 9: column interleaved (left is first), 10: anaglyph (cyan/red), 11: side by side (right eye is first), 12: anaglyph (green/magenta), 13 both eyes laced in one Block (left eye is first), 14 both eyes laced in one Block (right eye is first)) . There are some more details on 3D support in the Specification Notes.

OldStereoMode

4

[53][B9]

-

-

-

-

u





DEPRECATED, DO NOT USE. Bogus StereoMode value used in old versions of libmatroska. (0: mono, 1: right eye, 2: left eye, 3: both eyes).

PixelWidth

4

[B0]

*

-

not 0

-

u

*

*

*

*

Width of the encoded video frames in pixels.

PixelHeight

4

[BA]

*

-

not 0

-

u

*

*

*

*

Height of the encoded video frames in pixels.

PixelCropBottom

4

[54][AA]

-

-

-

0

u

*

*

*

*

The number of video pixels to remove at the bottom of the image (for HDTV content).

PixelCropTop

4

[54][BB]

-

-

-

0

u

*

*

*

*

The number of video pixels to remove at the top of the image.

PixelCropLeft

4

[54][CC]

-

-

-

0

u

*

*

*

*

The number of video pixels to remove on the left of the image.

PixelCropRight

4

[54][DD]

-

-

-

0

u

*

*

*

*

The number of video pixels to remove on the right of the image.

DisplayWidth

4

[54][B0]

-

-

not 0

PixelWidth

u

*

*

*

*

Width of the video frames to display. The default value is only valid when DisplayUnit is 0.

DisplayHeight

4

[54][BA]

-

-

not 0

PixelHeight

u

*

*

*

*

Height of the video frames to display. The default value is only valid when DisplayUnit is 0.

DisplayUnit

4

[54][B2]

-

-

-

0

u

*

*

*

*

How DisplayWidth & DisplayHeight should be interpreted (0: pixels, 1: centimeters, 2: inches, 3: Display Aspect Ratio).

AspectRatioType

4

[54][B3]

-

-

-

0

u

*

*

*

*

Specify the possible modifications to the aspect ratio (0: free resizing, 1: keep aspect ratio, 2: fixed).

ColourSpace

4

[2E][B5][24]

-

-

-

-

b

*

*

*


Same value as in AVI (32 bits).

GammaValue

4

[2F][B5][23]

-

-

> 0

-

f





Gamma Value.

FrameRate

4

[23][83][E3]

-

-

> 0

-

f





Number of frames per second. Informationalonly.

Audio

3

[E1]

-

-

-

-

m

*

*

*

*

Audio settings.

SamplingFrequency

4

[B5]

*

-

> 0

8000.0

f

*

*

*

*

Sampling frequency in Hz.

OutputSamplingFrequency

4

[78][B5]

-

-

> 0

Sampling Frequency

f

*

*

*

*

Real output sampling frequency in Hz (used for SBR techniques).

Channels

4

[9F]

*

-

not 0

1

u

*

*

*

*

Numbers of channels in the track.

ChannelPositions

4

[7D][7B]

-

-

-

-

b





Table of horizontal angles for each successive channel, see appendix.

BitDepth

4

[62][64]

-

-

not 0

-

u

*

*

*

*

Bits per sample, mostly used for PCM.

TrackOperation

3

[E2]

-

-

-

-

m



*


Operation that needs to be applied on tracks to create this virtual track. For more detailslook at the Specification Notes on the subject.

TrackCombinePlanes

4

[E3]

-

-

-

-

m



*


Contains the list of all video plane tracks that need to be combined to create this 3D track

TrackPlane

5

[E4]

*

*

-

-

m



*


Contains a video plane track that need to be combined to create this 3D track

TrackPlaneUID

6

[E5]

*

-

not 0

-

u



*


The trackUID number of the track representing the plane.

TrackPlaneType

6

[E6]

*

-

-

-

u



*


The kind of plane this track corresponds to (0: left eye, 1: right eye, 2: background).

TrackJoinBlocks

4

[E9]

-

-

-

-

m



*


Contains the list of all tracks whose Blocks need to be combined to create this virtual track

TrackJoinUID

5

[ED]

*

*

not 0

-

u



*


The trackUID number of a track whose blocks are used to create this virtual track.

TrickTrackUID

3

[C0]

-

-

-

-

u





DivX trick track extenstions

TrickTrackSegmentUID

3

[C1]

-

-

-

-

b





DivX trick track extenstions

TrickTrackFlag

3

[C6]

-

-

-

0

u





DivX trick track extenstions

TrickMasterTrackUID

3

[C7]

-

-

-

-

u





DivX trick track extenstions

TrickMasterTrackSegmentUID

3

[C4]

-

-

-

-

b





DivX trick track extenstions

ContentEncodings

3

[6D][80]

-

-

-

-

m

*

*

*


Settings for several content encoding mechanisms like compression or encryption.

ContentEncoding

4

[62][40]

*

*

-

-

m

*

*

*


Settings for one content encoding like compression or encryption.

ContentEncodingOrder

5

[50][31]

*

-

-

0

u

*

*

*


Tells when this modification was used during encoding/muxing starting with 0 and counting upwards. The decoder/demuxer has to start with the highest order number it finds and work its way down. This value has to be unique over all ContentEncodingOrder elements in the segment.

ContentEncodingScope

5

[50][32]

*

-

not 0

1

u

*

*

*


A bit field that describes which elements have been modified in this way. Values (big endian) can be OR'ed. Possible values:

1 - all frame contents,
2 - the track's private data,
4 - the next ContentEncoding (next ContentEncodingOrder. Either the data inside ContentCompression and/or ContentEncryption)

ContentEncodingType

5

[50][33]

*

-

-

0

u

*

*

*


A value describing what kind of transformation has been done. Possible values:

0 - compression,
1 - encryption

ContentCompression

5

[50][34]

-

-

-

-

m

*

*

*


Settings describing the compression used. Must be present if the value of ContentEncodingType is 0 and absent otherwise. Each block must be decompressable even if no previous block is available in order not to prevent seeking.

ContentCompAlgo

6

[42][54]

*

-

-

0

u

*

*

*


The compression algorithm used. Algorithms that have been specified so far are:

0 - zlib,
1 - bzlib,
2 - lzo1x
3 - Header Stripping

ContentCompSettings

6

[42][55]

-

-

-

-

b

*

*

*


Settings that might be needed by the decompressor. For Header Stripping (ContentCompAlgo=3), the bytes that were removed from the beggining of each frames of the track.

ContentEncryption

5

[50][35]

-

-

-

-

m

*

*

*


Settings describing the encryption used. Must be present if the value of ContentEncodingType is 1 and absent otherwise.

ContentEncAlgo

6

[47][E1]

-

-

-

0

u

*

*

*


The encryption algorithm used. The value '0' means that the contents have not been encrypted but only signed. Predefined values:

1 - DES, 2 - 3DES, 3 - Twofish, 4 - Blowfish, 5 - AES

ContentEncKeyID

6

[47][E2]

-

-

-

-

b

*

*

*


For public key algorithms this is the ID of the public key the the data was encrypted with.

ContentSignature

6

[47][E3]

-

-

-

-

b

*

*

*


A cryptographic signature of the contents.

ContentSigKeyID

6

[47][E4]

-

-

-

-

b

*

*

*


This is the ID of the private key the data was signed with.

ContentSigAlgo

6

[47][E5]

-

-

-

0

u

*

*

*


The algorithm used for the signature. A value of '0' means that the contents have not been signed but only encrypted. Predefined values:

1 - RSA

ContentSigHashAlgo

6

[47][E6]

-

-

-

0

u

*

*

*


The hash algorithm used for the signature. A value of '0' means that the contents have not been signed but only encrypted. Predefined values:

1 - SHA1-160
2 - MD5


上表是track部分的官方文档。咱们还是用上面同样的例子来简单解析一下track 信息。

Segment Information之后紧接着就是track信息了。

整个红色框里的就是track 信息。它包含了2路TrackEntry的信息,分别是蓝绿框中的。


先看看第一个track内容,也就是蓝色框内。只看重要信息

AE 代表着这个整个EBML单元是一个TrackEntry size 0xB5 

TrackNumber = 1    ID = D7  size = 1  data = 1;

TrackType = 1 ID = 83 size = 1 data = 1; 查表得知 1 表示的是video 也就是这个tarck信息是视频信息。

CodecID = V_MPEG4/ISO/AVC ID = 86  size = 15 data = "V_MPEG4/ISO/AVC"

PixelWidth = 1280  ID = B0 size = 2 data = 0x500(1280)

PixelHeight = 528   ID = BA size = 2 data = 0x210(528)

用官方的工具解析结果


用同样的方法解析第2路track的信息可以得到



通过对track部分的解析,我们就知道了这个MKV文件包含了2路音视频数据。1路为264的视频、1路为AC3的音频。音视频的相关参数也拿到了。


未完待续...



猜你喜欢

转载自blog.csdn.net/tx3344/article/details/8176288