forked from Ruslan-B/FFmpeg.AutoGen
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.py
More file actions
451 lines (372 loc) · 17.2 KB
/
generate.py
File metadata and controls
451 lines (372 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import re
import os
import ctypesgencore
import ctypesgencore.ctypedescs as ctypedescs
class DescriptionsEvaluationContext(ctypesgencore.expressions.EvaluationContext):
def __init__(self, descriptions):
self.descriptions = descriptions
self.ids_map = dict((c.name, c) for c in descriptions.constants)
def evaluate_identifier(self, name):
if name in self.ids_map:
expression = self.ids_map[name].value
return expression.evaluate(self)
#warnings.warn('Attempt to evaluate identifier "%s" failed' % name)
return 0
class FileWriter:
def __init__(self, filename):
self.filename = filename
self.indentation_level = 0
def out(self, line=''):
print >> self.f, ('\t' * self.indentation_level) + line
def begin_block(self):
self.out('{')
self.indentation_level += 1
def end_block(self):
self.indentation_level -= 1
self.out('}')
def __enter__(self):
self.f = file(self.filename, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
self.f.__exit__(exc_type, exc_val, exc_tb)
class GeneratorBase:
name_conversions = {'base', 'internal', 'in', 'out', 'ref'}
def escape_id_if_needed(self, name):
if name in self.name_conversions:
return '@' + name
return name
type_conversions = {'int8_t': 'sbyte', 'uint8_t': 'byte',
'int16_t': 'short', 'uint16_t': 'ushort',
'int32_t': 'int', 'uint32_t': 'uint',
'int64_t': 'long', 'uint64_t': 'ulong',
'float32_t': 'float',
'char': 'byte',
'intmax_t': 'long', 'uintmax_t': 'ulong',
'size_t': 'uint', 'SIZE_T': 'uint',
'va_list': 'void*',
'FILE': 'void'}
def get_type_name(self, ctype):
if isinstance(ctype, ctypedescs.CtypesPointer):
dst_type = ctype.destination
return '%s*' % self.get_type_name(dst_type)
if isinstance(ctype, ctypedescs.CtypesSimple) or isinstance(ctype, ctypedescs.CtypesTypedef):
if ctype.name in self.type_conversions:
return self.type_conversions[ctype.name]
else:
return ctype.name
if isinstance(ctype, ctypedescs.CtypesSpecial):
return ctype.name
if isinstance(ctype, ctypedescs.CtypesEnum):
return ctype.tag
if isinstance(ctype, ctypedescs.CtypesStruct):
return ctype.tag
if isinstance(ctype, ctypedescs.CtypesFunction):
restype_name = self.get_type_name(ctype.restype)
params = ', '.join(self.get_type_name(p) for p in ctype.argtypes)
if restype_name == 'void':
return "Action<%s>" % params
else:
return "Func<%s, %s>" % (params, restype_name)
if isinstance(ctype, ctypedescs.CtypesArray):
return "%s*" % self.get_type_name(ctype.base)
else:
return ctype.name
def write_to(self, writer):
pass
class LibraryGenerator(GeneratorBase):
def __init__(self, name):
p = re.compile(r'(?:lib)?(\w+)[.-](\d+)')
m = p.match(name)
libname = m.group(1)
version = m.group(2)
self.id = self.escape_id_if_needed("%s_LIBRARY" % libname.upper())
self.name = '%s-%s' % (libname, version)
def write_to(self, writer):
writer.out('public const string %s = "%s";' % (self.id, self.name))
class ConstGenerator(GeneratorBase):
def __init__(self, name, value, comment=None):
self.id = self.escape_id_if_needed(name)
self.value = value
self.comment = comment
pass
def write_to(self, writer):
value = self.value
if isinstance(value, int):
if value < 0x80000000L:
const_type = 'int'
else:
const_type = 'uint'
value = hex(value)
elif isinstance(value, long):
if value < 0x8000000000000000L:
const_type = 'long'
else:
const_type = 'ulong'
value = hex(value)
elif isinstance(value, float):
const_type = 'float'
value = "%ff" % value
elif isinstance(value, basestring):
const_type = 'string'
value = '"' + value + '"'
else:
const_type = '<unknown>'
if self.comment:
writer.out('public const %s %s = %s; // %s' % (const_type, self.id, value, self.comment))
else:
writer.out('public const %s %s = %s;' % (const_type, self.id, value))
class MethodGenerator(GeneratorBase):
def __init__(self, description, library):
self.id = self.escape_id_if_needed(description.name)
self.description = description
self.library = library
def write_to(self, writer):
writer.out(
'[DllImport(%s, EntryPoint="%s", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]'
% (self.library.id, self.description.name))
params = []
i = 0
for arg_type in self.description.argtypes:
arg_name = self.description.argnames[i]
arg_name = self.escape_id_if_needed(arg_name)
if isinstance(arg_type, ctypedescs.CtypesFunction):
p_type_name = "IntPtr"
else:
p_type_name = self.get_type_name(arg_type)
params.append('%s %s' % (p_type_name, arg_name))
i += 1
r_type_name = self.get_type_name(self.description.restype)
params_out = ', '.join(x for x in params)
writer.out('public static extern %s %s(%s);' % (r_type_name, self.id, params_out))
class WrapperGenerator(GeneratorBase):
def __init__(self, descriptions, options):
self.descriptions = descriptions
self.options = options
self.typedefs_map = dict((td.name, td) for td in descriptions.typedefs)
self.evaluation_context = DescriptionsEvaluationContext(self.descriptions)
self.indentation_level = 0
def type_was_included(self, ctype):
source_path, line_number = ctype.src
source_path = os.path.abspath(source_path).lower()
for output_only_from_path in options.output_only_from_paths:
output_only_from_path = os.path.abspath(output_only_from_path).lower()
if source_path.startswith(output_only_from_path):
return True
return False
def evaluate_expression(self, expression):
if isinstance(expression, ctypesgencore.expressions.TypeCastExpressionNode):
if isinstance(expression.base, ctypesgencore.expressions.CallExpressionNode):
expression = expression.base.arguments[0]
return expression.evaluate(self.evaluation_context)
def write_enum(self, enum_name, enum_type, writer):
writer.out('public enum %s' % self.escape_id_if_needed(enum_name))
writer.begin_block()
last_value = -1
for item in enum_type.enumerators:
name, expression = item
name = self.escape_id_if_needed(name)
value = self.evaluate_expression(expression)
if value == (last_value + 1):
writer.out('%s,' % name)
else:
writer.out('%s = %s, // %s' % (name, hex(value), expression.py_string(False)))
last_value = value
writer.end_block()
writer.out()
def write_struct(self, struct_name, struct, writer):
writer.out('[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]')
writer.out('public unsafe struct %s' % struct_name)
writer.begin_block()
bit_field = None
bit_field_count = 0
bit_field_shift = 0
if struct.members:
for member in struct.members:
name, ctype = member
name = self.escape_id_if_needed(name)
if isinstance(ctype, ctypedescs.CtypesFunction):
writer.out('public IntPtr %s; // %s' % (name, self.get_type_name(ctype)))
continue
if isinstance(ctype, ctypedescs.CtypesBitfield):
size = self.evaluate_expression(ctype.bitfield)
ctype_name = ctype.base.name
if not bit_field:
bit_field = '_bitfield0x%0.2X;' % bit_field_count
writer.out('public uint %s' % bit_field)
bit_field_count += 1
bit_field_shift += size
if bit_field_shift > 32:
raise Exception('Bit field allocation size error.', name)
elif bit_field_shift == 32:
bit_field = None
bit_field_shift = 0
writer.out('//bit field %s %s:%d' % (name, ctype_name, size))
continue
# review
if isinstance(ctype, ctypedescs.CtypesArray):
if ctype.count:
size = self.evaluate_expression(ctype.count)
base_type = ctype.base
if isinstance(base_type, ctypedescs.CtypesPointer) or isinstance(base_type,
ctypedescs.CtypesStruct):
# unfold fixed pointer array or structure array to set of indexed fields
ctype_name = self.get_type_name(base_type)
writer.out('// fixed %s %s[%d] - %s' % (ctype_name, name, size, ctype))
for i in range(size):
writer.out('public %s %s_%d;' % (ctype_name, name, i))
continue
else:
while isinstance(base_type, ctypedescs.CtypesArray) and base_type.count:
size *= self.evaluate_expression(ctype.base.count)
base_type = base_type.base
ctype_name = self.get_type_name(base_type)
#if isinstance(base_type, ctypedescs.CtypesTypedef):
# if base_type.name in self.typedefs_map:
# typedef = self.typedefs_map[base_type.name]
# base_type = typedef.ctype
#if isinstance(base_type, ctypedescs.CtypesStruct):
# self.out('[MarshalAs(UnmanagedType.ByValArray, SizeConst = %d)]' % size)
# self.out('public %s[] %s; // %s' % (ctype_name, name, ctype))
#else:
writer.out('public fixed %s %s[%d]; // %s' % (ctype_name, name, size, ctype))
continue
else:
ctype_name = self.get_type_name(ctype)
if isinstance(ctype, ctypedescs.CtypesPointer) and isinstance(ctype.destination, ctypedescs.CtypesTypedef):
try:
pointer_typedef = self.typedefs_map[ctype.destination.name]
if isinstance(pointer_typedef.ctype, ctypedescs.CtypesFunction):
writer.out('public IntPtr %s; // %s - %s' % (name, self.get_type_name(ctype), self.get_type_name(pointer_typedef.ctype)))
continue
except KeyError:
print "Warning: Could not find typedef:", ctype.destination.name
ctype_name = self.get_type_name(ctype)
else:
ctype_name = self.get_type_name(ctype)
if ctype_name == 'String':
ctype_name = 'byte*'
writer.out('public %s %s;' % (ctype_name, name))
writer.end_block()
writer.out()
def get_typedef(self, ctype):
for typedef in self.descriptions.typedefs:
if typedef.ctype is ctype:
return typedef
return None
def write_to(self, writer):
writer.out('using System;')
writer.out('using System.Runtime.InteropServices;')
writer.out()
writer.out('namespace %s' % self.options.namespace)
writer.begin_block()
# aliases
for typedef in self.descriptions.typedefs:
if self.type_was_included(typedef):
if isinstance(typedef.ctype, ctypedescs.CtypesEnum) or \
isinstance(typedef.ctype, ctypedescs.CtypesStruct):
type_name = typedef.ctype.tag
else:
# ignore rest
continue
if typedef.name != type_name:
writer.out('using %s=%s;' % (typedef.name, type_name))
writer.out()
# form functions and libraries list
methods = []
libraries_map = {}
for description in self.descriptions.functions:
if description.source_library is not None and self.type_was_included(description):
library_name = description.source_library
if library_name in libraries_map:
library = libraries_map[library_name]
else:
library = LibraryGenerator(library_name)
libraries_map[library.name] = library
method = MethodGenerator(description, library)
methods.append(method)
libraries = libraries_map.values()
constants = []
for macros in descriptions.macros:
if macros.expr and self.type_was_included(macros):
try:
value = self.evaluate_expression(macros.expr)
comment = macros.expr.py_string(False)
if str(value) == comment:
comment = None
constant = ConstGenerator(macros.name, value, comment)
constants.append(constant)
except (TypeError, ZeroDivisionError, AttributeError, ValueError):
print "Warning: Could not evaluate macro:", macros.name
for enum in self.descriptions.enums:
if self.type_was_included(enum):
enum_name = self.escape_id_if_needed(enum.tag)
self.write_enum(enum_name, enum.ctype, writer)
for struct in self.descriptions.structs:
if self.type_was_included(struct):
struct_name = self.escape_id_if_needed(struct.tag)
self.write_struct(struct_name, struct, writer)
# generate empty structures for void typedefs
for typedef in self.descriptions.typedefs:
if self.type_was_included(typedef):
if isinstance(typedef.ctype, ctypedescs.CtypesSimple) and typedef.ctype.name == 'void':
struct_name = self.escape_id_if_needed(typedef.name)
self.write_struct(struct_name, ctypedescs.CtypesStruct(struct_name, None, None), writer)
writer.out('public static unsafe partial class %s' % self.options.class_name)
writer.begin_block()
for library in libraries:
library.write_to(writer)
writer.out()
for constant in constants:
constant.write_to(writer)
writer.out()
for method in methods:
method.write_to(writer)
writer.end_block()
writer.end_block()
work_path = os.path.dirname(os.path.realpath(__file__))
class Options:
headers = ['FFmpeg/include/libavcodec/avcodec.h',
'FFmpeg/include/libavdevice/avdevice.h',
'FFmpeg/include/libavfilter/avfilter.h',
'FFmpeg/include/libavformat/avformat.h',
'FFmpeg/include/libavutil/avutil.h',
'FFmpeg/include/libpostproc/postprocess.h',
'FFmpeg/include/libswresample/swresample.h',
'FFmpeg/include/libswscale/swscale.h']
other_headers = []
cpp = 'gcc -E'
include_search_paths = ['./FFmpeg/include']
all_headers = True
save_preprocessed_headers = False
other_known_names = []
builtin_symbols = False
exclude_symbols = []
include_symbols = []
include_macros = True
compile_libdirs = ['/opt/local/lib']
libraries = ['libavutil.52', 'libavcodec.55', 'libavformat.55', 'libswresample.0',
'libswscale.2', 'libpostproc.52', 'libavfilter.3', 'libavdevice.55']
show_all_errors = True
show_long_errors = True
show_macro_warnings = True
# printer
strip_build_path = []
header_template = False
runtime_libdirs = []
modules = []
inserted_files = []
#printer
namespace = 'FFmpeg.AutoGen'
class_name = 'FFmpegInvoke'
output_only_from_paths = ['./FFmpeg/include']
options = Options()
# Step 1: Parse
descriptions = ctypesgencore.parser.parse(options.headers, options)
# Step 2: Process
ctypesgencore.processor.process(descriptions, options)
# Step 3: Generate output
output_filename = './FFmpeg.AutoGen/FFmpegInvoke.cs'
writer = FileWriter(output_filename)
with writer:
generator = WrapperGenerator(descriptions, options)
generator.write_to(writer)
#ctypesgencore.printer.WrapperPrinter(options.output, options, descriptions)