OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 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/test/testsupport/frame_reader.h" | 11 #include "webrtc/test/testsupport/frame_reader.h" |
12 | 12 |
13 #include <assert.h> | |
14 | |
15 #include "webrtc/api/video/i420_buffer.h" | 13 #include "webrtc/api/video/i420_buffer.h" |
16 #include "webrtc/test/frame_utils.h" | 14 #include "webrtc/test/frame_utils.h" |
17 #include "webrtc/test/testsupport/fileutils.h" | 15 #include "webrtc/test/testsupport/fileutils.h" |
18 | 16 |
19 namespace webrtc { | 17 namespace webrtc { |
20 namespace test { | 18 namespace test { |
21 | 19 |
22 FrameReaderImpl::FrameReaderImpl(std::string input_filename, | 20 YuvFrameReaderImpl::YuvFrameReaderImpl(std::string input_filename, |
23 int width, int height) | 21 int width, |
| 22 int height) |
24 : input_filename_(input_filename), | 23 : input_filename_(input_filename), |
25 width_(width), height_(height), | 24 frame_length_in_bytes_(0), |
26 input_file_(NULL) { | 25 width_(width), |
27 } | 26 height_(height), |
| 27 number_of_frames_(-1), |
| 28 input_file_(nullptr) {} |
28 | 29 |
29 FrameReaderImpl::~FrameReaderImpl() { | 30 YuvFrameReaderImpl::~YuvFrameReaderImpl() { |
30 Close(); | 31 Close(); |
31 } | 32 } |
32 | 33 |
33 bool FrameReaderImpl::Init() { | 34 bool YuvFrameReaderImpl::Init() { |
34 if (width_ <= 0 || height_ <= 0) { | 35 if (width_ <= 0 || height_ <= 0) { |
35 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", | 36 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", width_, |
36 width_, height_); | 37 height_); |
37 return false; | 38 return false; |
38 } | 39 } |
39 frame_length_in_bytes_ = | 40 frame_length_in_bytes_ = |
40 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2); | 41 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2); |
41 | 42 |
42 input_file_ = fopen(input_filename_.c_str(), "rb"); | 43 input_file_ = fopen(input_filename_.c_str(), "rb"); |
43 if (input_file_ == NULL) { | 44 if (input_file_ == nullptr) { |
44 fprintf(stderr, "Couldn't open input file for reading: %s\n", | 45 fprintf(stderr, "Couldn't open input file for reading: %s\n", |
45 input_filename_.c_str()); | 46 input_filename_.c_str()); |
46 return false; | 47 return false; |
47 } | 48 } |
48 // Calculate total number of frames. | 49 // Calculate total number of frames. |
49 size_t source_file_size = GetFileSize(input_filename_); | 50 size_t source_file_size = GetFileSize(input_filename_); |
50 if (source_file_size <= 0u) { | 51 if (source_file_size <= 0u) { |
51 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str()); | 52 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str()); |
52 return false; | 53 return false; |
53 } | 54 } |
54 number_of_frames_ = static_cast<int>(source_file_size / | 55 number_of_frames_ = |
55 frame_length_in_bytes_); | 56 static_cast<int>(source_file_size / frame_length_in_bytes_); |
56 return true; | 57 return true; |
57 } | 58 } |
58 | 59 |
59 void FrameReaderImpl::Close() { | 60 rtc::scoped_refptr<I420Buffer> YuvFrameReaderImpl::ReadFrame() { |
60 if (input_file_ != NULL) { | 61 if (input_file_ == nullptr) { |
61 fclose(input_file_); | 62 fprintf(stderr, |
62 input_file_ = NULL; | 63 "YuvFrameReaderImpl is not initialized (input file is NULL)\n"); |
63 } | |
64 } | |
65 | |
66 rtc::scoped_refptr<I420Buffer> FrameReaderImpl::ReadFrame() { | |
67 if (input_file_ == NULL) { | |
68 fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n"); | |
69 return nullptr; | 64 return nullptr; |
70 } | 65 } |
71 rtc::scoped_refptr<I420Buffer> buffer( | 66 rtc::scoped_refptr<I420Buffer> buffer( |
72 ReadI420Buffer(width_, height_, input_file_)); | 67 ReadI420Buffer(width_, height_, input_file_)); |
73 if (!buffer && ferror(input_file_)) { | 68 if (!buffer && ferror(input_file_)) { |
74 fprintf(stderr, "Error reading from input file: %s\n", | 69 fprintf(stderr, "Error reading from input file: %s\n", |
75 input_filename_.c_str()); | 70 input_filename_.c_str()); |
76 } | 71 } |
77 return buffer; | 72 return buffer; |
78 } | 73 } |
79 | 74 |
80 size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; } | 75 void YuvFrameReaderImpl::Close() { |
81 int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; } | 76 if (input_file_ != nullptr) { |
| 77 fclose(input_file_); |
| 78 input_file_ = nullptr; |
| 79 } |
| 80 } |
| 81 |
| 82 size_t YuvFrameReaderImpl::FrameLength() { |
| 83 return frame_length_in_bytes_; |
| 84 } |
| 85 |
| 86 int YuvFrameReaderImpl::NumberOfFrames() { |
| 87 return number_of_frames_; |
| 88 } |
82 | 89 |
83 } // namespace test | 90 } // namespace test |
84 } // namespace webrtc | 91 } // namespace webrtc |
OLD | NEW |