You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. from __future__ import absolute_import
  10. from __future__ import division
  11. from __future__ import print_function
  12. import json
  13. import optparse
  14. import os
  15. import shutil
  16. import subprocess
  17. import sys
  18. import tempfile
  19. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  20. # Chrome browsertests will throw away stderr; avoid that output gets lost.
  21. sys.stderr = sys.stdout
  22. def _ParseArgs():
  23. """Registers the command-line options."""
  24. usage = 'usage: %prog [options]'
  25. parser = optparse.OptionParser(usage=usage)
  26. parser.add_option('--label', type='string', default='MY_TEST',
  27. help=('Label of the test, used to identify different '
  28. 'tests. Default: %default'))
  29. parser.add_option('--ref_video', type='string',
  30. help='Reference video to compare with (YUV).')
  31. parser.add_option('--test_video', type='string',
  32. help=('Test video to be compared with the reference '
  33. 'video (YUV).'))
  34. parser.add_option('--frame_analyzer', type='string',
  35. help='Path to the frame analyzer executable.')
  36. parser.add_option('--aligned_output_file', type='string',
  37. help='Path for output aligned YUV or Y4M file.')
  38. parser.add_option('--vmaf', type='string',
  39. help='Path to VMAF executable.')
  40. parser.add_option('--vmaf_model', type='string',
  41. help='Path to VMAF model.')
  42. parser.add_option('--vmaf_phone_model', action='store_true',
  43. help='Whether to use phone model in VMAF.')
  44. parser.add_option('--yuv_frame_width', type='int', default=640,
  45. help='Width of the YUV file\'s frames. Default: %default')
  46. parser.add_option('--yuv_frame_height', type='int', default=480,
  47. help='Height of the YUV file\'s frames. Default: %default')
  48. parser.add_option('--chartjson_result_file', type='str', default=None,
  49. help='Where to store perf results in chartjson format.')
  50. options, _ = parser.parse_args()
  51. if not options.ref_video:
  52. parser.error('You must provide a path to the reference video!')
  53. if not os.path.exists(options.ref_video):
  54. parser.error('Cannot find the reference video at %s' % options.ref_video)
  55. if not options.test_video:
  56. parser.error('You must provide a path to the test video!')
  57. if not os.path.exists(options.test_video):
  58. parser.error('Cannot find the test video at %s' % options.test_video)
  59. if not options.frame_analyzer:
  60. parser.error('You must provide the path to the frame analyzer executable!')
  61. if not os.path.exists(options.frame_analyzer):
  62. parser.error('Cannot find frame analyzer executable at %s!' %
  63. options.frame_analyzer)
  64. if options.vmaf and not options.vmaf_model:
  65. parser.error('You must provide a path to a VMAF model to use VMAF.')
  66. return options
  67. def _DevNull():
  68. """On Windows, sometimes the inherited stdin handle from the parent process
  69. fails. Workaround this by passing null to stdin to the subprocesses commands.
  70. This function can be used to create the null file handler.
  71. """
  72. return open(os.devnull, 'r')
  73. def _RunFrameAnalyzer(options, yuv_directory=None):
  74. """Run frame analyzer to compare the videos and print output."""
  75. cmd = [
  76. options.frame_analyzer,
  77. '--label=%s' % options.label,
  78. '--reference_file=%s' % options.ref_video,
  79. '--test_file=%s' % options.test_video,
  80. '--width=%d' % options.yuv_frame_width,
  81. '--height=%d' % options.yuv_frame_height,
  82. ]
  83. if options.chartjson_result_file:
  84. cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file)
  85. if options.aligned_output_file:
  86. cmd.append('--aligned_output_file=%s' % options.aligned_output_file)
  87. if yuv_directory:
  88. cmd.append('--yuv_directory=%s' % yuv_directory)
  89. frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(),
  90. stdout=sys.stdout, stderr=sys.stderr)
  91. frame_analyzer.wait()
  92. if frame_analyzer.returncode != 0:
  93. print('Failed to run frame analyzer.')
  94. return frame_analyzer.returncode
  95. def _RunVmaf(options, yuv_directory, logfile):
  96. """ Run VMAF to compare videos and print output.
  97. The yuv_directory is assumed to have been populated with a reference and test
  98. video in .yuv format, with names according to the label.
  99. """
  100. cmd = [
  101. options.vmaf,
  102. 'yuv420p',
  103. str(options.yuv_frame_width),
  104. str(options.yuv_frame_height),
  105. os.path.join(yuv_directory, "ref.yuv"),
  106. os.path.join(yuv_directory, "test.yuv"),
  107. options.vmaf_model,
  108. '--log',
  109. logfile,
  110. '--log-fmt',
  111. 'json',
  112. ]
  113. if options.vmaf_phone_model:
  114. cmd.append('--phone-model')
  115. vmaf = subprocess.Popen(cmd, stdin=_DevNull(),
  116. stdout=sys.stdout, stderr=sys.stderr)
  117. vmaf.wait()
  118. if vmaf.returncode != 0:
  119. print('Failed to run VMAF.')
  120. return 1
  121. # Read per-frame scores from VMAF output and print.
  122. with open(logfile) as f:
  123. vmaf_data = json.load(f)
  124. vmaf_scores = []
  125. for frame in vmaf_data['frames']:
  126. vmaf_scores.append(frame['metrics']['vmaf'])
  127. print('RESULT VMAF: %s=' % options.label, vmaf_scores)
  128. return 0
  129. def main():
  130. """The main function.
  131. A simple invocation is:
  132. ./webrtc/rtc_tools/compare_videos.py
  133. --ref_video=<path_and_name_of_reference_video>
  134. --test_video=<path_and_name_of_test_video>
  135. --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable>
  136. Running vmaf requires the following arguments:
  137. --vmaf, --vmaf_model, --yuv_frame_width, --yuv_frame_height
  138. """
  139. options = _ParseArgs()
  140. if options.vmaf:
  141. try:
  142. # Directory to save temporary YUV files for VMAF in frame_analyzer.
  143. yuv_directory = tempfile.mkdtemp()
  144. _, vmaf_logfile = tempfile.mkstemp()
  145. # Run frame analyzer to compare the videos and print output.
  146. if _RunFrameAnalyzer(options, yuv_directory=yuv_directory) != 0:
  147. return 1
  148. # Run VMAF for further video comparison and print output.
  149. return _RunVmaf(options, yuv_directory, vmaf_logfile)
  150. finally:
  151. shutil.rmtree(yuv_directory)
  152. os.remove(vmaf_logfile)
  153. else:
  154. return _RunFrameAnalyzer(options)
  155. return 0
  156. if __name__ == '__main__':
  157. sys.exit(main())