Chromium Code Reviews| 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); |
| 130 bool errors = false; | 129 int frame_offset = 0; |
| 131 | 130 |
| 132 FILE* input_file = fopen(y4m_file_name, "rb"); | 131 FILE* input_file = fopen(y4m_file_name, "rb"); |
| 133 if (input_file == NULL) { | 132 if (input_file == NULL) { |
| 134 fprintf(stderr, "Couldn't open input file for reading: %s\n", | 133 fprintf(stderr, "Couldn't open input file for reading: %s\n", |
| 135 y4m_file_name); | 134 y4m_file_name); |
| 136 return false; | 135 return false; |
| 137 } | 136 } |
| 138 | 137 |
| 139 // YUV4MPEG2, a.k.a. Y4M File format has a file header and a frame header. The | 138 // 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". | 139 // 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. | 140 char frame_header[Y4M_FILE_HEADER_MAX_SIZE]; |
| 142 if (frame_number == 0) { | 141 size_t bytes_read = |
| 143 char frame_header[Y4M_FILE_HEADER_MAX_SIZE]; | 142 fread(frame_header, 1, Y4M_FILE_HEADER_MAX_SIZE, input_file); |
| 144 size_t bytes_read = | 143 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) { |
| 145 fread(frame_header, 1, Y4M_FILE_HEADER_MAX_SIZE, input_file); | 144 fprintf(stdout, "Error while reading frame from file %s\n", |
| 146 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) { | 145 y4m_file_name); |
| 147 fprintf(stdout, "Error while reading first frame from file %s\n", | 146 fclose(input_file); |
| 148 y4m_file_name); | 147 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 } | 148 } |
| 149 std::string header_contents(frame_header); | |
| 150 std::size_t found = header_contents.find(Y4M_FRAME_DELIMITER); | |
| 151 if (found == std::string::npos) { | |
| 152 fprintf(stdout, "Corrupted Y4M header, could not find \"FRAME\" in %s\n", | |
| 153 header_contents.c_str()); | |
| 154 fclose(input_file); | |
| 155 return false; | |
| 156 } | |
| 157 frame_offset = static_cast<int>(found); | |
| 162 | 158 |
| 163 // Change stream pointer to new offset, skipping the frame header as well. | 159 // Change stream pointer to new offset, skipping the frame header as well. |
| 164 fseek(input_file, frame_offset + Y4M_FRAME_HEADER_SIZE, SEEK_SET); | 160 fseek(input_file, inital_offset + frame_offset + Y4M_FRAME_HEADER_SIZE, |
| 161 SEEK_SET); | |
| 165 | 162 |
| 166 size_t bytes_read = fread(result_frame, 1, frame_size, input_file); | 163 bytes_read = fread(result_frame, 1, frame_size, input_file); |
| 167 if (bytes_read != static_cast<size_t>(frame_size) && | 164 if (feof(input_file)) { |
|
phoglund
2016/11/28 12:37:37
Hmm, you need a fclose here though.
| |
| 168 ferror(input_file)) { | 165 return false; |
| 166 } | |
| 167 if (bytes_read != static_cast<size_t>(frame_size) && ferror(input_file)) { | |
| 169 fprintf(stdout, "Error while reading frame no %d from file %s\n", | 168 fprintf(stdout, "Error while reading frame no %d from file %s\n", |
| 170 frame_number, y4m_file_name); | 169 frame_number, y4m_file_name); |
| 171 errors = true; | 170 fclose(input_file); |
| 171 return false; | |
| 172 } | 172 } |
| 173 | 173 |
| 174 fclose(input_file); | 174 fclose(input_file); |
| 175 return !errors; | 175 return true; |
| 176 } | 176 } |
| 177 | 177 |
| 178 double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type, | 178 double CalculateMetrics(VideoAnalysisMetricsType video_metrics_type, |
| 179 const uint8_t* ref_frame, | 179 const uint8_t* ref_frame, |
| 180 const uint8_t* test_frame, | 180 const uint8_t* test_frame, |
| 181 int width, | 181 int width, |
| 182 int height) { | 182 int height) { |
| 183 if (!ref_frame || !test_frame) | 183 if (!ref_frame || !test_frame) |
| 184 return -1; | 184 return -1; |
| 185 else if (height < 0 || width < 0) | 185 else if (height < 0 || width < 0) |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 for (iter = results->frames.begin(); iter != results->frames.end() - 1; | 366 for (iter = results->frames.begin(); iter != results->frames.end() - 1; |
| 367 ++iter) { | 367 ++iter) { |
| 368 fprintf(output, "%f,", iter->ssim_value); | 368 fprintf(output, "%f,", iter->ssim_value); |
| 369 } | 369 } |
| 370 fprintf(output, "%f] score\n", iter->ssim_value); | 370 fprintf(output, "%f] score\n", iter->ssim_value); |
| 371 } | 371 } |
| 372 } | 372 } |
| 373 | 373 |
| 374 } // namespace test | 374 } // namespace test |
| 375 } // namespace webrtc | 375 } // namespace webrtc |
| OLD | NEW |