blob: 899612990a226c94d12d02a6e081a5635c440840 [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>
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstddef>
pbos@webrtc.orgba7f6a82013-06-04 08:14:10 +000014#include <string>
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000015#include <vector>
16
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +020017#include "absl/flags/flag.h"
18#include "absl/flags/parse.h"
Steve Anton68586e82018-12-13 17:41:25 -080019#include "absl/strings/match.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010020#include "api/scoped_refptr.h"
Magnus Jedvertf2590782018-11-22 15:24:25 +010021#include "rtc_base/strings/string_builder.h"
Magnus Jedvertf2590782018-11-22 15:24:25 +010022#include "rtc_tools/frame_analyzer/video_color_aligner.h"
23#include "rtc_tools/frame_analyzer/video_geometry_aligner.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_tools/frame_analyzer/video_quality_analysis.h"
Magnus Jedvertb468ace2018-09-05 16:11:48 +020025#include "rtc_tools/frame_analyzer/video_temporal_aligner.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020026#include "rtc_tools/video_file_reader.h"
Paulina Hensmanb671d462018-09-14 11:32:00 +020027#include "rtc_tools/video_file_writer.h"
Edward Lemur2e5966b2018-01-30 15:33:02 +010028#include "test/testsupport/perf_test.h"
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000029
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +020030ABSL_FLAG(int32_t, width, -1, "The width of the reference and test files");
31ABSL_FLAG(int32_t, height, -1, "The height of the reference and test files");
32ABSL_FLAG(std::string,
33 label,
34 "MY_TEST",
35 "The label to use for the perf output");
36ABSL_FLAG(std::string,
37 reference_file,
38 "ref.yuv",
39 "The reference YUV file to run the analysis against");
40ABSL_FLAG(std::string,
41 test_file,
42 "test.yuv",
43 "The test YUV file to run the analysis for");
44ABSL_FLAG(std::string,
45 aligned_output_file,
46 "",
47 "Where to write aligned YUV/Y4M output file, f not present, no files "
48 "will be written");
49ABSL_FLAG(std::string,
50 yuv_directory,
51 "",
52 "Where to write aligned YUV ref+test output files, if not present, "
53 "no files will be written");
54ABSL_FLAG(std::string,
55 chartjson_result_file,
56 "",
57 "Where to store perf result in chartjson format, if not present, no "
58 "perf result will be stored");
59
Paulina Hensmana6471eb2018-10-05 14:34:33 +020060namespace {
61
62#ifdef WIN32
63const char* const kPathDelimiter = "\\";
64#else
65const char* const kPathDelimiter = "/";
66#endif
67
68std::string JoinFilename(std::string directory, std::string filename) {
69 return directory + kPathDelimiter + filename;
70}
71
72} // namespace
73
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000074/*
75 * A command line tool running PSNR and SSIM on a reference video and a test
76 * video. The test video is a record of the reference video which can start at
77 * an arbitrary point. It is possible that there will be repeated frames or
Magnus Jedvertb468ace2018-09-05 16:11:48 +020078 * skipped frames as well. The video files should be I420 .y4m or .yuv videos.
79 * If both files are .y4m, it's not needed to specify width/height. The tool
80 * prints the result to standard output in the Chromium perf format:
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000081 * RESULT <metric>:<label>= <values>
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000082 *
83 * The max value for PSNR is 48.0 (between equal frames), as for SSIM it is 1.0.
84 *
85 * Usage:
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000086 * frame_analyzer --label=<test_label> --reference_file=<name_of_file>
Magnus Jedvertb468ace2018-09-05 16:11:48 +020087 * --test_file_ref=<name_of_file> --width=<frame_width> --height=<frame_height>
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000088 */
Robin Raymond1c62ffa2017-12-03 16:45:56 -050089int main(int argc, char* argv[]) {
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +020090 absl::ParseCommandLine(argc, argv);
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000091
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +020092 int width = absl::GetFlag(FLAGS_width);
93 int height = absl::GetFlag(FLAGS_height);
94 const std::string reference_file_name = absl::GetFlag(FLAGS_reference_file);
95 const std::string test_file_name = absl::GetFlag(FLAGS_test_file);
Magnus Jedvertb468ace2018-09-05 16:11:48 +020096
97 // .yuv files require explicit resolution.
Steve Anton68586e82018-12-13 17:41:25 -080098 if ((absl::EndsWith(reference_file_name, ".yuv") ||
99 absl::EndsWith(test_file_name, ".yuv")) &&
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200100 (width <= 0 || height <= 0)) {
101 fprintf(stderr,
102 "Error: You need to specify width and height when using .yuv "
103 "files\n");
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200104 return -1;
Magnus Jedvert404be7f2018-08-22 19:23:34 +0200105 }
106
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200107 webrtc::test::ResultsContainer results;
Magnus Jedvert9bb55fc2018-08-24 14:56:03 +0200108
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200109 rtc::scoped_refptr<webrtc::test::Video> reference_video =
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200110 webrtc::test::OpenYuvOrY4mFile(reference_file_name, width, height);
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200111 rtc::scoped_refptr<webrtc::test::Video> test_video =
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200112 webrtc::test::OpenYuvOrY4mFile(test_file_name, width, height);
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200113
114 if (!reference_video || !test_video) {
115 fprintf(stderr, "Error opening video files\n");
Oleh Prypina9316c92019-01-25 12:08:19 +0100116 return 1;
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200117 }
118
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200119 const std::vector<size_t> matching_indices =
120 webrtc::test::FindMatchingFrameIndices(reference_video, test_video);
121
Magnus Jedvertf2590782018-11-22 15:24:25 +0100122 // Align the reference video both temporally and geometrically. I.e. align the
123 // frames to match up in order to the test video, and align a crop region of
124 // the reference video to match up to the test video.
125 const rtc::scoped_refptr<webrtc::test::Video> aligned_reference_video =
126 AdjustCropping(ReorderVideo(reference_video, matching_indices),
127 test_video);
128
129 // Calculate if there is any systematic color difference between the reference
130 // and test video.
131 const webrtc::test::ColorTransformationMatrix color_transformation =
132 CalculateColorTransformationMatrix(aligned_reference_video, test_video);
133
134 char buf[256];
135 rtc::SimpleStringBuilder string_builder(buf);
136 for (int i = 0; i < 3; ++i) {
137 string_builder << "\n";
138 for (int j = 0; j < 4; ++j)
139 string_builder.AppendFormat("%6.2f ", color_transformation[i][j]);
140 }
141 printf("Adjusting test video with color transformation: %s\n",
142 string_builder.str());
143
144 // Adjust all frames in the test video with the calculated color
145 // transformation.
146 const rtc::scoped_refptr<webrtc::test::Video> color_adjusted_test_video =
147 AdjustColors(color_transformation, test_video);
148
149 results.frames = webrtc::test::RunAnalysis(
150 aligned_reference_video, color_adjusted_test_video, matching_indices);
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200151
152 const std::vector<webrtc::test::Cluster> clusters =
153 webrtc::test::CalculateFrameClusters(matching_indices);
154 results.max_repeated_frames = webrtc::test::GetMaxRepeatedFrames(clusters);
155 results.max_skipped_frames = webrtc::test::GetMaxSkippedFrames(clusters);
156 results.total_skipped_frames =
157 webrtc::test::GetTotalNumberOfSkippedFrames(clusters);
158 results.decode_errors_ref = 0;
159 results.decode_errors_test = 0;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000160
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +0200161 webrtc::test::PrintAnalysisResults(absl::GetFlag(FLAGS_label), &results);
Edward Lemur2e5966b2018-01-30 15:33:02 +0100162
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +0200163 std::string chartjson_result_file =
164 absl::GetFlag(FLAGS_chartjson_result_file);
Edward Lemur2e5966b2018-01-30 15:33:02 +0100165 if (!chartjson_result_file.empty()) {
166 webrtc::test::WritePerfResults(chartjson_result_file);
167 }
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +0200168 std::string aligned_output_file = absl::GetFlag(FLAGS_aligned_output_file);
Paulina Hensmanb671d462018-09-14 11:32:00 +0200169 if (!aligned_output_file.empty()) {
Magnus Jedvertf2590782018-11-22 15:24:25 +0100170 webrtc::test::WriteVideoToFile(aligned_reference_video, aligned_output_file,
Paulina Hensmanb671d462018-09-14 11:32:00 +0200171 /*fps=*/30);
172 }
Mirko Bonadei6bafc8b2019-06-18 21:09:24 +0200173 std::string yuv_directory = absl::GetFlag(FLAGS_yuv_directory);
Paulina Hensman12c62b92018-09-28 15:14:07 +0200174 if (!yuv_directory.empty()) {
Magnus Jedvertf2590782018-11-22 15:24:25 +0100175 webrtc::test::WriteVideoToFile(aligned_reference_video,
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200176 JoinFilename(yuv_directory, "ref.yuv"),
Oleh Prypin3d3e08b2018-10-01 13:19:15 +0000177 /*fps=*/30);
Magnus Jedvertf2590782018-11-22 15:24:25 +0100178 webrtc::test::WriteVideoToFile(color_adjusted_test_video,
Paulina Hensmana6471eb2018-10-05 14:34:33 +0200179 JoinFilename(yuv_directory, "test.yuv"),
Oleh Prypin3d3e08b2018-10-01 13:19:15 +0000180 /*fps=*/30);
Paulina Hensman12c62b92018-09-28 15:14:07 +0200181 }
Edward Lemur2e5966b2018-01-30 15:33:02 +0100182
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500183 return 0;
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +0000184}