blob: 0cb4a6ddb199f560f467c4ddd9b2551cf443c601 [file] [log] [blame]
kjellanderd2b63cf2017-06-30 03:04:59 -07001#!/usr/bin/env python
2# Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
Paulina Hensmanede87962018-10-10 15:48:30 +020010import json
kjellanderd2b63cf2017-06-30 03:04:59 -070011import optparse
12import os
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000013import shutil
kjellanderd2b63cf2017-06-30 03:04:59 -070014import subprocess
15import sys
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000016import tempfile
kjellanderd2b63cf2017-06-30 03:04:59 -070017
18
19SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
20
21# Chrome browsertests will throw away stderr; avoid that output gets lost.
22sys.stderr = sys.stdout
23
24
25def _ParseArgs():
26 """Registers the command-line options."""
27 usage = 'usage: %prog [options]'
28 parser = optparse.OptionParser(usage=usage)
29
30 parser.add_option('--label', type='string', default='MY_TEST',
31 help=('Label of the test, used to identify different '
32 'tests. Default: %default'))
33 parser.add_option('--ref_video', type='string',
34 help='Reference video to compare with (YUV).')
35 parser.add_option('--test_video', type='string',
36 help=('Test video to be compared with the reference '
37 'video (YUV).'))
38 parser.add_option('--frame_analyzer', type='string',
39 help='Path to the frame analyzer executable.')
Paulina Hensmanb671d462018-09-14 11:32:00 +020040 parser.add_option('--aligned_output_file', type='string',
41 help='Path for output aligned YUV or Y4M file.')
Paulina Hensman12c62b92018-09-28 15:14:07 +020042 parser.add_option('--vmaf', type='string',
43 help='Path to VMAF executable.')
44 parser.add_option('--vmaf_model', type='string',
45 help='Path to VMAF model.')
46 parser.add_option('--vmaf_phone_model', action='store_true',
47 help='Whether to use phone model in VMAF.')
kjellanderd2b63cf2017-06-30 03:04:59 -070048 parser.add_option('--barcode_decoder', type='string',
Magnus Jedvert165148d2018-10-22 22:19:20 +020049 help=('DEPRECATED'))
kjellanderd2b63cf2017-06-30 03:04:59 -070050 parser.add_option('--ffmpeg_path', type='string',
Magnus Jedvert165148d2018-10-22 22:19:20 +020051 help=('DEPRECATED'))
kjellanderd2b63cf2017-06-30 03:04:59 -070052 parser.add_option('--zxing_path', type='string',
Magnus Jedvert165148d2018-10-22 22:19:20 +020053 help=('DEPRECATED'))
kjellanderd2b63cf2017-06-30 03:04:59 -070054 parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt',
Magnus Jedvert165148d2018-10-22 22:19:20 +020055 help=('DEPRECATED'))
kjellanderd2b63cf2017-06-30 03:04:59 -070056 parser.add_option('--stats_file_test', type='string',
Magnus Jedvert165148d2018-10-22 22:19:20 +020057 help=('DEPRECATED'))
kjellanderd2b63cf2017-06-30 03:04:59 -070058 parser.add_option('--stats_file', type='string',
59 help=('DEPRECATED'))
60 parser.add_option('--yuv_frame_width', type='int', default=640,
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000061 help='Width of the YUV file\'s frames. Default: %default')
kjellanderd2b63cf2017-06-30 03:04:59 -070062 parser.add_option('--yuv_frame_height', type='int', default=480,
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000063 help='Height of the YUV file\'s frames. Default: %default')
Edward Lemur2e5966b2018-01-30 15:33:02 +010064 parser.add_option('--chartjson_result_file', type='str', default=None,
65 help='Where to store perf results in chartjson format.')
kjellanderd2b63cf2017-06-30 03:04:59 -070066 options, _ = parser.parse_args()
67
kjellanderd2b63cf2017-06-30 03:04:59 -070068 if not options.ref_video:
69 parser.error('You must provide a path to the reference video!')
70 if not os.path.exists(options.ref_video):
71 parser.error('Cannot find the reference video at %s' % options.ref_video)
72
73 if not options.test_video:
74 parser.error('You must provide a path to the test video!')
75 if not os.path.exists(options.test_video):
76 parser.error('Cannot find the test video at %s' % options.test_video)
77
78 if not options.frame_analyzer:
79 parser.error('You must provide the path to the frame analyzer executable!')
80 if not os.path.exists(options.frame_analyzer):
81 parser.error('Cannot find frame analyzer executable at %s!' %
82 options.frame_analyzer)
Paulina Hensman12c62b92018-09-28 15:14:07 +020083
84 if options.vmaf and not options.vmaf_model:
85 parser.error('You must provide a path to a VMAF model to use VMAF.')
86
kjellanderd2b63cf2017-06-30 03:04:59 -070087 return options
88
89def _DevNull():
90 """On Windows, sometimes the inherited stdin handle from the parent process
91 fails. Workaround this by passing null to stdin to the subprocesses commands.
92 This function can be used to create the null file handler.
93 """
94 return open(os.devnull, 'r')
95
Paulina Hensman12c62b92018-09-28 15:14:07 +020096
Paulina Hensmana6471eb2018-10-05 14:34:33 +020097def _RunFrameAnalyzer(options, yuv_directory=None):
Paulina Hensman12c62b92018-09-28 15:14:07 +020098 """Run frame analyzer to compare the videos and print output."""
99 cmd = [
100 options.frame_analyzer,
101 '--label=%s' % options.label,
102 '--reference_file=%s' % options.ref_video,
103 '--test_file=%s' % options.test_video,
104 '--stats_file_ref=%s' % options.stats_file_ref,
105 '--stats_file_test=%s' % options.stats_file_test,
106 '--width=%d' % options.yuv_frame_width,
107 '--height=%d' % options.yuv_frame_height,
108 ]
109 if options.chartjson_result_file:
110 cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file)
111 if options.aligned_output_file:
112 cmd.append('--aligned_output_file=%s' % options.aligned_output_file)
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200113 if yuv_directory:
114 cmd.append('--yuv_directory=%s' % yuv_directory)
Paulina Hensman12c62b92018-09-28 15:14:07 +0200115 frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(),
116 stdout=sys.stdout, stderr=sys.stderr)
117 frame_analyzer.wait()
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200118 if frame_analyzer.returncode != 0:
119 print 'Failed to run frame analyzer.'
Paulina Hensman12c62b92018-09-28 15:14:07 +0200120 return frame_analyzer.returncode
121
122
Paulina Hensmanede87962018-10-10 15:48:30 +0200123def _RunVmaf(options, yuv_directory, logfile):
Paulina Hensman12c62b92018-09-28 15:14:07 +0200124 """ Run VMAF to compare videos and print output.
125
Paulina Hensman12c62b92018-09-28 15:14:07 +0200126 The yuv_directory is assumed to have been populated with a reference and test
127 video in .yuv format, with names according to the label.
128 """
129 cmd = [
130 options.vmaf,
131 'yuv420p',
132 str(options.yuv_frame_width),
133 str(options.yuv_frame_height),
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200134 os.path.join(yuv_directory, "ref.yuv"),
135 os.path.join(yuv_directory, "test.yuv"),
Paulina Hensman12c62b92018-09-28 15:14:07 +0200136 options.vmaf_model,
Paulina Hensmanede87962018-10-10 15:48:30 +0200137 '--log',
138 logfile,
139 '--log-fmt',
140 'json',
Paulina Hensman12c62b92018-09-28 15:14:07 +0200141 ]
142 if options.vmaf_phone_model:
143 cmd.append('--phone-model')
144
145 vmaf = subprocess.Popen(cmd, stdin=_DevNull(),
Paulina Hensmanede87962018-10-10 15:48:30 +0200146 stdout=sys.stdout, stderr=sys.stderr)
Paulina Hensman12c62b92018-09-28 15:14:07 +0200147 vmaf.wait()
148 if vmaf.returncode != 0:
149 print 'Failed to run VMAF.'
150 return 1
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200151
Paulina Hensmanede87962018-10-10 15:48:30 +0200152 # Read per-frame scores from VMAF output and print.
153 with open(logfile) as f:
154 vmaf_data = json.load(f)
155 vmaf_scores = []
156 for frame in vmaf_data['frames']:
157 vmaf_scores.append(frame['metrics']['vmaf'])
158 print 'RESULT VMAF: %s=' % options.label, vmaf_scores
159
Paulina Hensman12c62b92018-09-28 15:14:07 +0200160 return 0
161
162
kjellanderd2b63cf2017-06-30 03:04:59 -0700163def main():
164 """The main function.
165
166 A simple invocation is:
Paulina Hensmanb671d462018-09-14 11:32:00 +0200167 ./webrtc/rtc_tools/compare_videos.py
kjellanderd2b63cf2017-06-30 03:04:59 -0700168 --ref_video=<path_and_name_of_reference_video>
169 --test_video=<path_and_name_of_test_video>
170 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable>
Magnus Jedvert3e169ac2018-08-24 12:44:59 +0000171
Paulina Hensman12c62b92018-09-28 15:14:07 +0200172 Running vmaf requires the following arguments:
173 --vmaf, --vmaf_model, --yuv_frame_width, --yuv_frame_height
kjellanderd2b63cf2017-06-30 03:04:59 -0700174 """
175 options = _ParseArgs()
176
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200177 if options.vmaf:
178 try:
179 # Directory to save temporary YUV files for VMAF in frame_analyzer.
180 yuv_directory = tempfile.mkdtemp()
Paulina Hensmanede87962018-10-10 15:48:30 +0200181 _, vmaf_logfile = tempfile.mkstemp()
Paulina Hensman12c62b92018-09-28 15:14:07 +0200182
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200183 # Run frame analyzer to compare the videos and print output.
184 if _RunFrameAnalyzer(options, yuv_directory=yuv_directory) != 0:
185 return 1
Paulina Hensman12c62b92018-09-28 15:14:07 +0200186
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200187 # Run VMAF for further video comparison and print output.
Paulina Hensmanede87962018-10-10 15:48:30 +0200188 return _RunVmaf(options, yuv_directory, vmaf_logfile)
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200189 finally:
190 shutil.rmtree(yuv_directory)
Paulina Hensmanede87962018-10-10 15:48:30 +0200191 os.remove(vmaf_logfile)
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200192 else:
193 return _RunFrameAnalyzer(options)
kjellanderd2b63cf2017-06-30 03:04:59 -0700194
kjellanderd2b63cf2017-06-30 03:04:59 -0700195 return 0
196
197if __name__ == '__main__':
198 sys.exit(main())