OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/tools/frame_analyzer/video_quality_analysis.h" | 11 #include "webrtc/tools/frame_analyzer/video_quality_analysis.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 | |
17 #include <string> | 16 #include <string> |
18 | 17 |
19 #define STATS_LINE_LENGTH 32 | 18 #define STATS_LINE_LENGTH 32 |
20 #define Y4M_FILE_HEADER_MAX_SIZE 200 | 19 #define Y4M_FILE_HEADER_MAX_SIZE 200 |
21 #define Y4M_FRAME_DELIMITER "FRAME" | 20 #define Y4M_FRAME_DELIMITER "FRAME" |
22 #define Y4M_FRAME_HEADER_SIZE 6 | 21 #define Y4M_FRAME_HEADER_SIZE 6 |
23 | 22 |
24 namespace webrtc { | 23 namespace webrtc { |
25 namespace test { | 24 namespace test { |
26 | 25 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 fclose(input_file); | 118 fclose(input_file); |
120 return !errors; | 119 return !errors; |
121 } | 120 } |
122 | 121 |
123 bool ExtractFrameFromY4mFile(const char* y4m_file_name, | 122 bool ExtractFrameFromY4mFile(const char* y4m_file_name, |
124 int width, | 123 int width, |
125 int height, | 124 int height, |
126 int frame_number, | 125 int frame_number, |
127 uint8_t* result_frame) { | 126 uint8_t* result_frame) { |
128 int frame_size = GetI420FrameSize(width, height); | 127 int frame_size = GetI420FrameSize(width, height); |
129 int frame_offset = frame_number * frame_size; | 128 int inital_offset = frame_number * (frame_size + Y4M_FRAME_HEADER_SIZE); |
| 129 int frame_offset = 0; |
130 bool errors = false; | 130 bool errors = false; |
131 | 131 |
132 FILE* input_file = fopen(y4m_file_name, "rb"); | 132 FILE* input_file = fopen(y4m_file_name, "rb"); |
133 if (input_file == NULL) { | 133 if (input_file == NULL) { |
134 fprintf(stderr, "Couldn't open input file for reading: %s\n", | 134 fprintf(stderr, "Couldn't open input file for reading: %s\n", |
135 y4m_file_name); | 135 y4m_file_name); |
136 return false; | 136 return false; |
137 } | 137 } |
138 | 138 |
139 // YUV4MPEG2, a.k.a. Y4M File format has a file header and a frame header. The | 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". | 140 // file header has the aspect: "YUV4MPEG2 C420 W640 H360 Ip F30:1 A1:1". |
141 // Skip the header if this is the first frame of the file. | 141 char frame_header[Y4M_FILE_HEADER_MAX_SIZE]; |
142 if (frame_number == 0) { | 142 size_t bytes_read = |
143 char frame_header[Y4M_FILE_HEADER_MAX_SIZE]; | 143 fread(frame_header, 1, Y4M_FILE_HEADER_MAX_SIZE, input_file); |
144 size_t bytes_read = | 144 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) { |
145 fread(frame_header, 1, Y4M_FILE_HEADER_MAX_SIZE, input_file); | 145 fprintf(stdout, "Error while reading first frame from file %s\n", |
146 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) { | 146 y4m_file_name); |
147 fprintf(stdout, "Error while reading first frame from file %s\n", | 147 fclose(input_file); |
148 y4m_file_name); | 148 return false; |
149 fclose(input_file); | |
150 return false; | |
151 } | |
152 std::string header_contents(frame_header); | |
153 std::size_t found = header_contents.find(Y4M_FRAME_DELIMITER); | |
154 if (found == std::string::npos) { | |
155 fprintf(stdout, "Corrupted Y4M header, could not find \"FRAME\" in %s\n", | |
156 header_contents.c_str()); | |
157 fclose(input_file); | |
158 return false; | |
159 } | |
160 frame_offset = static_cast<int>(found); | |
161 } | 149 } |
| 150 std::string header_contents(frame_header); |
| 151 std::size_t found = header_contents.find(Y4M_FRAME_DELIMITER); |
| 152 if (found == std::string::npos) { |
| 153 fprintf(stdout, "Corrupted Y4M header, could not find \"FRAME\" in %s\n", |
| 154 header_contents.c_str()); |
| 155 fclose(input_file); |
| 156 return false; |
| 157 } |
| 158 frame_offset = static_cast<int>(found); |
162 | 159 |
163 // Change stream pointer to new offset, skipping the frame header as well. | 160 // Change stream pointer to new offset, skipping the frame header as well. |
164 fseek(input_file, frame_offset + Y4M_FRAME_HEADER_SIZE, SEEK_SET); | 161 fseek(input_file, inital_offset + frame_offset + Y4M_FRAME_HEADER_SIZE, |
| 162 SEEK_SET); |
165 | 163 |
166 size_t bytes_read = fread(result_frame, 1, frame_size, input_file); | 164 bytes_read = fread(result_frame, 1, frame_size, input_file); |
167 if (bytes_read != static_cast<size_t>(frame_size) && | 165 if (bytes_read != static_cast<size_t>(frame_size)) { |
168 ferror(input_file)) { | 166 if (ferror(input_file)) { |
169 fprintf(stdout, "Error while reading frame no %d from file %s\n", | 167 fprintf(stdout, "Error while reading frame no %d from file %s\n", |
170 frame_number, y4m_file_name); | 168 frame_number, y4m_file_name); |
| 169 } |
171 errors = true; | 170 errors = true; |
172 } | 171 } |
173 | 172 |
174 fclose(input_file); | 173 fclose(input_file); |
175 return !errors; | 174 return !errors; |
176 } | 175 } |
177 | 176 |
178 double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type, | 177 double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type, |
179 const uint8_t* ref_frame, | 178 const uint8_t* ref_frame, |
180 const uint8_t* test_frame, | 179 const uint8_t* test_frame, |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 for (iter = results->frames.begin(); iter != results->frames.end() - 1; | 365 for (iter = results->frames.begin(); iter != results->frames.end() - 1; |
367 ++iter) { | 366 ++iter) { |
368 fprintf(output, "%f,", iter->ssim_value); | 367 fprintf(output, "%f,", iter->ssim_value); |
369 } | 368 } |
370 fprintf(output, "%f] score\n", iter->ssim_value); | 369 fprintf(output, "%f] score\n", iter->ssim_value); |
371 } | 370 } |
372 } | 371 } |
373 | 372 |
374 } // namespace test | 373 } // namespace test |
375 } // namespace webrtc | 374 } // namespace webrtc |
OLD | NEW |