blob: dca719d295b9742efcf0c2083fc33de790635b97 [file] [log] [blame]
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_TOOLS_FRAME_ANALYZER_VIDEO_QUALITY_ANALYSIS_H_
12#define RTC_TOOLS_FRAME_ANALYZER_VIDEO_QUALITY_ANALYSIS_H_
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000013
14#include <string>
mandermo7cebe782017-02-16 01:36:43 -080015#include <utility>
Yves Gerey665174f2018-06-19 15:03:05 +020016#include <vector>
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000017
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020018#include "third_party/libyuv/include/libyuv/compare.h"
19#include "third_party/libyuv/include/libyuv/convert.h"
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000020
21namespace webrtc {
22namespace test {
23
24struct AnalysisResult {
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000025 AnalysisResult() {}
26 AnalysisResult(int frame_number, double psnr_value, double ssim_value)
27 : frame_number(frame_number),
28 psnr_value(psnr_value),
29 ssim_value(ssim_value) {}
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000030 int frame_number;
31 double psnr_value;
32 double ssim_value;
33};
34
35struct ResultsContainer {
Henrik Kjellander67bcb602015-10-07 08:42:52 +020036 ResultsContainer();
37 ~ResultsContainer();
38
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000039 std::vector<AnalysisResult> frames;
Edward Lemur2e5966b2018-01-30 15:33:02 +010040 int max_repeated_frames;
41 int max_skipped_frames;
42 int total_skipped_frames;
43 int decode_errors_ref;
44 int decode_errors_test;
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000045};
46
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020047enum VideoAnalysisMetricsType { kPSNR, kSSIM };
48
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000049// A function to run the PSNR and SSIM analysis on the test file. The test file
50// comprises the frames that were captured during the quality measurement test.
51// There may be missing or duplicate frames. Also the frames start at a random
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020052// position in the original video. We should provide a statistics file along
53// with the test video. The stats file contains the connection between the
54// actual frames in the test file and their bar code number. There is one file
55// for the reference video and one for the test video. The stats file should
56// be in the form 'frame_xxxx yyyy', where xxxx is the consecutive
57// number of the frame in the test video, and yyyy is the barcode number.
58// The stats file could be produced by
59// tools/barcode_tools/barcode_decoder.py. This script decodes the barcodes
60// integrated in every video and generates the stats file. If three was some
61// problem with the decoding there would be 'Barcode error' instead of yyyy.
62// The stat files are used to compare the right frames with each other and
63// to calculate statistics.
64void RunAnalysis(const char* reference_file_name,
65 const char* test_file_name,
66 const char* stats_file_reference_name,
67 const char* stats_file_test_name,
68 int width,
69 int height,
70 ResultsContainer* results);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000071
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020072// Compute PSNR or SSIM for an I420 frame (all planes). When we are calculating
73// PSNR values, the max return value (in the case where the test and reference
74// frames are exactly the same) will be 48. In the case of SSIM the max return
75// value will be 1.
76double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type,
77 const uint8_t* ref_frame,
78 const uint8_t* test_frame,
79 int width,
80 int height);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000081
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000082// Prints the result from the analysis in Chromium performance
83// numbers compatible format to stdout. If the results object contains no frames
84// no output will be written.
85void PrintAnalysisResults(const std::string& label, ResultsContainer* results);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000086
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +000087// Similar to the above, but will print to the specified file handle.
Yves Gerey665174f2018-06-19 15:03:05 +020088void PrintAnalysisResults(FILE* output,
89 const std::string& label,
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +000090 ResultsContainer* results);
91
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020092// The barcode number that means that the barcode could not be decoded.
93const int DECODE_ERROR = -1;
mandermo7cebe782017-02-16 01:36:43 -080094
Sami Kalliomäki0673bc92018-08-27 17:58:13 +020095// Clusters the frames in the file. First in the pair is the frame number and
96// second is the number of frames in that cluster. So if first frame in video
97// has number 100 and it is repeated 3 after each other, then the first entry
98// in the returned vector has first set to 100 and second set to 3.
99// Decode errors between two frames with same barcode, then it interprets
100// the frame with the decode error as having the same id as the two frames
101// around it. Eg. [400, DECODE_ERROR, DECODE_ERROR, 400] is becomes an entry
102// in return vector with first==400 and second==4. In other cases with decode
103// errors like [400, DECODE_ERROR, 401] becomes three entries, each with
104// second==1 and the middle has first==DECODE_ERROR.
105std::vector<std::pair<int, int> > CalculateFrameClusters(
106 FILE* file,
107 int* num_decode_errors);
mandermo7cebe782017-02-16 01:36:43 -0800108
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200109// Calculates max repeated and skipped frames and prints them to stdout in a
110// format that is compatible with Chromium performance numbers.
111void GetMaxRepeatedAndSkippedFrames(const std::string& stats_file_ref_name,
112 const std::string& stats_file_test_name,
113 ResultsContainer* results);
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000114
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200115// Gets the next line from an open stats file.
116bool GetNextStatsLine(FILE* stats_file, char* line);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000117
Sami Kalliomäki0673bc92018-08-27 17:58:13 +0200118// Calculates the size of a I420 frame if given the width and height.
119int GetI420FrameSize(int width, int height);
120
121// Extract the sequence of the frame in the video. I.e. if line is
122// frame_0023 0284, we will get 23.
123int ExtractFrameSequenceNumber(std::string line);
124
125// Checks if there is 'Barcode error' for the given line.
126bool IsThereBarcodeError(std::string line);
127
128// Extract the frame number in the reference video. I.e. if line is
129// frame_0023 0284, we will get 284.
130int ExtractDecodedFrameNumber(std::string line);
131
132// Extracts an I420 frame at position frame_number from the raw YUV file.
133bool ExtractFrameFromYuvFile(const char* i420_file_name,
134 int width,
135 int height,
136 int frame_number,
137 uint8_t* result_frame);
138
139// Extracts an I420 frame at position frame_number from the Y4M file. The first
140// frame has corresponded |frame_number| 0.
141bool ExtractFrameFromY4mFile(const char* i420_file_name,
142 int width,
143 int height,
144 int frame_number,
145 uint8_t* result_frame);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000146
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000147} // namespace test
148} // namespace webrtc
149
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200150#endif // RTC_TOOLS_FRAME_ANALYZER_VIDEO_QUALITY_ANALYSIS_H_