forked from Netflix/vmaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
394 lines (353 loc) · 10.5 KB
/
main.cpp
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
/**
*
* Copyright 2016-2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <cstdio>
#include <stdlib.h>
#include <stdexcept>
#include <exception>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdint>
#include <sys/stat.h>
#include "libvmaf.h"
extern "C" {
#include "common/frame.h"
}
#define read_image_b read_image_b2s
#define read_image_w read_image_w2s
char* getCmdOption(char ** begin, char ** end, const std::string & option)
{
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return std::find(begin, end, option) != end;
}
void print_usage(int argc, char *argv[])
{
fprintf(stderr, "Usage: %s fmt width height ref_path dis_path model_path [--log log_path] [--log-fmt log_fmt] [--thread n_thread] [--subsample n_subsample] [--disable-clip] [--disable-avx] [--psnr] [--ssim] [--ms-ssim] [--phone-model] [--ci]\n", argv[0]);
fprintf(stderr, "fmt:\n\tyuv420p\n\tyuv422p\n\tyuv444p\n\tyuv420p10le\n\tyuv422p10le\n\tyuv444p10le\n\n");
fprintf(stderr, "log_fmt:\n\txml (default)\n\tjson\n\tcsv\n\n");
fprintf(stderr, "n_thread:\n\tmaximum threads to use (default 0 - use all threads)\n\n");
fprintf(stderr, "n_subsample:\n\tn indicates computing on one of every n frames (default 1)\n\n");
}
#if MEM_LEAK_TEST_ENABLE
/*
* Measures the current (and peak) resident and virtual memories
* usage of your linux C process, in kB
*/
void getMemory(int itr_ctr, int state)
{
int currRealMem;
int peakRealMem;
int currVirtMem;
int peakVirtMem;
char state_str[10]="";
// stores each word in status file
char buffer[1024] = "";
if(state ==1)
strcpy(state_str,"start");
else
strcpy(state_str,"end");
// linux file contains this-process info
FILE* file = fopen("/proc/self/status", "r");
// read the entire file
while (fscanf(file, " %1023s", buffer) == 1)
{
if (strcmp(buffer, "VmRSS:") == 0)
{
fscanf(file, " %d", &currRealMem);
}
if (strcmp(buffer, "VmHWM:") == 0)
{
fscanf(file, " %d", &peakRealMem);
}
if (strcmp(buffer, "VmSize:") == 0)
{
fscanf(file, " %d", &currVirtMem);
}
if (strcmp(buffer, "VmPeak:") == 0)
{
fscanf(file, " %d", &peakVirtMem);
}
}
fclose(file);
printf("Iteration %d at %s of process: currRealMem: %6d, peakRealMem: %6d, currVirtMem: %6d, peakVirtMem: %6d\n",itr_ctr, state_str, currRealMem, peakRealMem, currVirtMem, peakVirtMem);
}
#endif
int run_wrapper(char *fmt, int width, int height, char *ref_path, char *dis_path, char *model_path,
char *log_path, char *log_fmt, bool disable_clip, bool disable_avx, bool enable_transform, bool phone_model,
bool do_psnr, bool do_ssim, bool do_ms_ssim, char *pool_method, int n_thread, int n_subsample, bool enable_conf_interval)
{
double score;
int ret = 0;
struct data *s;
s = (struct data *)malloc(sizeof(struct data));
s->format = fmt;
s->width = width;
s->height = height;
s->ref_rfile = NULL;
s->dis_rfile = NULL;
if (!strcmp(fmt, "yuv420p") || !strcmp(fmt, "yuv420p10le"))
{
if ((width * height) % 2 != 0)
{
fprintf(stderr, "(width * height) %% 2 != 0, width = %d, height = %d.\n", width, height);
ret = 1;
goto fail_or_end;
}
s->offset = width * height / 2;
}
else if (!strcmp(fmt, "yuv422p") || !strcmp(fmt, "yuv422p10le"))
{
s->offset = width * height;
}
else if (!strcmp(fmt, "yuv444p") || !strcmp(fmt, "yuv444p10le"))
{
s->offset = width * height * 2;
}
else
{
fprintf(stderr, "unknown format %s.\n", fmt);
ret = 1;
goto fail_or_end;
}
if (!(s->ref_rfile = fopen(ref_path, "rb")))
{
fprintf(stderr, "fopen ref_path %s failed.\n", ref_path);
ret = 1;
goto fail_or_end;
}
if (!(s->dis_rfile = fopen(dis_path, "rb")))
{
fprintf(stderr, "fopen dis_path %s failed.\n", dis_path);
ret = 1;
goto fail_or_end;
}
if (strcmp(ref_path, "-"))
{
#ifdef _WIN32
struct _stat64 ref_stat;
if (!_stat64(ref_path, &ref_stat))
#else
struct stat64 ref_stat;
if (!stat64(ref_path, &ref_stat))
#endif
{
size_t frame_size = width * height + s->offset;
if (!strcmp(fmt, "yuv420p10le") || !strcmp(fmt, "yuv422p10le") || !strcmp(fmt, "yuv444p10le"))
{
frame_size *= 2;
}
s->num_frames = ref_stat.st_size / frame_size;
}
else
{
s->num_frames = -1;
}
}
else
{
s->num_frames = -1;
}
/* Run VMAF */
ret = compute_vmaf(&score, fmt, width, height, read_frame, s, model_path, log_path, log_fmt,
disable_clip, disable_avx, enable_transform, phone_model, do_psnr, do_ssim,
do_ms_ssim, pool_method, n_thread, n_subsample, enable_conf_interval);
fail_or_end:
if (s->ref_rfile)
{
fclose(s->ref_rfile);
}
if (s->dis_rfile)
{
fclose(s->dis_rfile);
}
if (s)
{
free(s);
}
return ret;
}
int main(int argc, char *argv[])
{
char* fmt;
int width;
int height;
char* ref_path;
char* dis_path;
char *model_path;
char *log_path = NULL;
char *log_fmt = NULL;
bool disable_clip = false;
bool disable_avx = false;
bool enable_transform = false;
bool phone_model = false;
bool do_psnr = false;
bool do_ssim = false;
bool do_ms_ssim = false;
char *pool_method = NULL;
int n_thread = 0;
int n_subsample = 1;
bool enable_conf_interval = false;
char *temp;
#if MEM_LEAK_TEST_ENABLE
int itr_ctr;
int ret = 0;
#endif
/* Check parameters */
if (argc < 7)
{
print_usage(argc, argv);
return -1;
}
fmt = argv[1];
try
{
width = std::stoi(argv[2]);
height = std::stoi(argv[3]);
}
catch (std::logic_error& e)
{
fprintf(stderr, "Error: Invalid width/height format: %s\n", e.what());
print_usage(argc, argv);
return -1;
}
if (width <= 0 || height <= 0)
{
fprintf(stderr, "Error: Invalid width/height value: %d, %d\n", width, height);
print_usage(argc, argv);
return -1;
}
ref_path = argv[4];
dis_path = argv[5];
model_path = argv[6];
log_path = getCmdOption(argv + 7, argv + argc, "--log");
log_fmt = getCmdOption(argv + 7, argv + argc, "--log-fmt");
if (log_fmt != NULL && !(strcmp(log_fmt, "xml")==0 || strcmp(log_fmt, "json")==0 || strcmp(log_fmt, "csv") == 0))
{
fprintf(stderr, "Error: log_fmt must be xml, json or csv, but is %s\n", log_fmt);
return -1;
}
temp = getCmdOption(argv + 7, argv + argc, "--thread");
if (temp)
{
try
{
n_thread = std::stoi(temp);
}
catch (std::logic_error& e)
{
fprintf(stderr, "Error: Invalid n_thread format: %s\n", e.what());
print_usage(argc, argv);
return -1;
}
}
if (n_thread < 0)
{
fprintf(stderr, "Error: Invalid n_thread value: %d\n", n_thread);
print_usage(argc, argv);
return -1;
}
temp = getCmdOption(argv + 7, argv + argc, "--subsample");
if (temp)
{
try
{
n_subsample = std::stoi(temp);
}
catch (std::logic_error& e)
{
fprintf(stderr, "Error: Invalid n_subsample format: %s\n", e.what());
print_usage(argc, argv);
return -1;
}
}
if (n_subsample <= 0)
{
fprintf(stderr, "Error: Invalid n_subsample value: %d\n", n_subsample);
print_usage(argc, argv);
return -1;
}
if (cmdOptionExists(argv + 7, argv + argc, "--disable-clip"))
{
disable_clip = true;
}
if (cmdOptionExists(argv + 7, argv + argc, "--disable-avx"))
{
disable_avx = true;
}
if (cmdOptionExists(argv + 7, argv + argc, "--enable-transform"))
{
enable_transform = true;
}
if (cmdOptionExists(argv + 7, argv + argc, "--phone-model"))
{
phone_model = true;
}
if (cmdOptionExists(argv + 7, argv + argc, "--psnr"))
{
do_psnr = true;
}
if (cmdOptionExists(argv + 7, argv + argc, "--ssim"))
{
do_ssim = true;
}
if (cmdOptionExists(argv + 7, argv + argc, "--ms-ssim"))
{
do_ms_ssim = true;
}
pool_method = getCmdOption(argv + 7, argv + argc, "--pool");
if (pool_method != NULL && !(strcmp(pool_method, "min")==0 || strcmp(pool_method, "harmonic_mean")==0 || strcmp(pool_method, "mean")==0))
{
fprintf(stderr, "Error: pool_method must be min, harmonic_mean or mean, but is %s\n", pool_method);
return -1;
}
if (cmdOptionExists(argv + 7, argv + argc, "--ci"))
{
enable_conf_interval = true;
}
try
{
#if MEM_LEAK_TEST_ENABLE
for(itr_ctr=0;itr_ctr<1000;itr_ctr++)
{
getMemory(itr_ctr,1);
ret = run_wrapper(fmt, width, height, ref_path, dis_path, model_path,
log_path, log_fmt, disable_clip, disable_avx, enable_transform, phone_model,
do_psnr, do_ssim, do_ms_ssim, pool_method, n_thread, n_subsample, enable_conf_interval);
getMemory(itr_ctr,2);
}
#else
return run_wrapper(fmt, width, height, ref_path, dis_path, model_path,
log_path, log_fmt, disable_clip, disable_avx, enable_transform, phone_model,
do_psnr, do_ssim, do_ms_ssim, pool_method, n_thread, n_subsample, enable_conf_interval);
#endif
}
catch (const std::exception &e)
{
fprintf(stderr, "Error: %s\n", e.what());
print_usage(argc, argv);
return -1;
}
}