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 <vector> | |
kjellander_webrtc
2016/11/28 15:02:08
sort
| |
15 #include <numeric> | |
16 | |
17 | |
18 #include "webrtc/tools/frame_analyzer/reference_less_video_analysis_lib.h" | |
19 #include "webrtc/tools/frame_analyzer/video_quality_analysis.h" | |
20 #include "webrtc/tools/simple_command_line_parser.h" | |
kjellander_webrtc
2016/11/28 15:02:08
is this used? If not, also move the dependency to
| |
21 | |
22 #define STATS_LINE_LENGTH 28 | |
23 #define PSNR_FREEZE_THRESHOLD 47 | |
24 #define SSIM_FREEZE_THRESHOLD .999 | |
25 | |
26 void get_height_width_fps(int *height, int *width, int *fps, | |
kjellander_webrtc
2016/11/28 15:02:08
Can we use https://cs.chromium.org/chromium/src/th
| |
27 const char* video_file) { | |
28 /* | |
29 * Parse file header to get height width and fpd. | |
phoglund
2016/11/25 11:08:08
Nit: fps
Actually, the first line is pretty obvio
| |
30 * File header looks like : | |
31 * YUV4MPEG2 W1280 H720 F25:1 Ip A0:0 C420mpeg2 XYSCSS=420MPEG2. | |
32 */ | |
33 char frame_header[STATS_LINE_LENGTH]; | |
34 FILE* input_file = fopen(video_file, "rb"); | |
35 | |
36 fread(frame_header, 1, STATS_LINE_LENGTH, input_file); | |
37 | |
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, int frame) { | |
phoglund
2016/11/25 11:08:08
Nit: indent
| |
58 if (psnr_per_frame[frame] >= PSNR_FREEZE_THRESHOLD | |
phoglund
2016/11/25 11:08:08
Just do
return psnr_per_frame[frame] >= PSNR_FREE
| |
59 || ssim_per_frame[frame] >= SSIM_FREEZE_THRESHOLD) | |
60 return true; | |
61 return false; | |
62 } | |
63 | |
64 std::vector<int> find_frame_clusters(std::vector<double> psnr_per_frame, | |
65 std::vector<double> ssim_per_frame) { | |
66 std::vector<int> identical_frame_clusters; | |
67 int num_frozen = 0; | |
68 int total_no_of_frames = psnr_per_frame.size(); | |
69 | |
70 for (int 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) { | |
phoglund
2016/11/25 11:08:08
Nit: indent
| |
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(std::vector<double> psnr_per_frame, | |
83 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: /usr/local/google/home/charujain/restore/ | |
kjellander_webrtc
2016/11/28 15:02:08
Please remove paths referencing your local checkou
| |
94 webrtc-checkout/src/webrtc/tools/test_3.y4m | |
95 ============================= | |
96 Total number of frames received: 74 | |
97 Total identical frames: 5 | |
98 Number of unique frames: 69 | |
99 Printing Identical Frames: | |
100 Frame Number: 29 PSNR: 48.000000 SSIM: 0.999618 | |
101 Frame Number: 39 PSNR: 48.000000 SSIM: 0.999898 | |
102 Frame Number: 60 PSNR: 48.000000 SSIM: 0.999564 | |
103 Frame Number: 64 PSNR: 48.000000 SSIM: 0.999651 | |
104 Frame Number: 69 PSNR: 48.000000 SSIM: 0.999684 | |
105 Print identical frame which appears in clusters : | |
106 1 1 1 1 1 | |
kjellander_webrtc
2016/11/28 15:02:08
Can you provide an example where the result is 1 1
| |
107 * | |
108 */ | |
109 int total_no_of_frames = psnr_per_frame.size(); | |
110 std::vector<int> identical_frame_clusters = find_frame_clusters( | |
111 psnr_per_frame, ssim_per_frame); | |
112 int total_identical_frames = std::accumulate( | |
113 identical_frame_clusters.begin(), identical_frame_clusters.end(), 0); | |
114 int unique_frames = total_no_of_frames - total_identical_frames; | |
115 | |
116 printf("Total number of frames received: %d\n", total_no_of_frames); | |
117 printf("Total identical frames: %d\n", total_identical_frames); | |
118 printf("Number of unique frames: %d\n", unique_frames); | |
119 | |
120 printf("Printing Identical Frames: \n"); | |
121 for (int frame = 0; frame < total_no_of_frames; frame++) { | |
122 if (frozen_frame(psnr_per_frame, ssim_per_frame, frame)) { | |
123 printf(" Frame Number: %d PSNR: %f SSIM: %f \n", frame, | |
124 psnr_per_frame[frame], ssim_per_frame[frame]); | |
125 } | |
126 } | |
127 | |
128 printf("Print identical frame which appears in clusters : \n"); | |
129 for (int cluster = 0; | |
130 cluster < static_cast<int>(identical_frame_clusters.size()); cluster++) | |
131 printf("%d ", identical_frame_clusters[cluster]); | |
132 printf("\n"); | |
133 } | |
134 | |
135 void compute_metrics(char video_file_name[], | |
136 std::vector<double>* psnr_per_frame, | |
137 std::vector<double>* ssim_per_frame) { | |
138 int height = 0, width = 0, fps = 0; | |
139 get_height_width_fps(&height, &width, &fps, video_file_name); | |
140 | |
141 int no_of_frames = 0; | |
142 int size = webrtc::test::GetI420FrameSize(width, height); | |
143 | |
144 // Allocate buffers for test and reference frames. | |
145 uint8_t* current_frame = new uint8_t[size]; | |
146 uint8_t* next_frame = new uint8_t[size]; | |
147 | |
148 while (true) { | |
149 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name, | |
150 width, height, | |
151 no_of_frames, | |
152 current_frame))) { | |
153 break; | |
154 } | |
155 | |
156 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name, | |
157 width, height, | |
158 no_of_frames + 1, | |
159 next_frame))) { | |
160 break; | |
161 } | |
162 | |
163 double result_psnr = webrtc::test::CalculateMetrics(webrtc::test::kPSNR, | |
164 current_frame, | |
165 next_frame, | |
166 width, height); | |
167 double result_ssim = webrtc::test::CalculateMetrics(webrtc::test::kSSIM, | |
168 current_frame, | |
169 next_frame, | |
170 width, height); | |
171 | |
172 psnr_per_frame->push_back(result_psnr); | |
173 ssim_per_frame->push_back(result_ssim); | |
174 no_of_frames++; | |
175 } | |
176 // Cleanup. | |
177 delete[] current_frame; | |
178 delete[] next_frame; | |
179 } | |
180 | |
181 void run_analysis(const char* video_file) { | |
182 FILE* video_file_ptr = fopen(video_file, "r"); | |
183 if (ferror(video_file_ptr)) { | |
184 printf("Error opening file\n"); | |
kjellander_webrtc
2016/11/28 15:02:08
print the filename here as well for easier debuggi
| |
185 return; | |
kjellander_webrtc
2016/11/28 15:02:08
Make sure the program exits with a non-zero return
| |
186 } | |
187 | |
188 char video_file_name[200]; | |
kjellander_webrtc
2016/11/28 15:02:08
200? Please use some established (platform depende
| |
189 while (fgets(video_file_name, 200, video_file_ptr)) { | |
190 strtok(video_file_name, "\n"); | |
191 | |
192 // Check for video file extension. | |
kjellander_webrtc
2016/11/28 15:02:08
I think the checks for the file should be done in
| |
193 if (std::string(video_file_name).substr(strlen(video_file_name)-3, 3) | |
194 != "y4m") { | |
195 printf("Only y4m video file format are supported. Found: %s\n", | |
196 video_file_name); | |
197 return; | |
198 } | |
kjellander_webrtc
2016/11/28 15:02:08
Can you add a check that the file is actually y4m
| |
199 std::vector<double> psnr_per_frame; | |
200 std::vector<double> ssim_per_frame; | |
201 compute_metrics(video_file_name, &psnr_per_frame, &ssim_per_frame); | |
202 | |
203 printf("=============================\n"); | |
204 printf("Printing metrics for file: %s\n", video_file_name); | |
205 printf("=============================\n"); | |
206 print_freezing_metrics(psnr_per_frame, ssim_per_frame); | |
207 } | |
208 fclose(video_file_ptr); | |
209 } | |
OLD | NEW |