forked from xiph/rd_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.py
269 lines (254 loc) · 10.1 KB
/
work.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
from utility import *
import subprocess
import sys
# Finding files such as `this_(that)` requires `'` be placed on both
# sides of the quote so the `()` are both captured. Files such as
# `du_Parterre_d'Eau` must be converted into
#`'du_Parterre_d'"'"'Eau'
# ^^^ Required to make sure the `'` is captured.
def shellquote(s):
return "'" + s.replace("'", "'\"'\"'") + "'"
#set up Codec:QualityRange dictionary
quality_presets = {
"daala": [7,11,16,25,37],
"x264": list(range(1,52,5)),
"x265": list(range(5,52,5)),
"x265-rt": list(range(5,52,5)),
"vp8": list(range(12,64,5)),
"vp9": [20,32,43,55,63],
"vp9-rt": [20,32,43,55,63],
"vp10": [8,20,32,43,55,63],
"vp10-rt": [8,20,32,43,55,63],
"av1": [20,32,43,55,63],
"av1-rt": [20,32,43,55,63],
"thor": list(range(7,43,3)),
"thor-rt": list(range(7,43,3))
}
class Run:
def __init__(self, codec):
self.info = {}
self.codec = codec
self.quality = quality_presets[codec]
self.runid = get_time()
self.extra_options = ''
self.save_encode = False
self.work_items = []
self.prefix = './'
self.log = None
self.rundir = None
self.status = 'running'
self.work_items = []
def write_status(self):
f = open(self.rundir+'/status.txt','w')
f.write(self.status)
f.close()
def cancel(self):
self.status = 'cancelled'
self.write_status()
for work in self.work_items:
work.cancel()
def finish(self):
if self.log:
self.log.close()
class RDRun(Run):
def reduce(self):
rd_print(self.log,'Logging results...')
self.work_items.sort(key=lambda work: int(work.quality))
any_work_failed = False
for work in self.work_items:
if not work.failed:
f = open((self.prefix+'/'+work.filename+'-daala.out').encode('utf-8'),'a')
f.write(str(work.quality)+' ')
f.write(str(work.pixels)+' ')
f.write(str(work.size)+' ')
f.write(str(work.metric['psnr'][0])+' ')
f.write(str(work.metric['psnrhvs'][0])+' ')
f.write(str(work.metric['ssim'][0])+' ')
f.write(str(work.metric['fastssim'][0])+' ')
f.write(str(work.metric['ciede2000'])+' ')
f.write(str(work.metric['psnr'][1])+' ')
f.write(str(work.metric['psnr'][2])+' ')
f.write(str(work.metric['apsnr'][0])+' ')
f.write(str(work.metric['apsnr'][1])+' ')
f.write(str(work.metric['apsnr'][2])+' ')
f.write(str(work.metric['msssim'][0])+' ')
f.write(str(work.metric['encodetime'])+' ')
f.write(str(work.metric['vmaf'])+' ')
f.write(str(work.metric['decodetime'])+' ')
f.write('\n')
f.close()
else:
any_work_failed = True
subprocess.call('OUTPUT="'+self.prefix+'/'+'total" "'+sys.path[0]+'/rd_average.sh" "'+self.prefix+'/*.out"',
shell=True)
if any_work_failed:
self.status = 'failed'
else:
self.status = 'completed'
self.write_status()
class ABRun(Run):
def reduce(self):
pass
class Work:
def __init__(self):
self.log = None
self.retries = 0
self.done = False
self.failed = False
self.runid = ''
self.slot = None
def cancel(self):
self.failed = True
self.done = True
class RDWork(Work):
def __init__(self):
super().__init__()
self.no_delete = False
self.copy_back_files = ['-stdout.txt']
def parse(self, stdout, stderr):
self.raw = stdout
split = None
try:
split = self.raw.decode('utf-8').replace(')',' ').split()
self.pixels = split[1]
self.size = split[2]
self.metric = {}
self.metric['psnr'] = {}
self.metric["psnr"][0] = split[6]
self.metric["psnr"][1] = split[8]
self.metric["psnr"][2] = split[10]
self.metric['psnrhvs'] = {}
self.metric["psnrhvs"][0] = split[14]
self.metric["psnrhvs"][1] = split[16]
self.metric["psnrhvs"][2] = split[18]
self.metric['ssim'] = {}
self.metric["ssim"][0] = split[22]
self.metric["ssim"][1] = split[24]
self.metric["ssim"][2] = split[26]
self.metric['fastssim'] = {}
self.metric["fastssim"][0] = split[30]
self.metric["fastssim"][1] = split[32]
self.metric["fastssim"][2] = split[34]
self.metric['ciede2000'] = split[36]
self.metric['apsnr'] = {}
self.metric['apsnr'][0] = split[40]
self.metric['apsnr'][1] = split[42]
self.metric['apsnr'][2] = split[44]
self.metric['msssim'] = {}
self.metric['msssim'][0] = split[48]
self.metric['msssim'][1] = split[50]
self.metric['msssim'][2] = split[52]
self.metric['encodetime'] = split[53]
self.metric['vmaf'] = split[57]
self.metric['decodetime'] = split[58]
self.failed = False
except IndexError:
rd_print(self.log,'Decoding result for '+self.filename+' at quality '+str(self.quality)+' failed!')
rd_print(self.log,'stdout:')
rd_print(self.log,stdout.decode('utf-8'))
rd_print(self.log,'stderr:')
rd_print(self.log,stderr.decode('utf-8'))
self.failed = True
def execute(self, slot):
try:
self.slot = slot
slot.setup(self.codec,self.bindir)
work = self
input_path = slot.machine.media_path+'/'+work.set+'/'+work.filename
command = 'WORK_ROOT="'+slot.work_root+'" '
command += 'DAALATOOL_ROOT="'+slot.machine.work_root+'/daalatool" '
command += ' x="'+str(work.quality) + '" '
command += 'CODEC="'+work.codec+'" '
command += 'EXTRA_OPTIONS="'+work.extra_options + '" '
if self.no_delete:
command += 'NO_DELETE=1 '
command += slot.work_root + '/rd_tool/metrics_gather.sh '+shellquote(input_path)
slot.start_shell(command)
(stdout, stderr) = slot.gather()
for file in self.copy_back_files:
if slot.get_file(slot.work_root+'/'+work.filename+'-'+str(work.quality)+file,'../runs/'+work.runid+'/'+work.set+'/') != 0:
rd_print(self.log,'Failed to copy back '+work.filename+'-'+str(work.quality)+file+', continuing anyway')
self.parse(stdout, stderr)
self.slot = None
except Exception as e:
rd_print(self.log, 'Exception while running',self.get_name(),e)
self.failed = True
def get_name(self):
return self.filename + ' with quality ' + str(self.quality) + ' for run ' + self.runid
def cancel(self):
if self.done:
rd_print(self.log, 'Tried to cancel work item that was already complete')
else:
self.slot.kill()
self.failed = True
self.done = True
def create_rdwork(run, video_filenames):
work_items = []
for filename in video_filenames:
for q in sorted(run.quality, reverse = True):
work = RDWork()
work.log = run.log
work.quality = q
work.runid = run.runid
work.codec = run.codec
work.bindir = run.bindir
work.set = run.set
work.filename = filename
work.extra_options = run.extra_options
if run.save_encode:
work.no_delete = True
if work.codec == 'av1' or work.codec == 'av1-rt':
work.copy_back_files.append('.ivf')
work_items.append(work)
return work_items
class ABWork(Work):
def __init__(self):
super().__init__()
self.failed = False
def execute(self, slot):
work = self
input_path = slot.machine.media_path +'/' + work.set + '/' + work.filename
try:
slot.setup(self.codec,self.bindir)
slot.start_shell(slot.work_root+'/rd_tool/ab_meta_compare.sh ' + shellquote(str(self.bpp)) + ' ' + shellquote(self.runid) + ' ' + work.set + ' ' + shellquote(input_path) + ' ' + shellquote(self.codec))
(stdout, stderr) = slot.gather()
# filename with extension
if 'video' in work.set:
filename = input_path.split('/')[-1].rsplit('.', 1)[0] + '.ogv'
else:
filename = input_path.split('/')[-1].rsplit('.', 1)[0] + '.png'
middle = self.runid + '/' + work.set + '/bpp_' + str(self.bpp)
remote_file = slot.work_root+'/runs/' + middle + '/' + shellquote(filename)
local_folder = '../runs/' + middle
local_file = '../runs/' + middle + '/' + filename
subprocess.Popen(['mkdir', '--parents', local_folder])
slot.get_file(remote_file, local_file)
self.failed = False
except IndexError:
rd_print(self.log,'Encoding and copying', filename, 'at bpp', str(self.bpp), 'failed')
rd_print(self.log, 'stdout:')
rd_print(self.log, stdout.decode('utf-8'))
rd_print(self.log, 'stderr:')
rd_print(self.log, stderr.decode('utf-8'))
self.failed = True
def cancel(self):
self.failed = True
self.done = True
def get_name(self):
return self.filename + ' with bpp ' + str(self.bpp)
def create_abwork(run, video_filenames):
work_items = []
bits_per_pixel = [x/10.0 for x in range(1, 11)]
for filename in video_filenames:
for bpp in bits_per_pixel:
work = ABWork()
work.log = run.log
work.bpp = bpp
work.codec = run.codec
work.bindir = run.bindir
work.runid = run.runid
work.set = run.set
work.filename = filename
work.extra_options = run.extra_options
work_items.append(work)
return work_items