blob: 89440ea9845a25ef15898811db67d301affbcaf1 [file] [log] [blame]
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +00001/*
2 * Copyright (c) 2012 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 */
10
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <stdio.h>
12#include <stdlib.h>
13
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000014#include <map>
pbos@webrtc.orgba7f6a82013-06-04 08:14:10 +000015#include <string>
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000016#include <vector>
17
Magnus Jedvertb468ace2018-09-05 16:11:48 +020018#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_tools/frame_analyzer/video_quality_analysis.h"
Magnus Jedvertb468ace2018-09-05 16:11:48 +020020#include "rtc_tools/frame_analyzer/video_temporal_aligner.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_tools/simple_command_line_parser.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020022#include "rtc_tools/video_file_reader.h"
Paulina Hensmanb671d462018-09-14 11:32:00 +020023#include "rtc_tools/video_file_writer.h"
Edward Lemur2e5966b2018-01-30 15:33:02 +010024#include "test/testsupport/perf_test.h"
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000025
Paulina Hensmana6471eb2018-10-05 14:34:33 +020026namespace {
27
28#ifdef WIN32
29const char* const kPathDelimiter = "\\";
30#else
31const char* const kPathDelimiter = "/";
32#endif
33
34std::string JoinFilename(std::string directory, std::string filename) {
35 return directory + kPathDelimiter + filename;
36}
37
38} // namespace
39
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000040/*
41 * A command line tool running PSNR and SSIM on a reference video and a test
42 * video. The test video is a record of the reference video which can start at
43 * an arbitrary point. It is possible that there will be repeated frames or
Magnus Jedvertb468ace2018-09-05 16:11:48 +020044 * skipped frames as well. The video files should be I420 .y4m or .yuv videos.
45 * If both files are .y4m, it's not needed to specify width/height. The tool
46 * prints the result to standard output in the Chromium perf format:
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000047 * RESULT <metric>:<label>= <values>
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000048 *
49 * The max value for PSNR is 48.0 (between equal frames), as for SSIM it is 1.0.
50 *
51 * Usage:
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000052 * frame_analyzer --label=<test_label> --reference_file=<name_of_file>
Magnus Jedvertb468ace2018-09-05 16:11:48 +020053 * --test_file_ref=<name_of_file> --width=<frame_width> --height=<frame_height>
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000054 */
Robin Raymond1c62ffa2017-12-03 16:45:56 -050055int main(int argc, char* argv[]) {
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000056 std::string program_name = argv[0];
mandermo74568172017-01-17 03:24:57 -080057 std::string usage =
58 "Compares the output video with the initially sent video."
59 "\nExample usage:\n" +
60 program_name +
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020061 " --reference_file=ref.yuv --test_file=test.yuv --width=320 "
62 "--height=240\n"
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000063 "Command line flags:\n"
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020064 " - width(int): The width of the reference and test files. Default: -1\n"
65 " - height(int): The height of the reference and test files. "
66 " Default: -1\n"
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000067 " - label(string): The label to use for the perf output."
68 " Default: MY_TEST\n"
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020069 " Default: ref.yuv\n"
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000070 " - test_file(string): The test YUV file to run the analysis for."
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020071 " Default: test_file.yuv\n"
Edward Lemur2e5966b2018-01-30 15:33:02 +010072 " - chartjson_result_file: Where to store perf result in chartjson"
73 " format. If not present, no perf result will be stored."
Paulina Hensmanb671d462018-09-14 11:32:00 +020074 " Default: None\n"
75 " - aligned_output_file: Where to write aligned YUV/Y4M output file."
76 " If not present, no file will be written."
Paulina Hensman12c62b92018-09-28 15:14:07 +020077 " Default: None\n"
78 " - yuv_directory: Where to write aligned YUV ref+test output files."
79 " If not present, no files will be written."
Edward Lemur2e5966b2018-01-30 15:33:02 +010080 " Default: None\n";
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000081
82 webrtc::test::CommandLineParser parser;
83
84 // Init the parser and set the usage message
85 parser.Init(argc, argv);
86 parser.SetUsageMessage(usage);
87
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020088 parser.SetFlag("width", "-1");
89 parser.SetFlag("height", "-1");
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000090 parser.SetFlag("label", "MY_TEST");
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020091 parser.SetFlag("reference_file", "ref.yuv");
92 parser.SetFlag("test_file", "test.yuv");
Paulina Hensmanb671d462018-09-14 11:32:00 +020093 parser.SetFlag("aligned_output_file", "");
Paulina Hensman12c62b92018-09-28 15:14:07 +020094 parser.SetFlag("yuv_directory", "");
Edward Lemur2e5966b2018-01-30 15:33:02 +010095 parser.SetFlag("chartjson_result_file", "");
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000096 parser.SetFlag("help", "false");
97
98 parser.ProcessFlags();
99 if (parser.GetFlag("help") == "true") {
100 parser.PrintUsageMessage();
Thiago Farina3a939862015-04-09 15:45:12 +0200101 exit(EXIT_SUCCESS);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000102 }
103 parser.PrintEnteredFlags();
104
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200105 int width = strtol((parser.GetFlag("width")).c_str(), nullptr, 10);
106 int height = strtol((parser.GetFlag("height")).c_str(), nullptr, 10);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000107
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200108 const std::string reference_file_name = parser.GetFlag("reference_file");
109 const std::string test_file_name = parser.GetFlag("test_file");
110
111 // .yuv files require explicit resolution.
112 if ((rtc::ends_with(reference_file_name.c_str(), ".yuv") ||
113 rtc::ends_with(test_file_name.c_str(), ".yuv")) &&
114 (width <= 0 || height <= 0)) {
115 fprintf(stderr,
116 "Error: You need to specify width and height when using .yuv "
117 "files\n");
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200118 return -1;
Magnus Jedvert404be7f2018-08-22 19:23:34 +0200119 }
120
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200121 webrtc::test::ResultsContainer results;
Magnus Jedvert9bb55fc2018-08-24 14:56:03 +0200122
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200123 rtc::scoped_refptr<webrtc::test::Video> reference_video =
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200124 webrtc::test::OpenYuvOrY4mFile(reference_file_name, width, height);
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200125 rtc::scoped_refptr<webrtc::test::Video> test_video =
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200126 webrtc::test::OpenYuvOrY4mFile(test_file_name, width, height);
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200127
128 if (!reference_video || !test_video) {
129 fprintf(stderr, "Error opening video files\n");
130 return 0;
131 }
132
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200133 const std::vector<size_t> matching_indices =
134 webrtc::test::FindMatchingFrameIndices(reference_video, test_video);
135
136 results.frames =
137 webrtc::test::RunAnalysis(reference_video, test_video, matching_indices);
138
139 const std::vector<webrtc::test::Cluster> clusters =
140 webrtc::test::CalculateFrameClusters(matching_indices);
141 results.max_repeated_frames = webrtc::test::GetMaxRepeatedFrames(clusters);
142 results.max_skipped_frames = webrtc::test::GetMaxSkippedFrames(clusters);
143 results.total_skipped_frames =
144 webrtc::test::GetTotalNumberOfSkippedFrames(clusters);
145 results.decode_errors_ref = 0;
146 results.decode_errors_test = 0;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000147
Edward Lemur2e5966b2018-01-30 15:33:02 +0100148 webrtc::test::PrintAnalysisResults(parser.GetFlag("label"), &results);
149
150 std::string chartjson_result_file = parser.GetFlag("chartjson_result_file");
151 if (!chartjson_result_file.empty()) {
152 webrtc::test::WritePerfResults(chartjson_result_file);
153 }
Paulina Hensman12c62b92018-09-28 15:14:07 +0200154 rtc::scoped_refptr<webrtc::test::Video> reordered_video =
155 webrtc::test::GenerateAlignedReferenceVideo(reference_video,
156 matching_indices);
Paulina Hensmanb671d462018-09-14 11:32:00 +0200157 std::string aligned_output_file = parser.GetFlag("aligned_output_file");
158 if (!aligned_output_file.empty()) {
Paulina Hensmanb671d462018-09-14 11:32:00 +0200159 webrtc::test::WriteVideoToFile(reordered_video, aligned_output_file,
160 /*fps=*/30);
161 }
Paulina Hensman12c62b92018-09-28 15:14:07 +0200162 std::string yuv_directory = parser.GetFlag("yuv_directory");
163 if (!yuv_directory.empty()) {
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200164 webrtc::test::WriteVideoToFile(reordered_video,
165 JoinFilename(yuv_directory, "ref.yuv"),
Oleh Prypin3d3e08b2018-10-01 13:19:15 +0000166 /*fps=*/30);
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200167 webrtc::test::WriteVideoToFile(test_video,
168 JoinFilename(yuv_directory, "test.yuv"),
Oleh Prypin3d3e08b2018-10-01 13:19:15 +0000169 /*fps=*/30);
Paulina Hensman12c62b92018-09-28 15:14:07 +0200170 }
Edward Lemur2e5966b2018-01-30 15:33:02 +0100171
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500172 return 0;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000173}