kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 1 | #!/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 Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 10 | import json |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 11 | import optparse |
| 12 | import os |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 13 | import shutil |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 14 | import subprocess |
| 15 | import sys |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 16 | import tempfile |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 20 | |
| 21 | # Chrome browsertests will throw away stderr; avoid that output gets lost. |
| 22 | sys.stderr = sys.stdout |
| 23 | |
| 24 | |
| 25 | def _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 Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 40 | parser.add_option('--aligned_output_file', type='string', |
| 41 | help='Path for output aligned YUV or Y4M file.') |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 42 | 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.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 48 | parser.add_option('--barcode_decoder', type='string', |
Magnus Jedvert | 165148d | 2018-10-22 22:19:20 +0200 | [diff] [blame^] | 49 | help=('DEPRECATED')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 50 | parser.add_option('--ffmpeg_path', type='string', |
Magnus Jedvert | 165148d | 2018-10-22 22:19:20 +0200 | [diff] [blame^] | 51 | help=('DEPRECATED')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 52 | parser.add_option('--zxing_path', type='string', |
Magnus Jedvert | 165148d | 2018-10-22 22:19:20 +0200 | [diff] [blame^] | 53 | help=('DEPRECATED')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 54 | parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt', |
Magnus Jedvert | 165148d | 2018-10-22 22:19:20 +0200 | [diff] [blame^] | 55 | help=('DEPRECATED')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 56 | parser.add_option('--stats_file_test', type='string', |
Magnus Jedvert | 165148d | 2018-10-22 22:19:20 +0200 | [diff] [blame^] | 57 | help=('DEPRECATED')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 58 | parser.add_option('--stats_file', type='string', |
| 59 | help=('DEPRECATED')) |
| 60 | parser.add_option('--yuv_frame_width', type='int', default=640, |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 61 | help='Width of the YUV file\'s frames. Default: %default') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 62 | parser.add_option('--yuv_frame_height', type='int', default=480, |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 63 | help='Height of the YUV file\'s frames. Default: %default') |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 64 | parser.add_option('--chartjson_result_file', type='str', default=None, |
| 65 | help='Where to store perf results in chartjson format.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 66 | options, _ = parser.parse_args() |
| 67 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 68 | 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 Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 83 | |
| 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 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 87 | return options |
| 88 | |
| 89 | def _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 Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 96 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 97 | def _RunFrameAnalyzer(options, yuv_directory=None): |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 98 | """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 Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 113 | if yuv_directory: |
| 114 | cmd.append('--yuv_directory=%s' % yuv_directory) |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 115 | frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(), |
| 116 | stdout=sys.stdout, stderr=sys.stderr) |
| 117 | frame_analyzer.wait() |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 118 | if frame_analyzer.returncode != 0: |
| 119 | print 'Failed to run frame analyzer.' |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 120 | return frame_analyzer.returncode |
| 121 | |
| 122 | |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 123 | def _RunVmaf(options, yuv_directory, logfile): |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 124 | """ Run VMAF to compare videos and print output. |
| 125 | |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 126 | 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 Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 134 | os.path.join(yuv_directory, "ref.yuv"), |
| 135 | os.path.join(yuv_directory, "test.yuv"), |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 136 | options.vmaf_model, |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 137 | '--log', |
| 138 | logfile, |
| 139 | '--log-fmt', |
| 140 | 'json', |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 141 | ] |
| 142 | if options.vmaf_phone_model: |
| 143 | cmd.append('--phone-model') |
| 144 | |
| 145 | vmaf = subprocess.Popen(cmd, stdin=_DevNull(), |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 146 | stdout=sys.stdout, stderr=sys.stderr) |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 147 | vmaf.wait() |
| 148 | if vmaf.returncode != 0: |
| 149 | print 'Failed to run VMAF.' |
| 150 | return 1 |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 151 | |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 152 | # 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 Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 160 | return 0 |
| 161 | |
| 162 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 163 | def main(): |
| 164 | """The main function. |
| 165 | |
| 166 | A simple invocation is: |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 167 | ./webrtc/rtc_tools/compare_videos.py |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 168 | --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 Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 171 | |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 172 | Running vmaf requires the following arguments: |
| 173 | --vmaf, --vmaf_model, --yuv_frame_width, --yuv_frame_height |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 174 | """ |
| 175 | options = _ParseArgs() |
| 176 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 177 | if options.vmaf: |
| 178 | try: |
| 179 | # Directory to save temporary YUV files for VMAF in frame_analyzer. |
| 180 | yuv_directory = tempfile.mkdtemp() |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 181 | _, vmaf_logfile = tempfile.mkstemp() |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 182 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 183 | # Run frame analyzer to compare the videos and print output. |
| 184 | if _RunFrameAnalyzer(options, yuv_directory=yuv_directory) != 0: |
| 185 | return 1 |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 186 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 187 | # Run VMAF for further video comparison and print output. |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 188 | return _RunVmaf(options, yuv_directory, vmaf_logfile) |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 189 | finally: |
| 190 | shutil.rmtree(yuv_directory) |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame] | 191 | os.remove(vmaf_logfile) |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 192 | else: |
| 193 | return _RunFrameAnalyzer(options) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 194 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 195 | return 0 |
| 196 | |
| 197 | if __name__ == '__main__': |
| 198 | sys.exit(main()) |