blob: 8314e912089da652ec1f1c57d3fde18a47215668 [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
vspasova@webrtc.orgf61dc9b2012-08-22 08:12:00 +000011#include "tools/frame_analyzer/video_quality_analysis.h"
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000012
13#include <cassert>
14#include <cstdio>
15#include <cstdlib>
16#include <string>
17
18#define STATS_LINE_LENGTH 32
19
20namespace webrtc {
21namespace test {
22
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000023int GetI420FrameSize(int width, int height) {
24 int half_width = (width + 1) >> 1;
25 int half_height = (height + 1) >> 1;
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000026
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000027 int y_plane = width * height; // I420 Y plane.
28 int u_plane = half_width * half_height; // I420 U plane.
29 int v_plane = half_width * half_height; // I420 V plane.
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000030
31 return y_plane + u_plane + v_plane;
32}
33
34int ExtractFrameSequenceNumber(std::string line) {
35 int space_position = line.find(' ');
36 if (space_position == -1) {
37 return -1;
38 }
39 std::string frame = line.substr(0, space_position);
40
41 int underscore_position = frame.find('_');
42 if (underscore_position == -1) {
43 return -1;
44 }
45 std::string frame_number = frame.substr(underscore_position + 1);
46
47 return strtol(frame_number.c_str(), NULL, 10);
48}
49
50int ExtractDecodedFrameNumber(std::string line) {
51 int space_position = line.find(' ');
52 if (space_position == -1) {
53 return -1;
54 }
55 std::string decoded_number = line.substr(space_position + 1);
56
57 return strtol(decoded_number.c_str(), NULL, 10);
58}
59
60bool IsThereBarcodeError(std::string line) {
61 int barcode_error_position = line.find("Barcode error");
62 if (barcode_error_position != -1) {
63 return true;
64 }
65 return false;
66}
67
68bool GetNextStatsLine(FILE* stats_file, char* line) {
69 int chars = 0;
70 char buf = 0;
71
72 while (buf != '\n') {
73 size_t chars_read = fread(&buf, 1, 1, stats_file);
74 if (chars_read != 1 || feof(stats_file)) {
75 return false;
76 }
77 line[chars] = buf;
78 ++chars;
79 }
80 line[chars-1] = '\0'; // Strip the trailing \n and put end of string.
81 return true;
82}
83
84bool GetNextI420Frame(FILE* input_file, int width, int height,
85 uint8* result_frame) {
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000086 int frame_size = GetI420FrameSize(width, height);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000087 bool errors = false;
88
89 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +000090 if (bytes_read != static_cast<size_t>(frame_size)) {
91 // If end-of-file is reached, don't print an error.
92 if (feof(input_file)) {
93 return false;
94 }
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +000095 fprintf(stdout, "Error while reading frame from file\n");
96 errors = true;
97 }
98 return !errors;
99}
100
101bool ExtractFrameFromI420(const char* i420_file_name, int width, int height,
102 int frame_number, uint8* result_frame) {
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +0000103 int frame_size = GetI420FrameSize(width, height);
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000104 int offset = frame_number * frame_size; // Calculate offset for the frame.
105 bool errors = false;
106
107 FILE* input_file = fopen(i420_file_name, "rb");
108 if (input_file == NULL) {
109 fprintf(stderr, "Couldn't open input file for reading: %s\n",
110 i420_file_name);
111 return false;
112 }
113
114 // Change stream pointer to new offset.
115 fseek(input_file, offset, SEEK_SET);
116
117 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
vspasova@webrtc.orgac410e22012-08-27 14:57:19 +0000118 if (bytes_read != static_cast<size_t>(frame_size) &&
vspasova@webrtc.orgfd800702012-08-16 14:07:02 +0000119 ferror(input_file)) {
120 fprintf(stdout, "Error while reading frame no %d from file %s\n",
121 frame_number, i420_file_name);
122 errors = true;
123 }
124 fclose(input_file);
125 return !errors;
126}
127
128double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type,
129 const uint8* ref_frame, const uint8* test_frame,
130 int width, int height) {
131 if (!ref_frame || !test_frame)
132 return -1;
133 else if (height < 0 || width < 0)
134 return -1;
135 int half_width = (width + 1) >> 1;
136 int half_height = (height + 1) >> 1;
137 const uint8* src_y_a = ref_frame;
138 const uint8* src_u_a = src_y_a + width * height;
139 const uint8* src_v_a = src_u_a + half_width * half_height;
140 const uint8* src_y_b = test_frame;
141 const uint8* src_u_b = src_y_b + width * height;
142 const uint8* src_v_b = src_u_b + half_width * half_height;
143
144 int stride_y = width;
145 int stride_uv = half_width;
146
147 double result = 0.0;
148
149 switch (video_metrics_type) {
150 case kPSNR:
151 // In the following: stride is determined by width.
152 result = libyuv::I420Psnr(src_y_a, width, src_u_a, half_width,
153 src_v_a, half_width, src_y_b, width,
154 src_u_b, half_width, src_v_b, half_width,
155 width, height);
156 // LibYuv sets the max psnr value to 128, we restrict it to 48.
157 // In case of 0 mse in one frame, 128 can skew the results significantly.
158 result = (result > 48.0) ? 48.0 : result;
159 break;
160 case kSSIM:
161 result = libyuv::I420Ssim(src_y_a, stride_y, src_u_a, stride_uv,
162 src_v_a, stride_uv, src_y_b, stride_y,
163 src_u_b, stride_uv, src_v_b, stride_uv,
164 width, height);
165 break;
166 default:
167 assert(false);
168 }
169
170 return result;
171}
172
173void RunAnalysis(const char* reference_file_name, const char* test_file_name,
174 const char* stats_file_name, int width, int height,
175 ResultsContainer* results) {
176 int size = GetI420FrameSize(width, height);
177 FILE* stats_file = fopen(stats_file_name, "r");
178
179 // String buffer for the lines in the stats file.
180 char line[STATS_LINE_LENGTH];
181
182 // Allocate buffers for test and reference frames.
183 uint8* test_frame = new uint8[size];
184 uint8* reference_frame = new uint8[size];
185 int previous_frame_number = -1;
186
187 // While there are entries in the stats file.
188 while (GetNextStatsLine(stats_file, line)) {
189 int extracted_test_frame = ExtractFrameSequenceNumber(line);
190 int decoded_frame_number = ExtractDecodedFrameNumber(line);
191
192 // If there was problem decoding the barcode in this frame or the frame has
193 // been duplicated, continue.
194 if (IsThereBarcodeError(line) ||
195 decoded_frame_number == previous_frame_number) {
196 continue;
197 }
198
199 assert(extracted_test_frame != -1);
200 assert(decoded_frame_number != -1);
201
202 ExtractFrameFromI420(test_file_name, width, height, extracted_test_frame,
203 test_frame);
204 ExtractFrameFromI420(reference_file_name, width, height,
205 decoded_frame_number, reference_frame);
206
207 // Calculate the PSNR and SSIM.
208 double result_psnr = CalculateMetrics(kPSNR, reference_frame, test_frame,
209 width, height);
210 double result_ssim = CalculateMetrics(kSSIM, reference_frame, test_frame,
211 width, height);
212
213 previous_frame_number = decoded_frame_number;
214
215 // Fill in the result struct.
216 AnalysisResult result;
217 result.frame_number = decoded_frame_number;
218 result.psnr_value = result_psnr;
219 result.ssim_value = result_ssim;
220
221 results->frames.push_back(result);
222 }
223
224 // Cleanup.
225 fclose(stats_file);
226 delete[] test_frame;
227 delete[] reference_frame;
228}
229
230void PrintMaxRepeatedAndSkippedFrames(const char* stats_file_name) {
231 FILE* stats_file = fopen(stats_file_name, "r");
232 char line[STATS_LINE_LENGTH];
233
234 int repeated_frames = 1;
235 int max_repeated_frames = 1;
236 int max_skipped_frames = 1;
237 int previous_frame_number = -1;
238
239 while (GetNextStatsLine(stats_file, line)) {
240 int decoded_frame_number = ExtractDecodedFrameNumber(line);
241
242 if (decoded_frame_number == -1) {
243 continue;
244 }
245
246 // Calculate how many frames a cluster of repeated frames contains.
247 if (decoded_frame_number == previous_frame_number) {
248 ++repeated_frames;
249 if (repeated_frames > max_repeated_frames) {
250 max_repeated_frames = repeated_frames;
251 }
252 } else {
253 repeated_frames = 1;
254 }
255
256 // Calculate how much frames have been skipped.
257 if (decoded_frame_number != 0 && previous_frame_number != -1) {
258 int skipped_frames = decoded_frame_number - previous_frame_number - 1;
259 if (skipped_frames > max_skipped_frames) {
260 max_skipped_frames = skipped_frames;
261 }
262 }
263 previous_frame_number = decoded_frame_number;
264 }
265 fprintf(stdout, "Max_repeated:%d Max_skipped:%d\n", max_repeated_frames,
266 max_skipped_frames);
267}
268
269void PrintAnalysisResults(ResultsContainer* results) {
270 std::vector<AnalysisResult>::iterator iter;
271 int frames_counter = 0;
272
273 fprintf(stdout, "BSTATS\n");
274 for (iter = results->frames.begin(); iter != results->frames.end(); ++iter) {
275 ++frames_counter;
276 fprintf(stdout, "%f %f;", iter->psnr_value, iter->ssim_value);
277 }
278 fprintf(stdout, "ESTATS\n");
279 if (frames_counter > 0) {
280 fprintf(stdout, "Unique_frames_count:%d\n", frames_counter);
281 } else {
282 fprintf(stdout, "Unique_frames_count:undef\n");
283 }
284}
285
286} // namespace test
287} // namespace webrtc