-
Notifications
You must be signed in to change notification settings - Fork 4
/
muvs.py
1379 lines (1081 loc) · 42.6 KB
/
muvs.py
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
An interface to VapourSynth
*** DO NOT PUBLISH MODULES THAT DEPEND ON THIS ***
objects:
core (resembles vapoursynth.core)
functions:
pollute (poisons for foreign modules)
expr (switch for arithmetic expression)
Expr (resembles core.std.Expr(), but with infix expression)
record (computational graph recorder, resembles open())
Recorder (base class for recorder)
functions for arithmetic expression:
Abs, Exp, Not, And, Or, Xor, Log,
Sqrt, Min, Max, Conditional
"""
from abc import ABC, abstractmethod, abstractstaticmethod
from collections import OrderedDict
import collections.abc
from contextlib import contextmanager
import functools
import inspect
import itertools
import math
import numbers
import operator as op
from typing import Callable, Dict, List, MutableMapping, MutableSet
from typing import Optional, Sequence, Union
import weakref
import vapoursynth as vs
from vapoursynth import core as _vscore
__all__ = [
"core", "expr", "pollute", "Expr", "record", "Recorder",
"Abs", "Exp", "Not", "And", "Or", "Xor", "Log", "Sqrt",
"Min", "Max", "Conditional"]
_is_api4: bool = hasattr(vs, "__api_version__") and vs.__api_version__.api_major == 4
class _Core:
def __init__(self):
self._registered_funcs = {} # type: Dict[str, Callable[..., '_VideoNode']]
def __setattr__(self, name, value):
if name in ["num_threads", "max_cache_size"]:
setattr(_vscore, name, value)
else:
if callable(value):
if name[0].isupper() and not hasattr(_vscore, name):
self._registered_funcs[name] = value
else:
raise AttributeError("Attribute name should be capitalized")
else:
vars(self)[name] = value
def __getattr__(self, name):
try:
attr = getattr(_vscore, name)
except AttributeError as e:
if name in self._registered_funcs:
return self._registered_funcs[name]
else:
raise e
else:
if isinstance(attr, vs.Plugin):
return _Plugin(attr)
else:
return attr
def __dir__(self) -> List[str]:
return dir(_vscore) + sorted(list(self._registered_funcs.keys()))
def register_functions(self, **kwargs: Dict[str, Callable[..., '_VideoNode']]):
if all((name[0].isupper() and not hasattr(_vscore, name))
for name in kwargs.keys()):
self._registered_funcs.update(kwargs)
else:
raise ValueError("Registration error.")
core = _Core()
arithmetic_expr : bool = False
@contextmanager
def expr():
global arithmetic_expr
prev_expr = arithmetic_expr
arithmetic_expr = True
try:
yield None
finally:
arithmetic_expr = prev_expr
class Recorder:
_live_recorders : MutableSet["Recorder"] = weakref.WeakSet()
def __init__(self):
self.buffer : List[str] = []
self.is_recording : bool = False
Recorder._live_recorders.add(self)
def start_recording(self, include_header=False):
self.is_recording = True
if include_header:
self.buffer.append(
"import vapoursynth as vs\n"
"from vapoursynth import core\n"
"\n"
f"core.num_threads = {core.num_threads}\n"
f"core.max_cache_size = {core.max_cache_size}\n"
"\n")
def end_recording(self, filename_or_stream, mode='a', **open_kwargs):
self.is_recording = False
if self.buffer:
if isinstance(filename_or_stream, str):
with open(filename_or_stream, mode=mode, **open_kwargs) as f:
f.writelines(self.buffer)
else:
stream = filename_or_stream
stream.writelines(self.buffer)
self.buffer.clear()
def write(self, text):
assert isinstance(text, str)
self.buffer.append(text)
@contextmanager
def record(filename_or_stream, mode='a', include_header=False, **open_kwargs):
recorder = Recorder()
recorder.start_recording(include_header)
try:
yield recorder
finally:
recorder.end_recording(filename_or_stream=filename_or_stream, mode=mode, **open_kwargs)
def _build_repr() -> Callable[..., str]:
_clip_name_mapping = weakref.WeakKeyDictionary() # type: MutableMapping[vs.VideoNode, str]
counter = 0
def closure(obj, default_prefix="unknown") -> str:
if isinstance(obj, vs.VideoNode):
if obj in _clip_name_mapping:
return _clip_name_mapping[obj]
else:
nonlocal counter
name = f"{default_prefix}{counter}"
_clip_name_mapping[obj] = name
counter += 1
return name
elif isinstance(obj, _VideoNode):
return closure(obj._node, default_prefix)
elif isinstance(obj, collections.abc.Sequence) and not isinstance(obj, (str, bytes, bytearray)):
return f"[{', '.join(closure(elem, default_prefix) for elem in obj)}]"
elif isinstance(obj, (
vs.ColorFamily, vs.SampleType,
getattr(vs, "PresetFormat", getattr(vs, "PresetVideoFormat", None))
)):
return f"vs.{obj.name}"
elif isinstance(obj, (vs.VideoFormat if _is_api4 else vs.Format)):
arg_str = ', '.join(f"{k}={closure(v)}" for k, v in obj._as_dict().items())
return f"core.query_video_format({arg_str})" if _is_api4 else f"core.register_format({arg_str})"
else:
return repr(obj)
return closure
_repr = _build_repr()
class _Plugin:
def __init__(self, plugin: vs.Plugin, injected_clip: Optional[vs.VideoNode] = None):
if isinstance(plugin, vs.Plugin):
self._plugin = plugin
else:
raise TypeError(f"{type(self).__name__!r}: Unknown plugin ({type(plugin)})")
if injected_clip is None or isinstance(injected_clip, vs.VideoNode):
self._injected_clip = injected_clip
else:
raise TypeError(f"{type(self).__name__!r}: Unknown injected clip ({type(injected_clip)})")
def __getattr__(self, function_name):
attr = getattr(self._plugin, function_name)
if isinstance(attr, vs.Function):
func = attr
@functools.wraps(func)
def closure(*args, **kwargs):
if self._injected_clip is not None:
args = (self._injected_clip, ) + args
def get_node(obj):
if isinstance(obj, vs.VideoNode):
return obj
elif isinstance(obj, _VideoNode):
return obj._node
elif isinstance(obj, _ArithmeticExpr):
return obj.compute()._node
elif (
isinstance(obj, collections.abc.Sequence) and
not isinstance(obj, (str, bytes, bytearray))
):
return type(obj)(get_node(item) for item in obj)
elif callable(obj):
class _remove_wrap:
"""Fixes callables that returns VideoNode"""
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
output = self.func(*args, **kwargs)
if isinstance(output, _VideoNode):
output = output._node
return output
def __repr__(self):
return repr(self.func)
return _remove_wrap(obj)
else:
return obj
def get_key(key):
if key.startswith('_'):
return key[1:]
else:
return key
args = get_node(args)
kwargs = dict((get_key(key), get_node(value)) for key, value in kwargs.items())
func_arg_names = (
key[:key.find(':')]
for key in func.signature.split(';')
if key != '')
for _, arg_name in zip(args, func_arg_names):
if arg_name in kwargs:
raise TypeError(
f"{func.plugin.namespace}.{func.name}() "
f"got multiple values for argument \'{arg_name}\'")
# process
output = func(*args, **kwargs)
if isinstance(output, vs.VideoNode):
_ = _repr(output, default_prefix="clip") # register output
for recorder in Recorder._live_recorders:
if recorder.is_recording:
recorder.buffer.append(self._get_str(func, args, kwargs, output) + '\n')
return _VideoNode(output)
elif isinstance(output, list) and len(output) > 0 and isinstance(output[0], vs.VideoNode):
for item in output:
_ = _repr(item, default_prefix="clip") # register output
for recorder in Recorder._live_recorders:
if recorder.is_recording:
recorder.buffer.append(self._get_str(func, args, kwargs, output, check_output=False) + '\n')
return list(_VideoNode(item) for item in output)
else:
return output
return closure
else:
return attr
def __hash__(self):
return hash(self._plugin)
def __dir__(self):
return dir(self._plugin)
@staticmethod
def _get_str(func: vs.Function, args, kwargs, output, check_output=True):
output_str = ""
if check_output:
def diff_str(clip1: vs.VideoNode, clip2: vs.VideoNode):
"""Compare two clips and output a string of their difference"""
res = []
for attr in ["width", "height", "num_frames"]:
if getattr(clip1, attr) != getattr(clip2, attr):
res.append(f"{attr}: {getattr(clip1, attr)} -> {getattr(clip2, attr)}")
if clip1.format.name != clip2.format.name:
res.append(f"format: {clip1.format.name} -> {clip2.format.name}")
if clip1.fps != clip2.fps:
res.append(f"fps: {clip1.fps_num}/{clip1.fps_den} -> {clip2.fps_num}/{clip2.fps_den}")
return ', '.join(res)
if len(args) > 0 and isinstance(args[0], vs.VideoNode):
if diff_str(args[0], output) != "":
output_str += f"# {diff_str(args[0], output)}\n"
elif kwargs.get("clip", None):
if diff_str(kwargs["clip"], output) != "":
output_str += f"# {diff_str(kwargs['clip'], output)}\n"
else:
output_str += (f"# output: {output.width} x {output.height}, {output.format.name}, "
f"{output.num_frames} frames, {output.fps_num}/{output.fps_den} fps\n")
args_dict = inspect.signature(func).bind(*args, **kwargs).arguments
# replace clip in args_dict.values() with name of clip
call_args = ', '.join(f"{k}={_repr(v)}" for k, v in args_dict.items() if v is not None)
call_str = f"core.{func.plugin.namespace}.{func.name}({call_args})"
output_str += f"{_repr(output, default_prefix='clip')} = {call_str}\n"
return output_str
########################## Expr IR Start ##########################
class ExprIR(ABC):
""" AST-style expression """
@abstractmethod
def __eq__(self, other):
pass
@abstractmethod
def __repr__(self):
""" Infix and function call style """
pass
@abstractmethod
def __str__(self):
""" Postfix style """
pass
class DupN(ExprIR):
def __eq__(self, other):
return isinstance(other, DupN)
def __repr__(self):
return "DupN()"
def __str__(self):
return "dup"
dup = DupN()
class UnaryBaseOp(ExprIR):
@abstractstaticmethod
def cast(x):
pass
def __init__(self, x):
self.x = self.cast(x)
def __eq__(self, other):
return isinstance(other, type(self)) and self.x == other.x
def __repr__(self):
return f"{type(self).__name__}({self.x!r})"
def __str__(self):
return f"{self.x!s} {type(self).__name__.lower()}"
class ConstantN(UnaryBaseOp):
def __str__(self):
return f"{self.x!s}"
@staticmethod
def cast(x):
assert isinstance(x, numbers.Real)
return x
ConstantN_0 = ConstantN(0)
ConstantN_1 = ConstantN(1)
class VarN(UnaryBaseOp):
def __eq__(self, other):
return isinstance(other, VarN) and hash(self.x) == hash(other.x)
def __str__(self):
return f"{self.x!s}"
@staticmethod
def cast(x):
assert isinstance(x, _VideoNode)
return x
def Cast(x):
if isinstance(x, ExprIR):
return x
elif isinstance(x, numbers.Real):
return ConstantN(x)
elif isinstance(x, _VideoNode):
return VarN(x)
elif isinstance(x, vs.VideoNode):
return VarN(_VideoNode(x))
else:
raise TypeError(f"Unkonwn input ({type(x)})")
class UnaryOp(UnaryBaseOp):
@abstractstaticmethod
def compute(x):
pass
def __str__(self):
return f"{self.x!s} {self.op_name}"
@staticmethod
def cast(x):
return Cast(x)
class NotN(UnaryOp):
op_name = "not"
compute = op.not_
class AbsN(UnaryOp):
op_name = "abs"
compute = abs
class SqrtN(UnaryOp):
op_name = "sqrt"
compute = math.sqrt
class LogN(UnaryOp):
op_name = "log"
compute = math.log
class ExpN(UnaryOp):
op_name = "exp"
compute = math.exp
class BinaryOp(ExprIR):
@abstractstaticmethod
def compute(x, y):
pass
def __init__(self, x, y):
self.x, self.y = self.cast(x, y)
def __eq__(self, other):
return (
isinstance(other, type(self)) and
self.x == other.x and
self.y == other.y
)
def __repr__(self):
return f"{type(self).__name__}({self.x!r}, {self.y!r})"
def __str__(self):
return f"{self.x!s} {self.y!s} {self.op_name}"
@staticmethod
def cast(x, y):
return Cast(x), Cast(y)
class AddN(BinaryOp):
op_name = "+"
compute = op.add
class SubN(BinaryOp):
op_name = "-"
compute = op.sub
class MulN(BinaryOp):
op_name = "*"
compute = op.mul
class DivN(BinaryOp):
op_name = "/"
compute = op.truediv
class PowN(BinaryOp):
op_name = "pow"
compute = op.pow
class AndN(BinaryOp):
op_name = "and"
compute = op.and_
class OrN(BinaryOp):
op_name = "or"
compute = op.or_
class XorN(BinaryOp):
op_name = "xor"
compute = op.xor
class LtN(BinaryOp):
op_name = "<"
compute = op.lt
class LeN(BinaryOp):
op_name = "<="
compute = op.le
class EqN(BinaryOp):
op_name = "="
compute = op.eq
class NeN(BinaryOp):
op_name = "= not"
compute = op.ne
class GeN(BinaryOp):
op_name = ">="
compute = op.ge
class GtN(BinaryOp):
op_name = ">"
compute = op.gt
class MaxN(BinaryOp):
op_name = "max"
compute = max
class MinN(BinaryOp):
op_name = "min"
compute = min
class ConditionalN(ExprIR):
def __init__(self, x, y, z):
self.x, self.y, self.z = self.cast(x, y, z)
def __eq__(self, other):
return (
isinstance(other, ConditionalN) and
self.x == other.x and
self.y == other.y and
self.z == other.z
)
def __repr__(self):
return f"ConditionalN({self.x!r}, {self.y!r}, {self.z!r})"
def __str__(self):
return f"{self.x!s} {self.y!s} {self.z!s} ?"
@staticmethod
def cast(x, y, z):
return Cast(x), Cast(y), Cast(z)
@staticmethod
def compute(x, y, z):
return y if x else z
def _simplify(expr: ExprIR) -> ExprIR:
assert isinstance(expr, ExprIR)
while True:
prev_expr = expr
# early skipping
if isinstance(expr, (DupN, ConstantN, VarN)):
return expr
# constant foldings and universal eliminations
elif isinstance(expr, UnaryOp) and isinstance(expr.x, ConstantN):
# num op -> op(num)
return ConstantN(expr.compute(expr.x.x))
elif isinstance(expr, BinaryOp):
if isinstance(expr.x, ConstantN):
if isinstance(expr.y, ConstantN):
# num1 num2 op -> op(num1, num2)
return ConstantN(expr.compute(expr.x.x, expr.y.x))
elif expr.y == DupN:
# num dup op -> op(num, num)
return ConstantN(expr.compute(expr.x.x, expr.x.x))
elif expr.x == expr.y:
# x x op -> x dup op
expr = type(expr)(expr.x, dup)
# operator specific simplifications
if isinstance(expr, SqrtN):
x = _simplify(expr.x)
if isinstance(x, MulN) and isinstance(x.y, DupN):
# x dup * sqrt -> x abs
expr = AbsN(x.x)
else:
expr = SqrtN(x)
elif isinstance(expr, LogN):
x = _simplify(expr.x)
if isinstance(x, ExpN):
# x exp log -> x
expr = x.x
else:
expr = LogN(x)
elif isinstance(expr, ExpN):
x = _simplify(expr.x)
if isinstance(x, LogN):
# x log exp -> x
expr = x.x
else:
expr = ExpN(x)
elif isinstance(expr, AddN):
if expr.x == ConstantN_0:
# 0 x + -> x
expr = expr.y
elif expr.y == ConstantN_0:
# x 0 + -> x
expr = expr.x
elif isinstance(expr, SubN):
if isinstance(expr.y, DupN):
# x dup - -> 0
return ConstantN_0
elif expr.y == ConstantN_0:
# x 0 - -> x
expr = expr.x
elif isinstance(expr, MulN):
if expr.x == ConstantN_1:
# 1 x * -> x
expr = expr.y
elif expr.y == ConstantN_1:
# x 1 * -> x
expr = expr.x
elif isinstance(expr, DivN):
if isinstance(expr.y, DupN):
# x dup / -> 1
return ConstantN_1
elif expr.y == ConstantN_1:
# x 1 / -> x
expr = expr.x
elif isinstance(expr, PowN):
if isinstance(expr.x, ConstantN):
if expr.x == ConstantN_0:
# 0 x pow -> 0
expr = ConstantN_0
elif expr.x == ConstantN_1:
# 1 x pow -> 1
expr = ConstantN_1
elif expr.x == ConstantN(math.e):
# math.e x pow -> x exp
expr = ExpN(expr.y)
elif isinstance(expr.y, ConstantN):
if expr.y == ConstantN_0:
# x 0 pow -> 1
expr = ConstantN_1
elif expr.y == ConstantN_1:
# x 1 pow -> x
expr = expr.x
elif expr.y == ConstantN(2):
# x 2 pow -> x dup *
expr = MulN(expr.x, dup)
elif expr.y == ConstantN(0.5):
# x 0.5 pow -> x sqrt
expr = SqrtN(expr.x)
elif expr.y == ConstantN(-0.5):
# x -0.5 pow -> x dup sqrt /
expr = DivN(expr.x, SqrtN(dup))
elif isinstance(expr, (MaxN, MinN)) and isinstance(expr.y, DupN):
# x dup {max/min} -> x
expr = expr.x
elif isinstance(expr, ConditionalN):
if isinstance(expr.x, ConstantN):
# num x y ? -> (num ? x : y)
expr = ConditionalN.compute(expr.x, expr.y, expr.z)
elif expr.y == expr.z:
# _ x x ? -> x
expr = expr.y
# non-local simplification of binary operations
if isinstance(expr, BinaryOp):
expr = type(expr)(_simplify(expr.x), _simplify(expr.y))
if isinstance(expr.x, ConstantN):
if isinstance(expr.y, UnaryOp):
if isinstance(expr.y.x, DupN):
# num dup op1 op2 -> num num op1 op2
expr = type(expr)(expr.x, type(expr.y)(expr.x))
elif isinstance(expr.y, BinaryOp):
if isinstance(expr.y.x, DupN):
# num dup x op1 op2 -> num num1 x op1 op2
expr = type(expr)(expr.x, type(expr.y)(expr.x, expr.y.y))
elif isinstance(expr.y, BinaryOp) and expr.x == expr.y.x:
# x x y op1 op2 -> x dup y op1 op2
expr = type(expr)(expr.x, type(expr.y)(dup, expr.y.y))
if expr == prev_expr:
# no progress
return expr
else:
prev_expr = expr
# continue
def postfix(expr: ExprIR, namer: Optional[Callable[[VarN], str]] = None) -> str:
assert isinstance(expr, ExprIR)
if isinstance(expr, ConstantN):
return str(expr)
elif isinstance(expr, VarN):
if namer is None:
return str(expr)
else:
return namer(expr)
elif isinstance(expr, DupN):
return "dup"
elif isinstance(expr, UnaryOp):
return f"{postfix(expr.x, namer)} {expr.op_name}"
elif isinstance(expr, BinaryOp):
first = postfix(expr.x, namer)
return f"{first} {postfix(expr.y, namer)} {expr.op_name}"
elif isinstance(expr, ConditionalN):
first = postfix(expr.x, namer)
second = postfix(expr.y, namer)
return f"{first} {second} {postfix(expr.z, namer)} ?"
else:
raise TypeError(f"Unknwon type {type(expr)}")
def infix(expr: ExprIR, namer: Optional[Callable[[VarN], str]] = None,
top: Optional[str] = None
) -> str:
assert isinstance(expr, ExprIR)
if isinstance(expr, ConstantN):
return str(expr)
elif isinstance(expr, VarN):
if namer is None:
return str(expr)
else:
return namer(expr)
elif isinstance(expr, DupN):
if top:
return top
else:
raise ValueError("Empty dup node")
elif isinstance(expr, UnaryOp):
return f"{expr.op_name}({infix(expr.x, namer, top=top)})"
elif isinstance(expr, BinaryOp):
first = infix(expr.x, namer, top=top)
return f"({first} {expr.op_name} {infix(expr.y, namer, top=first)})"
elif isinstance(expr, ConditionalN):
first = infix(expr.x, namer, top=top)
second = infix(expr.y, namer, top=first)
return f"({second} if {first} else {infix(expr.z, namer, top=second)})"
else:
raise TypeError(f"Unknwon type {type(expr)}")
########################## Expr IR End ##########################
def namer_factory():
alphabet = "xyzabcdefghijklmnopqrstuvw"
mapping = OrderedDict() # type: MutableMapping[_VideoNode, str]
def namer(obj: VarN) -> str:
x = obj.x
if x in mapping or len(mapping) < len(alphabet):
return mapping.setdefault(x, f"{alphabet[len(mapping)]}")
else:
raise RuntimeError(f"{type(self).__name__!r}: Too many nodes")
return namer
class _Fake_VideoNode:
""" Fake VideoNode used to bypass instance check in other scripts """
pass
class _ArithmeticExpr(_Fake_VideoNode):
def __init__(self, obj):
self._expr = Cast(obj) # type: ExprIR
self._cached_clip = None # type: Optional[_VideoNode]
def __getattr__(self, name):
if hasattr(_vscore, name) or hasattr(self.clips[0], name):
return getattr(self.compute(), name)
else:
raise AttributeError(f"{type(self).__name__!r} object has no attribute {name!r}")
def __bool__(self):
raise RuntimeError("Impossible")
def __hash__(self):
return hash(self.clips + (self.expr,))
def __str__(self):
def namer(x: VarN):
return _repr(x.x)
return infix(self._expr, namer=namer).strip("()")
@property
def clips(self):
from collections import OrderedDict
clips_dict = OrderedDict()
exprs = [self._expr]
while exprs:
expr = exprs.pop()
if isinstance(expr, VarN):
clips_dict.setdefault(expr.x, None)
elif isinstance(expr, UnaryOp):
exprs.append(expr.x)
elif isinstance(expr, BinaryOp):
exprs.extend([expr.y, expr.x])
elif isinstance(expr, ConditionalN):
exprs.extend([expr.z, expr.y, expr.x])
return tuple(clips_dict.keys())
def get_expr(self, namer) -> str:
return postfix(self._expr, namer=namer)
@property
def expr(self) -> str:
return self.get_expr(namer=namer_factory())
@property
def lut_func(self) -> Callable[..., numbers.Integral]:
clips = self.clips
assert len(clips) in [1, 2]
func_impl = infix(self._expr, namer=namer_factory())
func_impl = f"min(max(int({func_impl} + 0.5), 0), {(2 ** clips[0].format.bits_per_sample) - 1})" # clamp
if len(clips) == 1:
lut_str = f"lambda x: {func_impl}"
else: # len(clips) == 2
lut_str = f"lambda x, y: {func_impl}"
class _LambdaFunction:
def __init__(self, func_str: str):
self.func = eval(func_str, {"exp": math.exp, "log": math.log, "sqrt": math.sqrt})
self.func_str = func_str
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
def __repr__(self):
return self.func_str
return _LambdaFunction(lut_str)
def compute(self, planes=None, bits=None, use_lut=None,
simplify: Union[bool, Callable[[ExprIR], ExprIR]] = True
) -> '_VideoNode':
if arithmetic_expr:
cacheable = planes is None and bits is None and use_lut is None
if cacheable and self._cached_clip is not None:
return self._cached_clip
if simplify:
if callable(simplify):
self._expr = simplify(self._expr)
else:
self._expr = _simplify(self._expr)
if len(self.clips) == 0:
raise ValueError("ArithmeticExpr becomes empty")
if self.expr in ['', 'x']: # empty expr
return _VideoNode(self.clips[0]._node)
else:
clips = self.clips
if len(clips) > 26:
raise RuntimeError("Too many clips.")
if bits is None:
not_equal_bits = (
lambda clip1, clip2:
clip1.format.bits_per_sample != clip2.format.bits_per_sample)
if len(clips) >= 2 and any(not_equal_bits(clips[0], clip) for clip in clips[1:]):
raise ValueError('"bits" must be specified.')
else:
bits = clips[0].format.bits_per_sample
is_int = lambda clip: clip.format.sample_type == vs.INTEGER
get_bits = lambda clip: clip.format.bits_per_sample
lut_available = (
lambda clips:
len(clips) <= 2 and all(map(is_int, clips)) and sum(map(get_bits, clips)) <= 20)
if use_lut is None:
use_lut = lut_available(clips) and len(self.expr.split()) >= 15
elif use_lut and not lut_available(clips):
raise ValueError("Lut computation is not available")
# process
if use_lut: # std.Lut() / std.Lut2()
if len(clips) == 1:
res = core.std.Lut(clips[0], planes=planes, bits=bits, function=self.lut_func)
else: # len(clips) == 2
res = core.std.Lut2(
clips[0], clips[1], planes=planes, bits=bits, function=self.lut_func)
else: # std.Expr()
if planes is None:
expr = self.expr
else:
if isinstance(planes, int):
planes = [planes]
expr = [
(self.expr if i in planes else "")
for i in range(clips[0].format.num_planes)]
in_format = clips[0].format
if bits == in_format.bits_per_sample:
out_format = None
else:
query_video_format = core.query_video_format if _is_api4 else core.register_format
out_format = query_video_format(
color_family=in_format.color_family,
sample_type=vs.INTEGER if bits <= 16 else vs.FLOAT,
bits_per_sample=bits,
subsampling_w=in_format.subsampling_w,
subsampling_h=in_format.subsampling_h
)
res = core.std.Expr(clips=clips, expr=expr, format=out_format)
if cacheable:
self._cached_clip = res
return res
else:
raise RuntimeError("Arithmetic expression is disabled.")
# Arithmetic methods
def _operate(self,
op: Union[UnaryOp, BinaryOp, ConditionalN],
*operands: Sequence[Union[numbers.Real, vs.VideoNode, "_VideoNode", ExprIR]]
) -> "_ArithmeticExpr":
unwrap = lambda x: x._expr if isinstance(x, type(self)) else x
result = op(*map(unwrap, operands))
return type(self)(result)
# unary operations
def __neg__(self):
return self._operate(SubN, 0, self)
def __pos__(self):
return self
def __abs__(self):
return self._operate(AbsN, self)
def __invert__(self):
return self._operate(NotN, self)
def __exp__(self):
return self._operate(ExpN, self)
def __log__(self):
return self._operate(LogN, self)
def __sqrt__(self):
return self._operate(SqrtN, self)
# binary operations
def __lt__(self, other):
return self._operate(LtN, self, other)
def __le__(self, other):
return self._operate(LeN, self, other)
def __eq__(self, other):
return self._operate(EqN, self, other)
def __ne__(self, other):
return self._operate(NeN, self, other)
def __gt__(self, other):
return self._operate(GtN, self, other)
def __ge__(self, other):
return self._operate(GeN, self, other)
def __add__(self, other):
return self._operate(AddN, self, other)