OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2016 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 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 #include <iostream> | |
14 #include <numeric> | |
15 #include <vector> | |
16 | |
17 #include "webrtc/tools/frame_analyzer/reference_less_video_analysis_lib.h" | |
18 #include "webrtc/tools/frame_analyzer/video_quality_analysis.h" | |
19 | |
20 #define STATS_LINE_LENGTH 28 | |
21 #define PSNR_FREEZE_THRESHOLD 47 | |
22 #define SSIM_FREEZE_THRESHOLD .999 | |
23 | |
24 #if defined(_WIN32) || defined(_WIN64) | |
25 #define strtok_r strtok_s | |
26 #endif | |
27 | |
28 void get_height_width_fps(int *height, int *width, int *fps, | |
29 const std::string& video_file) { | |
30 // File header looks like : | |
31 // YUV4MPEG2 W1280 H720 F25:1 Ip A0:0 C420mpeg2 XYSCSS=420MPEG2. | |
32 char frame_header[STATS_LINE_LENGTH]; | |
33 FILE* input_file = fopen(video_file.c_str(), "rb"); | |
34 | |
35 size_t bytes_read = fread(frame_header, 1, STATS_LINE_LENGTH - 1, input_file); | |
36 | |
37 frame_header[bytes_read] = '\0'; | |
38 std::string file_header_stats[5]; | |
39 int no_of_stats = 0; | |
40 char *save_ptr; | |
41 char *token = strtok_r(frame_header, " ", &save_ptr); | |
42 | |
43 while (token != NULL) { | |
44 file_header_stats[no_of_stats++] = token; | |
45 token = strtok_r(NULL, " ", &save_ptr); | |
46 } | |
47 | |
48 *width = std::stoi(file_header_stats[1].erase(0, 1)); | |
49 *height = std::stoi(file_header_stats[2].erase(0, 1)); | |
50 *fps = std::stoi(file_header_stats[3].erase(0, 1)); | |
51 | |
52 printf("Height: %d Width: %d fps:%d \n", *height, *width, *fps); | |
53 fclose(input_file); | |
54 } | |
55 | |
56 bool frozen_frame(std::vector<double> psnr_per_frame, | |
57 std::vector<double> ssim_per_frame, size_t frame) { | |
58 if (psnr_per_frame[frame] >= PSNR_FREEZE_THRESHOLD || | |
59 ssim_per_frame[frame] >= SSIM_FREEZE_THRESHOLD) | |
60 return true; | |
61 return false; | |
62 } | |
63 | |
64 std::vector<int> find_frame_clusters(const std::vector<double>& psnr_per_frame, | |
65 const std::vector<double>& ssim_per_frame) { | |
66 std::vector<int> identical_frame_clusters; | |
67 int num_frozen = 0; | |
68 size_t total_no_of_frames = psnr_per_frame.size(); | |
69 | |
70 for (size_t each_frame = 0; each_frame < total_no_of_frames; each_frame++) { | |
71 if (frozen_frame(psnr_per_frame, ssim_per_frame, each_frame)) { | |
72 num_frozen++; | |
73 } else if (num_frozen > 0) { | |
74 // Not frozen anymore. | |
75 identical_frame_clusters.push_back(num_frozen); | |
76 num_frozen = 0; | |
77 } | |
78 } | |
79 return identical_frame_clusters; | |
80 } | |
81 | |
82 void print_freezing_metrics(const std::vector<double>& psnr_per_frame, | |
83 const std::vector<double>& ssim_per_frame) { | |
84 /* | |
85 * Prints the different metrics mainly: | |
86 * 1) Identical frame number, PSNR and SSIM values. | |
87 * 2) Length of continuous frozen frames. | |
88 * 3) Max length of continuous freezed frames. | |
89 * 4) No of unique frames found. | |
90 * 5) Total different identical frames found. | |
91 * | |
92 * Sample output: | |
93 * Printing metrics for file: /src/webrtc/tools/test_3.y4m | |
94 ============================= | |
95 Total number of frames received: 74 | |
96 Total identical frames: 5 | |
97 Number of unique frames: 69 | |
98 Printing Identical Frames: | |
99 Frame Number: 29 PSNR: 48.000000 SSIM: 0.999618 | |
100 Frame Number: 30 PSNR: 48.000000 SSIM: 0.999898 | |
101 Frame Number: 60 PSNR: 48.000000 SSIM: 0.999564 | |
102 Frame Number: 64 PSNR: 48.000000 SSIM: 0.999651 | |
103 Frame Number: 69 PSNR: 48.000000 SSIM: 0.999684 | |
104 Print identical frame which appears in clusters : | |
105 2 1 1 1 | |
106 * | |
107 */ | |
108 size_t total_no_of_frames = psnr_per_frame.size(); | |
109 std::vector<int> identical_frame_clusters = find_frame_clusters( | |
110 psnr_per_frame, ssim_per_frame); | |
111 int total_identical_frames = std::accumulate( | |
112 identical_frame_clusters.begin(), identical_frame_clusters.end(), 0); | |
113 size_t unique_frames = total_no_of_frames - total_identical_frames; | |
114 | |
115 printf("Total number of frames received: %zu\n", total_no_of_frames); | |
116 printf("Total identical frames: %d\n", total_identical_frames); | |
117 printf("Number of unique frames: %zu\n", unique_frames); | |
118 | |
119 printf("Printing Identical Frames: \n"); | |
120 for (size_t frame = 0; frame < total_no_of_frames; frame++) { | |
121 if (frozen_frame(psnr_per_frame, ssim_per_frame, frame)) { | |
122 printf(" Frame Number: %zu PSNR: %f SSIM: %f \n", frame, | |
123 psnr_per_frame[frame], ssim_per_frame[frame]); | |
124 } | |
125 } | |
126 | |
127 printf("Print identical frame which appears in clusters : \n"); | |
128 for (int cluster = 0; | |
129 cluster < static_cast<int>(identical_frame_clusters.size()); cluster++) | |
130 printf("%d ", identical_frame_clusters[cluster]); | |
131 printf("\n"); | |
132 } | |
133 | |
134 void compute_metrics(const std::string& video_file_name, | |
135 std::vector<double>* psnr_per_frame, | |
136 std::vector<double>* ssim_per_frame) { | |
137 int height = 0, width = 0, fps = 0; | |
138 get_height_width_fps(&height, &width, &fps, video_file_name); | |
139 | |
140 int no_of_frames = 0; | |
141 int size = webrtc::test::GetI420FrameSize(width, height); | |
142 | |
143 // Allocate buffers for test and reference frames. | |
144 uint8_t* current_frame = new uint8_t[size]; | |
145 uint8_t* next_frame = new uint8_t[size]; | |
146 | |
147 while (true) { | |
148 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name.c_str(), | |
149 width, height, | |
150 no_of_frames, | |
151 current_frame))) { | |
152 break; | |
153 } | |
154 | |
155 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name.c_str(), | |
156 width, height, | |
157 no_of_frames + 1, | |
158 next_frame))) { | |
159 break; | |
160 } | |
161 | |
162 double result_psnr = webrtc::test::CalculateMetrics(webrtc::test::kPSNR, | |
163 current_frame, | |
164 next_frame, | |
165 width, height); | |
166 double result_ssim = webrtc::test::CalculateMetrics(webrtc::test::kSSIM, | |
167 current_frame, | |
168 next_frame, | |
169 width, height); | |
170 | |
171 psnr_per_frame->push_back(result_psnr); | |
172 ssim_per_frame->push_back(result_ssim); | |
173 no_of_frames++; | |
174 } | |
175 // Cleanup. | |
176 delete[] current_frame; | |
177 delete[] next_frame; | |
178 } | |
179 | |
180 bool check_file_extension(const std::string& video_file_name) { | |
181 if (video_file_name.substr(video_file_name.length()-3, 3) != "y4m") { | |
182 printf("Only y4m video file format is supported. Given: %s\n", | |
183 video_file_name.c_str()); | |
184 return false; | |
185 } | |
186 return true; | |
187 } | |
188 | |
189 int run_analysis(const std::string& video_file) { | |
190 std::vector<double> psnr_per_frame; | |
191 std::vector<double> ssim_per_frame; | |
192 if (check_file_extension(video_file)) { | |
193 compute_metrics(video_file, &psnr_per_frame, &ssim_per_frame); | |
194 } else { | |
195 return -1; | |
196 } | |
197 printf("=============================\n"); | |
198 printf("Printing metrics for file: %s\n", video_file.c_str()); | |
199 printf("=============================\n"); | |
200 print_freezing_metrics(psnr_per_frame, ssim_per_frame); | |
201 return 0; | |
202 } | |
OLD | NEW |