vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_TOOLS_FRAME_ANALYZER_VIDEO_QUALITY_ANALYSIS_H_ |
| 12 | #define RTC_TOOLS_FRAME_ANALYZER_VIDEO_QUALITY_ANALYSIS_H_ |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
mandermo | 7cebe78 | 2017-02-16 01:36:43 -0800 | [diff] [blame] | 16 | #include <utility> |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 6543206 | 2017-12-11 09:32:13 +0100 | [diff] [blame] | 18 | #include "third_party/libyuv/include/libyuv/compare.h" |
| 19 | #include "third_party/libyuv/include/libyuv/convert.h" |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
| 24 | struct AnalysisResult { |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 25 | 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.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 30 | int frame_number; |
| 31 | double psnr_value; |
| 32 | double ssim_value; |
| 33 | }; |
| 34 | |
| 35 | struct ResultsContainer { |
Henrik Kjellander | 67bcb60 | 2015-10-07 08:42:52 +0200 | [diff] [blame] | 36 | ResultsContainer(); |
| 37 | ~ResultsContainer(); |
| 38 | |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 39 | std::vector<AnalysisResult> frames; |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame^] | 40 | 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.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | enum VideoAnalysisMetricsType {kPSNR, kSSIM}; |
| 48 | |
| 49 | // 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 |
| 52 | // 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 |
mandermo | 7456817 | 2017-01-17 03:24:57 -0800 | [diff] [blame] | 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 |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 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. |
mandermo | 7456817 | 2017-01-17 03:24:57 -0800 | [diff] [blame] | 62 | // The stat files are used to compare the right frames with each other and |
| 63 | // to calculate statistics. |
| 64 | void 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, |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 70 | ResultsContainer* results); |
| 71 | |
| 72 | // 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. |
| 76 | double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 77 | const uint8_t* ref_frame, |
| 78 | const uint8_t* test_frame, |
| 79 | int width, |
| 80 | int height); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 81 | |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 82 | // 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. |
| 85 | void PrintAnalysisResults(const std::string& label, ResultsContainer* results); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 86 | |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 87 | // Similar to the above, but will print to the specified file handle. |
| 88 | void PrintAnalysisResults(FILE* output, const std::string& label, |
| 89 | ResultsContainer* results); |
| 90 | |
mandermo | 7cebe78 | 2017-02-16 01:36:43 -0800 | [diff] [blame] | 91 | // The barcode number that means that the barcode could not be decoded. |
| 92 | const int DECODE_ERROR = -1; |
| 93 | |
| 94 | // Clusters the frames in the file. First in the pair is the frame number and |
| 95 | // second is the number of frames in that cluster. So if first frame in video |
| 96 | // has number 100 and it is repeated 3 after each other, then the first entry |
| 97 | // in the returned vector has first set to 100 and second set to 3. |
| 98 | // Decode errors between two frames with same barcode, then it interprets |
| 99 | // the frame with the decode error as having the same id as the two frames |
| 100 | // around it. Eg. [400, DECODE_ERROR, DECODE_ERROR, 400] is becomes an entry |
| 101 | // in return vector with first==400 and second==4. In other cases with decode |
| 102 | // errors like [400, DECODE_ERROR, 401] becomes three entries, each with |
| 103 | // second==1 and the middle has first==DECODE_ERROR. |
| 104 | std::vector<std::pair<int, int> > CalculateFrameClusters( |
| 105 | FILE* file, |
| 106 | int* num_decode_errors); |
| 107 | |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 108 | // Calculates max repeated and skipped frames and prints them to stdout in a |
| 109 | // format that is compatible with Chromium performance numbers. |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame^] | 110 | void GetMaxRepeatedAndSkippedFrames(const std::string& stats_file_ref_name, |
| 111 | const std::string& stats_file_test_name, |
| 112 | ResultsContainer* results); |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 113 | |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 114 | // Gets the next line from an open stats file. |
| 115 | bool GetNextStatsLine(FILE* stats_file, char* line); |
| 116 | |
| 117 | // Calculates the size of a I420 frame if given the width and height. |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 118 | int GetI420FrameSize(int width, int height); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 119 | |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 120 | // Extract the sequence of the frame in the video. I.e. if line is |
| 121 | // frame_0023 0284, we will get 23. |
| 122 | int ExtractFrameSequenceNumber(std::string line); |
| 123 | |
| 124 | // Checks if there is 'Barcode error' for the given line. |
| 125 | bool IsThereBarcodeError(std::string line); |
| 126 | |
| 127 | // Extract the frame number in the reference video. I.e. if line is |
| 128 | // frame_0023 0284, we will get 284. |
| 129 | int ExtractDecodedFrameNumber(std::string line); |
| 130 | |
mcasas@webrtc.org | 6e2d012 | 2014-03-14 12:45:45 +0000 | [diff] [blame] | 131 | // Extracts an I420 frame at position frame_number from the raw YUV file. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 132 | bool ExtractFrameFromYuvFile(const char* i420_file_name, |
| 133 | int width, |
| 134 | int height, |
| 135 | int frame_number, |
| 136 | uint8_t* result_frame); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 137 | |
mcasas@webrtc.org | 6e2d012 | 2014-03-14 12:45:45 +0000 | [diff] [blame] | 138 | // Extracts an I420 frame at position frame_number from the Y4M file. The first |
| 139 | // frame has corresponded |frame_number| 0. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 140 | bool ExtractFrameFromY4mFile(const char* i420_file_name, |
| 141 | int width, |
| 142 | int height, |
| 143 | int frame_number, |
| 144 | uint8_t* result_frame); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 145 | |
| 146 | } // namespace test |
| 147 | } // namespace webrtc |
| 148 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 149 | #endif // RTC_TOOLS_FRAME_ANALYZER_VIDEO_QUALITY_ANALYSIS_H_ |