xref: /aosp_15_r20/external/webrtc/rtc_tools/frame_analyzer/frame_analyzer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include <cstddef>
15 #include <string>
16 #include <vector>
17 
18 #include "absl/flags/flag.h"
19 #include "absl/flags/parse.h"
20 #include "absl/strings/match.h"
21 #include "api/scoped_refptr.h"
22 #include "api/test/metrics/chrome_perf_dashboard_metrics_exporter.h"
23 #include "api/test/metrics/global_metrics_logger_and_exporter.h"
24 #include "api/test/metrics/metrics_exporter.h"
25 #include "api/test/metrics/stdout_metrics_exporter.h"
26 #include "rtc_base/strings/string_builder.h"
27 #include "rtc_tools/frame_analyzer/video_color_aligner.h"
28 #include "rtc_tools/frame_analyzer/video_geometry_aligner.h"
29 #include "rtc_tools/frame_analyzer/video_quality_analysis.h"
30 #include "rtc_tools/frame_analyzer/video_temporal_aligner.h"
31 #include "rtc_tools/video_file_reader.h"
32 #include "rtc_tools/video_file_writer.h"
33 
34 ABSL_FLAG(int32_t, width, -1, "The width of the reference and test files");
35 ABSL_FLAG(int32_t, height, -1, "The height of the reference and test files");
36 ABSL_FLAG(std::string,
37           label,
38           "MY_TEST",
39           "The label to use for the perf output");
40 ABSL_FLAG(std::string,
41           reference_file,
42           "ref.yuv",
43           "The reference YUV file to run the analysis against");
44 ABSL_FLAG(std::string,
45           test_file,
46           "test.yuv",
47           "The test YUV file to run the analysis for");
48 ABSL_FLAG(std::string,
49           aligned_output_file,
50           "",
51           "Where to write aligned YUV/Y4M output file, f not present, no files "
52           "will be written");
53 ABSL_FLAG(std::string,
54           yuv_directory,
55           "",
56           "Where to write aligned YUV ref+test output files, if not present, "
57           "no files will be written");
58 ABSL_FLAG(std::string,
59           chartjson_result_file,
60           "",
61           "Where to store perf result in chartjson format, if not present, no "
62           "perf result will be stored");
63 
64 namespace {
65 
66 #ifdef WIN32
67 const char* const kPathDelimiter = "\\";
68 #else
69 const char* const kPathDelimiter = "/";
70 #endif
71 
JoinFilename(std::string directory,std::string filename)72 std::string JoinFilename(std::string directory, std::string filename) {
73   return directory + kPathDelimiter + filename;
74 }
75 
76 }  // namespace
77 
78 /*
79  * A command line tool running PSNR and SSIM on a reference video and a test
80  * video. The test video is a record of the reference video which can start at
81  * an arbitrary point. It is possible that there will be repeated frames or
82  * skipped frames as well. The video files should be I420 .y4m or .yuv videos.
83  * If both files are .y4m, it's not needed to specify width/height. The tool
84  * prints the result to standard output in the Chromium perf format:
85  * RESULT <metric>:<label>= <values>
86  *
87  * The max value for PSNR is 48.0 (between equal frames), as for SSIM it is 1.0.
88  *
89  * Usage:
90  * frame_analyzer --label=<test_label> --reference_file=<name_of_file>
91  * --test_file_ref=<name_of_file> --width=<frame_width> --height=<frame_height>
92  */
main(int argc,char * argv[])93 int main(int argc, char* argv[]) {
94   absl::ParseCommandLine(argc, argv);
95 
96   int width = absl::GetFlag(FLAGS_width);
97   int height = absl::GetFlag(FLAGS_height);
98   const std::string reference_file_name = absl::GetFlag(FLAGS_reference_file);
99   const std::string test_file_name = absl::GetFlag(FLAGS_test_file);
100 
101   // .yuv files require explicit resolution.
102   if ((absl::EndsWith(reference_file_name, ".yuv") ||
103        absl::EndsWith(test_file_name, ".yuv")) &&
104       (width <= 0 || height <= 0)) {
105     fprintf(stderr,
106             "Error: You need to specify width and height when using .yuv "
107             "files\n");
108     return -1;
109   }
110 
111   webrtc::test::ResultsContainer results;
112 
113   rtc::scoped_refptr<webrtc::test::Video> reference_video =
114       webrtc::test::OpenYuvOrY4mFile(reference_file_name, width, height);
115   rtc::scoped_refptr<webrtc::test::Video> test_video =
116       webrtc::test::OpenYuvOrY4mFile(test_file_name, width, height);
117 
118   if (!reference_video || !test_video) {
119     fprintf(stderr, "Error opening video files\n");
120     return 1;
121   }
122 
123   const std::vector<size_t> matching_indices =
124       webrtc::test::FindMatchingFrameIndices(reference_video, test_video);
125 
126   // Align the reference video both temporally and geometrically. I.e. align the
127   // frames to match up in order to the test video, and align a crop region of
128   // the reference video to match up to the test video.
129   const rtc::scoped_refptr<webrtc::test::Video> aligned_reference_video =
130       AdjustCropping(ReorderVideo(reference_video, matching_indices),
131                      test_video);
132 
133   // Calculate if there is any systematic color difference between the reference
134   // and test video.
135   const webrtc::test::ColorTransformationMatrix color_transformation =
136       CalculateColorTransformationMatrix(aligned_reference_video, test_video);
137 
138   char buf[256];
139   rtc::SimpleStringBuilder string_builder(buf);
140   for (int i = 0; i < 3; ++i) {
141     string_builder << "\n";
142     for (int j = 0; j < 4; ++j)
143       string_builder.AppendFormat("%6.2f ", color_transformation[i][j]);
144   }
145   printf("Adjusting test video with color transformation: %s\n",
146          string_builder.str());
147 
148   // Adjust all frames in the test video with the calculated color
149   // transformation.
150   const rtc::scoped_refptr<webrtc::test::Video> color_adjusted_test_video =
151       AdjustColors(color_transformation, test_video);
152 
153   results.frames = webrtc::test::RunAnalysis(
154       aligned_reference_video, color_adjusted_test_video, matching_indices);
155 
156   const std::vector<webrtc::test::Cluster> clusters =
157       webrtc::test::CalculateFrameClusters(matching_indices);
158   results.max_repeated_frames = webrtc::test::GetMaxRepeatedFrames(clusters);
159   results.max_skipped_frames = webrtc::test::GetMaxSkippedFrames(clusters);
160   results.total_skipped_frames =
161       webrtc::test::GetTotalNumberOfSkippedFrames(clusters);
162   results.decode_errors_ref = 0;
163   results.decode_errors_test = 0;
164 
165   webrtc::test::PrintAnalysisResults(absl::GetFlag(FLAGS_label), results,
166                                      *webrtc::test::GetGlobalMetricsLogger());
167 
168   std::vector<std::unique_ptr<webrtc::test::MetricsExporter>> exporters;
169   exporters.push_back(std::make_unique<webrtc::test::StdoutMetricsExporter>());
170   std::string chartjson_result_file =
171       absl::GetFlag(FLAGS_chartjson_result_file);
172   if (!chartjson_result_file.empty()) {
173     exporters.push_back(
174         std::make_unique<webrtc::test::ChromePerfDashboardMetricsExporter>(
175             chartjson_result_file));
176   }
177   if (!webrtc::test::ExportPerfMetric(*webrtc::test::GetGlobalMetricsLogger(),
178                                       std::move(exporters))) {
179     return 1;
180   }
181   std::string aligned_output_file = absl::GetFlag(FLAGS_aligned_output_file);
182   if (!aligned_output_file.empty()) {
183     webrtc::test::WriteVideoToFile(aligned_reference_video, aligned_output_file,
184                                    /*fps=*/30);
185   }
186   std::string yuv_directory = absl::GetFlag(FLAGS_yuv_directory);
187   if (!yuv_directory.empty()) {
188     webrtc::test::WriteVideoToFile(aligned_reference_video,
189                                    JoinFilename(yuv_directory, "ref.yuv"),
190                                    /*fps=*/30);
191     webrtc::test::WriteVideoToFile(color_adjusted_test_video,
192                                    JoinFilename(yuv_directory, "test.yuv"),
193                                    /*fps=*/30);
194   }
195 
196   return 0;
197 }
198