免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
【精】【多媒體封裝格式詳解】---mkv
Matroska 開源多媒體容器標(biāo)準(zhǔn)。MKV屬于其中的一部分。
Matroska常見的有.MKV視頻格式、MKA音頻格式、.MKS字幕格式、.MK3D files (stereoscopic/3D video).
1.EBML(Extensible Binary Meta Language)
MKV是建立在EBML這種語言的基礎(chǔ)上,所以要了解MKV格式需要先了解EBML這種語言。
EBML是一種類似于XML格式的可擴展二進(jìn)制元語言,使用可變長度的整數(shù)存儲,以節(jié)省空間。
EBML基本元素結(jié)構(gòu):
typedef struct {
vint ID // EBML-ID
vint size // size of element
char[size] data // data
} EBML_ELEMENT;
ID標(biāo)志屬性類型
size為后面data部分的大小
data部分為ID所標(biāo)識屬性的實際數(shù)據(jù)
上面可以看到ID和size的類型都是vint,vint(Unsigned Integer Values of Variable Length)可變長度無符號整型,比傳統(tǒng)32/64位整型更加節(jié)省空間。
長度計算方法為
長度 = 1 +整數(shù)前綴0比特的個數(shù).
從MKV文件中簡單接一段來舉個例子。這是16進(jìn)制表示方式
因為每個EBML元素都是由ID size data 三部分組成,我們就按照這些來分析。
將0x428 轉(zhuǎn)成2進(jìn)制 為 01000010    按照上面規(guī)則  前面有1個0 所以知道ID的長度為2,也就是0x4282為ID值。
將0x88 轉(zhuǎn)成2進(jìn)制為 10001000 1為開頭 長度就是1,去掉前綴1變成了00001000 ,也就是 size的值為 8.
接下來的8個字節(jié)就是data值:6D 61 74 72 6F 73 6B 61  根據(jù)上面ID值查表得知 這個EMBL 名稱為DocType 也就是說data的內(nèi)容是string格式,所以轉(zhuǎn)成askII碼 data值就是“matroska” 和后面顯示的一致。
所以這個EBML元素就解析出來了
ID=0x4282;
size=8;
data=“matroska” ;
得到的信息就是 DocType = matroska。
2.整體結(jié)構(gòu)
讓我們看看MKV的整體框架結(jié)構(gòu)。
Level 0GroupingLevel 1Level 2Level 3
EBMLHeaderEBMLVersion
DocType
SegmentMeta Seek InformationSeekHeadSeekSeekID
SeekPosition
SeekSeekID
SeekPosition
Segment InformationInfoTitle
SegmentUID
TrackTracksTrackEntryName
TrackNumber
TrackType
TrackEntryName
TrackNumber
TrackType
ChaptersChaptersEdition Entry
ClustersClusterTimecode
BlockGroupBlock
BlockGroupBlock
ReferenceBlock
BlockGroupBlock
ClusterTimecode
BlockGroupBlock
BlockGroupBlock
BlockGroupBlock
BlockGroupBlock
BlockDuration
Cueing DataCuesCuePointCueTime
CuePosition
CuePointCueTime
CuePosition
AttachmentAttachmentsAttachedFileFileName
FileData
AttachedFileFileName
FileData
TaggingTagsTagMultiTitle
Language
TagMultiTitle
Language
EBML元素都有自己的級別,每一個高一級的元素由若干次一級的元素組成。
從整個MKV文件整體來看可分為2大部分:
EBML Header和Segment。
EBML Header由EBMLVersion、DocType等子元素組成,包含了文件的版本、文檔類型等相關(guān)信息。
Segment部分保存了媒體文件的視頻和音頻的實際數(shù)據(jù),其data部分又可以分為SeekHead、Tracks、Cluster等若干子元素。
上文MKV【1】已經(jīng)了解了EBML基本結(jié)構(gòu)和MKV文件的整體結(jié)構(gòu),接下來我們自己分析每一部分。
1.EBML Header
MKV文件的開頭部分是EBML header??赡軙ㄒ韵碌膬?nèi)容
Element NameLEBML IDMaMuRngDefaultT1234WDescription
EBML Header
EBML0[1A][45][DF][A3]**--m*****Set the EBML characteristics of the data to follow. Each EBML document has to start with this.
EBMLVersion1[42][86]*--1u*****The version of EBML parser used to create the file.
EBMLReadVersion1[42][F7]*--1u*****The minimum EBML version a parser has to support to read this file.
EBMLMaxIDLength1[42][F2]*--4u*****The maximum length of the IDs you'll find in this file (4 or less in Matroska).
EBMLMaxSizeLength1[42][F3]*--8u*****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.
DocType1[42][82]*--matroskas*****A string that describes the type of document that follows this EBML header. 'matroska' in our case or 'webm' for webm files.
DocTypeVersion1[42][87]*--1u*****The version of DocType interpreter used to create the file.
DocTypeReadVersion1[42][85]*--1u*****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個字節(jié)內(nèi)容
對照上面的表可以得知整個EBML header為紅線圈住的內(nèi)容
按照EBML規(guī)則進(jìn)一步解析內(nèi)容可得知
DocType = matroska
DocTypeVersion = 1
DocTypeReadVersion = 1
和官方工具對比發(fā)現(xiàn)完全一致
這里要說一下容器格式的可辨識性信息,一般的容器格式開頭都有自己唯一的可辨識性信息,如flv文件開頭三個字節(jié)'F' 'L' 'V' .這個可以讓播放器來自動辨識容器格式。
MKV頭部的1A 45 DF A3就是類似這種內(nèi)容,如果發(fā)現(xiàn)開頭是這4個字節(jié),很有可能就是MKV文件。會繼續(xù)進(jìn)行解析??梢钥纯磃fmpeg中matroska_probe
[cpp]view plaincopy
/* 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。里面包括了音視頻信息、音視頻數(shù)據(jù)等等。
還是用上面那個文件作為例子。用EBML方法繼續(xù)解析。
ID = 18 53 80 67
size = 01 EC D8 D4 (32299220)
查表可知 ID = 18 53 80 67 對應(yīng)的就是Segment
Element NameLEBML IDMaMuRngDefaultT1234WDescription
Segment
Segment0[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中可能出現(xiàn)的是下面這些內(nèi)容。
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這部分內(nèi)容比不一定必須有,但是有了它能讓你快速的定位到你關(guān)心的一些關(guān)鍵信息的位置,而不用按照文件順序的解析。
Meta Seek Information
SeekHead1[11][4D][9B][74]-*--m*****Contains the position of other level 1 elements.
Seek2[4D][BB]**--m*****Contains a single seek entry to an EBML element.
SeekID3[53][AB]*---b*****The binary ID corresponding to the element name.
SeekPosition3[53][AC]*---u*****The position of the element in the segment in octets (0 = first level 1 element).
上文提到過了EBML元素都有自己的級別,每一個高一級的元素由若干次一級的元素組成。下圖就是meta seek 結(jié)構(gòu)
Meta Seek InformationSeekHeadSeekSeekID
SeekPosition
SeekSeekID
SeekPosition
還是以上面的MKV文件為例子。Segment中第一部分很可能就是meta seek。
紅線圈里就是seek 內(nèi)同,按照規(guī)范中的表解析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字節(jié)的EBML header  和 12字節(jié)Segment的ID和size加上 正好是4135,咱們找到文件4135(0x1027)的位置看看
正好是info 的位置。所以有了meta seek的信息我們可以快速找到一些關(guān)鍵EBML元素的位置。
把剩下meta seek信息同樣方法解析出來就會等到如下信息:
2.Segment Information
Segment Information 包含識別文件的信息,包括 Title 、 SegmentUID,有個比較關(guān)心的文件時常信息Duration也在這一部分。
Element NameLEBML IDMaMuRngDefaultT123WDescription
Segment Information
Info1[15][49][A9][66]**--m****Contains miscellaneous general information and statistics on the file.
SegmentUID2[73][A4]--not 0-b***
A randomly generated unique ID to identify the current segment between many others (128 bits).
SegmentFilename2[73][84]----8***
A filename corresponding to this segment.
PrevUID2[3C][B9][23]----b***
A unique ID to identify the previous chained segment (128 bits).
PrevFilename2[3C][83][AB]----8***
An escaped filename corresponding to the previous segment.
NextUID2[3E][B9][23]----b***
A unique ID to identify the next chained segment (128 bits).
NextFilename2[3E][83][BB]----8***
An escaped filename corresponding to the next segment.
SegmentFamily2[44][44]-*--b***
A randomly generated unique ID that all segments related to each other must use (128 bits).
ChapterTranslate2[69][24]-*--m***
A tuple of corresponding ID used by chapter codecs to represent this segment.
ChapterTranslateEditionUID3[69][FC]-*--u***
Specify an edition UID on which this correspondance applies. When not specified, it means for all editions found in the segment.
ChapterTranslateCodec3[69][BF]*---u***
The chapter codec using this ID (0: Matroska Script, 1: DVD-menu).
ChapterTranslateID3[69][A5]*---b***
The binary value used to represent this segment in the chapter codec data. The format depends on theChapProcessCodecID used.
TimecodeScale2[2A][D7][B1]*--1000000u****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.
TimecodeScaleDenominator2[2A][D7][B2]*--1000000000u
Timecode scale numerator, seeTimecodeScale.
Duration2[44][89]--> 0-f****Duration of the segment (based on TimecodeScale).
DateUTC2[44][61]----d****Date of the origin of timecode (value 0), i.e. production date.
Title2[7B][A9]----8***
General name of the segment.
MuxingApp2[4D][80]*---8****Muxing application or library ("libmatroska-0.4.3").
WritingApp2[57][41]*---8****Writing application ("mkvmerge-0.3.3").
3.Track
Track包含了音視頻的基本信息,如音視頻解碼器類型、視頻分辨率、音頻采樣率等這。通過對Track部分的解析。我們就能得到音視頻的基本信息。為選擇相應(yīng)解碼器以及初始化這些解碼器做好準(zhǔn)備工作。每個 TrackEntry 代表著1條軌道信息。
Tracks
TrackEntryName
TrackNumber
TrackType
TrackEntryName
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.
AttachmentLink
3
[74][46]
-
-
not 0
-
u
*
*
*
The UID of an attachment that is used by this 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的信息,分別是藍(lán)綠框中的。
先看看第一個track內(nèi)容,也就是藍(lán)色框內(nèi)。只看重要信息
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)
用官方的工具解析結(jié)果
用同樣的方法解析第2路track的信息可以得到
通過對track部分的解析,我們就知道了這個MKV文件包含了2路音視頻數(shù)據(jù)。1路為264的視頻、1路為AC3的音頻。音視頻的相關(guān)參數(shù)也拿到了。
有了上文我們經(jīng)知道了MKV文件時長、音視頻的類型、分辨率、采樣率等基本信息,接下來就是音視頻的數(shù)據(jù)了。
4.Clusters
所有的音視頻幀數(shù)據(jù)都在這部分內(nèi)裝著。
1個Cluster內(nèi)可能有很多個BlockGroup組成,BlockGroup內(nèi)又由若干個Block組成。這些Block內(nèi)就是音視頻的幀數(shù)據(jù)。
1個Cluster并不一定只是音頻或者視頻。它是由不同的音視頻BlockGroup交叉組成。因為多媒體文件中的音視頻數(shù)據(jù)本來就是交叉出現(xiàn)的。
ClustersClusterTimecode
BlockGroupBlock
BlockGroupBlock
ReferenceBlock
BlockGroupBlock
ClusterTimecode
BlockGroupBlock
BlockGroupBlock
BlockGroupBlock
BlockGroupBlock
BlockDuration
Element NameLEBML IDMaMuRngDefaultT123WDescription
Cluster
Cluster1[1F][43][B6][75]-*--m****The lower level element containing the (monolithic) Block structure.
Timecode2[E7]*---u****Absolute timecode of the cluster (based on TimecodeScale).
SilentTracks2[58][54]----m*** The list of tracks that are not used in that part of the stream. It is useful when using overlay tracks on seeking. Then you should decide what track to use.
SilentTrackNumber3[58][D7]-*--u*** One of the track number that are not used from now on in the stream. It could change later if not specified as silent in a further Cluster.
Position2[A7]----u*** The Position of the Cluster in the segment (0 in live broadcast streams). It might help to resynchronise offset on damaged streams.
PrevSize2[AB]----u****Size of the previous Cluster, in octets. Can be useful for backward playing.
SimpleBlock2[A3]-*--b ***Similar to Block but without all the extra information, mostly used to reduced overhead when no extra feature is needed. (see SimpleBlock Structure)
BlockGroup2[A0]-*--m****Basic container of information containing a single Block or BlockVirtual, and information specific to that Block/VirtualBlock.
Block3[A1]*---b****Block containing the actual data to be rendered and a timecode relative to the Cluster Timecode. (see Block Structure)
BlockVirtual3[A2]----b    A Block with no data. It must be stored in the stream at the place the real Block should be in display order. (see Block Virtual)
BlockAdditions3[75][A1]----m*** Contain additional blocks to complete the main one. An EBML parser that has no knowledge of the Block structure could still see and use/skip these data.
BlockMore4[A6]**--m*** Contain the BlockAdditional and some parameters.
BlockAddID5[EE]*-not 01u*** An ID to identify the BlockAdditional level.
BlockAdditional5[A5]*---b*** Interpreted by the codec as it wishes (using the BlockAddID).
BlockDuration3[9B]---TrackDurationu****The duration of the Block (based on TimecodeScale). This element is mandatory when DefaultDuration is set for the track (but can be omitted as other default values). When not written and with no DefaultDuration, the value is assumed to be the difference between the timecode of this Block and the timecode of the next Block in "display" order (not coding order). This element can be useful at the end of a Track (as there is not other Block available), or when there is a break in a track like for subtitle tracks. When set to 0 that means the frame is not a keyframe.
ReferencePriority3[FA]*--0u*** This frame is referenced and has the specified cache priority. In cache only a frame of the same or higher priority can replace this frame. A value of 0 means the frame is not referenced.
ReferenceBlock3[FB]-*--i****Timecode of another frame used as a reference (ie: B or P frame). The timecode is relative to the block it's attached to.
ReferenceVirtual3[FD]----i    Relative position of the data that should be in position of the virtual block.
CodecState3[A4]----b ** The new codec state to use. Data interpretation is private to the codec. This information should always be referenced by a seek entry.
Slices3[8E]----m****Contains slices description.
TimeSlice4[E8]-*--m****Contains extra time information about the data contained in the Block. While there are a few files in the wild with this element, it is no longer in use and has been deprecated. Being able to interpret this element is not required for playback.
LaceNumber5[CC]---0u****The reverse number of the frame in the lace (0 is the last frame, 1 is the next to last, etc). While there are a few files in the wild with this element, it is no longer in use and has been deprecated. Being able to interpret this element is not required for playback.
FrameNumber5[CD]---0u    The number of the frame to generate from this lace with this delay (allow you to generate many frames from the same Block/Frame).
BlockAdditionID5[CB]---0u    The ID of the BlockAdditional element (0 is the main Block).
Delay5[CE]---0u    The (scaled) delay to apply to the element.
SliceDuration5[CF]---0u    The (scaled) duration to apply to the element.
ReferenceFrame3[C8]----m    DivX trick track extenstions
ReferenceOffset4[C9]*---u    DivX trick track extenstions
ReferenceTimeCode4[CA]*---u    DivX trick track extenstions
EncryptedBlock2[AF]-*--b    Similar to SimpleBlock but the data inside the Block are Transformed (encrypt and/or signed). (see EncryptedBlock Structure)
還有用之前的例子
Cluster ID = [1F][43][B6][75]
size = 0x12468f (1197711)
剩下的1197711的數(shù)據(jù)就是這個Cluster 的data
第一個EBML元素 是Timecode  ID = E7 size = 1 值為0 (紅框內(nèi))
第二個元素ID = A0 查表可知這個EBML元素就是BlockGroup  size = 96042
緊接著就是ID = A1 第三級EBML元素 Block  size = 96038
Block 結(jié)構(gòu)如下圖
Block Header
OffsetPlayerDescription
0x00+mustTrack 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+mustTimecode (relative to Cluster timecode, signed int16)
0x03+-Flags
BitPlayerDescription
0-3-Reserved, set to 0
4-Invisible, the codec should decode this frame but not display it
5-6mustLacing00 : no lacing
01 : Xiph lacing
11 : EBML lacing
10 : fixed-size lacing
7-not used
Lace (when lacing bit is set)
0x00mustNumber of frames in the lace-1 (uint8)
0x01 / 0xXXmust*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
0x00mustConsecutive laced frames
第1字節(jié) 表示Track Number
第2-3字節(jié)表示Timecode
第4字節(jié)表示 flags
看上面的例子,
Block data 第1個字節(jié) 0x81  按照EBML解釋方式 Track Number = 1,結(jié)合上文得知 這個Block 數(shù)據(jù)是track 1的數(shù)據(jù)。track 1對應(yīng)的是video數(shù)據(jù),解碼器類型是H.264.也就是這個block 的數(shù)據(jù)是264幀數(shù)據(jù)
Timecode 為 0000
flags = 0
Lace是根據(jù) flags 的值來確定的。上面這個flags 5-6位都是0 所有是no lacing。剩下的96038 - 4 都是視頻的幀數(shù)據(jù)。
將這個96034長度block 的數(shù)據(jù)轉(zhuǎn)成NALU格式,然后加上從track部分中的CodecPrivate數(shù)據(jù)中解析出來的sps 和 pps 信息 保存到本地,應(yīng)該就是1幀的264數(shù)據(jù)
用elecard 打開 果然是1幀I幀數(shù)據(jù)
按照這個套路,看看下一個
BlockGroup ID = A0 size = 0x2808
第一個block  ID = A1 size = 0x2805(10245)
第1字節(jié) 表示Track Number   =2  表示是track 2的數(shù)據(jù),track是ac3 的音頻。
第2-3字節(jié)表示Timecode = 0x0005;
第4字節(jié)表示 flags = 0x04
這個時候就要解析 Lace   flags 第5-6位為10 所有屬于fixed-size lacing
Fixed-size lacing 是如下的結(jié)構(gòu)
Fixed-size lacing
In this case only the number of frames in the lace is saved, the size of each frame is deduced from the total size of the Block. For example, for 3 frames of 800 octets each :
Block head (with lacing bits set to 10)
Lacing head: Number of frames in the lace -1, i.e. 2
Data in frame 1
Data in frame 2
Data in frame 3
07+1 就是包含的幀數(shù),因為是ac3的音頻 可以看到 07后面緊接著就是ac3的同步頭0x0b77(綠框)
用工具看看相對應(yīng)的解析結(jié)果
按照這種邏輯和方法,我們就可以把mkv文件中的音視頻數(shù)據(jù)流demux出來了。
5.Cueing Data
Cueing Data 這部分內(nèi)容其實是關(guān)鍵幀的index,如果沒有關(guān)鍵幀的index的話,在做seek、快進(jìn)快退的時候是十分困難的。你要逐個包去找。之前說過flv文件中官方?jīng)]有做I幀index的規(guī)定。但是在民間已經(jīng)做了補充。mkv官方有對index的規(guī)范。那就是Cueing Data
下面是結(jié)構(gòu)圖。
Cueing DataCuesCuePointCueTime
CuePosition
CuePointCueTime
CuePosition
Cueing Data
Cues1[1C][53][BB][6B]----m****A top-level element to speed seeking access. All entries are local to the segment. Should be mandatory for non .
CuePoint2[BB]**--m****Contains all information relative to a seek point in the segment.
CueTime3[B3]*---u****Absolute timecode according to the segment time base.
CueTrackPositions3[B7]**--m****Contain positions for different tracks corresponding to the timecode.
CueTrack4[F7]*-not 0-u****The track for which a position is given.
CueClusterPosition4[F1]*---u****The 
position of the Cluster containing the required Block.
CueRelativePosition4[F0]----u    The relative position of the referenced block inside the cluster with 0 being the first possible position for an element inside that cluster.
CueDuration4[B2]----u    The duration of the block according to the segment time base. If missing the track's DefaultDuration does not apply and no duration information is available in terms of the cues.
CueBlockNumber4[53][78]--not 01u****Number of the Block in the specified Cluster.
CueCodecState4[EA]---0u ** The position of the Codec State corresponding to this Cue element. 0 means that the data is taken from the initial Track Entry.
CueReference4[DB]-*--m ** The Clusters containing the required referenced Blocks.
CueRefTime5[96]*---u ** Timecode of the referenced Block.
繼續(xù)看上面的例子,我們找到了Cues  所在的位置。
ID = [1C][53][BB][6B] 表示Cues size = 0x7f
緊接著每個ID = 0xBB 就是一個CuePoint,圖中的綠色框中的就是一個。 size = 0xC
CueTime ID = 0xB3  size = 1 data = 0;
CueTrackPositions  ID=0xB7 size=7 data=0xf78101f18215ef
CueTrack ID=F7 size = 1 data = 1 表示這個位置的track num 值為1  針對這個流應(yīng)該是video
CueClusterPosition ID = F1 size = 2 data = 15ef   位置是在0x15ef(5615) 相對于Segment
找到這個位置發(fā)現(xiàn)是第一個Clusters 上面章節(jié)分析了,這個族的video內(nèi)容正好是關(guān)鍵幀。
按照這種方式 發(fā)現(xiàn)這個文件中共有8個cuepoint 信息
把這個文件中的264視頻demux出來,用工具查看發(fā)現(xiàn)關(guān)鍵幀正好也是8個。
6.小結(jié)
已經(jīng)把MKV主要部分的內(nèi)容作了一次詳細(xì)的敘述,現(xiàn)在對mkv文件做個小結(jié)。
1.MKV的基本組成單元都是EBML格式。每個元素都有級別。一級一級的包括組成了mkv不同的部分。
Level 0GroupingLevel 1Level 2Level 3
2.MKV是由EBML header 和Segment 2大部分組成。Segment中又分Meta Seek InformationSegment InformationTrackChaptersClustersCueing DataAttachmentTagging
3.EBML header 部分包含著MKV可辨識性的信息。
4.Meta Seek Information包含其實部分位置信息。
5.Segment Information 包含識別文件的信息,包括 Title 、 SegmentUID,有個比較關(guān)心的文件時常信息Duration也在這一部分
6.Track包含了音視頻的基本信息,如音視頻解碼器類型、視頻分辨率、音頻采樣率等。
7.真實的音視頻數(shù)據(jù)信息交叉裝在Clusters中
8.Cueing Data 關(guān)鍵幀index,對seek至關(guān)重要。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
視頻文件頭解析--mkv
MKV文件批量重新混流
高速、高質(zhì)的MKV切割工具
360云盤
[轉(zhuǎn)載]ondori雄雞社crochet lace,white lace
精美愛爾蘭禮服(15)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服