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 <limits.h>
12#include <stdio.h>
13#include <stdlib.h>
14
15#include <map>
16#include <string>
17#include <vector>
18
19#include "webrtc/tools/frame_analyzer/video_quality_analysis.h"
20#include "webrtc/tools/simple_command_line_parser.h"
21
22#define MAX_NUM_FRAMES_PER_FILE INT_MAX
23
24void CompareFiles(const char* reference_file_name, const char* test_file_name,
25                  const char* results_file_name, int width, int height) {
26  // Check if the reference_file_name ends with "y4m".
27  bool y4m_mode = false;
28  if (std::string(reference_file_name).find("y4m") != std::string::npos){
29    y4m_mode = true;
30  }
31
32  FILE* results_file = fopen(results_file_name, "w");
33
34  int size = webrtc::test::GetI420FrameSize(width, height);
35
36  // Allocate buffers for test and reference frames.
37  uint8* test_frame = new uint8[size];
38  uint8* ref_frame = new uint8[size];
39
40  bool read_result = true;
41  for(int frame_counter = 0; frame_counter < MAX_NUM_FRAMES_PER_FILE;
42      ++frame_counter){
43    read_result &= (y4m_mode) ? webrtc::test::ExtractFrameFromY4mFile(
44        reference_file_name, width, height, frame_counter, ref_frame):
45        webrtc::test::ExtractFrameFromYuvFile(reference_file_name, width,
46                                              height, frame_counter, ref_frame);
47    read_result &=  webrtc::test::ExtractFrameFromYuvFile(test_file_name, width,
48        height, frame_counter, test_frame);
49
50    if (!read_result)
51      break;
52
53    // Calculate the PSNR and SSIM.
54    double result_psnr = webrtc::test::CalculateMetrics(
55        webrtc::test::kPSNR, ref_frame, test_frame, width, height);
56    double result_ssim = webrtc::test::CalculateMetrics(
57        webrtc::test::kSSIM, ref_frame, test_frame, width, height);
58    fprintf(results_file, "Frame: %d, PSNR: %f, SSIM: %f\n", frame_counter,
59            result_psnr, result_ssim);
60  }
61  delete[] test_frame;
62  delete[] ref_frame;
63
64  fclose(results_file);
65}
66
67/*
68 * A tool running PSNR and SSIM analysis on two videos - a reference video and a
69 * test video. The two videos should be I420 YUV videos.
70 * The tool just runs PSNR and SSIM on the corresponding frames in the test and
71 * the reference videos until either the first or the second video runs out of
72 * frames. The result is written in a results text file in the format:
73 * Frame: <frame_number>, PSNR: <psnr_value>, SSIM: <ssim_value>
74 * Frame: <frame_number>, ........
75 *
76 * The max value for PSNR is 48.0 (between equal frames), as for SSIM it is 1.0.
77 *
78 * Usage:
79 * psnr_ssim_analyzer --reference_file=<name_of_file> --test_file=<name_of_file>
80 * --results_file=<name_of_file> --width=<width_of_frames>
81 * --height=<height_of_frames>
82 */
83int main(int argc, char** argv) {
84  std::string program_name = argv[0];
85  std::string usage = "Runs PSNR and SSIM on two I420 videos and write the"
86      "results in a file.\n"
87      "Example usage:\n" + program_name + " --reference_file=ref.yuv "
88      "--test_file=test.yuv --results_file=results.txt --width=320 "
89      "--height=240\n"
90      "Command line flags:\n"
91      "  - width(int): The width of the reference and test files. Default: -1\n"
92      "  - height(int): The height of the reference and test files. "
93      " Default: -1\n"
94      "  - reference_file(string): The reference YUV file to compare against."
95      " Default: ref.yuv\n"
96      "  - test_file(string): The test YUV file to run the analysis for."
97      " Default: test_file.yuv\n"
98      "  - results_file(string): The full name of the file where the results "
99      "will be written. Default: results.txt\n";
100
101  webrtc::test::CommandLineParser parser;
102
103  // Init the parser and set the usage message
104  parser.Init(argc, argv);
105  parser.SetUsageMessage(usage);
106
107  parser.SetFlag("width", "-1");
108  parser.SetFlag("height", "-1");
109  parser.SetFlag("results_file", "results.txt");
110  parser.SetFlag("reference_file", "ref.yuv");
111  parser.SetFlag("test_file", "test.yuv");
112  parser.SetFlag("results_file", "results.txt");
113  parser.SetFlag("help", "false");
114
115  parser.ProcessFlags();
116  if (parser.GetFlag("help") == "true") {
117    parser.PrintUsageMessage();
118  }
119  parser.PrintEnteredFlags();
120
121  int width = strtol((parser.GetFlag("width")).c_str(), NULL, 10);
122  int height = strtol((parser.GetFlag("height")).c_str(), NULL, 10);
123
124  if (width <= 0 || height <= 0) {
125    fprintf(stderr, "Error: width or height cannot be <= 0!\n");
126    return -1;
127  }
128
129  CompareFiles(parser.GetFlag("reference_file").c_str(),
130               parser.GetFlag("test_file").c_str(),
131               parser.GetFlag("results_file").c_str(), width, height);
132}
133