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