vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <cstddef> |
pbos@webrtc.org | ba7f6a8 | 2013-06-04 08:14:10 +0000 | [diff] [blame] | 15 | #include <string> |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 18 | #include "absl/flags/flag.h" |
| 19 | #include "absl/flags/parse.h" |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 20 | #include "absl/strings/match.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
Magnus Jedvert | f259078 | 2018-11-22 15:24:25 +0100 | [diff] [blame] | 22 | #include "rtc_base/strings/string_builder.h" |
Magnus Jedvert | f259078 | 2018-11-22 15:24:25 +0100 | [diff] [blame] | 23 | #include "rtc_tools/frame_analyzer/video_color_aligner.h" |
| 24 | #include "rtc_tools/frame_analyzer/video_geometry_aligner.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_tools/frame_analyzer/video_quality_analysis.h" |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 26 | #include "rtc_tools/frame_analyzer/video_temporal_aligner.h" |
Magnus Jedvert | 10e829a | 2018-09-05 10:46:18 +0200 | [diff] [blame] | 27 | #include "rtc_tools/video_file_reader.h" |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 28 | #include "rtc_tools/video_file_writer.h" |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 29 | #include "test/testsupport/perf_test.h" |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 30 | |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 31 | ABSL_FLAG(int32_t, width, -1, "The width of the reference and test files"); |
| 32 | ABSL_FLAG(int32_t, height, -1, "The height of the reference and test files"); |
| 33 | ABSL_FLAG(std::string, |
| 34 | label, |
| 35 | "MY_TEST", |
| 36 | "The label to use for the perf output"); |
| 37 | ABSL_FLAG(std::string, |
| 38 | reference_file, |
| 39 | "ref.yuv", |
| 40 | "The reference YUV file to run the analysis against"); |
| 41 | ABSL_FLAG(std::string, |
| 42 | test_file, |
| 43 | "test.yuv", |
| 44 | "The test YUV file to run the analysis for"); |
| 45 | ABSL_FLAG(std::string, |
| 46 | aligned_output_file, |
| 47 | "", |
| 48 | "Where to write aligned YUV/Y4M output file, f not present, no files " |
| 49 | "will be written"); |
| 50 | ABSL_FLAG(std::string, |
| 51 | yuv_directory, |
| 52 | "", |
| 53 | "Where to write aligned YUV ref+test output files, if not present, " |
| 54 | "no files will be written"); |
| 55 | ABSL_FLAG(std::string, |
| 56 | chartjson_result_file, |
| 57 | "", |
| 58 | "Where to store perf result in chartjson format, if not present, no " |
| 59 | "perf result will be stored"); |
| 60 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | #ifdef WIN32 |
| 64 | const char* const kPathDelimiter = "\\"; |
| 65 | #else |
| 66 | const char* const kPathDelimiter = "/"; |
| 67 | #endif |
| 68 | |
| 69 | std::string JoinFilename(std::string directory, std::string filename) { |
| 70 | return directory + kPathDelimiter + filename; |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 75 | /* |
| 76 | * A command line tool running PSNR and SSIM on a reference video and a test |
| 77 | * video. The test video is a record of the reference video which can start at |
| 78 | * an arbitrary point. It is possible that there will be repeated frames or |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 79 | * skipped frames as well. The video files should be I420 .y4m or .yuv videos. |
| 80 | * If both files are .y4m, it's not needed to specify width/height. The tool |
| 81 | * prints the result to standard output in the Chromium perf format: |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 82 | * RESULT <metric>:<label>= <values> |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 83 | * |
| 84 | * The max value for PSNR is 48.0 (between equal frames), as for SSIM it is 1.0. |
| 85 | * |
| 86 | * Usage: |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 87 | * frame_analyzer --label=<test_label> --reference_file=<name_of_file> |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 88 | * --test_file_ref=<name_of_file> --width=<frame_width> --height=<frame_height> |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 89 | */ |
Robin Raymond | 1c62ffa | 2017-12-03 16:45:56 -0500 | [diff] [blame] | 90 | int main(int argc, char* argv[]) { |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 91 | absl::ParseCommandLine(argc, argv); |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 92 | |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 93 | int width = absl::GetFlag(FLAGS_width); |
| 94 | int height = absl::GetFlag(FLAGS_height); |
| 95 | const std::string reference_file_name = absl::GetFlag(FLAGS_reference_file); |
| 96 | const std::string test_file_name = absl::GetFlag(FLAGS_test_file); |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 97 | |
| 98 | // .yuv files require explicit resolution. |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 99 | if ((absl::EndsWith(reference_file_name, ".yuv") || |
| 100 | absl::EndsWith(test_file_name, ".yuv")) && |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 101 | (width <= 0 || height <= 0)) { |
| 102 | fprintf(stderr, |
| 103 | "Error: You need to specify width and height when using .yuv " |
| 104 | "files\n"); |
Sami Kalliomäki | 0673bc9 | 2018-08-27 17:58:13 +0200 | [diff] [blame] | 105 | return -1; |
Magnus Jedvert | 404be7f | 2018-08-22 19:23:34 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Sami Kalliomäki | 0673bc9 | 2018-08-27 17:58:13 +0200 | [diff] [blame] | 108 | webrtc::test::ResultsContainer results; |
Magnus Jedvert | 9bb55fc | 2018-08-24 14:56:03 +0200 | [diff] [blame] | 109 | |
Magnus Jedvert | 10e829a | 2018-09-05 10:46:18 +0200 | [diff] [blame] | 110 | rtc::scoped_refptr<webrtc::test::Video> reference_video = |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 111 | webrtc::test::OpenYuvOrY4mFile(reference_file_name, width, height); |
Magnus Jedvert | 10e829a | 2018-09-05 10:46:18 +0200 | [diff] [blame] | 112 | rtc::scoped_refptr<webrtc::test::Video> test_video = |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 113 | webrtc::test::OpenYuvOrY4mFile(test_file_name, width, height); |
Magnus Jedvert | 10e829a | 2018-09-05 10:46:18 +0200 | [diff] [blame] | 114 | |
| 115 | if (!reference_video || !test_video) { |
| 116 | fprintf(stderr, "Error opening video files\n"); |
Oleh Prypin | a9316c9 | 2019-01-25 12:08:19 +0100 | [diff] [blame] | 117 | return 1; |
Magnus Jedvert | 10e829a | 2018-09-05 10:46:18 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 120 | const std::vector<size_t> matching_indices = |
| 121 | webrtc::test::FindMatchingFrameIndices(reference_video, test_video); |
| 122 | |
Magnus Jedvert | f259078 | 2018-11-22 15:24:25 +0100 | [diff] [blame] | 123 | // Align the reference video both temporally and geometrically. I.e. align the |
| 124 | // frames to match up in order to the test video, and align a crop region of |
| 125 | // the reference video to match up to the test video. |
| 126 | const rtc::scoped_refptr<webrtc::test::Video> aligned_reference_video = |
| 127 | AdjustCropping(ReorderVideo(reference_video, matching_indices), |
| 128 | test_video); |
| 129 | |
| 130 | // Calculate if there is any systematic color difference between the reference |
| 131 | // and test video. |
| 132 | const webrtc::test::ColorTransformationMatrix color_transformation = |
| 133 | CalculateColorTransformationMatrix(aligned_reference_video, test_video); |
| 134 | |
| 135 | char buf[256]; |
| 136 | rtc::SimpleStringBuilder string_builder(buf); |
| 137 | for (int i = 0; i < 3; ++i) { |
| 138 | string_builder << "\n"; |
| 139 | for (int j = 0; j < 4; ++j) |
| 140 | string_builder.AppendFormat("%6.2f ", color_transformation[i][j]); |
| 141 | } |
| 142 | printf("Adjusting test video with color transformation: %s\n", |
| 143 | string_builder.str()); |
| 144 | |
| 145 | // Adjust all frames in the test video with the calculated color |
| 146 | // transformation. |
| 147 | const rtc::scoped_refptr<webrtc::test::Video> color_adjusted_test_video = |
| 148 | AdjustColors(color_transformation, test_video); |
| 149 | |
| 150 | results.frames = webrtc::test::RunAnalysis( |
| 151 | aligned_reference_video, color_adjusted_test_video, matching_indices); |
Magnus Jedvert | b468ace | 2018-09-05 16:11:48 +0200 | [diff] [blame] | 152 | |
| 153 | const std::vector<webrtc::test::Cluster> clusters = |
| 154 | webrtc::test::CalculateFrameClusters(matching_indices); |
| 155 | results.max_repeated_frames = webrtc::test::GetMaxRepeatedFrames(clusters); |
| 156 | results.max_skipped_frames = webrtc::test::GetMaxSkippedFrames(clusters); |
| 157 | results.total_skipped_frames = |
| 158 | webrtc::test::GetTotalNumberOfSkippedFrames(clusters); |
| 159 | results.decode_errors_ref = 0; |
| 160 | results.decode_errors_test = 0; |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 161 | |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 162 | webrtc::test::PrintAnalysisResults(absl::GetFlag(FLAGS_label), &results); |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 163 | |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 164 | std::string chartjson_result_file = |
| 165 | absl::GetFlag(FLAGS_chartjson_result_file); |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 166 | if (!chartjson_result_file.empty()) { |
| 167 | webrtc::test::WritePerfResults(chartjson_result_file); |
| 168 | } |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 169 | std::string aligned_output_file = absl::GetFlag(FLAGS_aligned_output_file); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 170 | if (!aligned_output_file.empty()) { |
Magnus Jedvert | f259078 | 2018-11-22 15:24:25 +0100 | [diff] [blame] | 171 | webrtc::test::WriteVideoToFile(aligned_reference_video, aligned_output_file, |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 172 | /*fps=*/30); |
| 173 | } |
Mirko Bonadei | 6bafc8b | 2019-06-18 21:09:24 +0200 | [diff] [blame] | 174 | std::string yuv_directory = absl::GetFlag(FLAGS_yuv_directory); |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 175 | if (!yuv_directory.empty()) { |
Magnus Jedvert | f259078 | 2018-11-22 15:24:25 +0100 | [diff] [blame] | 176 | webrtc::test::WriteVideoToFile(aligned_reference_video, |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 177 | JoinFilename(yuv_directory, "ref.yuv"), |
Oleh Prypin | 3d3e08b | 2018-10-01 13:19:15 +0000 | [diff] [blame] | 178 | /*fps=*/30); |
Magnus Jedvert | f259078 | 2018-11-22 15:24:25 +0100 | [diff] [blame] | 179 | webrtc::test::WriteVideoToFile(color_adjusted_test_video, |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 180 | JoinFilename(yuv_directory, "test.yuv"), |
Oleh Prypin | 3d3e08b | 2018-10-01 13:19:15 +0000 | [diff] [blame] | 181 | /*fps=*/30); |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 182 | } |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 183 | |
Robin Raymond | 1c62ffa | 2017-12-03 16:45:56 -0500 | [diff] [blame] | 184 | return 0; |
vspasova@webrtc.org | f61dc9b | 2012-08-22 08:12:00 +0000 | [diff] [blame] | 185 | } |