1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
113f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <stdio.h>
123f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <stdlib.h>
133f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org
14b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <map>
15cff5c03bbf63004ab85478a5660d341d3366ef63pbos@webrtc.org#include <string>
16b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#include <vector>
17b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
18cff5c03bbf63004ab85478a5660d341d3366ef63pbos@webrtc.org#include "webrtc/tools/frame_analyzer/video_quality_analysis.h"
19cff5c03bbf63004ab85478a5660d341d3366ef63pbos@webrtc.org#include "webrtc/tools/simple_command_line_parser.h"
20b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
21b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * A command line tool running PSNR and SSIM on a reference video and a test
23b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * video. The test video is a record of the reference video which can start at
24b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * an arbitrary point. It is possible that there will be repeated frames or
25b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * skipped frames as well. In order to have a way to compare corresponding
26b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * frames from the two videos, a stats file should be provided. The stats file
27b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * is a text file assumed to be in the format:
28b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * frame_xxxx yyyy
29b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * where xxxx is the frame number in the test video and yyyy is the
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * corresponding frame number in the original video.
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * The video files should be 1420 YUV videos.
323524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org * The tool prints the result to standard output in the Chromium perf format:
333524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org * RESULT <metric>:<label>= <values>
34b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
35b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * The max value for PSNR is 48.0 (between equal frames), as for SSIM it is 1.0.
36b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
37b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * Usage:
383524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org * frame_analyzer --label=<test_label> --reference_file=<name_of_file>
393524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org * --test_file=<name_of_file> --stats_file=<name_of_file> --width=<frame_width>
403524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org * --height=<frame_height>
41b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
42b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgint main(int argc, char** argv) {
43b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  std::string program_name = argv[0];
44b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  std::string usage = "Compares the output video with the initially sent video."
45b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "\nExample usage:\n" + program_name + " --stats_file=stats.txt "
46b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "--reference_file=ref.yuv --test_file=test.yuv --width=320 --height=240\n"
47b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "Command line flags:\n"
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "  - width(int): The width of the reference and test files. Default: -1\n"
49b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "  - height(int): The height of the reference and test files. "
50b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      " Default: -1\n"
513524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org      "  - label(string): The label to use for the perf output."
523524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org      " Default: MY_TEST\n"
53b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "  - stats_file(string): The full name of the file containing the stats"
54b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      " after decoding of the received YUV video. Default: stats.txt\n"
55b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "  - reference_file(string): The reference YUV file to compare against."
56b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      " Default: ref.yuv\n"
57b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      "  - test_file(string): The test YUV file to run the analysis for."
58b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      " Default: test_file.yuv\n";
59b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
60b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  webrtc::test::CommandLineParser parser;
61b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
62b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  // Init the parser and set the usage message
63b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.Init(argc, argv);
64b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetUsageMessage(usage);
65b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
66b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetFlag("width", "-1");
67b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetFlag("height", "-1");
683524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org  parser.SetFlag("label", "MY_TEST");
69b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetFlag("stats_file", "stats.txt");
70b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetFlag("reference_file", "ref.yuv");
71b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetFlag("test_file", "test.yuv");
72b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.SetFlag("help", "false");
73b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
74b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.ProcessFlags();
75b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  if (parser.GetFlag("help") == "true") {
76b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    parser.PrintUsageMessage();
77b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  }
78b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  parser.PrintEnteredFlags();
79b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
80b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  int width = strtol((parser.GetFlag("width")).c_str(), NULL, 10);
81b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  int height = strtol((parser.GetFlag("height")).c_str(), NULL, 10);
82b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
83b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  if (width <= 0 || height <= 0) {
84b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    fprintf(stderr, "Error: width or height cannot be <= 0!\n");
85b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    return -1;
86b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  }
87b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
88b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  webrtc::test::ResultsContainer results;
89b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
90b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  webrtc::test::RunAnalysis(parser.GetFlag("reference_file").c_str(),
91b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                            parser.GetFlag("test_file").c_str(),
92b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                            parser.GetFlag("stats_file").c_str(), width, height,
93b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org                            &results);
94b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
953524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org  std::string label = parser.GetFlag("label");
963524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org  webrtc::test::PrintAnalysisResults(label, &results);
973524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org  webrtc::test::PrintMaxRepeatedAndSkippedFrames(label,
983524adea7db77f23c959a87847fd318a55986aa8kjellander@webrtc.org                                                 parser.GetFlag("stats_file"));
99b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
100