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 | |
pbos@webrtc.org | ba7f6a8 | 2013-06-04 08:14:10 +0000 | [diff] [blame] | 11 | #include "webrtc/tools/frame_analyzer/video_quality_analysis.h" |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 17 | #include <string> |
| 18 | |
| 19 | #define STATS_LINE_LENGTH 32 |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 24 | using std::string; |
| 25 | |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 26 | int GetI420FrameSize(int width, int height) { |
| 27 | int half_width = (width + 1) >> 1; |
| 28 | int half_height = (height + 1) >> 1; |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 29 | |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 30 | 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.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 33 | |
| 34 | return y_plane + u_plane + v_plane; |
| 35 | } |
| 36 | |
| 37 | int ExtractFrameSequenceNumber(std::string line) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 38 | size_t space_position = line.find(' '); |
| 39 | if (space_position == string::npos) { |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 40 | return -1; |
| 41 | } |
| 42 | std::string frame = line.substr(0, space_position); |
| 43 | |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 44 | size_t underscore_position = frame.find('_'); |
| 45 | if (underscore_position == string::npos) { |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 46 | 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 | |
| 53 | int ExtractDecodedFrameNumber(std::string line) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 54 | size_t space_position = line.find(' '); |
| 55 | if (space_position == string::npos) { |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 56 | 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 | |
| 63 | bool IsThereBarcodeError(std::string line) { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 64 | size_t barcode_error_position = line.find("Barcode error"); |
| 65 | if (barcode_error_position != string::npos) { |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | bool 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 | |
| 87 | bool GetNextI420Frame(FILE* input_file, int width, int height, |
| 88 | uint8* result_frame) { |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 89 | int frame_size = GetI420FrameSize(width, height); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 90 | bool errors = false; |
| 91 | |
| 92 | size_t bytes_read = fread(result_frame, 1, frame_size, input_file); |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 93 | 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.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 98 | fprintf(stdout, "Error while reading frame from file\n"); |
| 99 | errors = true; |
| 100 | } |
| 101 | return !errors; |
| 102 | } |
| 103 | |
| 104 | bool ExtractFrameFromI420(const char* i420_file_name, int width, int height, |
| 105 | int frame_number, uint8* result_frame) { |
vspasova@webrtc.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 106 | int frame_size = GetI420FrameSize(width, height); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 107 | 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.org | ac410e2 | 2012-08-27 14:57:19 +0000 | [diff] [blame] | 121 | if (bytes_read != static_cast<size_t>(frame_size) && |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 122 | 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 | |
| 131 | double 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 | |
| 176 | void 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.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 233 | void PrintMaxRepeatedAndSkippedFrames(const std::string& label, |
| 234 | const std::string& stats_file_name) { |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 235 | PrintMaxRepeatedAndSkippedFrames(stdout, label, stats_file_name); |
| 236 | } |
| 237 | |
| 238 | void PrintMaxRepeatedAndSkippedFrames(FILE* output, const std::string& label, |
| 239 | const std::string& stats_file_name) { |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 240 | 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.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 246 | 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.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 279 | fprintf(output, "RESULT Max_repeated: %s= %d\n", label.c_str(), |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 280 | max_repeated_frames); |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 281 | fprintf(output, "RESULT Max_skipped: %s= %d\n", label.c_str(), |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 282 | max_skipped_frames); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 283 | fclose(stats_file); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 284 | } |
| 285 | |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 286 | void PrintAnalysisResults(const std::string& label, ResultsContainer* results) { |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 287 | PrintAnalysisResults(stdout, label, results); |
| 288 | } |
| 289 | |
| 290 | void PrintAnalysisResults(FILE* output, const std::string& label, |
| 291 | ResultsContainer* results) { |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 292 | std::vector<AnalysisResult>::iterator iter; |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 293 | |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 294 | fprintf(output, "RESULT Unique_frames_count: %s= %u\n", label.c_str(), |
pbos@webrtc.org | 319c98d | 2013-09-10 15:23:50 +0000 | [diff] [blame] | 295 | static_cast<unsigned int>(results->frames.size())); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 296 | |
| 297 | if (results->frames.size() > 0u) { |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 298 | fprintf(output, "RESULT PSNR: %s= [", label.c_str()); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 299 | for (iter = results->frames.begin(); iter != results->frames.end() - 1; |
| 300 | ++iter) { |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 301 | fprintf(output, "%f,", iter->psnr_value); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 302 | } |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 303 | fprintf(output, "%f] dB\n", iter->psnr_value); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 304 | |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 305 | fprintf(output, "RESULT SSIM: %s= [", label.c_str()); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 306 | for (iter = results->frames.begin(); iter != results->frames.end() - 1; |
| 307 | ++iter) { |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 308 | fprintf(output, "%f,", iter->ssim_value); |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 309 | } |
kjellander@webrtc.org | e2df8b7 | 2013-11-03 18:34:51 +0000 | [diff] [blame] | 310 | fprintf(output, "%f]\n", iter->ssim_value); |
vspasova@webrtc.org | fd80070 | 2012-08-16 14:07:02 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | } // namespace test |
| 315 | } // namespace webrtc |