blob: b8c90b10718eefda9e0553308828476adc1965f0 [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
pbos@webrtc.orgba7f6a82013-06-04 08:14:10 +000011#include "webrtc/tools/frame_analyzer/video_quality_analysis.h"
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14#include <stdio.h>
15#include <stdlib.h>
16
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000017#include <string>
18
19#define STATS_LINE_LENGTH 32
20
21namespace webrtc {
22namespace test {
23
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000024using std::string;
25
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000026int GetI420FrameSize(int width, int height) {
27 int half_width = (width + 1) >> 1;
28 int half_height = (height + 1) >> 1;
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000029
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000030 int y_plane = width * height; // I420 Y plane.
31 int u_plane = half_width * half_height; // I420 U plane.
32 int v_plane = half_width * half_height; // I420 V plane.
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000033
34 return y_plane + u_plane + v_plane;
35}
36
37int ExtractFrameSequenceNumber(std::string line) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000038 size_t space_position = line.find(' ');
39 if (space_position == string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000040 return -1;
41 }
42 std::string frame = line.substr(0, space_position);
43
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000044 size_t underscore_position = frame.find('_');
45 if (underscore_position == string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000046 return -1;
47 }
48 std::string frame_number = frame.substr(underscore_position + 1);
49
50 return strtol(frame_number.c_str(), NULL, 10);
51}
52
53int ExtractDecodedFrameNumber(std::string line) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000054 size_t space_position = line.find(' ');
55 if (space_position == string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000056 return -1;
57 }
58 std::string decoded_number = line.substr(space_position + 1);
59
60 return strtol(decoded_number.c_str(), NULL, 10);
61}
62
63bool IsThereBarcodeError(std::string line) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000064 size_t barcode_error_position = line.find("Barcode error");
65 if (barcode_error_position != string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000066 return true;
67 }
68 return false;
69}
70
71bool GetNextStatsLine(FILE* stats_file, char* line) {
72 int chars = 0;
73 char buf = 0;
74
75 while (buf != '\n') {
76 size_t chars_read = fread(&buf, 1, 1, stats_file);
77 if (chars_read != 1 || feof(stats_file)) {
78 return false;
79 }
80 line[chars] = buf;
81 ++chars;
82 }
83 line[chars-1] = '\0'; // Strip the trailing \n and put end of string.
84 return true;
85}
86
87bool GetNextI420Frame(FILE* input_file, int width, int height,
88 uint8* result_frame) {
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000089 int frame_size = GetI420FrameSize(width, height);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000090 bool errors = false;
91
92 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000093 if (bytes_read != static_cast<size_t>(frame_size)) {
94 // If end-of-file is reached, don't print an error.
95 if (feof(input_file)) {
96 return false;
97 }
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000098 fprintf(stdout, "Error while reading frame from file\n");
99 errors = true;
100 }
101 return !errors;
102}
103
104bool ExtractFrameFromI420(const char* i420_file_name, int width, int height,
105 int frame_number, uint8* result_frame) {
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +0000106 int frame_size = GetI420FrameSize(width, height);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000107 int offset = frame_number * frame_size; // Calculate offset for the frame.
108 bool errors = false;
109
110 FILE* input_file = fopen(i420_file_name, "rb");
111 if (input_file == NULL) {
112 fprintf(stderr, "Couldn't open input file for reading: %s\n",
113 i420_file_name);
114 return false;
115 }
116
117 // Change stream pointer to new offset.
118 fseek(input_file, offset, SEEK_SET);
119
120 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +0000121 if (bytes_read != static_cast<size_t>(frame_size) &&
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000122 ferror(input_file)) {
123 fprintf(stdout, "Error while reading frame no %d from file %s\n",
124 frame_number, i420_file_name);
125 errors = true;
126 }
127 fclose(input_file);
128 return !errors;
129}
130
131double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type,
132 const uint8* ref_frame, const uint8* test_frame,
133 int width, int height) {
134 if (!ref_frame || !test_frame)
135 return -1;
136 else if (height < 0 || width < 0)
137 return -1;
138 int half_width = (width + 1) >> 1;
139 int half_height = (height + 1) >> 1;
140 const uint8* src_y_a = ref_frame;
141 const uint8* src_u_a = src_y_a + width * height;
142 const uint8* src_v_a = src_u_a + half_width * half_height;
143 const uint8* src_y_b = test_frame;
144 const uint8* src_u_b = src_y_b + width * height;
145 const uint8* src_v_b = src_u_b + half_width * half_height;
146
147 int stride_y = width;
148 int stride_uv = half_width;
149
150 double result = 0.0;
151
152 switch (video_metrics_type) {
153 case kPSNR:
154 // In the following: stride is determined by width.
155 result = libyuv::I420Psnr(src_y_a, width, src_u_a, half_width,
156 src_v_a, half_width, src_y_b, width,
157 src_u_b, half_width, src_v_b, half_width,
158 width, height);
159 // LibYuv sets the max psnr value to 128, we restrict it to 48.
160 // In case of 0 mse in one frame, 128 can skew the results significantly.
161 result = (result > 48.0) ? 48.0 : result;
162 break;
163 case kSSIM:
164 result = libyuv::I420Ssim(src_y_a, stride_y, src_u_a, stride_uv,
165 src_v_a, stride_uv, src_y_b, stride_y,
166 src_u_b, stride_uv, src_v_b, stride_uv,
167 width, height);
168 break;
169 default:
170 assert(false);
171 }
172
173 return result;
174}
175
176void RunAnalysis(const char* reference_file_name, const char* test_file_name,
177 const char* stats_file_name, int width, int height,
178 ResultsContainer* results) {
179 int size = GetI420FrameSize(width, height);
180 FILE* stats_file = fopen(stats_file_name, "r");
181
182 // String buffer for the lines in the stats file.
183 char line[STATS_LINE_LENGTH];
184
185 // Allocate buffers for test and reference frames.
186 uint8* test_frame = new uint8[size];
187 uint8* reference_frame = new uint8[size];
188 int previous_frame_number = -1;
189
190 // While there are entries in the stats file.
191 while (GetNextStatsLine(stats_file, line)) {
192 int extracted_test_frame = ExtractFrameSequenceNumber(line);
193 int decoded_frame_number = ExtractDecodedFrameNumber(line);
194
195 // If there was problem decoding the barcode in this frame or the frame has
196 // been duplicated, continue.
197 if (IsThereBarcodeError(line) ||
198 decoded_frame_number == previous_frame_number) {
199 continue;
200 }
201
202 assert(extracted_test_frame != -1);
203 assert(decoded_frame_number != -1);
204
205 ExtractFrameFromI420(test_file_name, width, height, extracted_test_frame,
206 test_frame);
207 ExtractFrameFromI420(reference_file_name, width, height,
208 decoded_frame_number, reference_frame);
209
210 // Calculate the PSNR and SSIM.
211 double result_psnr = CalculateMetrics(kPSNR, reference_frame, test_frame,
212 width, height);
213 double result_ssim = CalculateMetrics(kSSIM, reference_frame, test_frame,
214 width, height);
215
216 previous_frame_number = decoded_frame_number;
217
218 // Fill in the result struct.
219 AnalysisResult result;
220 result.frame_number = decoded_frame_number;
221 result.psnr_value = result_psnr;
222 result.ssim_value = result_ssim;
223
224 results->frames.push_back(result);
225 }
226
227 // Cleanup.
228 fclose(stats_file);
229 delete[] test_frame;
230 delete[] reference_frame;
231}
232
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000233void PrintMaxRepeatedAndSkippedFrames(const std::string& label,
234 const std::string& stats_file_name) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000235 PrintMaxRepeatedAndSkippedFrames(stdout, label, stats_file_name);
236}
237
238void PrintMaxRepeatedAndSkippedFrames(FILE* output, const std::string& label,
239 const std::string& stats_file_name) {
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000240 FILE* stats_file = fopen(stats_file_name.c_str(), "r");
241 if (stats_file == NULL) {
242 fprintf(stderr, "Couldn't open stats file for reading: %s\n",
243 stats_file_name.c_str());
244 return;
245 }
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000246 char line[STATS_LINE_LENGTH];
247
248 int repeated_frames = 1;
249 int max_repeated_frames = 1;
250 int max_skipped_frames = 1;
251 int previous_frame_number = -1;
252
253 while (GetNextStatsLine(stats_file, line)) {
254 int decoded_frame_number = ExtractDecodedFrameNumber(line);
255
256 if (decoded_frame_number == -1) {
257 continue;
258 }
259
260 // Calculate how many frames a cluster of repeated frames contains.
261 if (decoded_frame_number == previous_frame_number) {
262 ++repeated_frames;
263 if (repeated_frames > max_repeated_frames) {
264 max_repeated_frames = repeated_frames;
265 }
266 } else {
267 repeated_frames = 1;
268 }
269
270 // Calculate how much frames have been skipped.
271 if (decoded_frame_number != 0 && previous_frame_number != -1) {
272 int skipped_frames = decoded_frame_number - previous_frame_number - 1;
273 if (skipped_frames > max_skipped_frames) {
274 max_skipped_frames = skipped_frames;
275 }
276 }
277 previous_frame_number = decoded_frame_number;
278 }
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000279 fprintf(output, "RESULT Max_repeated: %s= %d\n", label.c_str(),
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000280 max_repeated_frames);
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000281 fprintf(output, "RESULT Max_skipped: %s= %d\n", label.c_str(),
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000282 max_skipped_frames);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000283 fclose(stats_file);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000284}
285
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000286void PrintAnalysisResults(const std::string& label, ResultsContainer* results) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000287 PrintAnalysisResults(stdout, label, results);
288}
289
290void PrintAnalysisResults(FILE* output, const std::string& label,
291 ResultsContainer* results) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000292 std::vector<AnalysisResult>::iterator iter;
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000293
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000294 fprintf(output, "RESULT Unique_frames_count: %s= %u\n", label.c_str(),
pbos@webrtc.org319c98d2013-09-10 15:23:50 +0000295 static_cast<unsigned int>(results->frames.size()));
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000296
297 if (results->frames.size() > 0u) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000298 fprintf(output, "RESULT PSNR: %s= [", label.c_str());
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000299 for (iter = results->frames.begin(); iter != results->frames.end() - 1;
300 ++iter) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000301 fprintf(output, "%f,", iter->psnr_value);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000302 }
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000303 fprintf(output, "%f] dB\n", iter->psnr_value);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000304
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000305 fprintf(output, "RESULT SSIM: %s= [", label.c_str());
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000306 for (iter = results->frames.begin(); iter != results->frames.end() - 1;
307 ++iter) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000308 fprintf(output, "%f,", iter->ssim_value);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000309 }
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000310 fprintf(output, "%f]\n", iter->ssim_value);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000311 }
312}
313
314} // namespace test
315} // namespace webrtc