blob: 5c707bb6e3df1cd95b6479654c7c189e33b815a8 [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
mcasas@webrtc.org6e2d0122014-03-14 12:45:45 +000020#define Y4M_FILE_HEADER_MAX_SIZE 200
21#define Y4M_FRAME_DELIMITER "FRAME"
22#define Y4M_FRAME_HEADER_SIZE 6
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000023
24namespace webrtc {
25namespace test {
26
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000027using std::string;
28
Henrik Kjellander67bcb602015-10-07 08:42:52 +020029ResultsContainer::ResultsContainer() {}
30ResultsContainer::~ResultsContainer() {}
31
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000032int GetI420FrameSize(int width, int height) {
33 int half_width = (width + 1) >> 1;
34 int half_height = (height + 1) >> 1;
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000035
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000036 int y_plane = width * height; // I420 Y plane.
37 int u_plane = half_width * half_height; // I420 U plane.
38 int v_plane = half_width * half_height; // I420 V plane.
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000039
40 return y_plane + u_plane + v_plane;
41}
42
43int ExtractFrameSequenceNumber(std::string line) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000044 size_t space_position = line.find(' ');
45 if (space_position == string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000046 return -1;
47 }
48 std::string frame = line.substr(0, space_position);
49
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000050 size_t underscore_position = frame.find('_');
51 if (underscore_position == string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000052 return -1;
53 }
54 std::string frame_number = frame.substr(underscore_position + 1);
55
56 return strtol(frame_number.c_str(), NULL, 10);
57}
58
59int ExtractDecodedFrameNumber(std::string line) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000060 size_t space_position = line.find(' ');
61 if (space_position == string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000062 return -1;
63 }
64 std::string decoded_number = line.substr(space_position + 1);
65
66 return strtol(decoded_number.c_str(), NULL, 10);
67}
68
69bool IsThereBarcodeError(std::string line) {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000070 size_t barcode_error_position = line.find("Barcode error");
71 if (barcode_error_position != string::npos) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000072 return true;
73 }
74 return false;
75}
76
77bool GetNextStatsLine(FILE* stats_file, char* line) {
78 int chars = 0;
79 char buf = 0;
80
81 while (buf != '\n') {
82 size_t chars_read = fread(&buf, 1, 1, stats_file);
83 if (chars_read != 1 || feof(stats_file)) {
84 return false;
85 }
86 line[chars] = buf;
87 ++chars;
88 }
89 line[chars-1] = '\0'; // Strip the trailing \n and put end of string.
90 return true;
91}
92
mcasas@webrtc.org6e2d0122014-03-14 12:45:45 +000093bool ExtractFrameFromYuvFile(const char* i420_file_name, int width, int height,
94 int frame_number, uint8* result_frame) {
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000095 int frame_size = GetI420FrameSize(width, height);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000096 int offset = frame_number * frame_size; // Calculate offset for the frame.
97 bool errors = false;
98
99 FILE* input_file = fopen(i420_file_name, "rb");
100 if (input_file == NULL) {
101 fprintf(stderr, "Couldn't open input file for reading: %s\n",
102 i420_file_name);
103 return false;
104 }
105
106 // Change stream pointer to new offset.
107 fseek(input_file, offset, SEEK_SET);
108
109 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +0000110 if (bytes_read != static_cast<size_t>(frame_size) &&
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000111 ferror(input_file)) {
112 fprintf(stdout, "Error while reading frame no %d from file %s\n",
113 frame_number, i420_file_name);
114 errors = true;
115 }
116 fclose(input_file);
117 return !errors;
118}
119
mcasas@webrtc.org6e2d0122014-03-14 12:45:45 +0000120bool ExtractFrameFromY4mFile(const char* y4m_file_name, int width, int height,
121 int frame_number, uint8* result_frame) {
122 int frame_size = GetI420FrameSize(width, height);
mcasas@webrtc.org4375e1a2014-03-14 12:51:02 +0000123 int frame_offset = frame_number * frame_size;
mcasas@webrtc.org6e2d0122014-03-14 12:45:45 +0000124 bool errors = false;
125
126 FILE* input_file = fopen(y4m_file_name, "rb");
127 if (input_file == NULL) {
128 fprintf(stderr, "Couldn't open input file for reading: %s\n",
129 y4m_file_name);
130 return false;
131 }
132
133 // YUV4MPEG2, a.k.a. Y4M File format has a file header and a frame header. The
134 // file header has the aspect: "YUV4MPEG2 C420 W640 H360 Ip F30:1 A1:1".
135 // Skip the header if this is the first frame of the file.
136 if (frame_number == 0) {
137 char frame_header[Y4M_FILE_HEADER_MAX_SIZE];
138 size_t bytes_read =
139 fread(frame_header, 1, Y4M_FILE_HEADER_MAX_SIZE, input_file);
140 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) {
141 fprintf(stdout, "Error while reading first frame from file %s\n",
142 y4m_file_name);
143 fclose(input_file);
144 return false;
145 }
146 std::string header_contents(frame_header);
147 std::size_t found = header_contents.find(Y4M_FRAME_DELIMITER);
148 if (found == std::string::npos) {
149 fprintf(stdout, "Corrupted Y4M header, could not find \"FRAME\" in %s\n",
150 header_contents.c_str());
151 fclose(input_file);
152 return false;
153 }
154 frame_offset = static_cast<int>(found);
155 }
156
157 // Change stream pointer to new offset, skipping the frame header as well.
158 fseek(input_file, frame_offset + Y4M_FRAME_HEADER_SIZE, SEEK_SET);
159
160 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
161 if (bytes_read != static_cast<size_t>(frame_size) &&
162 ferror(input_file)) {
163 fprintf(stdout, "Error while reading frame no %d from file %s\n",
164 frame_number, y4m_file_name);
165 errors = true;
166 }
167
168 fclose(input_file);
169 return !errors;
170}
171
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000172double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type,
173 const uint8* ref_frame, const uint8* test_frame,
174 int width, int height) {
175 if (!ref_frame || !test_frame)
176 return -1;
177 else if (height < 0 || width < 0)
178 return -1;
179 int half_width = (width + 1) >> 1;
180 int half_height = (height + 1) >> 1;
181 const uint8* src_y_a = ref_frame;
182 const uint8* src_u_a = src_y_a + width * height;
183 const uint8* src_v_a = src_u_a + half_width * half_height;
184 const uint8* src_y_b = test_frame;
185 const uint8* src_u_b = src_y_b + width * height;
186 const uint8* src_v_b = src_u_b + half_width * half_height;
187
188 int stride_y = width;
189 int stride_uv = half_width;
190
191 double result = 0.0;
192
193 switch (video_metrics_type) {
194 case kPSNR:
195 // In the following: stride is determined by width.
196 result = libyuv::I420Psnr(src_y_a, width, src_u_a, half_width,
197 src_v_a, half_width, src_y_b, width,
198 src_u_b, half_width, src_v_b, half_width,
199 width, height);
200 // LibYuv sets the max psnr value to 128, we restrict it to 48.
201 // In case of 0 mse in one frame, 128 can skew the results significantly.
202 result = (result > 48.0) ? 48.0 : result;
203 break;
204 case kSSIM:
205 result = libyuv::I420Ssim(src_y_a, stride_y, src_u_a, stride_uv,
206 src_v_a, stride_uv, src_y_b, stride_y,
207 src_u_b, stride_uv, src_v_b, stride_uv,
208 width, height);
209 break;
210 default:
211 assert(false);
212 }
213
214 return result;
215}
216
217void RunAnalysis(const char* reference_file_name, const char* test_file_name,
218 const char* stats_file_name, int width, int height,
219 ResultsContainer* results) {
mcasas@webrtc.org6e2d0122014-03-14 12:45:45 +0000220 // Check if the reference_file_name ends with "y4m".
221 bool y4m_mode = false;
222 if (std::string(reference_file_name).find("y4m") != std::string::npos){
223 y4m_mode = true;
224 }
225
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000226 int size = GetI420FrameSize(width, height);
227 FILE* stats_file = fopen(stats_file_name, "r");
228
229 // String buffer for the lines in the stats file.
230 char line[STATS_LINE_LENGTH];
231
232 // Allocate buffers for test and reference frames.
233 uint8* test_frame = new uint8[size];
234 uint8* reference_frame = new uint8[size];
235 int previous_frame_number = -1;
236
237 // While there are entries in the stats file.
238 while (GetNextStatsLine(stats_file, line)) {
239 int extracted_test_frame = ExtractFrameSequenceNumber(line);
240 int decoded_frame_number = ExtractDecodedFrameNumber(line);
241
242 // If there was problem decoding the barcode in this frame or the frame has
243 // been duplicated, continue.
244 if (IsThereBarcodeError(line) ||
245 decoded_frame_number == previous_frame_number) {
246 continue;
247 }
248
249 assert(extracted_test_frame != -1);
250 assert(decoded_frame_number != -1);
251
mcasas@webrtc.org6e2d0122014-03-14 12:45:45 +0000252 ExtractFrameFromYuvFile(test_file_name, width, height, extracted_test_frame,
253 test_frame);
254 if (y4m_mode) {
255 ExtractFrameFromY4mFile(reference_file_name, width, height,
256 decoded_frame_number, reference_frame);
257 } else {
258 ExtractFrameFromYuvFile(reference_file_name, width, height,
259 decoded_frame_number, reference_frame);
260 }
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000261
262 // Calculate the PSNR and SSIM.
263 double result_psnr = CalculateMetrics(kPSNR, reference_frame, test_frame,
264 width, height);
265 double result_ssim = CalculateMetrics(kSSIM, reference_frame, test_frame,
266 width, height);
267
268 previous_frame_number = decoded_frame_number;
269
270 // Fill in the result struct.
271 AnalysisResult result;
272 result.frame_number = decoded_frame_number;
273 result.psnr_value = result_psnr;
274 result.ssim_value = result_ssim;
275
276 results->frames.push_back(result);
277 }
278
279 // Cleanup.
280 fclose(stats_file);
281 delete[] test_frame;
282 delete[] reference_frame;
283}
284
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000285void PrintMaxRepeatedAndSkippedFrames(const std::string& label,
286 const std::string& stats_file_name) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000287 PrintMaxRepeatedAndSkippedFrames(stdout, label, stats_file_name);
288}
289
290void PrintMaxRepeatedAndSkippedFrames(FILE* output, const std::string& label,
291 const std::string& stats_file_name) {
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000292 FILE* stats_file = fopen(stats_file_name.c_str(), "r");
293 if (stats_file == NULL) {
294 fprintf(stderr, "Couldn't open stats file for reading: %s\n",
295 stats_file_name.c_str());
296 return;
297 }
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000298 char line[STATS_LINE_LENGTH];
299
300 int repeated_frames = 1;
301 int max_repeated_frames = 1;
302 int max_skipped_frames = 1;
303 int previous_frame_number = -1;
304
305 while (GetNextStatsLine(stats_file, line)) {
306 int decoded_frame_number = ExtractDecodedFrameNumber(line);
307
308 if (decoded_frame_number == -1) {
309 continue;
310 }
311
312 // Calculate how many frames a cluster of repeated frames contains.
313 if (decoded_frame_number == previous_frame_number) {
314 ++repeated_frames;
315 if (repeated_frames > max_repeated_frames) {
316 max_repeated_frames = repeated_frames;
317 }
318 } else {
319 repeated_frames = 1;
320 }
321
322 // Calculate how much frames have been skipped.
323 if (decoded_frame_number != 0 && previous_frame_number != -1) {
324 int skipped_frames = decoded_frame_number - previous_frame_number - 1;
325 if (skipped_frames > max_skipped_frames) {
326 max_skipped_frames = skipped_frames;
327 }
328 }
329 previous_frame_number = decoded_frame_number;
330 }
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000331 fprintf(output, "RESULT Max_repeated: %s= %d\n", label.c_str(),
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000332 max_repeated_frames);
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000333 fprintf(output, "RESULT Max_skipped: %s= %d\n", label.c_str(),
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000334 max_skipped_frames);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000335 fclose(stats_file);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000336}
337
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000338void PrintAnalysisResults(const std::string& label, ResultsContainer* results) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000339 PrintAnalysisResults(stdout, label, results);
340}
341
342void PrintAnalysisResults(FILE* output, const std::string& label,
343 ResultsContainer* results) {
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000344 std::vector<AnalysisResult>::iterator iter;
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000345
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000346 fprintf(output, "RESULT Unique_frames_count: %s= %u\n", label.c_str(),
pbos@webrtc.org319c98d2013-09-10 15:23:50 +0000347 static_cast<unsigned int>(results->frames.size()));
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000348
349 if (results->frames.size() > 0u) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000350 fprintf(output, "RESULT PSNR: %s= [", label.c_str());
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000351 for (iter = results->frames.begin(); iter != results->frames.end() - 1;
352 ++iter) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000353 fprintf(output, "%f,", iter->psnr_value);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000354 }
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000355 fprintf(output, "%f] dB\n", iter->psnr_value);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000356
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000357 fprintf(output, "RESULT SSIM: %s= [", label.c_str());
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000358 for (iter = results->frames.begin(); iter != results->frames.end() - 1;
359 ++iter) {
kjellander@webrtc.orge2df8b72013-11-03 18:34:51 +0000360 fprintf(output, "%f,", iter->ssim_value);
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000361 }
kjellander@webrtc.org22c2f052015-01-28 13:52:08 +0000362 fprintf(output, "%f] score\n", iter->ssim_value);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000363 }
364}
365
366} // namespace test
367} // namespace webrtc