-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_makefile.py
executable file
·118 lines (87 loc) · 4.9 KB
/
gen_makefile.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
#!/usr/bin/env python3
import sys
import math
VPXENC = 'vpxenc --ivf --codec=vp8 --good --cpu-used=0 --end-usage=cq --min-q=0 --max-q=63 --cq-level={quality} --buf-initial-sz=10000 --buf-optimal-sz=20000 --buf-sz=40000 --undershoot-pct=100 --passes=2 --auto-alt-ref=1 --threads=1 --token-parts=0 --tune=ssim --target-bitrate=4294967295 -o {output}.ivf {input}.y4m'
TERMINATE_CHUNK = "xc-terminate-chunk {input}.ivf {output}.ivf"
XC_DUMP_0 = 'xc-dump {input}.ivf {output}.state'
XC_ENC_FIRST_FRAME = 'xc-enc -W -w 0.75 -i y4m -o {output}.ivf -r -I {source_state}.state -p {input_pred}.ivf {extra} {input}.y4m'
XC_ENC_REBASE = 'xc-enc -W -w 0.75 -i y4m -o {output}.ivf -r -I {source_state}.state -p {input_pred}.ivf -S {pred_state}.state {extra} {input}.y4m'
def bname(i):
return "{:08d}".format(i)
def make_command(cmd):
return "\t{}".format(cmd)
def generate_batch(start, end, quality):
print("### batch %d: [%s, %s] ###\n" % (batch_index, batch_start, batch_end))
print("# stage 1: vpxenc\n")
for i in range(start, end + 1):
name = bname(i)
print("## {}\n".format(name))
print("{i}-vpxenc.ivf: {i}.y4m".format(i=name))
print(make_command(VPXENC.format(quality=quality, input=name, output="%s-vpxenc" % name)))
print()
if i == start:
print("{i}.ivf: {i}-vpxenc.ivf".format(i=name))
print(make_command(TERMINATE_CHUNK.format(input="%s-vpxenc" % name,
output="%s" % name)))
else:
print("{i}-0.ivf: {i}-vpxenc.ivf".format(i=name))
print(make_command(TERMINATE_CHUNK.format(input="%s-vpxenc" % name,
output="%s-0" % name)))
print()
if i == start:
print("{i}-0.state: {i}.ivf".format(i=name))
print(make_command(XC_DUMP_0.format(input=name, output="%s-0" % name)))
else:
print("{i}-0.state: {i}-0.ivf".format(i=name))
print(make_command(XC_DUMP_0.format(input="%s-0" % name, output="%s-0" % name)))
print()
print("# stage 2: reencode-first-frame\n")
for i in range(start + 1, end + 1):
name = bname(i)
prev_name = bname(i - 1)
print("## {}\n".format(name))
base_command = ''
if i == (start + 1):
print("{i}-1.state {i}.ivf: {i}.y4m {i}-0.ivf {j}-0.state".format(i=name, j=prev_name))
base_command = XC_ENC_FIRST_FRAME.format(input=name, output=name,
source_state="%s-0" % prev_name,
extra="-O %s-1.state" % name,
input_pred="%s-0" % name)
else:
print("{i}-1.ivf: {i}.y4m {i}-0.ivf {j}-0.state".format(i=name, j=prev_name))
base_command = XC_ENC_FIRST_FRAME.format(input=name, output="%s-1" % name,
source_state="%s-0" % prev_name,
extra='',
input_pred="%s-0" % name)
print(make_command(base_command))
print("# stage 3: rebase\n")
for i in range(start + 2, end + 1):
name = bname(i)
prev_name = bname(i - 1)
print("## {}\n".format(name))
if i != end:
print("{i}-1.state {i}.ivf: {i}.y4m {i}-1.ivf {j}-0.state {j}-1.state".format(i=name, j=prev_name))
else:
print("{i}.ivf: {i}.y4m {i}-1.ivf {j}-0.state {j}-1.state".format(i=name, j=prev_name))
base_command = XC_ENC_REBASE.format(output=name, input=name,
source_state="%s-1" % prev_name,
extra=("-O %s-1.state" % name) if i != end else '',
input_pred="%s-1" % name, pred_state="%s-0" % prev_name)
print(make_command(base_command))
if __name__ == '__main__':
if len(sys.argv) != 5:
print("Usage: gen_makefile.py <start> <end> <batch-size> <cq-level>", file=sys.stderr)
sys.exit(1)
start = int(sys.argv[1])
end = int(sys.argv[2])
batch_size = int(sys.argv[3])
quality = int(sys.argv[4])
print("all: {}\n".format(" ".join(["%s.ivf" % bname(i) for i in range(start, end + 1)])))
print("clean:")
print(make_command("rm -rf *.ivf *.state"))
print()
batch_start = start
for batch_index in range(math.ceil((end - start + 1) / batch_size)):
batch_end = min(batch_start + batch_size - 1, end)
generate_batch(batch_start, batch_end, quality)
batch_start = batch_end + 1