3 Generate C definitions for parsing Matroska files.
4 Can also be used to directly parse Matroska files and display their contents.
8 # This file is part of MPlayer.
10 # MPlayer is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # MPlayer is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 'EBML, 1a45dfa3, sub', (
28 'EBMLVersion, 4286, uint',
29 'EBMLReadVersion, 42f7, uint',
30 'EBMLMaxIDLength, 42f2, uint',
31 'EBMLMaxSizeLength, 42f3, uint',
33 'DocTypeVersion, 4287, uint',
34 'DocTypeReadVersion, 4285, uint',
42 'Segment, 18538067, sub', (
44 'SeekHead*, 114d9b74, sub', (
46 'SeekID, 53ab, ebml_id',
47 'SeekPosition, 53ac, uint',
51 'Info*, 1549a966, sub', (
52 'SegmentUID, 73a4, binary',
53 'PrevUID, 3cb923, binary',
54 'NextUID, 3eb923, binary',
55 'TimecodeScale, 2ad7b1, uint',
56 'DateUTC, 4461, sint',
58 'MuxingApp, 4d80, str',
59 'WritingApp, 5741, str',
60 'Duration, 4489, float',
63 'Cluster*, 1f43b675, sub', (
65 'BlockGroup*, a0, sub', (
67 'BlockDuration, 9b, uint',
68 'ReferenceBlock*, fb, sint',
70 'SimpleBlock*, a3, binary',
73 'Tracks*, 1654ae6b, sub', (
74 'TrackEntry*, ae, sub', (
75 'TrackNumber, d7, uint',
76 'TrackUID, 73c5, uint',
77 'TrackType, 83, uint',
78 'FlagEnabled, b9, uint',
79 'FlagDefault, 88, uint',
80 'FlagForced, 55aa, uint',
81 'FlagLacing, 9c, uint',
82 'MinCache, 6de7, uint',
83 'MaxCache, 6df8, uint',
84 'DefaultDuration, 23e383, uint',
85 'TrackTimecodeScale, 23314f, float',
86 'MaxBlockAdditionID, 55ee, uint',
88 'Language, 22b59c, str',
90 'CodecPrivate, 63a2, binary',
91 'CodecDecodeAll, aa, uint',
93 'FlagInterlaced, 9a, uint',
94 'PixelWidth, b0, uint',
95 'PixelHeight, ba, uint',
96 'DisplayWidth, 54b0, uint',
97 'DisplayHeight, 54ba, uint',
98 'FrameRate, 2383e3, float',
101 'SamplingFrequency, b5, float',
102 'Channels, 9f, uint',
103 'BitDepth, 6264, uint',
105 'ContentEncodings, 6d80, sub', (
106 'ContentEncoding*, 6240, sub', (
107 'ContentEncodingOrder, 5031, uint',
108 'ContentEncodingScope, 5032, uint',
109 'ContentEncodingType, 5033, uint',
110 'ContentCompression, 5034, sub', (
111 'ContentCompAlgo, 4254, uint',
112 'ContentCompSettings, 4255, binary',
119 'Cues, 1c53bb6b, sub', (
120 'CuePoint*, bb, sub', (
122 'CueTrackPositions*, b7, sub', (
123 'CueTrack, f7, uint',
124 'CueClusterPosition, f1, uint',
129 'Attachments, 1941a469, sub', (
130 'AttachedFile*, 61a7, sub', (
131 'FileName, 466e, str',
132 'FileMimeType, 4660, str',
133 'FileData, 465c, binary',
134 'FileUID, 46ae, uint',
138 'Chapters, 1043a770, sub', (
139 'EditionEntry*, 45b9, sub', (
140 'EditionUID, 45bc, uint',
141 'EditionFlagHidden, 45bd, uint',
142 'EditionFlagDefault, 45db, uint',
143 'EditionFlagOrdered, 45dd, uint',
144 'ChapterAtom*, b6, sub', (
145 'ChapterUID, 73c4, uint',
146 'ChapterTimeStart, 91, uint',
147 'ChapterTimeEnd, 92, uint',
148 'ChapterFlagHidden, 98, uint',
149 'ChapterFlagEnabled, 4598, uint',
150 'ChapterSegmentUID, 6e67, binary',
151 'ChapterSegmentEditionUID, 6ebc, uint',
152 'ChapterDisplay*, 80, sub', (
153 'ChapString, 85, str',
154 'ChapLanguage*, 437c, str',
155 'ChapCountry*, 437e, str',
160 'Tags*, 1254c367, sub', (
162 'Targets, 63c0, sub', (
163 'TargetTypeValue, 68ca, uint',
164 'TargetTrackUID, 63c5, uint',
165 'TargetEditionUID, 63c9, uint',
166 'TargetChapterUID, 63c4, uint',
167 'TargetAttachmentUID, 63c6, uint',
169 'SimpleTag*, 67c8, sub', (
170 'TagName, 45a3, str',
171 'TagLanguage, 447a, str',
172 'TagString, 4487, str'
181 from math
import ldexp
182 from binascii
import hexlify
185 return int(hexlify(s
), 16)
187 class EOF(Exception): pass
189 def camelcase_to_words(name
):
192 for i
in range(1, len(name
)):
193 if name
[i
].isupper() and (name
[i
-1].islower() or
194 name
[i
+1:i
+2].islower()):
195 parts
.append(name
[start
:i
])
197 parts
.append(name
[start
:])
198 return '_'.join(parts
).lower()
200 class MatroskaElement(object):
202 def __init__(self
, name
, elid
, valtype
, namespace
):
204 self
.definename
= '{}_ID_{}'.format(namespace
, name
.upper())
205 self
.fieldname
= camelcase_to_words(name
)
206 self
.structname
= 'ebml_' + self
.fieldname
208 self
.valtype
= valtype
210 self
.ebmltype
= 'EBML_TYPE_SUBELEMENTS'
211 self
.valname
= 'struct ' + self
.structname
213 self
.ebmltype
= 'EBML_TYPE_' + valtype
.upper()
215 self
.valname
= {'uint': 'uint64_t', 'str': 'struct bstr',
216 'binary': 'struct bstr', 'ebml_id': 'uint32_t',
217 'float': 'double', 'sint': 'int64_t',
220 raise SyntaxError('Unrecognized value type ' + valtype
)
221 self
.subelements
= ()
223 def add_subelements(self
, subelements
):
224 self
.subelements
= subelements
225 self
.subids
= set(x
[0].elid
for x
in subelements
)
229 def parse_elems(l
, namespace
):
232 if isinstance(el
, str):
233 name
, hexid
, eltype
= [x
.strip() for x
in el
.split(',')]
234 multiple
= name
.endswith('*')
235 name
= name
.strip('*')
236 new
= MatroskaElement(name
, hexid
, eltype
, namespace
)
237 elementd
[hexid
] = new
238 elementlist
.append(new
)
239 subelements
.append((new
, multiple
))
241 new
.add_subelements(parse_elems(el
, namespace
))
244 parse_elems(elements_ebml
, 'EBML')
245 parse_elems(elements_matroska
, 'MATROSKA')
247 def generate_C_header():
248 print('// Generated by TOOLS/matroska.py, do not edit manually')
251 for el
in elementlist
:
252 print('#define {0.definename:40} 0x{0.elid}'.format(el
))
256 for el
in reversed(elementlist
):
257 if not el
.subelements
:
260 print('struct {0.structname} {{'.format(el
))
261 l
= max(len(subel
.valname
) for subel
, multiple
in el
.subelements
)+1
262 for subel
, multiple
in el
.subelements
:
263 print(' {e.valname:{l}} {star}{e.fieldname};'.format(
264 e
=subel
, l
=l
, star
=' *'[multiple
]))
266 for subel
, multiple
in el
.subelements
:
267 print(' int n_{0.fieldname};'.format(subel
))
270 for el
in elementlist
:
271 if not el
.subelements
:
273 print('extern const struct ebml_elem_desc {0.structname}_desc;'.format(
277 print('#define MAX_EBML_SUBELEMENTS', max(len(el
.subelements
)
278 for el
in elementlist
))
282 def generate_C_definitions():
283 print('// Generated by TOOLS/matroska.py, do not edit manually')
285 for el
in reversed(elementlist
):
288 print('#define N', el
.fieldname
)
289 print('E_S("{}", {})'.format(el
.name
, len(el
.subelements
)))
290 for subel
, multiple
in el
.subelements
:
291 print('F({0.definename}, {0.fieldname}, {1})'.format(
292 subel
, int(multiple
)))
296 print('E("{0.name}", {0.fieldname}, {0.ebmltype})'.format(el
))
310 while not ord(t
) & mask
:
322 while not ord(t
) & mask
:
325 t
= bytes((ord(t
) & (mask
- 1),))
327 return i
+1, byte2num(t
)
329 def read_str(s
, length
):
330 return read(s
, length
)
332 def read_uint(s
, length
):
336 def read_sint(s
, length
):
337 i
= read_uint(s
, length
)
338 mask
= 1 << (length
* 8 - 1)
343 def read_float(s
, length
):
347 f
= ldexp((i
& 0x7fffff) + (1 << 23), (i
>> 23 & 0xff) - 150)
351 f
= ldexp((i
& ((1 << 52) - 1)) + (1 << 52), (i
>> 52 & 0x7ff) - 1075)
358 def parse_one(s
, depth
, parent
, maxlen
):
359 elid
= hexlify(read_id(s
)).decode('ascii')
360 elem
= elementd
.get(elid
)
361 if parent
is not None and elid
not in parent
.subids
and elid
not in ('ec', 'bf'):
362 print('Unexpected:', elid
)
364 raise NotImplementedError
365 size
, length
= read_vint(s
)
366 this_length
= len(elid
) / 2 + size
+ length
368 if elem
.valtype
!= 'skip':
369 print(depth
, elid
, elem
.name
, 'size:', length
, 'value:', end
=' ')
370 if elem
.valtype
== 'sub':
371 print('subelements:')
373 length
-= parse_one(s
, depth
+ 1, elem
, length
)
376 elif elem
.valtype
== 'str':
377 print('string', repr(read_str(s
, length
).decode('utf8', 'replace')))
378 elif elem
.valtype
in ('binary', 'ebml_id'):
379 t
= read_str(s
, length
)
381 if elem
.valtype
== 'ebml_id':
382 idelem
= elementd
.get(hexlify(t
).decode('ascii'))
386 dec
= '({0.name})'.format(idelem
)
388 t
= hexlify(t
).decode('ascii')
390 t
= '<skipped {} bytes>'.format(len(t
))
391 print('binary', t
, dec
)
392 elif elem
.valtype
== 'uint':
393 print('uint', read_uint(s
, length
))
394 elif elem
.valtype
== 'sint':
395 print('sint', read_sint(s
, length
))
396 elif elem
.valtype
== 'float':
397 print('float', read_float(s
, length
))
398 elif elem
.valtype
== 'skip':
401 raise NotImplementedError
403 print(depth
, 'Unknown element:', elid
, 'size:', length
)
407 def parse_toplevel(s
):
408 parse_one(s
, 0, None, 1 << 63)
410 if sys
.argv
[1] == '--generate-header':
412 elif sys
.argv
[1] == '--generate-definitions':
413 generate_C_definitions()
415 s
= open(sys
.argv
[1], "rb")
421 if s
.tell() != start
:
422 raise Exception("Unexpected end of file")