Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(394)

Side by Side Diff: webrtc/tools/frame_analyzer/video_quality_analysis.cc

Issue 2965593002: Move webrtc/{tools => rtc_tools} (Closed)
Patch Set: Adding back root changes Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
11 #include "webrtc/tools/frame_analyzer/video_quality_analysis.h"
12
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <algorithm>
17 #include <string>
18 #include <map>
19 #include <utility>
20
21 #define STATS_LINE_LENGTH 32
22 #define Y4M_FILE_HEADER_MAX_SIZE 200
23 #define Y4M_FRAME_DELIMITER "FRAME"
24 #define Y4M_FRAME_HEADER_SIZE 6
25
26 namespace webrtc {
27 namespace test {
28
29 ResultsContainer::ResultsContainer() {}
30 ResultsContainer::~ResultsContainer() {}
31
32 int GetI420FrameSize(int width, int height) {
33 int half_width = (width + 1) >> 1;
34 int half_height = (height + 1) >> 1;
35
36 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.
39
40 return y_plane + u_plane + v_plane;
41 }
42
43 int ExtractFrameSequenceNumber(std::string line) {
44 size_t space_position = line.find(' ');
45 if (space_position == std::string::npos) {
46 return -1;
47 }
48 std::string frame = line.substr(0, space_position);
49
50 size_t underscore_position = frame.find('_');
51 if (underscore_position == std::string::npos) {
52 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
59 int ExtractDecodedFrameNumber(std::string line) {
60 size_t space_position = line.find(' ');
61 if (space_position == std::string::npos) {
62 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
69 bool IsThereBarcodeError(std::string line) {
70 size_t barcode_error_position = line.find("Barcode error");
71 if (barcode_error_position != std::string::npos) {
72 return true;
73 }
74 return false;
75 }
76
77 bool 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
93 bool ExtractFrameFromYuvFile(const char* i420_file_name,
94 int width,
95 int height,
96 int frame_number,
97 uint8_t* result_frame) {
98 int frame_size = GetI420FrameSize(width, height);
99 int offset = frame_number * frame_size; // Calculate offset for the frame.
100 bool errors = false;
101
102 FILE* input_file = fopen(i420_file_name, "rb");
103 if (input_file == NULL) {
104 fprintf(stderr, "Couldn't open input file for reading: %s\n",
105 i420_file_name);
106 return false;
107 }
108
109 // Change stream pointer to new offset.
110 fseek(input_file, offset, SEEK_SET);
111
112 size_t bytes_read = fread(result_frame, 1, frame_size, input_file);
113 if (bytes_read != static_cast<size_t>(frame_size) &&
114 ferror(input_file)) {
115 fprintf(stdout, "Error while reading frame no %d from file %s\n",
116 frame_number, i420_file_name);
117 errors = true;
118 }
119 fclose(input_file);
120 return !errors;
121 }
122
123 bool ExtractFrameFromY4mFile(const char* y4m_file_name,
124 int width,
125 int height,
126 int frame_number,
127 uint8_t* result_frame) {
128 int frame_size = GetI420FrameSize(width, height);
129 int inital_offset = frame_number * (frame_size + Y4M_FRAME_HEADER_SIZE);
130 int frame_offset = 0;
131
132 FILE* input_file = fopen(y4m_file_name, "rb");
133 if (input_file == NULL) {
134 fprintf(stderr, "Couldn't open input file for reading: %s\n",
135 y4m_file_name);
136 return false;
137 }
138
139 // YUV4MPEG2, a.k.a. Y4M File format has a file header and a frame header. The
140 // file header has the aspect: "YUV4MPEG2 C420 W640 H360 Ip F30:1 A1:1".
141 char frame_header[Y4M_FILE_HEADER_MAX_SIZE];
142 size_t bytes_read =
143 fread(frame_header, 1, Y4M_FILE_HEADER_MAX_SIZE - 1, input_file);
144 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) {
145 fprintf(stdout, "Error while reading frame from file %s\n",
146 y4m_file_name);
147 fclose(input_file);
148 return false;
149 }
150 frame_header[bytes_read] = '\0';
151 std::string header_contents(frame_header);
152 std::size_t found = header_contents.find(Y4M_FRAME_DELIMITER);
153 if (found == std::string::npos) {
154 fprintf(stdout, "Corrupted Y4M header, could not find \"FRAME\" in %s\n",
155 header_contents.c_str());
156 fclose(input_file);
157 return false;
158 }
159 frame_offset = static_cast<int>(found);
160
161 // Change stream pointer to new offset, skipping the frame header as well.
162 fseek(input_file, inital_offset + frame_offset + Y4M_FRAME_HEADER_SIZE,
163 SEEK_SET);
164
165 bytes_read = fread(result_frame, 1, frame_size, input_file);
166 if (feof(input_file)) {
167 fclose(input_file);
168 return false;
169 }
170 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) {
171 fprintf(stdout, "Error while reading frame no %d from file %s\n",
172 frame_number, y4m_file_name);
173 fclose(input_file);
174 return false;
175 }
176
177 fclose(input_file);
178 return true;
179 }
180
181 double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type,
182 const uint8_t* ref_frame,
183 const uint8_t* test_frame,
184 int width,
185 int height) {
186 if (!ref_frame || !test_frame)
187 return -1;
188 else if (height < 0 || width < 0)
189 return -1;
190 int half_width = (width + 1) >> 1;
191 int half_height = (height + 1) >> 1;
192 const uint8_t* src_y_a = ref_frame;
193 const uint8_t* src_u_a = src_y_a + width * height;
194 const uint8_t* src_v_a = src_u_a + half_width * half_height;
195 const uint8_t* src_y_b = test_frame;
196 const uint8_t* src_u_b = src_y_b + width * height;
197 const uint8_t* src_v_b = src_u_b + half_width * half_height;
198
199 int stride_y = width;
200 int stride_uv = half_width;
201
202 double result = 0.0;
203
204 switch (video_metrics_type) {
205 case kPSNR:
206 // In the following: stride is determined by width.
207 result = libyuv::I420Psnr(src_y_a, width, src_u_a, half_width,
208 src_v_a, half_width, src_y_b, width,
209 src_u_b, half_width, src_v_b, half_width,
210 width, height);
211 // LibYuv sets the max psnr value to 128, we restrict it to 48.
212 // In case of 0 mse in one frame, 128 can skew the results significantly.
213 result = (result > 48.0) ? 48.0 : result;
214 break;
215 case kSSIM:
216 result = libyuv::I420Ssim(src_y_a, stride_y, src_u_a, stride_uv,
217 src_v_a, stride_uv, src_y_b, stride_y,
218 src_u_b, stride_uv, src_v_b, stride_uv,
219 width, height);
220 break;
221 default:
222 assert(false);
223 }
224
225 return result;
226 }
227
228 void RunAnalysis(const char* reference_file_name,
229 const char* test_file_name,
230 const char* stats_file_reference_name,
231 const char* stats_file_test_name,
232 int width,
233 int height,
234 ResultsContainer* results) {
235 // Check if the reference_file_name ends with "y4m".
236 bool y4m_mode = false;
237 if (std::string(reference_file_name).find("y4m") != std::string::npos) {
238 y4m_mode = true;
239 }
240
241 int size = GetI420FrameSize(width, height);
242 FILE* stats_file_ref = fopen(stats_file_reference_name, "r");
243 FILE* stats_file_test = fopen(stats_file_test_name, "r");
244
245 // String buffer for the lines in the stats file.
246 char line[STATS_LINE_LENGTH];
247
248 // Allocate buffers for test and reference frames.
249 uint8_t* test_frame = new uint8_t[size];
250 uint8_t* reference_frame = new uint8_t[size];
251 int previous_frame_number = -1;
252
253 // Maps barcode id to the frame id for the reference video.
254 // In case two frames have same id, then we only save the first one.
255 std::map<int, int> ref_barcode_to_frame;
256 // While there are entries in the stats file.
257 while (GetNextStatsLine(stats_file_ref, line)) {
258 int extracted_ref_frame = ExtractFrameSequenceNumber(line);
259 int decoded_frame_number = ExtractDecodedFrameNumber(line);
260
261 // Insert will only add if it is not in map already.
262 ref_barcode_to_frame.insert(
263 std::make_pair(decoded_frame_number, extracted_ref_frame));
264 }
265
266 while (GetNextStatsLine(stats_file_test, line)) {
267 int extracted_test_frame = ExtractFrameSequenceNumber(line);
268 int decoded_frame_number = ExtractDecodedFrameNumber(line);
269 auto it = ref_barcode_to_frame.find(decoded_frame_number);
270 if (it == ref_barcode_to_frame.end()) {
271 // Not found in the reference video.
272 // TODO(mandermo) print
273 continue;
274 }
275 int extracted_ref_frame = it->second;
276
277 // If there was problem decoding the barcode in this frame or the frame has
278 // been duplicated, continue.
279 if (IsThereBarcodeError(line) ||
280 decoded_frame_number == previous_frame_number) {
281 continue;
282 }
283
284 assert(extracted_test_frame != -1);
285 assert(decoded_frame_number != -1);
286
287 ExtractFrameFromYuvFile(test_file_name, width, height, extracted_test_frame,
288 test_frame);
289 if (y4m_mode) {
290 ExtractFrameFromY4mFile(reference_file_name, width, height,
291 extracted_ref_frame, reference_frame);
292 } else {
293 ExtractFrameFromYuvFile(reference_file_name, width, height,
294 extracted_ref_frame, reference_frame);
295 }
296
297 // Calculate the PSNR and SSIM.
298 double result_psnr =
299 CalculateMetrics(kPSNR, reference_frame, test_frame, width, height);
300 double result_ssim =
301 CalculateMetrics(kSSIM, reference_frame, test_frame, width, height);
302
303 previous_frame_number = decoded_frame_number;
304
305 // Fill in the result struct.
306 AnalysisResult result;
307 result.frame_number = decoded_frame_number;
308 result.psnr_value = result_psnr;
309 result.ssim_value = result_ssim;
310
311 results->frames.push_back(result);
312 }
313
314 // Cleanup.
315 fclose(stats_file_ref);
316 fclose(stats_file_test);
317 delete[] test_frame;
318 delete[] reference_frame;
319 }
320
321 void PrintMaxRepeatedAndSkippedFrames(const std::string& label,
322 const std::string& stats_file_ref_name,
323 const std::string& stats_file_test_name) {
324 PrintMaxRepeatedAndSkippedFrames(stdout, label, stats_file_ref_name,
325 stats_file_test_name);
326 }
327
328 std::vector<std::pair<int, int> > CalculateFrameClusters(
329 FILE* file,
330 int* num_decode_errors) {
331 if (num_decode_errors) {
332 *num_decode_errors = 0;
333 }
334 std::vector<std::pair<int, int> > frame_cnt;
335 char line[STATS_LINE_LENGTH];
336 while (GetNextStatsLine(file, line)) {
337 int decoded_frame_number;
338 if (IsThereBarcodeError(line)) {
339 decoded_frame_number = DECODE_ERROR;
340 if (num_decode_errors) {
341 ++*num_decode_errors;
342 }
343 } else {
344 decoded_frame_number = ExtractDecodedFrameNumber(line);
345 }
346 if (frame_cnt.size() >= 2 && decoded_frame_number != DECODE_ERROR &&
347 frame_cnt.back().first == DECODE_ERROR &&
348 frame_cnt[frame_cnt.size() - 2].first == decoded_frame_number) {
349 // Handle when there is a decoding error inside a cluster of frames.
350 frame_cnt[frame_cnt.size() - 2].second += frame_cnt.back().second + 1;
351 frame_cnt.pop_back();
352 } else if (frame_cnt.empty() ||
353 frame_cnt.back().first != decoded_frame_number) {
354 frame_cnt.push_back(std::make_pair(decoded_frame_number, 1));
355 } else {
356 ++frame_cnt.back().second;
357 }
358 }
359 return frame_cnt;
360 }
361
362 void PrintMaxRepeatedAndSkippedFrames(FILE* output,
363 const std::string& label,
364 const std::string& stats_file_ref_name,
365 const std::string& stats_file_test_name) {
366 FILE* stats_file_ref = fopen(stats_file_ref_name.c_str(), "r");
367 FILE* stats_file_test = fopen(stats_file_test_name.c_str(), "r");
368 if (stats_file_ref == NULL) {
369 fprintf(stderr, "Couldn't open reference stats file for reading: %s\n",
370 stats_file_ref_name.c_str());
371 return;
372 }
373 if (stats_file_test == NULL) {
374 fprintf(stderr, "Couldn't open test stats file for reading: %s\n",
375 stats_file_test_name.c_str());
376 fclose(stats_file_ref);
377 return;
378 }
379
380 int max_repeated_frames = 1;
381 int max_skipped_frames = 0;
382
383 int decode_errors_ref = 0;
384 int decode_errors_test = 0;
385
386 std::vector<std::pair<int, int> > frame_cnt_ref =
387 CalculateFrameClusters(stats_file_ref, &decode_errors_ref);
388
389 std::vector<std::pair<int, int> > frame_cnt_test =
390 CalculateFrameClusters(stats_file_test, &decode_errors_test);
391
392 fclose(stats_file_ref);
393 fclose(stats_file_test);
394
395 auto it_ref = frame_cnt_ref.begin();
396 auto it_test = frame_cnt_test.begin();
397 auto end_ref = frame_cnt_ref.end();
398 auto end_test = frame_cnt_test.end();
399
400 if (it_test == end_test || it_ref == end_ref) {
401 fprintf(stderr, "Either test or ref file is empty, nothing to print\n");
402 return;
403 }
404
405 while (it_test != end_test && it_test->first == DECODE_ERROR) {
406 ++it_test;
407 }
408
409 if (it_test == end_test) {
410 fprintf(stderr, "Test video only has barcode decode errors\n");
411 return;
412 }
413
414 // Find the first frame in the reference video that match the first frame in
415 // the test video.
416 while (it_ref != end_ref &&
417 (it_ref->first == DECODE_ERROR || it_ref->first != it_test->first)) {
418 ++it_ref;
419 }
420 if (it_ref == end_ref) {
421 fprintf(stderr,
422 "The barcode in the test video's first frame is not in the "
423 "reference video.\n");
424 return;
425 }
426
427 int total_skipped_frames = 0;
428 for (;;) {
429 max_repeated_frames =
430 std::max(max_repeated_frames, it_test->second - it_ref->second + 1);
431
432 bool passed_error = false;
433
434 ++it_test;
435 while (it_test != end_test && it_test->first == DECODE_ERROR) {
436 ++it_test;
437 passed_error = true;
438 }
439 if (it_test == end_test) {
440 break;
441 }
442
443 int skipped_frames = 0;
444 ++it_ref;
445 for (; it_ref != end_ref; ++it_ref) {
446 if (it_ref->first != DECODE_ERROR && it_ref->first >= it_test->first) {
447 break;
448 }
449 ++skipped_frames;
450 }
451 if (passed_error) {
452 // If we pass an error in the test video, then we are conservative
453 // and will not calculate skipped frames for that part.
454 skipped_frames = 0;
455 }
456 if (it_ref != end_ref && it_ref->first == it_test->first) {
457 total_skipped_frames += skipped_frames;
458 if (skipped_frames > max_skipped_frames) {
459 max_skipped_frames = skipped_frames;
460 }
461 continue;
462 }
463 fprintf(output,
464 "Found barcode %d in test video, which is not in reference video\n",
465 it_test->first);
466 break;
467 }
468
469 fprintf(output, "RESULT Max_repeated: %s= %d\n", label.c_str(),
470 max_repeated_frames);
471 fprintf(output, "RESULT Max_skipped: %s= %d\n", label.c_str(),
472 max_skipped_frames);
473 fprintf(output, "RESULT Total_skipped: %s= %d\n", label.c_str(),
474 total_skipped_frames);
475 fprintf(output, "RESULT Decode_errors_reference: %s= %d\n", label.c_str(),
476 decode_errors_ref);
477 fprintf(output, "RESULT Decode_errors_test: %s= %d\n", label.c_str(),
478 decode_errors_test);
479 }
480
481 void PrintAnalysisResults(const std::string& label, ResultsContainer* results) {
482 PrintAnalysisResults(stdout, label, results);
483 }
484
485 void PrintAnalysisResults(FILE* output, const std::string& label,
486 ResultsContainer* results) {
487 std::vector<AnalysisResult>::iterator iter;
488
489 fprintf(output, "RESULT Unique_frames_count: %s= %u score\n", label.c_str(),
490 static_cast<unsigned int>(results->frames.size()));
491
492 if (results->frames.size() > 0u) {
493 fprintf(output, "RESULT PSNR: %s= [", label.c_str());
494 for (iter = results->frames.begin(); iter != results->frames.end() - 1;
495 ++iter) {
496 fprintf(output, "%f,", iter->psnr_value);
497 }
498 fprintf(output, "%f] dB\n", iter->psnr_value);
499
500 fprintf(output, "RESULT SSIM: %s= [", label.c_str());
501 for (iter = results->frames.begin(); iter != results->frames.end() - 1;
502 ++iter) {
503 fprintf(output, "%f,", iter->ssim_value);
504 }
505 fprintf(output, "%f] score\n", iter->ssim_value);
506 }
507 }
508
509 } // namespace test
510 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/tools/frame_analyzer/video_quality_analysis.h ('k') | webrtc/tools/frame_analyzer/video_quality_analysis_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698