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

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

Issue 2515253004: Added tool for reference less video analysis (Closed)
Patch Set: precision and size_t to int Created 4 years 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) 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 /* We are on Windows */
kjellander_webrtc 2016/11/30 07:52:45 This comment doesn't add any value.
26 #define strtok_r strtok_s
27 #endif
28
29 void get_height_width_fps(int *height, int *width, int *fps,
30 const char* video_file) {
kjellander_webrtc 2016/11/30 07:52:45 Please make this const std::string& video_file
charujain 2016/11/30 13:56:27 So y4m_input_open need FILE* as an argument. So I
kjellander_webrtc 2016/11/30 14:02:34 I was thinking for API consistency and ease of usi
31 // File header looks like :
32 // YUV4MPEG2 W1280 H720 F25:1 Ip A0:0 C420mpeg2 XYSCSS=420MPEG2.
33 char frame_header[STATS_LINE_LENGTH];
34 FILE* input_file = fopen(video_file, "rb");
35
36 size_t bytes_read = fread(frame_header, 1, STATS_LINE_LENGTH - 1, input_file);
37
38 frame_header[bytes_read] = '\0';
39 std::string file_header_stats[5];
40 int no_of_stats = 0;
41 char *save_ptr;
42 char *token = strtok_r(frame_header, " ", &save_ptr);
43
44 while (token != NULL) {
45 file_header_stats[no_of_stats++] = token;
46 token = strtok_r(NULL, " ", &save_ptr);
47 }
48
49 *width = std::stoi(file_header_stats[1].erase(0, 1));
50 *height = std::stoi(file_header_stats[2].erase(0, 1));
51 *fps = std::stoi(file_header_stats[3].erase(0, 1));
52
53 printf("Height: %d Width: %d fps:%d \n", *height, *width, *fps);
54 fclose(input_file);
55 }
56
57 bool frozen_frame(std::vector<double> psnr_per_frame,
58 std::vector<double> ssim_per_frame, size_t frame) {
59 if (psnr_per_frame[frame] >= PSNR_FREEZE_THRESHOLD ||
60 ssim_per_frame[frame] >= SSIM_FREEZE_THRESHOLD)
61 return true;
62 return false;
63 }
64
65 std::vector<int> find_frame_clusters(std::vector<double> psnr_per_frame,
kjellander_webrtc 2016/11/30 07:52:45 Use const std::vector<double>& for the arguments
66 std::vector<double> ssim_per_frame) {
67 std::vector<int> identical_frame_clusters;
68 int num_frozen = 0;
69 size_t total_no_of_frames = psnr_per_frame.size();
70
71 for (size_t each_frame = 0; each_frame < total_no_of_frames; each_frame++) {
72 if (frozen_frame(psnr_per_frame, ssim_per_frame, each_frame)) {
73 num_frozen++;
74 } else if (num_frozen > 0) {
75 // Not frozen anymore.
76 identical_frame_clusters.push_back(num_frozen);
kjellander_webrtc 2016/11/30 07:52:45 -2 spaces indent
kjellander_webrtc 2016/11/30 14:02:34 Please fix
77 num_frozen = 0;
78 }
79 }
80 return identical_frame_clusters;
81 }
82
83 void print_freezing_metrics(std::vector<double> psnr_per_frame,
kjellander_webrtc 2016/11/30 07:52:45 Use const std::vector<double>& for the arguments
84 std::vector<double> ssim_per_frame) {
85 /*
86 * Prints the different metrics mainly:
87 * 1) Identical frame number, PSNR and SSIM values.
88 * 2) Length of continuous frozen frames.
89 * 3) Max length of continuous freezed frames.
90 * 4) No of unique frames found.
91 * 5) Total different identical frames found.
92 *
93 * Sample output:
94 * Printing metrics for file: /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: 30 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 2 1 1 1
107 *
108 */
109 size_t 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 size_t unique_frames = total_no_of_frames - total_identical_frames;
115
116 printf("Total number of frames received: %zu\n", total_no_of_frames);
117 printf("Total identical frames: %d\n", total_identical_frames);
118 printf("Number of unique frames: %zu\n", unique_frames);
119
120 printf("Printing Identical Frames: \n");
121 for (size_t frame = 0; frame < total_no_of_frames; frame++) {
122 if (frozen_frame(psnr_per_frame, ssim_per_frame, frame)) {
123 printf(" Frame Number: %zu 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(std::string video_file_name,
kjellander_webrtc 2016/11/30 07:52:46 Please use const std::string&
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.c_str());
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.c_str(),
150 width, height,
151 no_of_frames,
152 current_frame))) {
153 break;
154 }
155
156 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name.c_str(),
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 bool check_file_extension(std::string video_file_name) {
kjellander_webrtc 2016/11/30 07:52:45 const std::string&
182 if (video_file_name.substr(video_file_name.length()-3, 3) != "y4m") {
183 printf("Only y4m video file format is supported. Given: %s",
184 video_file_name.c_str());
185 return false;
186 }
187 return true;
188 }
189
190 int run_analysis(std::string video_file) {
kjellander_webrtc 2016/11/30 07:52:45 const std::string&
191 std::vector<double> psnr_per_frame;
192 std::vector<double> ssim_per_frame;
193 if (check_file_extension(video_file)) {
194 compute_metrics(video_file, &psnr_per_frame, &ssim_per_frame);
195 } else {
196 return -1;
197 }
198 printf("=============================\n");
199 printf("Printing metrics for file: %s\n", video_file.c_str());
200 printf("=============================\n");
201 print_freezing_metrics(psnr_per_frame, ssim_per_frame);
202 return 0;
203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698