Repository navigation
Expand file tree
/
Copy pathbench.py
More file actions
185 lines (173 loc) · 6.92 KB
/
Copy pathbench.py
File metadata and controls
185 lines (173 loc) · 6.92 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
import argparse
import ctypes
import os
import subprocess
import sys
import time
from pathlib import Path
REFINE = "refine:[{thsad:250,search:{distance:-1,satd:true}}]"
MEDIUM = "main:{search:{distance:0,coarse:{distance:-1,trymany:true,bad:{sad:2000}}}}"
FASTER = "main:{search:{coarse:{distance:-1,trymany:true,bad:{sad:2000}}}}"
PRESETS = {
("faster", "cpu"): ("{pel:1,gpu:0}", f"{{block:{{w:16,overlap:2}},{FASTER}}}"),
("faster", "gpu"): ("{pel:1,gpu:1}", f"{{block:{{w:16,overlap:1}},{FASTER}}}"),
("medium", "cpu"): ("{gpu:0}", f"{{block:{{w:16,overlap:2}},{MEDIUM},{REFINE}}}"),
("medium", "gpu"): ("{gpu:1}", f"{{block:{{w:8,overlap:2}},{MEDIUM},{REFINE}}}"),
}
SMOOTH = (
"{{rate:{{num:{fps},den:1,abs:true}},algo:23,mask:{{cover:80,area:0,area_sharp:1.2}},"
"scene:{{blend:true,mode:0,limits:{{blocks:50}}}}}}"
)
def decode(core, vs, path, count):
probe = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-of", "csv=p=0"]
probe += ["-show_entries", "stream=width,height,r_frame_rate", path]
width, height, rate = (
subprocess.check_output(probe, text=True).strip().split(",")[:3]
)
width, height = int(width), int(height)
num, den = map(int, rate.split("/"))
pipe = ["ffmpeg", "-v", "error", "-i", path, "-frames:v", str(count)]
pipe += ["-pix_fmt", "yuv420p", "-f", "rawvideo", "-"]
data = subprocess.check_output(pipe)
size = width * height * 3 // 2
blank = core.std.BlankClip(
format=vs.YUV420P8,
width=width,
height=height,
fpsnum=num,
fpsden=den,
length=len(data) // size,
keep=True,
)
frames = []
for index in range(blank.num_frames):
frame, offset = blank.get_frame(0).copy(), index * size
for plane in range(3):
w, h = (width, height) if plane == 0 else (width // 2, height // 2)
target, stride = frame.get_write_ptr(plane).value, frame.get_stride(plane)
for row in range(h):
ctypes.memmove(target + row * stride, data[offset : offset + w], w)
offset += w
frames.append(frame)
return core.std.ModifyFrame(blank, blank, lambda n, f: frames[n])
def with_fast(options, level):
if not level:
return options
return f"{options[:-1]},fast:{level}}}"
def measure(args):
import vapoursynth as vs
core = vs.core
for name in ("svpflow1", "svpflow2"):
libraries = Path(args.plugins[0]).glob(f"*{name}*")
core.std.LoadPlugin(
next(str(p) for p in libraries if p.suffix in (".so", ".dll", ".dylib"))
)
clip = decode(core, vs, args.clip, args.frames) * args.loops
preset, path = args.run
super_options, analyse_options = PRESETS[preset, path]
super_options = with_fast(super_options, args.fast_level)
analyse_options = with_fast(analyse_options, args.fast_level)
smooth_options = with_fast(SMOOTH.format(fps=args.fps), args.fast_level)
sup = core.svp1.Super(clip, super_options)
vectors = core.svp1.Analyse(sup["clip"], sup["data"], clip, analyse_options)
out = core.svp2.SmoothFps(
clip,
sup["clip"],
sup["data"],
vectors["clip"],
vectors["data"],
smooth_options,
)
begin = mark = now = time.perf_counter()
count = 0
for _ in out.frames(prefetch=args.prefetch, close=True):
now = time.perf_counter()
if now - begin < args.warmup:
mark = now
continue
count += 1
if now - mark >= args.seconds:
break
print(count / (now - mark), flush=True)
os._exit(0)
def main():
parser = argparse.ArgumentParser(
description="Compare the speed of SVPflow builds on one clip."
)
parser.add_argument("clip", help="8-bit 4:2:0 video, read with ffmpeg")
parser.add_argument(
"plugins", nargs="+", help="directory with svpflow1 and svpflow2"
)
parser.add_argument("--fps", type=int, default=1920, help="output frame rate")
parser.add_argument("--frames", type=int, default=200, help="source frames to use")
parser.add_argument("--loops", type=int, default=200, help="times to repeat them")
parser.add_argument("--seconds", type=float, default=10, help="timed part of a run")
parser.add_argument(
"--warmup", type=float, default=3, help="untimed start of a run"
)
parser.add_argument("--rounds", type=int, default=3)
parser.add_argument(
"--prefetch", type=int, default=48, help="frames requested at once"
)
parser.add_argument(
"--fast",
type=int,
nargs="*",
default=[],
help="fast levels to run on the last plugin directory",
)
parser.add_argument("--fast-level", type=int, default=0, help=argparse.SUPPRESS)
parser.add_argument("--run", nargs=2, help=argparse.SUPPRESS)
args = parser.parse_args()
if args.run:
return measure(args)
passed = [
f"--{name}={getattr(args, name)}"
for name in ("fps", "frames", "loops", "seconds", "warmup", "prefetch")
]
targets = [(plugins, 0) for plugins in args.plugins] + [
(args.plugins[-1], level) for level in args.fast
]
best = {}
for _ in range(args.rounds):
for run in PRESETS:
for plugins, level in targets:
command = [
sys.executable,
__file__,
args.clip,
plugins,
"--run",
*run,
f"--fast-level={level}",
*passed,
]
fps = float(subprocess.check_output(command, text=True).split()[-1])
best[run, plugins, level] = max(best.get((run, plugins, level), 0), fps)
tag = f"fast:{level}" if level else Path(plugins).resolve().name
print(*run, tag, f"{fps:.1f}", file=sys.stderr)
names = [
f"fast:{level}" if level else Path(plugins).resolve().name
for plugins, level in targets
]
if args.fast:
print("| Preset | Path |", " | ".join(names), "|")
print("| --- | --- |", " | ".join("---:" for _ in names), "|")
for run in PRESETS:
speeds = [best[run, plugins, level] for plugins, level in targets]
base = speeds[0]
cells = [f"{base:.1f} fps"] + [
f"{fps:.1f} fps ({fps / base:.2f}x)" for fps in speeds[1:]
]
print(f"| {run[0]} | {run[1].upper()} |", " | ".join(cells), "|")
else:
print("| Preset | Path |", " | ".join(names), "| Speed |")
print("| --- | --- |", " | ".join("---:" for _ in names), "| ---: |")
for run in PRESETS:
speeds = [best[run, plugins, level] for plugins, level in targets]
cells = " | ".join(f"{fps:.1f} fps" for fps in speeds)
print(
f"| {run[0]} | {run[1].upper()} | {cells} | {speeds[-1] / speeds[0]:.2f}x |"
)
if __name__ == "__main__":
main()